blob: 8e26c2bc7fdfc971f92c730a2dad5a3c62fb6499 [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 Brown15e4c722008-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 Brown15e4c722008-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 Brown15e4c722008-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 Brown15e4c722008-07-02 11:51:20 +0100141 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200142
143 kfree(buf);
Mark Brown15e4c722008-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 Clausen8be4da22014-10-25 17:42:01 +0200162void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
Mark Browne2d32ff2012-08-31 17:38:32 -0700163{
Mark Browne2d32ff2012-08-31 17:38:32 -0700164 struct snd_soc_dapm_widget *w;
165
166 mutex_lock(&card->dapm_mutex);
167
168 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200169 if (w->is_sink || w->is_source)
170 dapm_mark_dirty(w, "Rechecking endpoints");
Mark Browne2d32ff2012-08-31 17:38:32 -0700171 }
172
173 mutex_unlock(&card->dapm_mutex);
174}
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200175EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
Mark Browne2d32ff2012-08-31 17:38:32 -0700176
Richard Purdie2b97eab2006-10-06 18:32:18 +0200177/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100178static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200179 const struct snd_soc_dapm_widget *_widget)
180{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100181 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200182}
183
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200184struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200185 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200186 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200187 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200188 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200189};
190
191static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
192 struct snd_kcontrol *kcontrol)
193{
194 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200195 struct soc_mixer_control *mc;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200196
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200197 data = kzalloc(sizeof(*data), GFP_KERNEL);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200198 if (!data) {
199 dev_err(widget->dapm->dev,
200 "ASoC: can't allocate kcontrol data for %s\n",
201 widget->name);
202 return -ENOMEM;
203 }
204
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200205 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200206
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200207 switch (widget->id) {
208 case snd_soc_dapm_switch:
209 case snd_soc_dapm_mixer:
210 case snd_soc_dapm_mixer_named_ctl:
211 mc = (struct soc_mixer_control *)kcontrol->private_value;
212
213 if (mc->autodisable) {
214 struct snd_soc_dapm_widget template;
215
216 memset(&template, 0, sizeof(template));
217 template.reg = mc->reg;
218 template.mask = (1 << fls(mc->max)) - 1;
219 template.shift = mc->shift;
220 if (mc->invert)
221 template.off_val = mc->max;
222 else
223 template.off_val = 0;
224 template.on_val = template.off_val;
225 template.id = snd_soc_dapm_kcontrol;
226 template.name = kcontrol->id.name;
227
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200228 data->value = template.on_val;
229
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200230 data->widget = snd_soc_dapm_new_control(widget->dapm,
231 &template);
232 if (!data->widget) {
233 kfree(data);
234 return -ENOMEM;
235 }
236 }
237 break;
238 default:
239 break;
240 }
241
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200242 kcontrol->private_data = data;
243
244 return 0;
245}
246
247static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
248{
249 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200250 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200251 kfree(data);
252}
253
254static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
255 const struct snd_kcontrol *kcontrol)
256{
257 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
258
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200259 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200260}
261
262static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
263 struct snd_soc_dapm_widget *widget)
264{
265 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200266 struct snd_soc_dapm_widget_list *new_wlist;
267 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200268
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200269 if (data->wlist)
270 n = data->wlist->num_widgets + 1;
271 else
272 n = 1;
273
274 new_wlist = krealloc(data->wlist,
275 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
276 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200277 return -ENOMEM;
278
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200279 new_wlist->widgets[n - 1] = widget;
280 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200281
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200282 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200283
284 return 0;
285}
286
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200287static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
288 struct snd_soc_dapm_path *path)
289{
290 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
291
292 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200293
294 if (data->widget) {
295 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
296 path->source, NULL, NULL);
297 }
298}
299
300static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
301{
302 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
303
304 if (!data->widget)
305 return true;
306
307 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200308}
309
310static struct list_head *dapm_kcontrol_get_path_list(
311 const struct snd_kcontrol *kcontrol)
312{
313 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
314
315 return &data->paths;
316}
317
318#define dapm_kcontrol_for_each_path(path, kcontrol) \
319 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
320 list_kcontrol)
321
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530322unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200323{
324 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
325
326 return data->value;
327}
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530328EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200329
330static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
331 unsigned int value)
332{
333 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
334
335 if (data->value == value)
336 return false;
337
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200338 if (data->widget)
339 data->widget->on_val = value;
340
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200341 data->value = value;
342
343 return true;
344}
345
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200346/**
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200347 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
348 * kcontrol
349 * @kcontrol: The kcontrol
350 *
351 * Note: This function must only be used on kcontrols that are known to have
352 * been registered for a CODEC. Otherwise the behaviour is undefined.
353 */
354struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
355 struct snd_kcontrol *kcontrol)
356{
357 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
358}
359EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
360
361/**
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200362 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
363 * @kcontrol: The kcontrol
364 */
365struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
366{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200367 return snd_soc_dapm_to_codec(snd_soc_dapm_kcontrol_dapm(kcontrol));
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200368}
369EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
370
Liam Girdwood6c120e12012-02-15 15:15:34 +0000371static void dapm_reset(struct snd_soc_card *card)
372{
373 struct snd_soc_dapm_widget *w;
374
Mark Brownf9fa2b12014-03-06 16:49:11 +0800375 lockdep_assert_held(&card->dapm_mutex);
376
Liam Girdwood6c120e12012-02-15 15:15:34 +0000377 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
378
379 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200380 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000381 w->power_checked = false;
382 w->inputs = -1;
383 w->outputs = -1;
384 }
385}
386
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200387static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
388{
389 if (!dapm->component)
390 return NULL;
391 return dapm->component->name_prefix;
392}
393
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200394static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800395 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100396{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200397 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200398 return -EIO;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200399 return snd_soc_component_read(dapm->component, reg, value);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100400}
401
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200402static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
Bard Liao34775012014-04-17 20:12:56 +0800403 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100404{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200405 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200406 return -EIO;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200407 return snd_soc_component_update_bits_async(dapm->component, reg,
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200408 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000409}
410
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200411static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
412 int reg, unsigned int mask, unsigned int value)
413{
414 if (!dapm->component)
415 return -EIO;
416 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
417}
418
Mark Browneb270e92013-10-09 13:52:52 +0100419static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
420{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200421 if (dapm->component)
422 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100423}
424
Mark Brown452c5ea2009-05-17 21:41:23 +0100425/**
426 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800427 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100428 * @level: level to configure
429 *
430 * Configure the bias (power) levels for the SoC audio device.
431 *
432 * Returns 0 for success else error.
433 */
Mark Browned5a4c42011-02-18 11:12:42 -0800434static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200435 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100436{
Mark Browned5a4c42011-02-18 11:12:42 -0800437 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100438 int ret = 0;
439
Mark Brown84e90932010-11-04 00:07:02 -0400440 trace_snd_soc_bias_level_start(card, level);
441
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000442 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100443 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100444 if (ret != 0)
445 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100446
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +0200447 if (dapm->set_bias_level)
448 ret = dapm->set_bias_level(dapm, level);
449 else if (!card || dapm != &card->dapm)
Liam Girdwood41231282012-07-06 16:56:16 +0100450 dapm->bias_level = level;
451
Mark Brown171ec6b2011-06-06 18:15:19 +0100452 if (ret != 0)
453 goto out;
454
455 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100456 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100457out:
Mark Brown84e90932010-11-04 00:07:02 -0400458 trace_snd_soc_bias_level_done(card, level);
459
Mark Brown452c5ea2009-05-17 21:41:23 +0100460 return ret;
461}
462
Mark Brown74b8f952009-06-06 11:26:15 +0100463/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200464static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200465 struct snd_soc_dapm_path *path, const char *control_name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200466{
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200467 const struct snd_kcontrol_new *kcontrol = &path->sink->kcontrol_news[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200468 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100469 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200470 int i;
471
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100472 if (e->reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200473 soc_dapm_read(dapm, e->reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100474 val = (val >> e->shift_l) & e->mask;
475 item = snd_soc_enum_val_to_item(e, val);
476 } else {
477 /* since a virtual mux has no backing registers to
478 * decide which path to connect, it will try to match
479 * with the first enumeration. This is to ensure
480 * that the default mux choice (the first) will be
481 * correctly powered up during initialization.
482 */
483 item = 0;
484 }
485
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100486 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200487 if (!(strcmp(control_name, e->texts[i]))) {
Rasmus Villemoes98ad73c2014-10-21 17:01:15 +0200488 path->name = e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100489 if (i == item)
490 path->connect = 1;
491 else
492 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200493 return 0;
494 }
495 }
496
497 return -ENODEV;
498}
499
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100500/* set up initial codec paths */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200501static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i)
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100502{
503 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200504 p->sink->kcontrol_news[i].private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100505 unsigned int reg = mc->reg;
506 unsigned int shift = mc->shift;
507 unsigned int max = mc->max;
508 unsigned int mask = (1 << fls(max)) - 1;
509 unsigned int invert = mc->invert;
510 unsigned int val;
511
512 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200513 soc_dapm_read(p->sink->dapm, reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100514 val = (val >> shift) & mask;
515 if (invert)
516 val = max - val;
517 p->connect = !!val;
518 } else {
519 p->connect = 0;
520 }
521}
522
Mark Brown74b8f952009-06-06 11:26:15 +0100523/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200524static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200525 struct snd_soc_dapm_path *path, const char *control_name)
526{
527 int i;
528
529 /* search for mixer kcontrol */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200530 for (i = 0; i < path->sink->num_kcontrols; i++) {
531 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
532 path->name = path->sink->kcontrol_news[i].name;
533 dapm_set_mixer_path_status(path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200534 return 0;
535 }
536 }
537 return -ENODEV;
538}
539
Stephen Warrenaf468002011-04-28 17:38:01 -0600540static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600541 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600542 const struct snd_kcontrol_new *kcontrol_new,
543 struct snd_kcontrol **kcontrol)
544{
545 struct snd_soc_dapm_widget *w;
546 int i;
547
548 *kcontrol = NULL;
549
550 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600551 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
552 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600553 for (i = 0; i < w->num_kcontrols; i++) {
554 if (&w->kcontrol_news[i] == kcontrol_new) {
555 if (w->kcontrols)
556 *kcontrol = w->kcontrols[i];
557 return 1;
558 }
559 }
560 }
561
562 return 0;
563}
564
Stephen Warren85762e72013-03-29 15:40:10 -0600565/*
566 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
567 * create it. Either way, add the widget into the control's widget list
568 */
569static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100570 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200571{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200572 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000573 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000574 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600575 size_t prefix_len;
576 int shared;
577 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600578 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200579 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600580 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200581 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000582
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200583 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000584 if (prefix)
585 prefix_len = strlen(prefix) + 1;
586 else
587 prefix_len = 0;
588
Stephen Warren85762e72013-03-29 15:40:10 -0600589 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
590 &kcontrol);
591
Stephen Warren85762e72013-03-29 15:40:10 -0600592 if (!kcontrol) {
593 if (shared) {
594 wname_in_long_name = false;
595 kcname_in_long_name = true;
596 } else {
597 switch (w->id) {
598 case snd_soc_dapm_switch:
599 case snd_soc_dapm_mixer:
600 wname_in_long_name = true;
601 kcname_in_long_name = true;
602 break;
603 case snd_soc_dapm_mixer_named_ctl:
604 wname_in_long_name = false;
605 kcname_in_long_name = true;
606 break;
607 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600608 wname_in_long_name = true;
609 kcname_in_long_name = false;
610 break;
611 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600612 return -EINVAL;
613 }
614 }
615
616 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600617 /*
618 * The control will get a prefix from the control
619 * creation process but we're also using the same
620 * prefix for widgets so cut the prefix off the
621 * front of the widget name.
622 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200623 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600624 w->name + prefix_len,
625 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200626 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200627 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600628
629 name = long_name;
630 } else if (wname_in_long_name) {
631 long_name = NULL;
632 name = w->name + prefix_len;
633 } else {
634 long_name = NULL;
635 name = w->kcontrol_news[kci].name;
636 }
637
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200638 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600639 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200640 if (!kcontrol) {
641 ret = -ENOMEM;
642 goto exit_free;
643 }
644
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200645 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200646
647 ret = dapm_kcontrol_data_alloc(w, kcontrol);
648 if (ret) {
649 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200650 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200651 }
652
Stephen Warren85762e72013-03-29 15:40:10 -0600653 ret = snd_ctl_add(card, kcontrol);
654 if (ret < 0) {
655 dev_err(dapm->dev,
656 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
657 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200658 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600659 }
Stephen Warren85762e72013-03-29 15:40:10 -0600660 }
661
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200662 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200663 if (ret == 0)
664 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200665
Daniel Macke5092c92014-10-07 13:41:24 +0200666exit_free:
667 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600668
Daniel Macke5092c92014-10-07 13:41:24 +0200669 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600670}
671
672/* create new dapm mixer control */
673static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
674{
675 int i, ret;
676 struct snd_soc_dapm_path *path;
677
Richard Purdie2b97eab2006-10-06 18:32:18 +0200678 /* add kcontrol */
679 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200680 /* match name */
681 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200682 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600683 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200684 continue;
685
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200686 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200687 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200688 continue;
689 }
690
Mark Brown946d92a2013-08-12 23:28:42 +0100691 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600692 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200693 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100694
695 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200696 }
697 }
Stephen Warren85762e72013-03-29 15:40:10 -0600698
699 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200700}
701
702/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200703static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200704{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200705 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600706 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600707 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200708
Stephen Warrenaf468002011-04-28 17:38:01 -0600709 if (w->num_kcontrols != 1) {
710 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000711 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600712 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200713 return -EINVAL;
714 }
715
Lars-Peter Clausenfe581392013-08-01 18:30:38 +0200716 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600717 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
718 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600719 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200720
Mark Brown946d92a2013-08-12 23:28:42 +0100721 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600722 if (ret < 0)
723 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600724
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +0200725 list_for_each_entry(path, &w->sources, list_sink) {
726 if (path->name)
727 dapm_kcontrol_add_path(w->kcontrols[0], path);
728 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200729
Stephen Warrenaf468002011-04-28 17:38:01 -0600730 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200731}
732
733/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200734static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200735{
Mark Browna6c65732010-03-03 17:45:21 +0000736 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200737 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000738 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200739
Mark Browna6c65732010-03-03 17:45:21 +0000740 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200741}
742
Mark Brown9949788b2010-05-07 20:24:05 +0100743/* We implement power down on suspend by checking the power state of
744 * the ALSA card - when we are suspending the ALSA state for the card
745 * is set to D3.
746 */
747static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
748{
Mark Brown12ea2c72011-03-02 18:17:32 +0000749 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100750
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000751 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100752 case SNDRV_CTL_POWER_D3hot:
753 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100754 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000755 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200756 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100757 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100758 default:
759 return 1;
760 }
761}
762
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100763/* add widget to list if it's not already in the list */
764static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
765 struct snd_soc_dapm_widget *w)
766{
767 struct snd_soc_dapm_widget_list *wlist;
768 int wlistsize, wlistentries, i;
769
770 if (*list == NULL)
771 return -EINVAL;
772
773 wlist = *list;
774
775 /* is this widget already in the list */
776 for (i = 0; i < wlist->num_widgets; i++) {
777 if (wlist->widgets[i] == w)
778 return 0;
779 }
780
781 /* allocate some new space */
782 wlistentries = wlist->num_widgets + 1;
783 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
784 wlistentries * sizeof(struct snd_soc_dapm_widget *);
785 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
786 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000787 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100788 w->name);
789 return -ENOMEM;
790 }
791 wlist = *list;
792
793 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000794 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100795 w->name, wlist->num_widgets);
796
797 wlist->widgets[wlist->num_widgets] = w;
798 wlist->num_widgets++;
799 return 1;
800}
801
Richard Purdie2b97eab2006-10-06 18:32:18 +0200802/*
803 * Recursively check for a completed path to an active or physically connected
804 * output widget. Returns number of complete paths.
805 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100806static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
807 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200808{
809 struct snd_soc_dapm_path *path;
810 int con = 0;
811
Mark Brown024dc072011-10-09 11:52:05 +0100812 if (widget->outputs >= 0)
813 return widget->outputs;
814
Mark Brownde02d072011-09-20 21:43:24 +0100815 DAPM_UPDATE_STAT(widget, path_checks);
816
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +0200817 if (widget->is_sink && widget->connected) {
818 widget->outputs = snd_soc_dapm_suspend_check(widget);
819 return widget->outputs;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200820 }
821
822 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100823 DAPM_UPDATE_STAT(widget, neighbour_checks);
824
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +0200825 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +0100826 continue;
827
Mark Brown8af294b2013-02-22 17:48:15 +0000828 if (path->walking)
829 return 1;
830
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100831 trace_snd_soc_dapm_output_path(widget, path);
832
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +0200833 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +0000834 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100835
836 /* do we need to add this widget to the list ? */
837 if (list) {
838 int err;
839 err = dapm_list_add_widget(list, path->sink);
840 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000841 dev_err(widget->dapm->dev,
842 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100843 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000844 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100845 return con;
846 }
847 }
848
849 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000850
851 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200852 }
853 }
854
Mark Brown024dc072011-10-09 11:52:05 +0100855 widget->outputs = con;
856
Richard Purdie2b97eab2006-10-06 18:32:18 +0200857 return con;
858}
859
860/*
861 * Recursively check for a completed path to an active or physically connected
862 * input widget. Returns number of complete paths.
863 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100864static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
865 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200866{
867 struct snd_soc_dapm_path *path;
868 int con = 0;
869
Mark Brown024dc072011-10-09 11:52:05 +0100870 if (widget->inputs >= 0)
871 return widget->inputs;
872
Mark Brownde02d072011-09-20 21:43:24 +0100873 DAPM_UPDATE_STAT(widget, path_checks);
874
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +0200875 if (widget->is_source && widget->connected) {
876 widget->inputs = snd_soc_dapm_suspend_check(widget);
877 return widget->inputs;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200878 }
879
880 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +0100881 DAPM_UPDATE_STAT(widget, neighbour_checks);
882
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +0200883 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +0100884 continue;
885
Mark Brown8af294b2013-02-22 17:48:15 +0000886 if (path->walking)
887 return 1;
888
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100889 trace_snd_soc_dapm_input_path(widget, path);
890
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +0200891 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +0000892 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100893
894 /* do we need to add this widget to the list ? */
895 if (list) {
896 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +0100897 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100898 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000899 dev_err(widget->dapm->dev,
900 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100901 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000902 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100903 return con;
904 }
905 }
906
907 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000908
909 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200910 }
911 }
912
Mark Brown024dc072011-10-09 11:52:05 +0100913 widget->inputs = con;
914
Richard Purdie2b97eab2006-10-06 18:32:18 +0200915 return con;
916}
917
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100918/**
919 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
920 * @dai: the soc DAI.
921 * @stream: stream direction.
922 * @list: list of active widgets for this stream.
923 *
924 * Queries DAPM graph as to whether an valid audio stream path exists for
925 * the initial stream specified by name. This takes into account
926 * current mixer and mux kcontrol settings. Creates list of valid widgets.
927 *
928 * Returns the number of valid paths or negative error.
929 */
930int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
931 struct snd_soc_dapm_widget_list **list)
932{
933 struct snd_soc_card *card = dai->card;
934 int paths;
935
936 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
937 dapm_reset(card);
938
Lars-Peter Clausen130897a2014-10-20 19:36:39 +0200939 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100940 paths = is_connected_output_ep(dai->playback_widget, list);
Lars-Peter Clausen130897a2014-10-20 19:36:39 +0200941 else
Liam Girdwoodd298caa2012-06-01 18:03:00 +0100942 paths = is_connected_input_ep(dai->capture_widget, list);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100943
944 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100945 mutex_unlock(&card->dapm_mutex);
946
947 return paths;
948}
949
Richard Purdie2b97eab2006-10-06 18:32:18 +0200950/*
Mark Brown62ea8742012-01-21 21:14:48 +0000951 * Handler for regulator supply widget.
952 */
953int dapm_regulator_event(struct snd_soc_dapm_widget *w,
954 struct snd_kcontrol *kcontrol, int event)
955{
Mark Brownc05b84d2012-09-07 12:57:11 +0800956 int ret;
957
Mark Browneb270e92013-10-09 13:52:52 +0100958 soc_dapm_async_complete(w->dapm);
959
Mark Brownc05b84d2012-09-07 12:57:11 +0800960 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +0200961 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +0000962 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +0800963 if (ret != 0)
964 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +0000965 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +0800966 w->name, ret);
967 }
968
Liam Girdwooda3cc0562012-03-09 17:20:16 +0000969 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +0800970 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +0200971 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +0000972 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +0800973 if (ret != 0)
974 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +0000975 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +0800976 w->name, ret);
977 }
978
Liam Girdwooda3cc0562012-03-09 17:20:16 +0000979 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +0800980 }
Mark Brown62ea8742012-01-21 21:14:48 +0000981}
982EXPORT_SYMBOL_GPL(dapm_regulator_event);
983
Ola Liljad7e7eb92012-05-24 15:26:25 +0200984/*
985 * Handler for clock supply widget.
986 */
987int dapm_clock_event(struct snd_soc_dapm_widget *w,
988 struct snd_kcontrol *kcontrol, int event)
989{
990 if (!w->clk)
991 return -EIO;
992
Mark Browneb270e92013-10-09 13:52:52 +0100993 soc_dapm_async_complete(w->dapm);
994
Mark Brownec029952012-06-04 08:16:20 +0100995#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +0200996 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +0200997 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +0200998 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +0200999 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001000 return 0;
1001 }
Mark Brownec029952012-06-04 08:16:20 +01001002#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001003 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001004}
1005EXPORT_SYMBOL_GPL(dapm_clock_event);
1006
Mark Brownd8050022011-09-28 18:28:23 +01001007static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1008{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001009 if (w->power_checked)
1010 return w->new_power;
1011
Mark Brownd8050022011-09-28 18:28:23 +01001012 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001013 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001014 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001015 w->new_power = w->power_check(w);
1016
1017 w->power_checked = true;
1018
1019 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001020}
1021
Mark Browncd0f2d42009-04-20 16:56:59 +01001022/* Generic check to see if a widget should be powered.
1023 */
1024static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1025{
1026 int in, out;
1027
Mark Brownde02d072011-09-20 21:43:24 +01001028 DAPM_UPDATE_STAT(w, power_checks);
1029
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001030 in = is_connected_input_ep(w, NULL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001031 out = is_connected_output_ep(w, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001032 return out != 0 && in != 0;
1033}
1034
Mark Brown246d0a12009-04-22 18:24:55 +01001035/* Check to see if a power supply is needed */
1036static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1037{
1038 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001039
Mark Brownde02d072011-09-20 21:43:24 +01001040 DAPM_UPDATE_STAT(w, power_checks);
1041
Mark Brown246d0a12009-04-22 18:24:55 +01001042 /* Check if one of our outputs is connected */
1043 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001044 DAPM_UPDATE_STAT(w, neighbour_checks);
1045
Mark Brownbf3a9e12011-06-13 16:42:29 +01001046 if (path->weak)
1047 continue;
1048
Mark Brown215edda2009-09-08 18:59:05 +01001049 if (path->connected &&
1050 !path->connected(path->source, path->sink))
1051 continue;
1052
Mark Brownf68d7e12011-10-04 22:57:50 +01001053 if (dapm_widget_power_check(path->sink))
1054 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001055 }
1056
Mark Brownf68d7e12011-10-04 22:57:50 +01001057 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001058}
1059
Mark Brown35c64bc2011-09-28 18:23:53 +01001060static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1061{
1062 return 1;
1063}
1064
Mark Brown38357ab2009-06-06 19:03:23 +01001065static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1066 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001067 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001068{
Mark Brown828a8422011-01-15 13:14:30 +00001069 int *sort;
1070
1071 if (power_up)
1072 sort = dapm_up_seq;
1073 else
1074 sort = dapm_down_seq;
1075
Mark Brown38357ab2009-06-06 19:03:23 +01001076 if (sort[a->id] != sort[b->id])
1077 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001078 if (a->subseq != b->subseq) {
1079 if (power_up)
1080 return a->subseq - b->subseq;
1081 else
1082 return b->subseq - a->subseq;
1083 }
Mark Brownb22ead22009-06-07 12:51:26 +01001084 if (a->reg != b->reg)
1085 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001086 if (a->dapm != b->dapm)
1087 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001088
Mark Brown38357ab2009-06-06 19:03:23 +01001089 return 0;
1090}
Mark Brown42aa3412009-03-01 19:21:10 +00001091
Mark Brown38357ab2009-06-06 19:03:23 +01001092/* Insert a widget in order into a DAPM power sequence. */
1093static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1094 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001095 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001096{
1097 struct snd_soc_dapm_widget *w;
1098
1099 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001100 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001101 list_add_tail(&new_widget->power_list, &w->power_list);
1102 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001103 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001104
Mark Brown38357ab2009-06-06 19:03:23 +01001105 list_add_tail(&new_widget->power_list, list);
1106}
Mark Brown42aa3412009-03-01 19:21:10 +00001107
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001108static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001109 struct snd_soc_dapm_widget *w, int event)
1110{
Mark Brown68f89ad2010-11-03 23:51:49 -04001111 const char *ev_name;
1112 int power, ret;
1113
1114 switch (event) {
1115 case SND_SOC_DAPM_PRE_PMU:
1116 ev_name = "PRE_PMU";
1117 power = 1;
1118 break;
1119 case SND_SOC_DAPM_POST_PMU:
1120 ev_name = "POST_PMU";
1121 power = 1;
1122 break;
1123 case SND_SOC_DAPM_PRE_PMD:
1124 ev_name = "PRE_PMD";
1125 power = 0;
1126 break;
1127 case SND_SOC_DAPM_POST_PMD:
1128 ev_name = "POST_PMD";
1129 power = 0;
1130 break;
Mark Brown80114122013-02-25 15:14:19 +00001131 case SND_SOC_DAPM_WILL_PMU:
1132 ev_name = "WILL_PMU";
1133 power = 1;
1134 break;
1135 case SND_SOC_DAPM_WILL_PMD:
1136 ev_name = "WILL_PMD";
1137 power = 0;
1138 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001139 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001140 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001141 return;
1142 }
1143
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001144 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001145 return;
1146
1147 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001148 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001149 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001150 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001151 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001152 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001153 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001154 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001155 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001156 ev_name, w->name, ret);
1157 }
1158}
1159
Mark Brownb22ead22009-06-07 12:51:26 +01001160/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001161static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001162 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001163{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001164 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001165 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001166 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001167 unsigned int value = 0;
1168 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001169
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001170 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1171 reg = w->reg;
1172 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001173
1174 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001175 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001176 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001177
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001178 mask |= w->mask << w->shift;
1179 if (w->power)
1180 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001181 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001182 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001183
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001184 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001185 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1186 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001187
Mark Brown68f89ad2010-11-03 23:51:49 -04001188 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001189 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1190 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001191 }
1192
Mark Brown81628102009-06-07 13:21:24 +01001193 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001194 /* Any widget will do, they should all be updating the
1195 * same register.
1196 */
Mark Brown29376bc2011-06-19 13:49:28 +01001197
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001198 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001199 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001200 value, mask, reg, card->pop_time);
1201 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001202 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001203 }
1204
1205 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001206 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1207 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001208 }
Mark Brown42aa3412009-03-01 19:21:10 +00001209}
1210
Mark Brownb22ead22009-06-07 12:51:26 +01001211/* Apply a DAPM power sequence.
1212 *
1213 * We walk over a pre-sorted list of widgets to apply power to. In
1214 * order to minimise the number of writes to the device required
1215 * multiple widgets will be updated in a single write where possible.
1216 * Currently anything that requires more than a single write is not
1217 * handled.
1218 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001219static void dapm_seq_run(struct snd_soc_card *card,
1220 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001221{
1222 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001223 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001224 LIST_HEAD(pending);
1225 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001226 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001227 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001228 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001229 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001230 int *sort;
1231
1232 if (power_up)
1233 sort = dapm_up_seq;
1234 else
1235 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001236
Mark Brownb22ead22009-06-07 12:51:26 +01001237 list_for_each_entry_safe(w, n, list, power_list) {
1238 ret = 0;
1239
1240 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001241 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001242 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001243 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001244 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001245
Mark Brown474b62d2011-01-18 16:14:44 +00001246 if (cur_dapm && cur_dapm->seq_notifier) {
1247 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1248 if (sort[i] == cur_sort)
1249 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001250 i,
1251 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001252 }
1253
Mark Browneb270e92013-10-09 13:52:52 +01001254 if (cur_dapm && w->dapm != cur_dapm)
1255 soc_dapm_async_complete(cur_dapm);
1256
Mark Brownb22ead22009-06-07 12:51:26 +01001257 INIT_LIST_HEAD(&pending);
1258 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001259 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001260 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001261 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001262 }
1263
Mark Brown163cac02009-06-07 10:12:52 +01001264 switch (w->id) {
1265 case snd_soc_dapm_pre:
1266 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001267 list_for_each_entry_safe_continue(w, n, list,
1268 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001269
Mark Brownb22ead22009-06-07 12:51:26 +01001270 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001271 ret = w->event(w,
1272 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001273 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001274 ret = w->event(w,
1275 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001276 break;
1277
1278 case snd_soc_dapm_post:
1279 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001280 list_for_each_entry_safe_continue(w, n, list,
1281 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001282
Mark Brownb22ead22009-06-07 12:51:26 +01001283 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001284 ret = w->event(w,
1285 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001286 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001287 ret = w->event(w,
1288 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001289 break;
1290
Mark Brown163cac02009-06-07 10:12:52 +01001291 default:
Mark Brown81628102009-06-07 13:21:24 +01001292 /* Queue it up for application */
1293 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001294 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001295 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001296 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001297 list_move(&w->power_list, &pending);
1298 break;
Mark Brown163cac02009-06-07 10:12:52 +01001299 }
Mark Brownb22ead22009-06-07 12:51:26 +01001300
1301 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001302 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001303 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001304 }
Mark Brownb22ead22009-06-07 12:51:26 +01001305
1306 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001307 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001308
1309 if (cur_dapm && cur_dapm->seq_notifier) {
1310 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1311 if (sort[i] == cur_sort)
1312 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001313 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001314 }
Mark Browneb270e92013-10-09 13:52:52 +01001315
1316 list_for_each_entry(d, &card->dapm_list, list) {
1317 soc_dapm_async_complete(d);
1318 }
Mark Brown163cac02009-06-07 10:12:52 +01001319}
1320
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001321static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001322{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001323 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001324 struct snd_soc_dapm_widget_list *wlist;
1325 struct snd_soc_dapm_widget *w = NULL;
1326 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001327 int ret;
1328
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001329 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001330 return;
1331
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001332 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001333
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001334 for (wi = 0; wi < wlist->num_widgets; wi++) {
1335 w = wlist->widgets[wi];
1336
1337 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1338 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1339 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001340 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001341 w->name, ret);
1342 }
Mark Brown97404f22010-12-14 16:13:57 +00001343 }
1344
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001345 if (!w)
1346 return;
1347
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001348 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1349 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001350 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001351 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001352 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001353
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001354 for (wi = 0; wi < wlist->num_widgets; wi++) {
1355 w = wlist->widgets[wi];
1356
1357 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1358 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1359 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001360 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001361 w->name, ret);
1362 }
Mark Brown97404f22010-12-14 16:13:57 +00001363 }
1364}
1365
Mark Brown9d0624a2011-02-18 11:49:43 -08001366/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1367 * they're changing state.
1368 */
1369static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1370{
1371 struct snd_soc_dapm_context *d = data;
1372 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001373
Mark Brown56fba412011-06-04 11:25:10 +01001374 /* If we're off and we're not supposed to be go into STANDBY */
1375 if (d->bias_level == SND_SOC_BIAS_OFF &&
1376 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001377 if (d->dev)
1378 pm_runtime_get_sync(d->dev);
1379
Mark Brown9d0624a2011-02-18 11:49:43 -08001380 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1381 if (ret != 0)
1382 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001383 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001384 }
1385
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001386 /* Prepare for a transition to ON or away from ON */
1387 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1388 d->bias_level != SND_SOC_BIAS_ON) ||
1389 (d->target_bias_level != SND_SOC_BIAS_ON &&
1390 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001391 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1392 if (ret != 0)
1393 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001394 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001395 }
1396}
1397
1398/* Async callback run prior to DAPM sequences - brings to their final
1399 * state.
1400 */
1401static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1402{
1403 struct snd_soc_dapm_context *d = data;
1404 int ret;
1405
1406 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001407 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1408 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1409 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001410 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1411 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001412 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001413 ret);
1414 }
1415
1416 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001417 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1418 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001419 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1420 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001421 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1422 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001423
1424 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001425 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001426 }
1427
1428 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001429 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1430 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001431 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1432 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001433 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001434 ret);
1435 }
1436}
Mark Brown97404f22010-12-14 16:13:57 +00001437
Mark Brownfe4fda52011-10-03 22:36:57 +01001438static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1439 bool power, bool connect)
1440{
1441 /* If a connection is being made or broken then that update
1442 * will have marked the peer dirty, otherwise the widgets are
1443 * not connected and this update has no impact. */
1444 if (!connect)
1445 return;
1446
1447 /* If the peer is already in the state we're moving to then we
1448 * won't have an impact on it. */
1449 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001450 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001451}
1452
Mark Brown05623c42011-09-28 17:02:31 +01001453static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1454 struct list_head *up_list,
1455 struct list_head *down_list)
1456{
Mark Browndb432b42011-10-03 21:06:40 +01001457 struct snd_soc_dapm_path *path;
1458
Mark Brown05623c42011-09-28 17:02:31 +01001459 if (w->power == power)
1460 return;
1461
1462 trace_snd_soc_dapm_widget_power(w, power);
1463
Mark Browndb432b42011-10-03 21:06:40 +01001464 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001465 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001466 */
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001467 list_for_each_entry(path, &w->sources, list_sink)
1468 dapm_widget_set_peer_power(path->source, power, path->connect);
1469
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001470 /* Supplies can't affect their outputs, only their inputs */
1471 if (!w->is_supply) {
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001472 list_for_each_entry(path, &w->sinks, list_source)
1473 dapm_widget_set_peer_power(path->sink, power,
1474 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001475 }
1476
Mark Brown05623c42011-09-28 17:02:31 +01001477 if (power)
1478 dapm_seq_insert(w, up_list, true);
1479 else
1480 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001481}
1482
Mark Brown7c81beb2011-09-20 22:22:32 +01001483static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1484 struct list_head *up_list,
1485 struct list_head *down_list)
1486{
Mark Brown7c81beb2011-09-20 22:22:32 +01001487 int power;
1488
1489 switch (w->id) {
1490 case snd_soc_dapm_pre:
1491 dapm_seq_insert(w, down_list, false);
1492 break;
1493 case snd_soc_dapm_post:
1494 dapm_seq_insert(w, up_list, true);
1495 break;
1496
1497 default:
Mark Brownd8050022011-09-28 18:28:23 +01001498 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001499
Mark Brown05623c42011-09-28 17:02:31 +01001500 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001501 break;
1502 }
1503}
1504
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001505static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1506{
1507 if (dapm->idle_bias_off)
1508 return true;
1509
1510 switch (snd_power_get_state(dapm->card->snd_card)) {
1511 case SNDRV_CTL_POWER_D3hot:
1512 case SNDRV_CTL_POWER_D3cold:
1513 return dapm->suspend_bias_off;
1514 default:
1515 break;
1516 }
1517
1518 return false;
1519}
1520
Mark Brown42aa3412009-03-01 19:21:10 +00001521/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001522 * Scan each dapm widget for complete audio path.
1523 * A complete path is a route that has valid endpoints i.e.:-
1524 *
1525 * o DAC to output pin.
1526 * o Input Pin to ADC.
1527 * o Input pin to Output pin (bypass, sidetone)
1528 * o DAC to ADC (loopback).
1529 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001530static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001531{
1532 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001533 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001534 LIST_HEAD(up_list);
1535 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001536 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001537 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001538
Mark Brownf9fa2b12014-03-06 16:49:11 +08001539 lockdep_assert_held(&card->dapm_mutex);
1540
Mark Brown84e90932010-11-04 00:07:02 -04001541 trace_snd_soc_dapm_start(card);
1542
Mark Brown56fba412011-06-04 11:25:10 +01001543 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001544 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001545 d->target_bias_level = SND_SOC_BIAS_OFF;
1546 else
1547 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001548 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001549
Liam Girdwood6c120e12012-02-15 15:15:34 +00001550 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001551
Mark Brown6d3ddc82009-05-16 17:47:29 +01001552 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001553 * lists indicating if they should be powered up or down. We
1554 * only check widgets that have been flagged as dirty but note
1555 * that new widgets may be added to the dirty list while we
1556 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001557 */
Mark Browndb432b42011-10-03 21:06:40 +01001558 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001559 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001560 }
1561
Mark Brownf9de6d72011-09-28 17:19:47 +01001562 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001563 switch (w->id) {
1564 case snd_soc_dapm_pre:
1565 case snd_soc_dapm_post:
1566 /* These widgets always need to be powered */
1567 break;
1568 default:
1569 list_del_init(&w->dirty);
1570 break;
1571 }
Mark Browndb432b42011-10-03 21:06:40 +01001572
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001573 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001574 d = w->dapm;
1575
1576 /* Supplies and micbiases only bring the
1577 * context up to STANDBY as unless something
1578 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001579 * generally don't require full power. Signal
1580 * generators are virtual pins and have no
1581 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001582 */
1583 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001584 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001585 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001586 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001587 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001588 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001589 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001590 case snd_soc_dapm_micbias:
1591 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1592 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1593 break;
1594 default:
1595 d->target_bias_level = SND_SOC_BIAS_ON;
1596 break;
1597 }
1598 }
1599
1600 }
1601
Mark Brown85a843c2011-09-21 21:29:47 +01001602 /* Force all contexts in the card to the same bias state if
1603 * they're not ground referenced.
1604 */
Mark Brown56fba412011-06-04 11:25:10 +01001605 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001606 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001607 if (d->target_bias_level > bias)
1608 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001609 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001610 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01001611 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001612
Mark Brownde02d072011-09-20 21:43:24 +01001613 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001614
Xiang Xiao17282ba2014-03-02 00:04:03 +08001615 /* Run card bias changes at first */
1616 dapm_pre_sequence_async(&card->dapm, 0);
1617 /* Run other bias changes in parallel */
1618 list_for_each_entry(d, &card->dapm_list, list) {
1619 if (d != &card->dapm)
1620 async_schedule_domain(dapm_pre_sequence_async, d,
1621 &async_domain);
1622 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001623 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001624
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001625 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001626 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001627 }
1628
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001629 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001630 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001631 }
1632
Mark Brown6d3ddc82009-05-16 17:47:29 +01001633 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001634 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001635
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001636 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001637
Mark Brown6d3ddc82009-05-16 17:47:29 +01001638 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001639 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001640
Mark Brown9d0624a2011-02-18 11:49:43 -08001641 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001642 list_for_each_entry(d, &card->dapm_list, list) {
1643 if (d != &card->dapm)
1644 async_schedule_domain(dapm_post_sequence_async, d,
1645 &async_domain);
1646 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001647 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001648 /* Run card bias changes at last */
1649 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001650
Liam Girdwood8078d872012-02-15 15:15:35 +00001651 /* do we need to notify any clients that DAPM event is complete */
1652 list_for_each_entry(d, &card->dapm_list, list) {
1653 if (d->stream_event)
1654 d->stream_event(d, event);
1655 }
1656
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001657 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001658 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001659 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001660
Mark Brown84e90932010-11-04 00:07:02 -04001661 trace_snd_soc_dapm_done(card);
1662
Mark Brown42aa3412009-03-01 19:21:10 +00001663 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001664}
1665
Mark Brown79fb9382009-08-21 16:38:13 +01001666#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001667static ssize_t dapm_widget_power_read_file(struct file *file,
1668 char __user *user_buf,
1669 size_t count, loff_t *ppos)
1670{
1671 struct snd_soc_dapm_widget *w = file->private_data;
1672 char *buf;
1673 int in, out;
1674 ssize_t ret;
1675 struct snd_soc_dapm_path *p = NULL;
1676
1677 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1678 if (!buf)
1679 return -ENOMEM;
1680
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001681 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
1682 if (w->is_supply) {
1683 in = 0;
1684 out = 0;
1685 } else {
1686 in = is_connected_input_ep(w, NULL);
1687 out = is_connected_output_ep(w, NULL);
1688 }
Mark Brown79fb9382009-08-21 16:38:13 +01001689
Mark Brownf13ebad2012-03-03 18:01:01 +00001690 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1691 w->name, w->power ? "On" : "Off",
1692 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001693
Mark Brownd033c362009-12-04 15:25:56 +00001694 if (w->reg >= 0)
1695 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001696 " - R%d(0x%x) mask 0x%x",
1697 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001698
1699 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1700
Mark Brown3eef08b2009-09-14 16:49:00 +01001701 if (w->sname)
1702 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1703 w->sname,
1704 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001705
1706 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001707 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001708 continue;
1709
Mark Brown79fb9382009-08-21 16:38:13 +01001710 if (p->connect)
1711 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001712 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001713 p->name ? p->name : "static",
1714 p->source->name);
1715 }
1716 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001717 if (p->connected && !p->connected(w, p->sink))
1718 continue;
1719
Mark Brown79fb9382009-08-21 16:38:13 +01001720 if (p->connect)
1721 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001722 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001723 p->name ? p->name : "static",
1724 p->sink->name);
1725 }
1726
1727 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1728
1729 kfree(buf);
1730 return ret;
1731}
1732
1733static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001734 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001735 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001736 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001737};
1738
Mark Brownef49e4f2011-04-04 20:48:13 +09001739static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1740 size_t count, loff_t *ppos)
1741{
1742 struct snd_soc_dapm_context *dapm = file->private_data;
1743 char *level;
1744
1745 switch (dapm->bias_level) {
1746 case SND_SOC_BIAS_ON:
1747 level = "On\n";
1748 break;
1749 case SND_SOC_BIAS_PREPARE:
1750 level = "Prepare\n";
1751 break;
1752 case SND_SOC_BIAS_STANDBY:
1753 level = "Standby\n";
1754 break;
1755 case SND_SOC_BIAS_OFF:
1756 level = "Off\n";
1757 break;
1758 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001759 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09001760 level = "Unknown\n";
1761 break;
1762 }
1763
1764 return simple_read_from_buffer(user_buf, count, ppos, level,
1765 strlen(level));
1766}
1767
1768static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001769 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001770 .read = dapm_bias_read_file,
1771 .llseek = default_llseek,
1772};
1773
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001774void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1775 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001776{
Mark Brown79fb9382009-08-21 16:38:13 +01001777 struct dentry *d;
1778
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001779 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1780
1781 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001782 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001783 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001784 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001785 }
Mark Brown79fb9382009-08-21 16:38:13 +01001786
Mark Brownef49e4f2011-04-04 20:48:13 +09001787 d = debugfs_create_file("bias_level", 0444,
1788 dapm->debugfs_dapm, dapm,
1789 &dapm_bias_fops);
1790 if (!d)
1791 dev_warn(dapm->dev,
1792 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001793}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001794
1795static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1796{
1797 struct snd_soc_dapm_context *dapm = w->dapm;
1798 struct dentry *d;
1799
1800 if (!dapm->debugfs_dapm || !w->name)
1801 return;
1802
1803 d = debugfs_create_file(w->name, 0444,
1804 dapm->debugfs_dapm, w,
1805 &dapm_widget_power_fops);
1806 if (!d)
1807 dev_warn(w->dapm->dev,
1808 "ASoC: Failed to create %s debugfs file\n",
1809 w->name);
1810}
1811
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001812static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1813{
1814 debugfs_remove_recursive(dapm->debugfs_dapm);
1815}
1816
Mark Brown79fb9382009-08-21 16:38:13 +01001817#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001818void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1819 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001820{
1821}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001822
1823static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1824{
1825}
1826
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001827static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1828{
1829}
1830
Mark Brown79fb9382009-08-21 16:38:13 +01001831#endif
1832
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02001833/*
1834 * soc_dapm_connect_path() - Connects or disconnects a path
1835 * @path: The path to update
1836 * @connect: The new connect state of the path. True if the path is connected,
1837 * false if it is disconneted.
1838 * @reason: The reason why the path changed (for debugging only)
1839 */
1840static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
1841 bool connect, const char *reason)
1842{
1843 if (path->connect == connect)
1844 return;
1845
1846 path->connect = connect;
1847 dapm_mark_dirty(path->source, reason);
1848 dapm_mark_dirty(path->sink, reason);
1849}
1850
Richard Purdie2b97eab2006-10-06 18:32:18 +02001851/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001852static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001853 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001854{
1855 struct snd_soc_dapm_path *path;
1856 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02001857 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001858
Mark Brownf9fa2b12014-03-06 16:49:11 +08001859 lockdep_assert_held(&card->dapm_mutex);
1860
Richard Purdie2b97eab2006-10-06 18:32:18 +02001861 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02001862 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001863 found = 1;
1864 /* we now need to match the string in the enum to the path */
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02001865 if (!(strcmp(path->name, e->texts[mux])))
1866 connect = true;
1867 else
1868 connect = false;
1869
1870 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001871 }
1872
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001873 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001874 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001875
Liam Girdwood618dae12012-04-25 12:12:51 +01001876 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001877}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001878
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001879int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02001880 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
1881 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001882{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001883 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001884 int ret;
1885
Liam Girdwood3cd04342012-03-09 12:02:08 +00001886 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02001887 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001888 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02001889 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001890 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001891 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001892 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001893 return ret;
1894}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001895EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001896
Milan plzik1b075e32008-01-10 14:39:46 +01001897/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001898static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00001899 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001900{
1901 struct snd_soc_dapm_path *path;
1902 int found = 0;
1903
Mark Brownf9fa2b12014-03-06 16:49:11 +08001904 lockdep_assert_held(&card->dapm_mutex);
1905
Richard Purdie2b97eab2006-10-06 18:32:18 +02001906 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02001907 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001908 found = 1;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02001909 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001910 }
1911
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001912 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001913 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001914
Liam Girdwood618dae12012-04-25 12:12:51 +01001915 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001916}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001917
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001918int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02001919 struct snd_kcontrol *kcontrol, int connect,
1920 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001921{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001922 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001923 int ret;
1924
Liam Girdwood3cd04342012-03-09 12:02:08 +00001925 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02001926 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001927 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02001928 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001929 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001930 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001931 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001932 return ret;
1933}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001934EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001935
Benoit Cousson44ba2642014-07-08 23:19:36 +02001936static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001937{
Richard Purdie2b97eab2006-10-06 18:32:18 +02001938 struct snd_soc_dapm_widget *w;
1939 int count = 0;
1940 char *state = "not set";
1941
Lars-Peter Clausen00200102014-07-17 22:01:07 +02001942 list_for_each_entry(w, &codec->component.card->widgets, list) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001943 if (w->dapm != &codec->dapm)
1944 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001945
1946 /* only display widgets that burnm power */
1947 switch (w->id) {
1948 case snd_soc_dapm_hp:
1949 case snd_soc_dapm_mic:
1950 case snd_soc_dapm_spk:
1951 case snd_soc_dapm_line:
1952 case snd_soc_dapm_micbias:
1953 case snd_soc_dapm_dac:
1954 case snd_soc_dapm_adc:
1955 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001956 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001957 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001958 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001959 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001960 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001961 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001962 if (w->name)
1963 count += sprintf(buf + count, "%s: %s\n",
1964 w->name, w->power ? "On":"Off");
1965 break;
1966 default:
1967 break;
1968 }
1969 }
1970
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001971 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001972 case SND_SOC_BIAS_ON:
1973 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001974 break;
Mark Brown0be98982008-05-19 12:31:28 +02001975 case SND_SOC_BIAS_PREPARE:
1976 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001977 break;
Mark Brown0be98982008-05-19 12:31:28 +02001978 case SND_SOC_BIAS_STANDBY:
1979 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001980 break;
Mark Brown0be98982008-05-19 12:31:28 +02001981 case SND_SOC_BIAS_OFF:
1982 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001983 break;
1984 }
1985 count += sprintf(buf + count, "PM State: %s\n", state);
1986
1987 return count;
1988}
1989
Benoit Cousson44ba2642014-07-08 23:19:36 +02001990/* show dapm widget status in sys fs */
1991static ssize_t dapm_widget_show(struct device *dev,
1992 struct device_attribute *attr, char *buf)
1993{
1994 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
1995 int i, count = 0;
1996
1997 for (i = 0; i < rtd->num_codecs; i++) {
1998 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
1999 count += dapm_widget_show_codec(codec, buf + count);
2000 }
2001
2002 return count;
2003}
2004
Richard Purdie2b97eab2006-10-06 18:32:18 +02002005static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2006
2007int snd_soc_dapm_sys_add(struct device *dev)
2008{
Troy Kisky12ef1932008-10-13 17:42:14 -07002009 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002010}
2011
2012static void snd_soc_dapm_sys_remove(struct device *dev)
2013{
Mark Brownaef90842009-05-16 17:53:16 +01002014 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002015}
2016
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002017static void dapm_free_path(struct snd_soc_dapm_path *path)
2018{
2019 list_del(&path->list_sink);
2020 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002021 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002022 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002023 kfree(path);
2024}
2025
Richard Purdie2b97eab2006-10-06 18:32:18 +02002026/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002027static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002028{
2029 struct snd_soc_dapm_widget *w, *next_w;
2030 struct snd_soc_dapm_path *p, *next_p;
2031
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002032 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2033 if (w->dapm != dapm)
2034 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002035 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002036 /*
2037 * remove source and sink paths associated to this widget.
2038 * While removing the path, remove reference to it from both
2039 * source and sink widgets so that path is removed only once.
2040 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002041 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2042 dapm_free_path(p);
2043
2044 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2045 dapm_free_path(p);
2046
Stephen Warrenfad59882011-04-28 17:37:59 -06002047 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002048 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002049 kfree(w);
2050 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002051}
2052
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002053static struct snd_soc_dapm_widget *dapm_find_widget(
2054 struct snd_soc_dapm_context *dapm, const char *pin,
2055 bool search_other_contexts)
2056{
2057 struct snd_soc_dapm_widget *w;
2058 struct snd_soc_dapm_widget *fallback = NULL;
2059
2060 list_for_each_entry(w, &dapm->card->widgets, list) {
2061 if (!strcmp(w->name, pin)) {
2062 if (w->dapm == dapm)
2063 return w;
2064 else
2065 fallback = w;
2066 }
2067 }
2068
2069 if (search_other_contexts)
2070 return fallback;
2071
2072 return NULL;
2073}
2074
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002075static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002076 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002077{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002078 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002079
Mark Brownf9fa2b12014-03-06 16:49:11 +08002080 dapm_assert_locked(dapm);
2081
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002082 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002083 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002084 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002085 }
2086
Mark Brown1a8b2d92012-02-16 11:50:07 -08002087 if (w->connected != status)
2088 dapm_mark_dirty(w, "pin configuration");
2089
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002090 w->connected = status;
2091 if (status == 0)
2092 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002093
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002094 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002095}
2096
Richard Purdie2b97eab2006-10-06 18:32:18 +02002097/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002098 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2099 * @dapm: DAPM context
2100 *
2101 * Walks all dapm audio paths and powers widgets according to their
2102 * stream or path usage.
2103 *
2104 * Requires external locking.
2105 *
2106 * Returns 0 for success.
2107 */
2108int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2109{
2110 /*
2111 * Suppress early reports (eg, jacks syncing their state) to avoid
2112 * silly DAPM runs during card startup.
2113 */
2114 if (!dapm->card || !dapm->card->instantiated)
2115 return 0;
2116
2117 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2118}
2119EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2120
2121/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002122 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002123 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002124 *
2125 * Walks all dapm audio paths and powers widgets according to their
2126 * stream or path usage.
2127 *
2128 * Returns 0 for success.
2129 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002130int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002131{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002132 int ret;
2133
Liam Girdwood3cd04342012-03-09 12:02:08 +00002134 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002135 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002136 mutex_unlock(&dapm->card->dapm_mutex);
2137 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002138}
Liam Girdwooda5302182008-07-07 13:35:17 +01002139EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002140
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002141/*
2142 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2143 * @w: The widget for which to update the flags
2144 *
2145 * Some widgets have a dynamic category which depends on which neighbors they
2146 * are connected to. This function update the category for these widgets.
2147 *
2148 * This function must be called whenever a path is added or removed to a widget.
2149 */
2150static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2151{
2152 struct snd_soc_dapm_path *p;
2153
2154 switch (w->id) {
2155 case snd_soc_dapm_input:
2156 w->is_source = 1;
2157 list_for_each_entry(p, &w->sources, list_sink) {
2158 if (p->source->id == snd_soc_dapm_micbias ||
2159 p->source->id == snd_soc_dapm_mic ||
2160 p->source->id == snd_soc_dapm_line ||
2161 p->source->id == snd_soc_dapm_output) {
2162 w->is_source = 0;
2163 break;
2164 }
2165 }
2166 break;
2167 case snd_soc_dapm_output:
2168 w->is_sink = 1;
2169 list_for_each_entry(p, &w->sinks, list_source) {
2170 if (p->sink->id == snd_soc_dapm_spk ||
2171 p->sink->id == snd_soc_dapm_hp ||
2172 p->sink->id == snd_soc_dapm_line ||
2173 p->sink->id == snd_soc_dapm_input) {
2174 w->is_sink = 0;
2175 break;
2176 }
2177 }
2178 break;
2179 case snd_soc_dapm_line:
2180 w->is_sink = !list_empty(&w->sources);
2181 w->is_source = !list_empty(&w->sinks);
2182 break;
2183 default:
2184 break;
2185 }
2186}
2187
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002188static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2189 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2190 const char *control,
2191 int (*connected)(struct snd_soc_dapm_widget *source,
2192 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002193{
2194 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002195 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002196
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002197 if (wsink->is_supply && !wsource->is_supply) {
2198 dev_err(dapm->dev,
2199 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2200 wsource->name, wsink->name);
2201 return -EINVAL;
2202 }
2203
2204 if (connected && !wsource->is_supply) {
2205 dev_err(dapm->dev,
2206 "connected() callback only supported for supply widgets (%s -> %s)\n",
2207 wsource->name, wsink->name);
2208 return -EINVAL;
2209 }
2210
2211 if (wsource->is_supply && control) {
2212 dev_err(dapm->dev,
2213 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2214 wsource->name, control, wsink->name);
2215 return -EINVAL;
2216 }
2217
Richard Purdie2b97eab2006-10-06 18:32:18 +02002218 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2219 if (!path)
2220 return -ENOMEM;
2221
2222 path->source = wsource;
2223 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002224 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002225 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002226 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002227 INIT_LIST_HEAD(&path->list_source);
2228 INIT_LIST_HEAD(&path->list_sink);
2229
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002230 if (wsource->is_supply || wsink->is_supply)
2231 path->is_supply = 1;
2232
Richard Purdie2b97eab2006-10-06 18:32:18 +02002233 /* connect static paths */
2234 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002235 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002236 } else {
2237 /* connect dynamic paths */
2238 switch (wsink->id) {
2239 case snd_soc_dapm_mux:
2240 ret = dapm_connect_mux(dapm, path, control);
2241 if (ret != 0)
2242 goto err;
2243 break;
2244 case snd_soc_dapm_switch:
2245 case snd_soc_dapm_mixer:
2246 case snd_soc_dapm_mixer_named_ctl:
2247 ret = dapm_connect_mixer(dapm, path, control);
2248 if (ret != 0)
2249 goto err;
2250 break;
2251 default:
2252 dev_err(dapm->dev,
2253 "Control not supported for path %s -> [%s] -> %s\n",
2254 wsource->name, control, wsink->name);
2255 ret = -EINVAL;
2256 goto err;
2257 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002258 }
2259
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002260 list_add(&path->list, &dapm->card->paths);
2261 list_add(&path->list_sink, &wsink->sources);
2262 list_add(&path->list_source, &wsource->sinks);
2263
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002264 dapm_update_widget_flags(wsource);
2265 dapm_update_widget_flags(wsink);
2266
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002267 dapm_mark_dirty(wsource, "Route added");
2268 dapm_mark_dirty(wsink, "Route added");
Mark Brownfabd0382012-07-05 17:20:06 +01002269
Richard Purdie2b97eab2006-10-06 18:32:18 +02002270 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002271err:
2272 kfree(path);
2273 return ret;
2274}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002275
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002276static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002277 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002278{
2279 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2280 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2281 const char *sink;
2282 const char *source;
2283 char prefixed_sink[80];
2284 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002285 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002286 int ret;
2287
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002288 prefix = soc_dapm_prefix(dapm);
2289 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002290 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002291 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002292 sink = prefixed_sink;
2293 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002294 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002295 source = prefixed_source;
2296 } else {
2297 sink = route->sink;
2298 source = route->source;
2299 }
2300
2301 /*
2302 * find src and dest widgets over all widgets but favor a widget from
2303 * current DAPM context
2304 */
2305 list_for_each_entry(w, &dapm->card->widgets, list) {
2306 if (!wsink && !(strcmp(w->name, sink))) {
2307 wtsink = w;
2308 if (w->dapm == dapm)
2309 wsink = w;
2310 continue;
2311 }
2312 if (!wsource && !(strcmp(w->name, source))) {
2313 wtsource = w;
2314 if (w->dapm == dapm)
2315 wsource = w;
2316 }
2317 }
2318 /* use widget from another DAPM context if not found from this */
2319 if (!wsink)
2320 wsink = wtsink;
2321 if (!wsource)
2322 wsource = wtsource;
2323
2324 if (wsource == NULL) {
2325 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2326 route->source);
2327 return -ENODEV;
2328 }
2329 if (wsink == NULL) {
2330 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2331 route->sink);
2332 return -ENODEV;
2333 }
2334
2335 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2336 route->connected);
2337 if (ret)
2338 goto err;
2339
2340 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002341err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002342 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002343 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002344 return ret;
2345}
Mark Brown105f1c22008-05-13 14:52:19 +02002346
Mark Brownefcc3c62012-07-05 17:24:19 +01002347static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2348 const struct snd_soc_dapm_route *route)
2349{
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002350 struct snd_soc_dapm_widget *wsource, *wsink;
Mark Brownefcc3c62012-07-05 17:24:19 +01002351 struct snd_soc_dapm_path *path, *p;
2352 const char *sink;
2353 const char *source;
2354 char prefixed_sink[80];
2355 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002356 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002357
2358 if (route->control) {
2359 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002360 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002361 return -EINVAL;
2362 }
2363
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002364 prefix = soc_dapm_prefix(dapm);
2365 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002366 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002367 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002368 sink = prefixed_sink;
2369 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002370 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002371 source = prefixed_source;
2372 } else {
2373 sink = route->sink;
2374 source = route->source;
2375 }
2376
2377 path = NULL;
2378 list_for_each_entry(p, &dapm->card->paths, list) {
2379 if (strcmp(p->source->name, source) != 0)
2380 continue;
2381 if (strcmp(p->sink->name, sink) != 0)
2382 continue;
2383 path = p;
2384 break;
2385 }
2386
2387 if (path) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002388 wsource = path->source;
2389 wsink = path->sink;
2390
2391 dapm_mark_dirty(wsource, "Route removed");
2392 dapm_mark_dirty(wsink, "Route removed");
Mark Brownefcc3c62012-07-05 17:24:19 +01002393
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002394 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002395
2396 /* Update any path related flags */
2397 dapm_update_widget_flags(wsource);
2398 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002399 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002400 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002401 source, sink);
2402 }
2403
2404 return 0;
2405}
2406
Mark Brown105f1c22008-05-13 14:52:19 +02002407/**
Mark Brown105f1c22008-05-13 14:52:19 +02002408 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002409 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002410 * @route: audio routes
2411 * @num: number of routes
2412 *
2413 * Connects 2 dapm widgets together via a named audio path. The sink is
2414 * the widget receiving the audio signal, whilst the source is the sender
2415 * of the audio signal.
2416 *
2417 * Returns 0 for success else error. On error all resources can be freed
2418 * with a call to snd_soc_card_free().
2419 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002420int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002421 const struct snd_soc_dapm_route *route, int num)
2422{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002423 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002424
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002425 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002426 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002427 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002428 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002429 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2430 route->source,
2431 route->control ? route->control : "direct",
2432 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002433 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002434 }
2435 route++;
2436 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002437 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002438
Dan Carpenter60884c22012-04-13 22:25:43 +03002439 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002440}
2441EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2442
Mark Brownefcc3c62012-07-05 17:24:19 +01002443/**
2444 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2445 * @dapm: DAPM context
2446 * @route: audio routes
2447 * @num: number of routes
2448 *
2449 * Removes routes from the DAPM context.
2450 */
2451int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2452 const struct snd_soc_dapm_route *route, int num)
2453{
2454 int i, ret = 0;
2455
2456 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2457 for (i = 0; i < num; i++) {
2458 snd_soc_dapm_del_route(dapm, route);
2459 route++;
2460 }
2461 mutex_unlock(&dapm->card->dapm_mutex);
2462
2463 return ret;
2464}
2465EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2466
Mark Brownbf3a9e12011-06-13 16:42:29 +01002467static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2468 const struct snd_soc_dapm_route *route)
2469{
2470 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2471 route->source,
2472 true);
2473 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2474 route->sink,
2475 true);
2476 struct snd_soc_dapm_path *path;
2477 int count = 0;
2478
2479 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002480 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002481 route->source);
2482 return -ENODEV;
2483 }
2484
2485 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002486 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002487 route->sink);
2488 return -ENODEV;
2489 }
2490
2491 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002492 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002493 route->source, route->sink);
2494
2495 list_for_each_entry(path, &source->sinks, list_source) {
2496 if (path->sink == sink) {
2497 path->weak = 1;
2498 count++;
2499 }
2500 }
2501
2502 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002503 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002504 route->source, route->sink);
2505 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002506 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002507 count, route->source, route->sink);
2508
2509 return 0;
2510}
2511
2512/**
2513 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2514 * @dapm: DAPM context
2515 * @route: audio routes
2516 * @num: number of routes
2517 *
2518 * Mark existing routes matching those specified in the passed array
2519 * as being weak, meaning that they are ignored for the purpose of
2520 * power decisions. The main intended use case is for sidetone paths
2521 * which couple audio between other independent paths if they are both
2522 * active in order to make the combination work better at the user
2523 * level but which aren't intended to be "used".
2524 *
2525 * Note that CODEC drivers should not use this as sidetone type paths
2526 * can frequently also be used as bypass paths.
2527 */
2528int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2529 const struct snd_soc_dapm_route *route, int num)
2530{
2531 int i, err;
2532 int ret = 0;
2533
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002534 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002535 for (i = 0; i < num; i++) {
2536 err = snd_soc_dapm_weak_route(dapm, route);
2537 if (err)
2538 ret = err;
2539 route++;
2540 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002541 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002542
2543 return ret;
2544}
2545EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2546
Mark Brown105f1c22008-05-13 14:52:19 +02002547/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002548 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002549 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002550 *
2551 * Checks the codec for any new dapm widgets and creates them if found.
2552 *
2553 * Returns 0 for success.
2554 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002555int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002556{
2557 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002558 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002559
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002560 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002561
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002562 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002563 {
2564 if (w->new)
2565 continue;
2566
Stephen Warrenfad59882011-04-28 17:37:59 -06002567 if (w->num_kcontrols) {
2568 w->kcontrols = kzalloc(w->num_kcontrols *
2569 sizeof(struct snd_kcontrol *),
2570 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002571 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002572 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002573 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002574 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002575 }
2576
Richard Purdie2b97eab2006-10-06 18:32:18 +02002577 switch(w->id) {
2578 case snd_soc_dapm_switch:
2579 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002580 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002581 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002582 break;
2583 case snd_soc_dapm_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002584 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002585 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002586 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002587 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002588 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002589 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002590 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002591 break;
2592 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002593
2594 /* Read the initial power state from the device */
2595 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002596 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002597 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002598 val &= w->mask;
2599 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002600 w->power = 1;
2601 }
2602
Richard Purdie2b97eab2006-10-06 18:32:18 +02002603 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002604
Mark Brown7508b122011-10-05 12:09:12 +01002605 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002606 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002607 }
2608
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002609 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2610 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002611 return 0;
2612}
2613EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2614
2615/**
2616 * snd_soc_dapm_get_volsw - dapm mixer get callback
2617 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002618 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002619 *
2620 * Callback to get the value of a dapm mixer control.
2621 *
2622 * Returns 0 for success.
2623 */
2624int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2625 struct snd_ctl_elem_value *ucontrol)
2626{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002627 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2628 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002629 struct soc_mixer_control *mc =
2630 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002631 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002632 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002633 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002634 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002635 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002636 unsigned int val;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002637 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002638
2639 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002640 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002641 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002642 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002643
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002644 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002645 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
2646 ret = soc_dapm_read(dapm, reg, &val);
2647 val = (val >> shift) & mask;
2648 } else {
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002649 val = dapm_kcontrol_get_value(kcontrol);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002650 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002651 mutex_unlock(&card->dapm_mutex);
2652
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002653 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002654 ucontrol->value.integer.value[0] = max - val;
2655 else
2656 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002657
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002658 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002659}
2660EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2661
2662/**
2663 * snd_soc_dapm_put_volsw - dapm mixer set callback
2664 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002665 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002666 *
2667 * Callback to set the value of a dapm mixer control.
2668 *
2669 * Returns 0 for success.
2670 */
2671int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2672 struct snd_ctl_elem_value *ucontrol)
2673{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002674 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2675 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002676 struct soc_mixer_control *mc =
2677 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002678 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002679 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002680 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002681 unsigned int mask = (1 << fls(max)) - 1;
2682 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002683 unsigned int val;
Jarkko Nikula18626c72014-06-09 14:20:29 +03002684 int connect, change, reg_change = 0;
Mark Brown97404f22010-12-14 16:13:57 +00002685 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002686 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002687
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002688 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002689 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002690 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002691 kcontrol->id.name);
2692
Richard Purdie2b97eab2006-10-06 18:32:18 +02002693 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002694 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002695
2696 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002697 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002698
Liam Girdwood3cd04342012-03-09 12:02:08 +00002699 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002700
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002701 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown283375c2009-12-07 18:09:03 +00002702
Jarkko Nikula18626c72014-06-09 14:20:29 +03002703 if (reg != SND_SOC_NOPM) {
2704 mask = mask << shift;
2705 val = val << shift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02002706
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002707 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
Jarkko Nikula18626c72014-06-09 14:20:29 +03002708 }
2709
2710 if (change || reg_change) {
2711 if (reg_change) {
2712 update.kcontrol = kcontrol;
2713 update.reg = reg;
2714 update.mask = mask;
2715 update.val = val;
2716 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002717 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03002718 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00002719
Nenghua Cao52765972013-12-13 20:13:49 +08002720 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002721
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002722 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002723 }
2724
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002725 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002726
2727 if (ret > 0)
2728 soc_dpcm_runtime_update(card);
2729
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002730 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002731}
2732EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2733
2734/**
2735 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2736 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002737 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002738 *
2739 * Callback to get the value of a dapm enumerated double mixer control.
2740 *
2741 * Returns 0 for success.
2742 */
2743int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2744 struct snd_ctl_elem_value *ucontrol)
2745{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002746 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002747 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002748 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002749
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002750 if (e->reg != SND_SOC_NOPM) {
2751 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
2752 if (ret)
2753 return ret;
2754 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002755 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002756 }
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002757
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002758 val = (reg_val >> e->shift_l) & e->mask;
2759 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2760 if (e->shift_l != e->shift_r) {
2761 val = (reg_val >> e->shift_r) & e->mask;
2762 val = snd_soc_enum_val_to_item(e, val);
2763 ucontrol->value.enumerated.item[1] = val;
2764 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002765
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002766 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002767}
2768EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2769
2770/**
2771 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2772 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002773 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002774 *
2775 * Callback to set the value of a dapm enumerated double mixer control.
2776 *
2777 * Returns 0 for success.
2778 */
2779int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2780 struct snd_ctl_elem_value *ucontrol)
2781{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002782 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2783 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002784 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002785 unsigned int *item = ucontrol->value.enumerated.item;
2786 unsigned int val, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002787 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002788 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002789 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002790
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002791 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002792 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002793
2794 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002795 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002796 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002797 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002798 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002799 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002800 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002801 }
2802
Liam Girdwood3cd04342012-03-09 12:02:08 +00002803 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002804
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002805 if (e->reg != SND_SOC_NOPM)
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002806 change = soc_dapm_test_bits(dapm, e->reg, mask, val);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002807 else
2808 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002809
Richard Purdie2b97eab2006-10-06 18:32:18 +02002810 if (change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002811 if (e->reg != SND_SOC_NOPM) {
2812 update.kcontrol = kcontrol;
2813 update.reg = e->reg;
2814 update.mask = mask;
2815 update.val = val;
2816 card->update = &update;
2817 }
Mark Brown3a655772009-10-05 17:23:30 +01002818
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002819 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002820
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002821 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002822 }
2823
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002824 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002825
2826 if (ret > 0)
2827 soc_dpcm_runtime_update(card);
2828
Mark Brown97404f22010-12-14 16:13:57 +00002829 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002830}
2831EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2832
2833/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002834 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2835 *
2836 * @kcontrol: mixer control
2837 * @uinfo: control element information
2838 *
2839 * Callback to provide information about a pin switch control.
2840 */
2841int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2842 struct snd_ctl_elem_info *uinfo)
2843{
2844 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2845 uinfo->count = 1;
2846 uinfo->value.integer.min = 0;
2847 uinfo->value.integer.max = 1;
2848
2849 return 0;
2850}
2851EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2852
2853/**
2854 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2855 *
2856 * @kcontrol: mixer control
2857 * @ucontrol: Value
2858 */
2859int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2860 struct snd_ctl_elem_value *ucontrol)
2861{
Mark Brown48a8c392012-02-14 17:11:15 -08002862 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002863 const char *pin = (const char *)kcontrol->private_value;
2864
Liam Girdwood3cd04342012-03-09 12:02:08 +00002865 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002866
2867 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002868 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002869
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002870 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002871
2872 return 0;
2873}
2874EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2875
2876/**
2877 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2878 *
2879 * @kcontrol: mixer control
2880 * @ucontrol: Value
2881 */
2882int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2883 struct snd_ctl_elem_value *ucontrol)
2884{
Mark Brown48a8c392012-02-14 17:11:15 -08002885 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002886 const char *pin = (const char *)kcontrol->private_value;
2887
Mark Brown8b37dbd2009-02-28 21:14:20 +00002888 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002889 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002890 else
Mark Brown48a8c392012-02-14 17:11:15 -08002891 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002892
Mark Brown48a8c392012-02-14 17:11:15 -08002893 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002894 return 0;
2895}
2896EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2897
Mark Brown5ba06fc2012-02-16 11:07:13 -08002898static struct snd_soc_dapm_widget *
2899snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2900 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002901{
2902 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002903 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00002904 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002905
2906 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08002907 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002908
Mark Brown62ea8742012-01-21 21:14:48 +00002909 switch (w->id) {
2910 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00002911 w->regulator = devm_regulator_get(dapm->dev, w->name);
2912 if (IS_ERR(w->regulator)) {
2913 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002914 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00002915 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002916 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00002917 }
Mark Brown8784c772013-01-10 19:33:47 +00002918
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002919 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00002920 ret = regulator_allow_bypass(w->regulator, true);
2921 if (ret != 0)
2922 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00002923 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00002924 w->name, ret);
2925 }
Mark Brown62ea8742012-01-21 21:14:48 +00002926 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02002927 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01002928#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01002929 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02002930 if (IS_ERR(w->clk)) {
2931 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002932 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02002933 w->name, ret);
2934 return NULL;
2935 }
Mark Brownec029952012-06-04 08:16:20 +01002936#else
2937 return NULL;
2938#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02002939 break;
Mark Brown62ea8742012-01-21 21:14:48 +00002940 default:
2941 break;
2942 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002943
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002944 prefix = soc_dapm_prefix(dapm);
2945 if (prefix)
2946 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002947 else
2948 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
2949
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002950 if (w->name == NULL) {
2951 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002952 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002953 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002954
Mark Brown7ca3a182011-10-08 14:04:50 +01002955 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002956 case snd_soc_dapm_mic:
2957 case snd_soc_dapm_input:
2958 w->is_source = 1;
2959 w->power_check = dapm_generic_check_power;
2960 break;
2961 case snd_soc_dapm_spk:
2962 case snd_soc_dapm_hp:
2963 case snd_soc_dapm_output:
2964 w->is_sink = 1;
2965 w->power_check = dapm_generic_check_power;
2966 break;
2967 case snd_soc_dapm_vmid:
2968 case snd_soc_dapm_siggen:
2969 w->is_source = 1;
2970 w->power_check = dapm_always_on_check_power;
2971 break;
2972 case snd_soc_dapm_mux:
Mark Brown7ca3a182011-10-08 14:04:50 +01002973 case snd_soc_dapm_switch:
2974 case snd_soc_dapm_mixer:
2975 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01002976 case snd_soc_dapm_adc:
2977 case snd_soc_dapm_aif_out:
2978 case snd_soc_dapm_dac:
2979 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01002980 case snd_soc_dapm_pga:
2981 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01002982 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01002983 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01002984 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02002985 case snd_soc_dapm_dai_out:
2986 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01002987 w->power_check = dapm_generic_check_power;
2988 break;
2989 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002990 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002991 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002992 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002993 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01002994 w->power_check = dapm_supply_check_power;
2995 break;
2996 default:
2997 w->power_check = dapm_always_on_check_power;
2998 break;
2999 }
3000
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003001 w->dapm = dapm;
Jarkko Nikulab2d9de52014-10-03 15:32:40 +03003002 if (dapm->component)
3003 w->codec = dapm->component->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003004 INIT_LIST_HEAD(&w->sources);
3005 INIT_LIST_HEAD(&w->sinks);
3006 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003007 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003008 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003009
3010 /* machine layer set ups unconnected pins and insertions */
3011 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003012 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003013}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003014
3015/**
Mark Brown4ba13272008-05-13 14:51:19 +02003016 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003017 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003018 * @widget: widget array
3019 * @num: number of widgets
3020 *
3021 * Creates new DAPM controls based upon the templates.
3022 *
3023 * Returns 0 for success else error.
3024 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003025int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003026 const struct snd_soc_dapm_widget *widget,
3027 int num)
3028{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003029 struct snd_soc_dapm_widget *w;
3030 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003031 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003032
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003033 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003034 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003035 w = snd_soc_dapm_new_control(dapm, widget);
3036 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003037 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003038 "ASoC: Failed to create DAPM control %s\n",
3039 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003040 ret = -ENOMEM;
3041 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003042 }
Mark Brown4ba13272008-05-13 14:51:19 +02003043 widget++;
3044 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003045 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003046 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003047}
3048EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3049
Mark Brownc74184e2012-04-04 22:12:09 +01003050static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3051 struct snd_kcontrol *kcontrol, int event)
3052{
3053 struct snd_soc_dapm_path *source_p, *sink_p;
3054 struct snd_soc_dai *source, *sink;
3055 const struct snd_soc_pcm_stream *config = w->params;
3056 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003057 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003058 u64 fmt;
3059 int ret;
3060
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003061 if (WARN_ON(!config) ||
3062 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3063 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003064
3065 /* We only support a single source and sink, pick the first */
3066 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3067 list_sink);
3068 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3069 list_source);
3070
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003071 if (WARN_ON(!source_p || !sink_p) ||
3072 WARN_ON(!sink_p->source || !source_p->sink) ||
3073 WARN_ON(!source_p->source || !sink_p->sink))
3074 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003075
3076 source = source_p->source->priv;
3077 sink = sink_p->sink->priv;
3078
3079 /* Be a little careful as we don't want to overflow the mask array */
3080 if (config->formats) {
3081 fmt = ffs(config->formats) - 1;
3082 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003083 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003084 config->formats);
3085 fmt = 0;
3086 }
3087
3088 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003089 params = kzalloc(sizeof(*params), GFP_KERNEL);
3090 if (!params) {
3091 ret = -ENOMEM;
3092 goto out;
3093 }
3094 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003095
Mark Brown9747cec2012-04-26 19:12:21 +01003096 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003097 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003098 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003099 config->rate_max;
3100
Mark Brown9747cec2012-04-26 19:12:21 +01003101 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003102 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003103 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003104 = config->channels_max;
3105
3106 memset(&substream, 0, sizeof(substream));
3107
3108 switch (event) {
3109 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003110 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3111 ret = soc_dai_hw_params(&substream, params, source);
3112 if (ret < 0)
3113 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003114
Benoit Cousson93e69582014-07-08 23:19:38 +02003115 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3116 ret = soc_dai_hw_params(&substream, params, sink);
3117 if (ret < 0)
3118 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003119 break;
3120
3121 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003122 ret = snd_soc_dai_digital_mute(sink, 0,
3123 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003124 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003125 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003126 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003127 break;
3128
3129 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003130 ret = snd_soc_dai_digital_mute(sink, 1,
3131 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003132 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003133 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003134 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003135 break;
3136
3137 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003138 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003139 return -EINVAL;
3140 }
3141
Mark Brown9747cec2012-04-26 19:12:21 +01003142out:
3143 kfree(params);
3144 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003145}
3146
3147int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3148 const struct snd_soc_pcm_stream *params,
3149 struct snd_soc_dapm_widget *source,
3150 struct snd_soc_dapm_widget *sink)
3151{
Mark Brownc74184e2012-04-04 22:12:09 +01003152 struct snd_soc_dapm_widget template;
3153 struct snd_soc_dapm_widget *w;
3154 size_t len;
3155 char *link_name;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003156 int ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003157
3158 len = strlen(source->name) + strlen(sink->name) + 2;
3159 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3160 if (!link_name)
3161 return -ENOMEM;
3162 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3163
3164 memset(&template, 0, sizeof(template));
3165 template.reg = SND_SOC_NOPM;
3166 template.id = snd_soc_dapm_dai_link;
3167 template.name = link_name;
3168 template.event = snd_soc_dai_link_event;
3169 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3170 SND_SOC_DAPM_PRE_PMD;
3171
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003172 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003173
3174 w = snd_soc_dapm_new_control(&card->dapm, &template);
3175 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003176 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003177 link_name);
3178 return -ENOMEM;
3179 }
3180
3181 w->params = params;
3182
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003183 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3184 if (ret)
3185 return ret;
3186 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Mark Brownc74184e2012-04-04 22:12:09 +01003187}
3188
Mark Brown888df392012-02-16 19:37:51 -08003189int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3190 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003191{
Mark Brown888df392012-02-16 19:37:51 -08003192 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003193 struct snd_soc_dapm_widget *w;
3194
Mark Brown888df392012-02-16 19:37:51 -08003195 WARN_ON(dapm->dev != dai->dev);
3196
3197 memset(&template, 0, sizeof(template));
3198 template.reg = SND_SOC_NOPM;
3199
3200 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003201 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003202 template.name = dai->driver->playback.stream_name;
3203 template.sname = dai->driver->playback.stream_name;
3204
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003205 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003206 template.name);
3207
3208 w = snd_soc_dapm_new_control(dapm, &template);
3209 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003210 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003211 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003212 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003213 }
3214
3215 w->priv = dai;
3216 dai->playback_widget = w;
3217 }
3218
3219 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003220 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003221 template.name = dai->driver->capture.stream_name;
3222 template.sname = dai->driver->capture.stream_name;
3223
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003224 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003225 template.name);
3226
3227 w = snd_soc_dapm_new_control(dapm, &template);
3228 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003229 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003230 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003231 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003232 }
3233
3234 w->priv = dai;
3235 dai->capture_widget = w;
3236 }
3237
3238 return 0;
3239}
3240
3241int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3242{
3243 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003244 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003245 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003246
3247 /* For each DAI widget... */
3248 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003249 switch (dai_w->id) {
3250 case snd_soc_dapm_dai_in:
3251 case snd_soc_dapm_dai_out:
3252 break;
3253 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003254 continue;
Mark Brown46162742013-06-05 19:36:11 +01003255 }
Mark Brown888df392012-02-16 19:37:51 -08003256
3257 dai = dai_w->priv;
3258
3259 /* ...find all widgets with the same stream and link them */
3260 list_for_each_entry(w, &card->widgets, list) {
3261 if (w->dapm != dai_w->dapm)
3262 continue;
3263
Mark Brown46162742013-06-05 19:36:11 +01003264 switch (w->id) {
3265 case snd_soc_dapm_dai_in:
3266 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003267 continue;
Mark Brown46162742013-06-05 19:36:11 +01003268 default:
3269 break;
3270 }
Mark Brown888df392012-02-16 19:37:51 -08003271
Russell King19c2c5f2013-08-04 20:24:03 +01003272 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003273 continue;
3274
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003275 if (dai_w->id == snd_soc_dapm_dai_in) {
3276 src = dai_w;
3277 sink = w;
3278 } else {
3279 src = w;
3280 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003281 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003282 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3283 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003284 }
3285 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003286
Mark Brown888df392012-02-16 19:37:51 -08003287 return 0;
3288}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003289
Benoit Cousson44ba2642014-07-08 23:19:36 +02003290static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3291 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003292{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003293 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003294 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003295 int i;
3296
Benoit Cousson44ba2642014-07-08 23:19:36 +02003297 for (i = 0; i < rtd->num_codecs; i++) {
3298 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003299
3300 /* there is no point in connecting BE DAI links with dummies */
3301 if (snd_soc_dai_is_dummy(codec_dai) ||
3302 snd_soc_dai_is_dummy(cpu_dai))
3303 continue;
3304
3305 /* connect BE DAI playback if widgets are valid */
3306 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003307 source = cpu_dai->playback_widget;
3308 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003309 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003310 cpu_dai->component->name, source->name,
3311 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003312
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003313 snd_soc_dapm_add_path(&card->dapm, source, sink,
3314 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003315 }
3316
3317 /* connect BE DAI capture if widgets are valid */
3318 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003319 source = codec_dai->capture_widget;
3320 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003321 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003322 codec_dai->component->name, source->name,
3323 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003324
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003325 snd_soc_dapm_add_path(&card->dapm, source, sink,
3326 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003327 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003328 }
3329}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003330
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003331static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3332 int event)
3333{
3334 struct snd_soc_dapm_widget *w;
3335
3336 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3337 w = dai->playback_widget;
3338 else
3339 w = dai->capture_widget;
3340
3341 if (w) {
3342 dapm_mark_dirty(w, "stream event");
3343
3344 switch (event) {
3345 case SND_SOC_DAPM_STREAM_START:
3346 w->active = 1;
3347 break;
3348 case SND_SOC_DAPM_STREAM_STOP:
3349 w->active = 0;
3350 break;
3351 case SND_SOC_DAPM_STREAM_SUSPEND:
3352 case SND_SOC_DAPM_STREAM_RESUME:
3353 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3354 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3355 break;
3356 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003357
3358 if (w->id == snd_soc_dapm_dai_in)
3359 w->is_source = w->active;
3360 else
3361 w->is_sink = w->active;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003362 }
3363}
3364
Benoit Cousson44ba2642014-07-08 23:19:36 +02003365void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3366{
3367 struct snd_soc_pcm_runtime *rtd = card->rtd;
3368 int i;
3369
3370 /* for each BE DAI link... */
3371 for (i = 0; i < card->num_rtd; i++) {
3372 rtd = &card->rtd[i];
3373
3374 /*
3375 * dynamic FE links have no fixed DAI mapping.
3376 * CODEC<->CODEC links have no direct connection.
3377 */
3378 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3379 continue;
3380
3381 dapm_connect_dai_link_widgets(card, rtd);
3382 }
3383}
3384
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003385static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3386 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003387{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003388 int i;
3389
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003390 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Benoit Cousson44ba2642014-07-08 23:19:36 +02003391 for (i = 0; i < rtd->num_codecs; i++)
3392 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003393
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003394 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003395}
3396
3397/**
3398 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3399 * @rtd: PCM runtime data
3400 * @stream: stream name
3401 * @event: stream event
3402 *
3403 * Sends a stream event to the dapm core. The core then makes any
3404 * necessary widget power changes.
3405 *
3406 * Returns 0 for success else error.
3407 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003408void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3409 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003410{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003411 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003412
Liam Girdwood3cd04342012-03-09 12:02:08 +00003413 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003414 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003415 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003416}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003417
3418/**
Charles Keepax11391102014-02-18 15:22:14 +00003419 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3420 * @dapm: DAPM context
3421 * @pin: pin name
3422 *
3423 * Enables input/output pin and its parents or children widgets iff there is
3424 * a valid audio route and active audio stream.
3425 *
3426 * Requires external locking.
3427 *
3428 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3429 * do any widget power switching.
3430 */
3431int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3432 const char *pin)
3433{
3434 return snd_soc_dapm_set_pin(dapm, pin, 1);
3435}
3436EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3437
3438/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003439 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003440 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003441 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003442 *
Mark Brown74b8f952009-06-06 11:26:15 +01003443 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003444 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003445 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003446 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3447 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003448 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003449int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003450{
Charles Keepax11391102014-02-18 15:22:14 +00003451 int ret;
3452
3453 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3454
3455 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3456
3457 mutex_unlock(&dapm->card->dapm_mutex);
3458
3459 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003460}
Liam Girdwooda5302182008-07-07 13:35:17 +01003461EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003462
3463/**
Charles Keepax11391102014-02-18 15:22:14 +00003464 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3465 * @dapm: DAPM context
3466 * @pin: pin name
3467 *
3468 * Enables input/output pin regardless of any other state. This is
3469 * intended for use with microphone bias supplies used in microphone
3470 * jack detection.
3471 *
3472 * Requires external locking.
3473 *
3474 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3475 * do any widget power switching.
3476 */
3477int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3478 const char *pin)
3479{
3480 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3481
3482 if (!w) {
3483 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3484 return -EINVAL;
3485 }
3486
3487 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3488 w->connected = 1;
3489 w->force = 1;
3490 dapm_mark_dirty(w, "force enable");
3491
3492 return 0;
3493}
3494EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3495
3496/**
Mark Brownda341832010-03-15 19:23:37 +00003497 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003498 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003499 * @pin: pin name
3500 *
3501 * Enables input/output pin regardless of any other state. This is
3502 * intended for use with microphone bias supplies used in microphone
3503 * jack detection.
3504 *
3505 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3506 * do any widget power switching.
3507 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003508int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3509 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003510{
Charles Keepax11391102014-02-18 15:22:14 +00003511 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003512
Charles Keepax11391102014-02-18 15:22:14 +00003513 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003514
Charles Keepax11391102014-02-18 15:22:14 +00003515 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003516
Charles Keepax11391102014-02-18 15:22:14 +00003517 mutex_unlock(&dapm->card->dapm_mutex);
3518
3519 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003520}
3521EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3522
3523/**
Charles Keepax11391102014-02-18 15:22:14 +00003524 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3525 * @dapm: DAPM context
3526 * @pin: pin name
3527 *
3528 * Disables input/output pin and its parents or children widgets.
3529 *
3530 * Requires external locking.
3531 *
3532 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3533 * do any widget power switching.
3534 */
3535int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3536 const char *pin)
3537{
3538 return snd_soc_dapm_set_pin(dapm, pin, 0);
3539}
3540EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3541
3542/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003543 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003544 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003545 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003546 *
Mark Brown74b8f952009-06-06 11:26:15 +01003547 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003548 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003549 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3550 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003551 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003552int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3553 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003554{
Charles Keepax11391102014-02-18 15:22:14 +00003555 int ret;
3556
3557 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3558
3559 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3560
3561 mutex_unlock(&dapm->card->dapm_mutex);
3562
3563 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003564}
3565EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3566
3567/**
Charles Keepax11391102014-02-18 15:22:14 +00003568 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3569 * @dapm: DAPM context
3570 * @pin: pin name
3571 *
3572 * Marks the specified pin as being not connected, disabling it along
3573 * any parent or child widgets. At present this is identical to
3574 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3575 * additional things such as disabling controls which only affect
3576 * paths through the pin.
3577 *
3578 * Requires external locking.
3579 *
3580 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3581 * do any widget power switching.
3582 */
3583int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3584 const char *pin)
3585{
3586 return snd_soc_dapm_set_pin(dapm, pin, 0);
3587}
3588EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3589
3590/**
Mark Brown5817b522008-09-24 11:23:11 +01003591 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003592 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003593 * @pin: pin name
3594 *
3595 * Marks the specified pin as being not connected, disabling it along
3596 * any parent or child widgets. At present this is identical to
3597 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3598 * additional things such as disabling controls which only affect
3599 * paths through the pin.
3600 *
3601 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3602 * do any widget power switching.
3603 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003604int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003605{
Charles Keepax11391102014-02-18 15:22:14 +00003606 int ret;
3607
3608 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3609
3610 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3611
3612 mutex_unlock(&dapm->card->dapm_mutex);
3613
3614 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003615}
3616EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3617
3618/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003619 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003620 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003621 * @pin: audio signal pin endpoint (or start point)
3622 *
3623 * Get audio pin status - connected or disconnected.
3624 *
3625 * Returns 1 for connected otherwise 0.
3626 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003627int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3628 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003629{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003630 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003631
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003632 if (w)
3633 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003634
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003635 return 0;
3636}
Liam Girdwooda5302182008-07-07 13:35:17 +01003637EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003638
3639/**
Mark Brown1547aba2010-05-07 21:11:40 +01003640 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003641 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003642 * @pin: audio signal pin endpoint (or start point)
3643 *
3644 * Mark the given endpoint or pin as ignoring suspend. When the
3645 * system is disabled a path between two endpoints flagged as ignoring
3646 * suspend will not be disabled. The path must already be enabled via
3647 * normal means at suspend time, it will not be turned on if it was not
3648 * already enabled.
3649 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003650int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3651 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003652{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003653 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003654
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003655 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003656 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003657 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003658 }
3659
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003660 w->ignore_suspend = 1;
3661
3662 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003663}
3664EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3665
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02003666/**
3667 * dapm_is_external_path() - Checks if a path is a external path
3668 * @card: The card the path belongs to
3669 * @path: The path to check
3670 *
3671 * Returns true if the path is either between two different DAPM contexts or
3672 * between two external pins of the same DAPM context. Otherwise returns
3673 * false.
3674 */
3675static bool dapm_is_external_path(struct snd_soc_card *card,
3676 struct snd_soc_dapm_path *path)
3677{
3678 dev_dbg(card->dev,
3679 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3680 path->source->name, path->source->id, path->source->dapm,
3681 path->sink->name, path->sink->id, path->sink->dapm);
3682
3683 /* Connection between two different DAPM contexts */
3684 if (path->source->dapm != path->sink->dapm)
3685 return true;
3686
3687 /* Loopback connection from external pin to external pin */
3688 if (path->sink->id == snd_soc_dapm_input) {
3689 switch (path->source->id) {
3690 case snd_soc_dapm_output:
3691 case snd_soc_dapm_micbias:
3692 return true;
3693 default:
3694 break;
3695 }
3696 }
3697
3698 return false;
3699}
3700
Stephen Warren16332812011-11-23 12:42:04 -07003701static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3702 struct snd_soc_dapm_widget *w)
3703{
3704 struct snd_soc_dapm_path *p;
3705
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02003706 list_for_each_entry(p, &w->sources, list_sink) {
3707 if (dapm_is_external_path(card, p))
3708 return true;
3709 }
Stephen Warren16332812011-11-23 12:42:04 -07003710
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02003711 list_for_each_entry(p, &w->sinks, list_source) {
3712 if (dapm_is_external_path(card, p))
3713 return true;
Stephen Warren16332812011-11-23 12:42:04 -07003714 }
3715
3716 return false;
3717}
3718
3719/**
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003720 * snd_soc_dapm_auto_nc_pins - call snd_soc_dapm_nc_pin for unused pins
3721 * @card: The card whose pins should be processed
Stephen Warren16332812011-11-23 12:42:04 -07003722 *
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003723 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the card
3724 * which are unused. Pins are used if they are connected externally to a
3725 * component, whether that be to some other device, or a loop-back connection to
3726 * the component itself.
Stephen Warren16332812011-11-23 12:42:04 -07003727 */
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003728void snd_soc_dapm_auto_nc_pins(struct snd_soc_card *card)
Stephen Warren16332812011-11-23 12:42:04 -07003729{
Stephen Warren16332812011-11-23 12:42:04 -07003730 struct snd_soc_dapm_widget *w;
3731
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003732 dev_dbg(card->dev, "ASoC: Auto NC: DAPMs: card:%p\n", &card->dapm);
Stephen Warren16332812011-11-23 12:42:04 -07003733
3734 list_for_each_entry(w, &card->widgets, list) {
Stephen Warren16332812011-11-23 12:42:04 -07003735 switch (w->id) {
3736 case snd_soc_dapm_input:
3737 case snd_soc_dapm_output:
3738 case snd_soc_dapm_micbias:
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003739 dev_dbg(card->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003740 w->name);
3741 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003742 dev_dbg(card->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003743 "... Not in map; disabling\n");
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003744 snd_soc_dapm_nc_pin(w->dapm, w->name);
Stephen Warren16332812011-11-23 12:42:04 -07003745 }
3746 break;
3747 default:
3748 break;
3749 }
3750 }
3751}
3752
Mark Brown1547aba2010-05-07 21:11:40 +01003753/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003754 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003755 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003756 *
3757 * Free all dapm widgets and resources.
3758 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003759void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003760{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003761 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003762 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003763 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003764 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003765}
3766EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3767
Xiang Xiao57996352014-03-02 00:04:02 +08003768static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003769{
Liam Girdwood01005a72012-07-06 16:57:05 +01003770 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003771 struct snd_soc_dapm_widget *w;
3772 LIST_HEAD(down_list);
3773 int powerdown = 0;
3774
Liam Girdwood01005a72012-07-06 16:57:05 +01003775 mutex_lock(&card->dapm_mutex);
3776
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003777 list_for_each_entry(w, &dapm->card->widgets, list) {
3778 if (w->dapm != dapm)
3779 continue;
Mark Brown51737472009-06-22 13:16:51 +01003780 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003781 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003782 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003783 powerdown = 1;
3784 }
3785 }
3786
3787 /* If there were no widgets to power down we're already in
3788 * standby.
3789 */
3790 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003791 if (dapm->bias_level == SND_SOC_BIAS_ON)
3792 snd_soc_dapm_set_bias_level(dapm,
3793 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003794 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003795 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3796 snd_soc_dapm_set_bias_level(dapm,
3797 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003798 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003799
3800 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003801}
Mark Brown51737472009-06-22 13:16:51 +01003802
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003803/*
3804 * snd_soc_dapm_shutdown - callback for system shutdown
3805 */
3806void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3807{
Xiang Xiao57996352014-03-02 00:04:02 +08003808 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003809
Xiang Xiao57996352014-03-02 00:04:02 +08003810 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08003811 if (dapm != &card->dapm) {
3812 soc_dapm_shutdown_dapm(dapm);
3813 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3814 snd_soc_dapm_set_bias_level(dapm,
3815 SND_SOC_BIAS_OFF);
3816 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003817 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08003818
3819 soc_dapm_shutdown_dapm(&card->dapm);
3820 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3821 snd_soc_dapm_set_bias_level(&card->dapm,
3822 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01003823}
3824
Richard Purdie2b97eab2006-10-06 18:32:18 +02003825/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003826MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003827MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3828MODULE_LICENSE("GPL");