blob: 5db8df2f8866b040ebf8e40bdd0ee6a97d9f4ea2 [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
Richard Purdie2b97eab2006-10-06 18:32:18 +020050/* dapm power sequences - make this per codec in the future */
51static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010052 [snd_soc_dapm_pre] = 0,
53 [snd_soc_dapm_supply] = 1,
Mark Brown62ea8742012-01-21 21:14:48 +000054 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020055 [snd_soc_dapm_clock_supply] = 1,
Mark Brown38357ab2009-06-06 19:03:23 +010056 [snd_soc_dapm_micbias] = 2,
Mark Brownc74184e2012-04-04 22:12:09 +010057 [snd_soc_dapm_dai_link] = 2,
Mark Brown46162742013-06-05 19:36:11 +010058 [snd_soc_dapm_dai_in] = 3,
59 [snd_soc_dapm_dai_out] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010060 [snd_soc_dapm_aif_in] = 3,
61 [snd_soc_dapm_aif_out] = 3,
62 [snd_soc_dapm_mic] = 4,
63 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000064 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010065 [snd_soc_dapm_value_mux] = 5,
66 [snd_soc_dapm_dac] = 6,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020067 [snd_soc_dapm_switch] = 7,
Mark Brown010ff262009-08-17 17:39:22 +010068 [snd_soc_dapm_mixer] = 7,
69 [snd_soc_dapm_mixer_named_ctl] = 7,
70 [snd_soc_dapm_pga] = 8,
71 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060072 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010073 [snd_soc_dapm_hp] = 10,
74 [snd_soc_dapm_spk] = 10,
Mark Brown7e1f7c82012-04-12 17:29:36 +010075 [snd_soc_dapm_line] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010076 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020077};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000078
Richard Purdie2b97eab2006-10-06 18:32:18 +020079static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010080 [snd_soc_dapm_pre] = 0,
81 [snd_soc_dapm_adc] = 1,
82 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010083 [snd_soc_dapm_spk] = 2,
Mark Brown7e1f7c82012-04-12 17:29:36 +010084 [snd_soc_dapm_line] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060085 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010086 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020087 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +010088 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010089 [snd_soc_dapm_mixer] = 5,
90 [snd_soc_dapm_dac] = 6,
91 [snd_soc_dapm_mic] = 7,
92 [snd_soc_dapm_micbias] = 8,
93 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000094 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010095 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010096 [snd_soc_dapm_aif_in] = 10,
97 [snd_soc_dapm_aif_out] = 10,
Mark Brown46162742013-06-05 19:36:11 +010098 [snd_soc_dapm_dai_in] = 10,
99 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100100 [snd_soc_dapm_dai_link] = 11,
Ola Liljad7e7eb92012-05-24 15:26:25 +0200101 [snd_soc_dapm_clock_supply] = 12,
Mark Brownc74184e2012-04-04 22:12:09 +0100102 [snd_soc_dapm_regulator_supply] = 12,
103 [snd_soc_dapm_supply] = 12,
104 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200105};
106
Troy Kisky12ef1932008-10-13 17:42:14 -0700107static void pop_wait(u32 pop_time)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100108{
109 if (pop_time)
110 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
111}
112
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200113static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100114{
115 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200116 char *buf;
117
118 if (!pop_time)
119 return;
120
121 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
122 if (buf == NULL)
123 return;
Mark Brown15e4c72f2008-07-02 11:51:20 +0100124
125 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200126 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100127 dev_info(dev, "%s", buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100128 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200129
130 kfree(buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100131}
132
Mark Browndb432b42011-10-03 21:06:40 +0100133static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
134{
135 return !list_empty(&w->dirty);
136}
137
Mark Brown25c77c52011-10-08 13:36:03 +0100138void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100139{
Mark Brown75c1f892011-10-04 22:28:08 +0100140 if (!dapm_dirty_widget(w)) {
141 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
142 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100143 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100144 }
Mark Browndb432b42011-10-03 21:06:40 +0100145}
Mark Brown25c77c52011-10-08 13:36:03 +0100146EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100147
Mark Browne2d32ff2012-08-31 17:38:32 -0700148void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
149{
150 struct snd_soc_card *card = dapm->card;
151 struct snd_soc_dapm_widget *w;
152
153 mutex_lock(&card->dapm_mutex);
154
155 list_for_each_entry(w, &card->widgets, list) {
156 switch (w->id) {
157 case snd_soc_dapm_input:
158 case snd_soc_dapm_output:
159 dapm_mark_dirty(w, "Rechecking inputs and outputs");
160 break;
161 default:
162 break;
163 }
164 }
165
166 mutex_unlock(&card->dapm_mutex);
167}
168EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
169
Richard Purdie2b97eab2006-10-06 18:32:18 +0200170/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100171static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200172 const struct snd_soc_dapm_widget *_widget)
173{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100174 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200175}
176
Liam Girdwood6c120e12012-02-15 15:15:34 +0000177static void dapm_reset(struct snd_soc_card *card)
178{
179 struct snd_soc_dapm_widget *w;
180
181 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
182
183 list_for_each_entry(w, &card->widgets, list) {
184 w->power_checked = false;
185 w->inputs = -1;
186 w->outputs = -1;
187 }
188}
189
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100190static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
191{
192 if (w->codec)
193 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100194 else if (w->platform)
195 return snd_soc_platform_read(w->platform, reg);
196
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000197 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100198 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100199}
200
201static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
202{
203 if (w->codec)
204 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100205 else if (w->platform)
206 return snd_soc_platform_write(w->platform, reg, val);
207
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000208 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100209 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100210}
211
Liam Girdwood49575fb52012-03-06 18:16:19 +0000212static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
213{
Mark Browne06ab3b2012-03-06 23:58:22 +0000214 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000215 mutex_lock(&w->codec->mutex);
216 else if (w->platform)
217 mutex_lock(&w->platform->mutex);
218}
219
220static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
221{
Mark Browne06ab3b2012-03-06 23:58:22 +0000222 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000223 mutex_unlock(&w->codec->mutex);
224 else if (w->platform)
225 mutex_unlock(&w->platform->mutex);
226}
227
228static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100229 unsigned short reg, unsigned int mask, unsigned int value)
230{
Mark Brown8a713da2011-12-03 12:33:55 +0000231 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100232 unsigned int old, new;
233 int ret;
234
Mark Brown8a713da2011-12-03 12:33:55 +0000235 if (w->codec && w->codec->using_regmap) {
236 ret = regmap_update_bits_check(w->codec->control_data,
237 reg, mask, value, &change);
238 if (ret != 0)
239 return ret;
240 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000241 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000242 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000243 if (ret < 0) {
244 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100245 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000246 }
Mark Brown8a713da2011-12-03 12:33:55 +0000247
248 old = ret;
249 new = (old & ~mask) | (value & mask);
250 change = old != new;
251 if (change) {
252 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000253 if (ret < 0) {
254 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000255 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000256 }
Mark Brown8a713da2011-12-03 12:33:55 +0000257 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000258 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100259 }
260
261 return change;
262}
263
Mark Brown452c5ea2009-05-17 21:41:23 +0100264/**
265 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800266 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100267 * @level: level to configure
268 *
269 * Configure the bias (power) levels for the SoC audio device.
270 *
271 * Returns 0 for success else error.
272 */
Mark Browned5a4c42011-02-18 11:12:42 -0800273static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200274 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100275{
Mark Browned5a4c42011-02-18 11:12:42 -0800276 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100277 int ret = 0;
278
Mark Brown84e90932010-11-04 00:07:02 -0400279 trace_snd_soc_bias_level_start(card, level);
280
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000281 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100282 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100283 if (ret != 0)
284 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100285
Mark Browncc4c6702011-06-06 19:03:34 +0100286 if (dapm->codec) {
287 if (dapm->codec->driver->set_bias_level)
288 ret = dapm->codec->driver->set_bias_level(dapm->codec,
289 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100290 else
291 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100292 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100293 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100294 }
Liam Girdwood41231282012-07-06 16:56:16 +0100295
Mark Brown171ec6b2011-06-06 18:15:19 +0100296 if (ret != 0)
297 goto out;
298
299 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100300 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100301out:
Mark Brown84e90932010-11-04 00:07:02 -0400302 trace_snd_soc_bias_level_done(card, level);
303
Mark Brown452c5ea2009-05-17 21:41:23 +0100304 return ret;
305}
306
Richard Purdie2b97eab2006-10-06 18:32:18 +0200307/* set up initial codec paths */
308static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
309 struct snd_soc_dapm_path *p, int i)
310{
311 switch (w->id) {
312 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000313 case snd_soc_dapm_mixer:
314 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200315 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100316 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600317 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400318 unsigned int reg = mc->reg;
319 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100320 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400321 unsigned int mask = (1 << fls(max)) - 1;
322 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200323
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100324 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200325 val = (val >> shift) & mask;
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200326 if (invert)
327 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200328
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200329 p->connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200330 }
331 break;
332 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600333 struct soc_enum *e = (struct soc_enum *)
334 w->kcontrol_news[i].private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200335 int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200336
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100337 val = soc_widget_read(w, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200338 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200339
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200340 if (item < e->max && !strcmp(p->name, e->texts[item]))
341 p->connect = 1;
342 else
343 p->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200344 }
345 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000346 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600347 struct soc_enum *e = (struct soc_enum *)
348 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000349
350 p->connect = 0;
351 /* since a virtual mux has no backing registers to
352 * decide which path to connect, it will try to match
353 * with the first enumeration. This is to ensure
354 * that the default mux choice (the first) will be
355 * correctly powered up during initialization.
356 */
357 if (!strcmp(p->name, e->texts[0]))
358 p->connect = 1;
359 }
360 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200361 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200362 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600363 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200364 int val, item;
365
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100366 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200367 val = (val >> e->shift_l) & e->mask;
368 for (item = 0; item < e->max; item++) {
369 if (val == e->values[item])
370 break;
371 }
372
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200373 if (item < e->max && !strcmp(p->name, e->texts[item]))
374 p->connect = 1;
375 else
376 p->connect = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200377 }
378 break;
Mark Brown56563102011-10-03 22:41:09 +0100379 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200380 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600381 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200382 case snd_soc_dapm_output:
383 case snd_soc_dapm_adc:
384 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000385 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200386 case snd_soc_dapm_dac:
387 case snd_soc_dapm_micbias:
388 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100389 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000390 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200391 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100392 case snd_soc_dapm_aif_in:
393 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100394 case snd_soc_dapm_dai_in:
395 case snd_soc_dapm_dai_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200396 case snd_soc_dapm_hp:
397 case snd_soc_dapm_mic:
398 case snd_soc_dapm_spk:
399 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100400 case snd_soc_dapm_dai_link:
Mark Brown56563102011-10-03 22:41:09 +0100401 p->connect = 1;
402 break;
403 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200404 case snd_soc_dapm_pre:
405 case snd_soc_dapm_post:
406 p->connect = 0;
407 break;
408 }
409}
410
Mark Brown74b8f952009-06-06 11:26:15 +0100411/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200412static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200413 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
414 struct snd_soc_dapm_path *path, const char *control_name,
415 const struct snd_kcontrol_new *kcontrol)
416{
417 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
418 int i;
419
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100420 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200421 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200422 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200423 list_add(&path->list_sink, &dest->sources);
424 list_add(&path->list_source, &src->sinks);
425 path->name = (char*)e->texts[i];
426 dapm_set_path_status(dest, path, 0);
427 return 0;
428 }
429 }
430
431 return -ENODEV;
432}
433
Mark Brown74b8f952009-06-06 11:26:15 +0100434/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200435static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200436 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
437 struct snd_soc_dapm_path *path, const char *control_name)
438{
439 int i;
440
441 /* search for mixer kcontrol */
442 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600443 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200444 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200445 list_add(&path->list_sink, &dest->sources);
446 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600447 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200448 dapm_set_path_status(dest, path, i);
449 return 0;
450 }
451 }
452 return -ENODEV;
453}
454
Stephen Warrenaf468002011-04-28 17:38:01 -0600455static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600456 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600457 const struct snd_kcontrol_new *kcontrol_new,
458 struct snd_kcontrol **kcontrol)
459{
460 struct snd_soc_dapm_widget *w;
461 int i;
462
463 *kcontrol = NULL;
464
465 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600466 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
467 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600468 for (i = 0; i < w->num_kcontrols; i++) {
469 if (&w->kcontrol_news[i] == kcontrol_new) {
470 if (w->kcontrols)
471 *kcontrol = w->kcontrols[i];
472 return 1;
473 }
474 }
475 }
476
477 return 0;
478}
479
Lars-Peter Clausen6b75bf02013-06-14 13:16:51 +0200480static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
481{
482 kfree(kctl->private_data);
483}
484
Stephen Warren85762e72013-03-29 15:40:10 -0600485/*
486 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
487 * create it. Either way, add the widget into the control's widget list
488 */
489static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
490 int kci, struct snd_soc_dapm_path *path)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200491{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200492 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000493 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000494 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600495 size_t prefix_len;
496 int shared;
497 struct snd_kcontrol *kcontrol;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600498 struct snd_soc_dapm_widget_list *wlist;
Stephen Warren85762e72013-03-29 15:40:10 -0600499 int wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600500 size_t wlistsize;
Stephen Warren85762e72013-03-29 15:40:10 -0600501 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600502 char *long_name;
503 const char *name;
504 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000505
506 if (dapm->codec)
507 prefix = dapm->codec->name_prefix;
508 else
509 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200510
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000511 if (prefix)
512 prefix_len = strlen(prefix) + 1;
513 else
514 prefix_len = 0;
515
Stephen Warren85762e72013-03-29 15:40:10 -0600516 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
517 &kcontrol);
518
519 if (kcontrol) {
520 wlist = kcontrol->private_data;
521 wlistentries = wlist->num_widgets + 1;
522 } else {
523 wlist = NULL;
524 wlistentries = 1;
525 }
526
527 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
528 wlistentries * sizeof(struct snd_soc_dapm_widget *);
529 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
530 if (wlist == NULL) {
531 dev_err(dapm->dev, "ASoC: can't allocate widget list for %s\n",
532 w->name);
533 return -ENOMEM;
534 }
535 wlist->num_widgets = wlistentries;
536 wlist->widgets[wlistentries - 1] = w;
537
538 if (!kcontrol) {
539 if (shared) {
540 wname_in_long_name = false;
541 kcname_in_long_name = true;
542 } else {
543 switch (w->id) {
544 case snd_soc_dapm_switch:
545 case snd_soc_dapm_mixer:
546 wname_in_long_name = true;
547 kcname_in_long_name = true;
548 break;
549 case snd_soc_dapm_mixer_named_ctl:
550 wname_in_long_name = false;
551 kcname_in_long_name = true;
552 break;
553 case snd_soc_dapm_mux:
554 case snd_soc_dapm_virt_mux:
555 case snd_soc_dapm_value_mux:
556 wname_in_long_name = true;
557 kcname_in_long_name = false;
558 break;
559 default:
560 kfree(wlist);
561 return -EINVAL;
562 }
563 }
564
565 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600566 /*
567 * The control will get a prefix from the control
568 * creation process but we're also using the same
569 * prefix for widgets so cut the prefix off the
570 * front of the widget name.
571 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200572 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600573 w->name + prefix_len,
574 w->kcontrol_news[kci].name);
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200575 if (long_name == NULL) {
576 kfree(wlist);
577 return -ENOMEM;
578 }
Stephen Warren85762e72013-03-29 15:40:10 -0600579
580 name = long_name;
581 } else if (wname_in_long_name) {
582 long_name = NULL;
583 name = w->name + prefix_len;
584 } else {
585 long_name = NULL;
586 name = w->kcontrol_news[kci].name;
587 }
588
589 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], wlist, name,
590 prefix);
Lars-Peter Clausen6b75bf02013-06-14 13:16:51 +0200591 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200592 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600593 ret = snd_ctl_add(card, kcontrol);
594 if (ret < 0) {
595 dev_err(dapm->dev,
596 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
597 w->name, name, ret);
598 kfree(wlist);
Stephen Warren85762e72013-03-29 15:40:10 -0600599 return ret;
600 }
Stephen Warren85762e72013-03-29 15:40:10 -0600601 }
602
603 kcontrol->private_data = wlist;
604 w->kcontrols[kci] = kcontrol;
605 path->kcontrol = kcontrol;
606
607 return 0;
608}
609
610/* create new dapm mixer control */
611static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
612{
613 int i, ret;
614 struct snd_soc_dapm_path *path;
615
Richard Purdie2b97eab2006-10-06 18:32:18 +0200616 /* add kcontrol */
617 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200618 /* match name */
619 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200620 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600621 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200622 continue;
623
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200624 if (w->kcontrols[i]) {
625 path->kcontrol = w->kcontrols[i];
626 continue;
627 }
628
Stephen Warren85762e72013-03-29 15:40:10 -0600629 ret = dapm_create_or_share_mixmux_kcontrol(w, i, path);
630 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200631 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200632 }
633 }
Stephen Warren85762e72013-03-29 15:40:10 -0600634
635 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200636}
637
638/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200639static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200640{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200641 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600642 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600643 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200644
Stephen Warrenaf468002011-04-28 17:38:01 -0600645 if (w->num_kcontrols != 1) {
646 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000647 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600648 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200649 return -EINVAL;
650 }
651
Stephen Warren85762e72013-03-29 15:40:10 -0600652 path = list_first_entry(&w->sources, struct snd_soc_dapm_path,
653 list_sink);
654 if (!path) {
655 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
656 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600657 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200658
Stephen Warren85762e72013-03-29 15:40:10 -0600659 ret = dapm_create_or_share_mixmux_kcontrol(w, 0, path);
660 if (ret < 0)
661 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600662
Richard Purdie2b97eab2006-10-06 18:32:18 +0200663 list_for_each_entry(path, &w->sources, list_sink)
Stephen Warren85762e72013-03-29 15:40:10 -0600664 path->kcontrol = w->kcontrols[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200665
Stephen Warrenaf468002011-04-28 17:38:01 -0600666 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200667}
668
669/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200670static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200671{
Mark Browna6c65732010-03-03 17:45:21 +0000672 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200673 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000674 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200675
Mark Browna6c65732010-03-03 17:45:21 +0000676 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200677}
678
679/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100680static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
681 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200682{
683 struct snd_soc_dapm_path *p;
684
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100685 list_for_each_entry(p, sink, list_source) {
686 if (p->walked) {
687 p->walked = 0;
688 dapm_clear_walk_output(dapm, &p->sink->sinks);
689 }
690 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200691}
692
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100693static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
694 struct list_head *source)
695{
696 struct snd_soc_dapm_path *p;
697
698 list_for_each_entry(p, source, list_sink) {
699 if (p->walked) {
700 p->walked = 0;
701 dapm_clear_walk_input(dapm, &p->source->sources);
702 }
703 }
704}
705
706
Mark Brown9949788b2010-05-07 20:24:05 +0100707/* We implement power down on suspend by checking the power state of
708 * the ALSA card - when we are suspending the ALSA state for the card
709 * is set to D3.
710 */
711static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
712{
Mark Brown12ea2c72011-03-02 18:17:32 +0000713 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100714
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000715 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100716 case SNDRV_CTL_POWER_D3hot:
717 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100718 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000719 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200720 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100721 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100722 default:
723 return 1;
724 }
725}
726
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100727/* add widget to list if it's not already in the list */
728static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
729 struct snd_soc_dapm_widget *w)
730{
731 struct snd_soc_dapm_widget_list *wlist;
732 int wlistsize, wlistentries, i;
733
734 if (*list == NULL)
735 return -EINVAL;
736
737 wlist = *list;
738
739 /* is this widget already in the list */
740 for (i = 0; i < wlist->num_widgets; i++) {
741 if (wlist->widgets[i] == w)
742 return 0;
743 }
744
745 /* allocate some new space */
746 wlistentries = wlist->num_widgets + 1;
747 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
748 wlistentries * sizeof(struct snd_soc_dapm_widget *);
749 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
750 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000751 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100752 w->name);
753 return -ENOMEM;
754 }
755 wlist = *list;
756
757 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000758 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100759 w->name, wlist->num_widgets);
760
761 wlist->widgets[wlist->num_widgets] = w;
762 wlist->num_widgets++;
763 return 1;
764}
765
Richard Purdie2b97eab2006-10-06 18:32:18 +0200766/*
767 * Recursively check for a completed path to an active or physically connected
768 * output widget. Returns number of complete paths.
769 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100770static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
771 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200772{
773 struct snd_soc_dapm_path *path;
774 int con = 0;
775
Mark Brown024dc072011-10-09 11:52:05 +0100776 if (widget->outputs >= 0)
777 return widget->outputs;
778
Mark Brownde02d072011-09-20 21:43:24 +0100779 DAPM_UPDATE_STAT(widget, path_checks);
780
Mark Brown62ea8742012-01-21 21:14:48 +0000781 switch (widget->id) {
782 case snd_soc_dapm_supply:
783 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200784 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100785 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000786 default:
787 break;
788 }
Mark Brown246d0a12009-04-22 18:24:55 +0100789
Mark Brown010ff262009-08-17 17:39:22 +0100790 switch (widget->id) {
791 case snd_soc_dapm_adc:
792 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100793 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100794 if (widget->active) {
795 widget->outputs = snd_soc_dapm_suspend_check(widget);
796 return widget->outputs;
797 }
Mark Brown010ff262009-08-17 17:39:22 +0100798 default:
799 break;
800 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200801
802 if (widget->connected) {
803 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100804 if (widget->id == snd_soc_dapm_output && !widget->ext) {
805 widget->outputs = snd_soc_dapm_suspend_check(widget);
806 return widget->outputs;
807 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200808
809 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100810 if (widget->id == snd_soc_dapm_hp ||
811 widget->id == snd_soc_dapm_spk ||
812 (widget->id == snd_soc_dapm_line &&
813 !list_empty(&widget->sources))) {
814 widget->outputs = snd_soc_dapm_suspend_check(widget);
815 return widget->outputs;
816 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200817 }
818
819 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100820 DAPM_UPDATE_STAT(widget, neighbour_checks);
821
Mark Brownbf3a9e12011-06-13 16:42:29 +0100822 if (path->weak)
823 continue;
824
Mark Brown8af294b2013-02-22 17:48:15 +0000825 if (path->walking)
826 return 1;
827
Richard Purdie2b97eab2006-10-06 18:32:18 +0200828 if (path->walked)
829 continue;
830
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100831 trace_snd_soc_dapm_output_path(widget, path);
832
Richard Purdie2b97eab2006-10-06 18:32:18 +0200833 if (path->sink && path->connect) {
834 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000835 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100836
837 /* do we need to add this widget to the list ? */
838 if (list) {
839 int err;
840 err = dapm_list_add_widget(list, path->sink);
841 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000842 dev_err(widget->dapm->dev,
843 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100844 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000845 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100846 return con;
847 }
848 }
849
850 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000851
852 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200853 }
854 }
855
Mark Brown024dc072011-10-09 11:52:05 +0100856 widget->outputs = con;
857
Richard Purdie2b97eab2006-10-06 18:32:18 +0200858 return con;
859}
860
861/*
862 * Recursively check for a completed path to an active or physically connected
863 * input widget. Returns number of complete paths.
864 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100865static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
866 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200867{
868 struct snd_soc_dapm_path *path;
869 int con = 0;
870
Mark Brown024dc072011-10-09 11:52:05 +0100871 if (widget->inputs >= 0)
872 return widget->inputs;
873
Mark Brownde02d072011-09-20 21:43:24 +0100874 DAPM_UPDATE_STAT(widget, path_checks);
875
Mark Brown62ea8742012-01-21 21:14:48 +0000876 switch (widget->id) {
877 case snd_soc_dapm_supply:
878 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200879 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100880 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000881 default:
882 break;
883 }
Mark Brown246d0a12009-04-22 18:24:55 +0100884
Richard Purdie2b97eab2006-10-06 18:32:18 +0200885 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100886 switch (widget->id) {
887 case snd_soc_dapm_dac:
888 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +0100889 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +0100890 if (widget->active) {
891 widget->inputs = snd_soc_dapm_suspend_check(widget);
892 return widget->inputs;
893 }
Mark Brown010ff262009-08-17 17:39:22 +0100894 default:
895 break;
896 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200897
898 if (widget->connected) {
899 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100900 if (widget->id == snd_soc_dapm_input && !widget->ext) {
901 widget->inputs = snd_soc_dapm_suspend_check(widget);
902 return widget->inputs;
903 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200904
905 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100906 if (widget->id == snd_soc_dapm_vmid) {
907 widget->inputs = snd_soc_dapm_suspend_check(widget);
908 return widget->inputs;
909 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200910
911 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300912 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100913 (widget->id == snd_soc_dapm_line &&
914 !list_empty(&widget->sinks))) {
915 widget->inputs = snd_soc_dapm_suspend_check(widget);
916 return widget->inputs;
917 }
918
Mark Brown1ab97c82011-11-27 16:21:51 +0000919 /* signal generator */
920 if (widget->id == snd_soc_dapm_siggen) {
921 widget->inputs = snd_soc_dapm_suspend_check(widget);
922 return widget->inputs;
923 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200924 }
925
926 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +0100927 DAPM_UPDATE_STAT(widget, neighbour_checks);
928
Mark Brownbf3a9e12011-06-13 16:42:29 +0100929 if (path->weak)
930 continue;
931
Mark Brown8af294b2013-02-22 17:48:15 +0000932 if (path->walking)
933 return 1;
934
Richard Purdie2b97eab2006-10-06 18:32:18 +0200935 if (path->walked)
936 continue;
937
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100938 trace_snd_soc_dapm_input_path(widget, path);
939
Richard Purdie2b97eab2006-10-06 18:32:18 +0200940 if (path->source && path->connect) {
941 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000942 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100943
944 /* do we need to add this widget to the list ? */
945 if (list) {
946 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +0100947 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100948 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000949 dev_err(widget->dapm->dev,
950 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100951 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000952 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100953 return con;
954 }
955 }
956
957 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000958
959 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200960 }
961 }
962
Mark Brown024dc072011-10-09 11:52:05 +0100963 widget->inputs = con;
964
Richard Purdie2b97eab2006-10-06 18:32:18 +0200965 return con;
966}
967
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100968/**
969 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
970 * @dai: the soc DAI.
971 * @stream: stream direction.
972 * @list: list of active widgets for this stream.
973 *
974 * Queries DAPM graph as to whether an valid audio stream path exists for
975 * the initial stream specified by name. This takes into account
976 * current mixer and mux kcontrol settings. Creates list of valid widgets.
977 *
978 * Returns the number of valid paths or negative error.
979 */
980int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
981 struct snd_soc_dapm_widget_list **list)
982{
983 struct snd_soc_card *card = dai->card;
984 int paths;
985
986 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
987 dapm_reset(card);
988
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100989 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100990 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100991 dapm_clear_walk_output(&card->dapm,
992 &dai->playback_widget->sinks);
993 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +0100994 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100995 dapm_clear_walk_input(&card->dapm,
996 &dai->capture_widget->sources);
997 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100998
999 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001000 mutex_unlock(&card->dapm_mutex);
1001
1002 return paths;
1003}
1004
Richard Purdie2b97eab2006-10-06 18:32:18 +02001005/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001006 * Handler for generic register modifier widget.
1007 */
1008int dapm_reg_event(struct snd_soc_dapm_widget *w,
1009 struct snd_kcontrol *kcontrol, int event)
1010{
1011 unsigned int val;
1012
1013 if (SND_SOC_DAPM_EVENT_ON(event))
1014 val = w->on_val;
1015 else
1016 val = w->off_val;
1017
Liam Girdwood49575fb52012-03-06 18:16:19 +00001018 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001019 w->mask << w->shift, val << w->shift);
1020
1021 return 0;
1022}
Mark Brown11589412008-07-29 11:42:23 +01001023EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001024
Mark Brown62ea8742012-01-21 21:14:48 +00001025/*
1026 * Handler for regulator supply widget.
1027 */
1028int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1029 struct snd_kcontrol *kcontrol, int event)
1030{
Mark Brownc05b84d2012-09-07 12:57:11 +08001031 int ret;
1032
1033 if (SND_SOC_DAPM_EVENT_ON(event)) {
1034 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001035 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001036 if (ret != 0)
1037 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001038 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001039 w->name, ret);
1040 }
1041
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001042 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001043 } else {
1044 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001045 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001046 if (ret != 0)
1047 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001048 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001049 w->name, ret);
1050 }
1051
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001052 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001053 }
Mark Brown62ea8742012-01-21 21:14:48 +00001054}
1055EXPORT_SYMBOL_GPL(dapm_regulator_event);
1056
Ola Liljad7e7eb92012-05-24 15:26:25 +02001057/*
1058 * Handler for clock supply widget.
1059 */
1060int dapm_clock_event(struct snd_soc_dapm_widget *w,
1061 struct snd_kcontrol *kcontrol, int event)
1062{
1063 if (!w->clk)
1064 return -EIO;
1065
Mark Brownec029952012-06-04 08:16:20 +01001066#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001067 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001068 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001069 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001070 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001071 return 0;
1072 }
Mark Brownec029952012-06-04 08:16:20 +01001073#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001074 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001075}
1076EXPORT_SYMBOL_GPL(dapm_clock_event);
1077
Mark Brownd805002b2011-09-28 18:28:23 +01001078static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1079{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001080 if (w->power_checked)
1081 return w->new_power;
1082
Mark Brownd805002b2011-09-28 18:28:23 +01001083 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001084 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +01001085 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001086 w->new_power = w->power_check(w);
1087
1088 w->power_checked = true;
1089
1090 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +01001091}
1092
Mark Browncd0f2d42009-04-20 16:56:59 +01001093/* Generic check to see if a widget should be powered.
1094 */
1095static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1096{
1097 int in, out;
1098
Mark Brownde02d072011-09-20 21:43:24 +01001099 DAPM_UPDATE_STAT(w, power_checks);
1100
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001101 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001102 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001103 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001104 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001105 return out != 0 && in != 0;
1106}
1107
Mark Brown6ea31b92009-04-20 17:15:41 +01001108/* Check to see if an ADC has power */
1109static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1110{
1111 int in;
1112
Mark Brownde02d072011-09-20 21:43:24 +01001113 DAPM_UPDATE_STAT(w, power_checks);
1114
Mark Brown6ea31b92009-04-20 17:15:41 +01001115 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001116 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001117 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001118 return in != 0;
1119 } else {
1120 return dapm_generic_check_power(w);
1121 }
1122}
1123
1124/* Check to see if a DAC has power */
1125static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1126{
1127 int out;
1128
Mark Brownde02d072011-09-20 21:43:24 +01001129 DAPM_UPDATE_STAT(w, power_checks);
1130
Mark Brown6ea31b92009-04-20 17:15:41 +01001131 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001132 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001133 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001134 return out != 0;
1135 } else {
1136 return dapm_generic_check_power(w);
1137 }
1138}
1139
Mark Brown246d0a12009-04-22 18:24:55 +01001140/* Check to see if a power supply is needed */
1141static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1142{
1143 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001144
Mark Brownde02d072011-09-20 21:43:24 +01001145 DAPM_UPDATE_STAT(w, power_checks);
1146
Mark Brown246d0a12009-04-22 18:24:55 +01001147 /* Check if one of our outputs is connected */
1148 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001149 DAPM_UPDATE_STAT(w, neighbour_checks);
1150
Mark Brownbf3a9e12011-06-13 16:42:29 +01001151 if (path->weak)
1152 continue;
1153
Mark Brown215edda2009-09-08 18:59:05 +01001154 if (path->connected &&
1155 !path->connected(path->source, path->sink))
1156 continue;
1157
Mark Brown30173582011-02-11 11:42:19 +00001158 if (!path->sink)
1159 continue;
1160
Mark Brownf68d7e12011-10-04 22:57:50 +01001161 if (dapm_widget_power_check(path->sink))
1162 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001163 }
1164
Mark Brownf68d7e12011-10-04 22:57:50 +01001165 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001166}
1167
Mark Brown35c64bc2011-09-28 18:23:53 +01001168static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1169{
1170 return 1;
1171}
1172
Mark Brown38357ab2009-06-06 19:03:23 +01001173static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1174 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001175 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001176{
Mark Brown828a8422011-01-15 13:14:30 +00001177 int *sort;
1178
1179 if (power_up)
1180 sort = dapm_up_seq;
1181 else
1182 sort = dapm_down_seq;
1183
Mark Brown38357ab2009-06-06 19:03:23 +01001184 if (sort[a->id] != sort[b->id])
1185 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001186 if (a->subseq != b->subseq) {
1187 if (power_up)
1188 return a->subseq - b->subseq;
1189 else
1190 return b->subseq - a->subseq;
1191 }
Mark Brownb22ead22009-06-07 12:51:26 +01001192 if (a->reg != b->reg)
1193 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001194 if (a->dapm != b->dapm)
1195 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001196
Mark Brown38357ab2009-06-06 19:03:23 +01001197 return 0;
1198}
Mark Brown42aa3412009-03-01 19:21:10 +00001199
Mark Brown38357ab2009-06-06 19:03:23 +01001200/* Insert a widget in order into a DAPM power sequence. */
1201static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1202 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001203 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001204{
1205 struct snd_soc_dapm_widget *w;
1206
1207 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001208 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001209 list_add_tail(&new_widget->power_list, &w->power_list);
1210 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001211 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001212
Mark Brown38357ab2009-06-06 19:03:23 +01001213 list_add_tail(&new_widget->power_list, list);
1214}
Mark Brown42aa3412009-03-01 19:21:10 +00001215
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001216static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001217 struct snd_soc_dapm_widget *w, int event)
1218{
Mark Brown68f89ad2010-11-03 23:51:49 -04001219 const char *ev_name;
1220 int power, ret;
1221
1222 switch (event) {
1223 case SND_SOC_DAPM_PRE_PMU:
1224 ev_name = "PRE_PMU";
1225 power = 1;
1226 break;
1227 case SND_SOC_DAPM_POST_PMU:
1228 ev_name = "POST_PMU";
1229 power = 1;
1230 break;
1231 case SND_SOC_DAPM_PRE_PMD:
1232 ev_name = "PRE_PMD";
1233 power = 0;
1234 break;
1235 case SND_SOC_DAPM_POST_PMD:
1236 ev_name = "POST_PMD";
1237 power = 0;
1238 break;
Mark Brown80114122013-02-25 15:14:19 +00001239 case SND_SOC_DAPM_WILL_PMU:
1240 ev_name = "WILL_PMU";
1241 power = 1;
1242 break;
1243 case SND_SOC_DAPM_WILL_PMD:
1244 ev_name = "WILL_PMD";
1245 power = 0;
1246 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001247 default:
1248 BUG();
1249 return;
1250 }
1251
1252 if (w->power != power)
1253 return;
1254
1255 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001256 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001257 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001258 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001259 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001260 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001261 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001262 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001263 ev_name, w->name, ret);
1264 }
1265}
1266
Mark Brownb22ead22009-06-07 12:51:26 +01001267/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001268static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001269 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001270{
Mark Brown68f89ad2010-11-03 23:51:49 -04001271 struct snd_soc_dapm_widget *w;
1272 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001273 unsigned int value = 0;
1274 unsigned int mask = 0;
1275 unsigned int cur_mask;
1276
1277 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1278 power_list)->reg;
1279
1280 list_for_each_entry(w, pending, power_list) {
1281 cur_mask = 1 << w->shift;
1282 BUG_ON(reg != w->reg);
1283
1284 if (w->invert)
1285 power = !w->power;
1286 else
1287 power = w->power;
1288
1289 mask |= cur_mask;
1290 if (power)
1291 value |= cur_mask;
1292
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001293 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001294 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1295 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001296
Mark Brown68f89ad2010-11-03 23:51:49 -04001297 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001298 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1299 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001300 }
1301
Mark Brown81628102009-06-07 13:21:24 +01001302 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001303 /* Any widget will do, they should all be updating the
1304 * same register.
1305 */
1306 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1307 power_list);
1308
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001309 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001310 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001311 value, mask, reg, card->pop_time);
1312 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001313 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001314 }
1315
1316 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001317 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1318 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001319 }
Mark Brown42aa3412009-03-01 19:21:10 +00001320}
1321
Mark Brownb22ead22009-06-07 12:51:26 +01001322/* Apply a DAPM power sequence.
1323 *
1324 * We walk over a pre-sorted list of widgets to apply power to. In
1325 * order to minimise the number of writes to the device required
1326 * multiple widgets will be updated in a single write where possible.
1327 * Currently anything that requires more than a single write is not
1328 * handled.
1329 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001330static void dapm_seq_run(struct snd_soc_card *card,
1331 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001332{
1333 struct snd_soc_dapm_widget *w, *n;
1334 LIST_HEAD(pending);
1335 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001336 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001337 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001338 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001339 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001340 int *sort;
1341
1342 if (power_up)
1343 sort = dapm_up_seq;
1344 else
1345 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001346
Mark Brownb22ead22009-06-07 12:51:26 +01001347 list_for_each_entry_safe(w, n, list, power_list) {
1348 ret = 0;
1349
1350 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001351 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001352 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001353 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001354 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001355
Mark Brown474b62d2011-01-18 16:14:44 +00001356 if (cur_dapm && cur_dapm->seq_notifier) {
1357 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1358 if (sort[i] == cur_sort)
1359 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001360 i,
1361 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001362 }
1363
Mark Brownb22ead22009-06-07 12:51:26 +01001364 INIT_LIST_HEAD(&pending);
1365 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001366 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001367 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001368 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001369 }
1370
Mark Brown163cac02009-06-07 10:12:52 +01001371 switch (w->id) {
1372 case snd_soc_dapm_pre:
1373 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001374 list_for_each_entry_safe_continue(w, n, list,
1375 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001376
Mark Brownb22ead22009-06-07 12:51:26 +01001377 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001378 ret = w->event(w,
1379 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001380 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001381 ret = w->event(w,
1382 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001383 break;
1384
1385 case snd_soc_dapm_post:
1386 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001387 list_for_each_entry_safe_continue(w, n, list,
1388 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001389
Mark Brownb22ead22009-06-07 12:51:26 +01001390 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001391 ret = w->event(w,
1392 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001393 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001394 ret = w->event(w,
1395 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001396 break;
1397
Mark Brown163cac02009-06-07 10:12:52 +01001398 default:
Mark Brown81628102009-06-07 13:21:24 +01001399 /* Queue it up for application */
1400 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001401 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001402 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001403 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001404 list_move(&w->power_list, &pending);
1405 break;
Mark Brown163cac02009-06-07 10:12:52 +01001406 }
Mark Brownb22ead22009-06-07 12:51:26 +01001407
1408 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001409 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001410 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001411 }
Mark Brownb22ead22009-06-07 12:51:26 +01001412
1413 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001414 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001415
1416 if (cur_dapm && cur_dapm->seq_notifier) {
1417 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1418 if (sort[i] == cur_sort)
1419 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001420 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001421 }
Mark Brown163cac02009-06-07 10:12:52 +01001422}
1423
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001424static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001425{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001426 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001427 struct snd_soc_dapm_widget_list *wlist;
1428 struct snd_soc_dapm_widget *w = NULL;
1429 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001430 int ret;
1431
1432 if (!update)
1433 return;
1434
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001435 wlist = snd_kcontrol_chip(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001436
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001437 for (wi = 0; wi < wlist->num_widgets; wi++) {
1438 w = wlist->widgets[wi];
1439
1440 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1441 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1442 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001443 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001444 w->name, ret);
1445 }
Mark Brown97404f22010-12-14 16:13:57 +00001446 }
1447
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001448 if (!w)
1449 return;
1450
Liam Girdwood49575fb52012-03-06 18:16:19 +00001451 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001452 update->val);
1453 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001454 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001455 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001456
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001457 for (wi = 0; wi < wlist->num_widgets; wi++) {
1458 w = wlist->widgets[wi];
1459
1460 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1461 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1462 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001463 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001464 w->name, ret);
1465 }
Mark Brown97404f22010-12-14 16:13:57 +00001466 }
1467}
1468
Mark Brown9d0624a2011-02-18 11:49:43 -08001469/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1470 * they're changing state.
1471 */
1472static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1473{
1474 struct snd_soc_dapm_context *d = data;
1475 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001476
Mark Brown56fba412011-06-04 11:25:10 +01001477 /* If we're off and we're not supposed to be go into STANDBY */
1478 if (d->bias_level == SND_SOC_BIAS_OFF &&
1479 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001480 if (d->dev)
1481 pm_runtime_get_sync(d->dev);
1482
Mark Brown9d0624a2011-02-18 11:49:43 -08001483 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1484 if (ret != 0)
1485 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001486 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001487 }
1488
Mark Brown56fba412011-06-04 11:25:10 +01001489 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1490 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001491 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1492 if (ret != 0)
1493 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001494 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001495 }
1496}
1497
1498/* Async callback run prior to DAPM sequences - brings to their final
1499 * state.
1500 */
1501static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1502{
1503 struct snd_soc_dapm_context *d = data;
1504 int ret;
1505
1506 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001507 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1508 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1509 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001510 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1511 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001512 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001513 ret);
1514 }
1515
1516 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001517 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1518 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001519 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1520 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001521 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1522 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001523
1524 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001525 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001526 }
1527
1528 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001529 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1530 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001531 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1532 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001533 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001534 ret);
1535 }
1536}
Mark Brown97404f22010-12-14 16:13:57 +00001537
Mark Brownfe4fda52011-10-03 22:36:57 +01001538static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1539 bool power, bool connect)
1540{
1541 /* If a connection is being made or broken then that update
1542 * will have marked the peer dirty, otherwise the widgets are
1543 * not connected and this update has no impact. */
1544 if (!connect)
1545 return;
1546
1547 /* If the peer is already in the state we're moving to then we
1548 * won't have an impact on it. */
1549 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001550 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001551}
1552
Mark Brown05623c42011-09-28 17:02:31 +01001553static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1554 struct list_head *up_list,
1555 struct list_head *down_list)
1556{
Mark Browndb432b42011-10-03 21:06:40 +01001557 struct snd_soc_dapm_path *path;
1558
Mark Brown05623c42011-09-28 17:02:31 +01001559 if (w->power == power)
1560 return;
1561
1562 trace_snd_soc_dapm_widget_power(w, power);
1563
Mark Browndb432b42011-10-03 21:06:40 +01001564 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001565 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001566 */
1567 list_for_each_entry(path, &w->sources, list_sink) {
1568 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001569 dapm_widget_set_peer_power(path->source, power,
1570 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001571 }
1572 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001573 switch (w->id) {
1574 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001575 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001576 case snd_soc_dapm_clock_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001577 /* Supplies can't affect their outputs, only their inputs */
1578 break;
1579 default:
1580 list_for_each_entry(path, &w->sinks, list_source) {
1581 if (path->sink) {
1582 dapm_widget_set_peer_power(path->sink, power,
1583 path->connect);
1584 }
Mark Browndb432b42011-10-03 21:06:40 +01001585 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001586 break;
Mark Browndb432b42011-10-03 21:06:40 +01001587 }
1588
Mark Brown05623c42011-09-28 17:02:31 +01001589 if (power)
1590 dapm_seq_insert(w, up_list, true);
1591 else
1592 dapm_seq_insert(w, down_list, false);
1593
1594 w->power = power;
1595}
1596
Mark Brown7c81beb2011-09-20 22:22:32 +01001597static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1598 struct list_head *up_list,
1599 struct list_head *down_list)
1600{
Mark Brown7c81beb2011-09-20 22:22:32 +01001601 int power;
1602
1603 switch (w->id) {
1604 case snd_soc_dapm_pre:
1605 dapm_seq_insert(w, down_list, false);
1606 break;
1607 case snd_soc_dapm_post:
1608 dapm_seq_insert(w, up_list, true);
1609 break;
1610
1611 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001612 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001613
Mark Brown05623c42011-09-28 17:02:31 +01001614 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001615 break;
1616 }
1617}
1618
Mark Brown42aa3412009-03-01 19:21:10 +00001619/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001620 * Scan each dapm widget for complete audio path.
1621 * A complete path is a route that has valid endpoints i.e.:-
1622 *
1623 * o DAC to output pin.
1624 * o Input Pin to ADC.
1625 * o Input pin to Output pin (bypass, sidetone)
1626 * o DAC to ADC (loopback).
1627 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001628static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001629{
1630 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001631 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001632 LIST_HEAD(up_list);
1633 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001634 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001635 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001636
Mark Brown84e90932010-11-04 00:07:02 -04001637 trace_snd_soc_dapm_start(card);
1638
Mark Brown56fba412011-06-04 11:25:10 +01001639 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001640 if (d->idle_bias_off)
1641 d->target_bias_level = SND_SOC_BIAS_OFF;
1642 else
1643 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001644 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001645
Liam Girdwood6c120e12012-02-15 15:15:34 +00001646 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001647
Mark Brown6d3ddc82009-05-16 17:47:29 +01001648 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001649 * lists indicating if they should be powered up or down. We
1650 * only check widgets that have been flagged as dirty but note
1651 * that new widgets may be added to the dirty list while we
1652 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001653 */
Mark Browndb432b42011-10-03 21:06:40 +01001654 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001655 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001656 }
1657
Mark Brownf9de6d72011-09-28 17:19:47 +01001658 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001659 switch (w->id) {
1660 case snd_soc_dapm_pre:
1661 case snd_soc_dapm_post:
1662 /* These widgets always need to be powered */
1663 break;
1664 default:
1665 list_del_init(&w->dirty);
1666 break;
1667 }
Mark Browndb432b42011-10-03 21:06:40 +01001668
Mark Brownf9de6d72011-09-28 17:19:47 +01001669 if (w->power) {
1670 d = w->dapm;
1671
1672 /* Supplies and micbiases only bring the
1673 * context up to STANDBY as unless something
1674 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001675 * generally don't require full power. Signal
1676 * generators are virtual pins and have no
1677 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001678 */
1679 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001680 case snd_soc_dapm_siggen:
1681 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001682 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001683 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001684 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001685 case snd_soc_dapm_micbias:
1686 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1687 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1688 break;
1689 default:
1690 d->target_bias_level = SND_SOC_BIAS_ON;
1691 break;
1692 }
1693 }
1694
1695 }
1696
Mark Brown85a843c2011-09-21 21:29:47 +01001697 /* Force all contexts in the card to the same bias state if
1698 * they're not ground referenced.
1699 */
Mark Brown56fba412011-06-04 11:25:10 +01001700 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001701 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001702 if (d->target_bias_level > bias)
1703 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001704 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001705 if (!d->idle_bias_off)
1706 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001707
Mark Brownde02d072011-09-20 21:43:24 +01001708 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001709
Mark Brown9d0624a2011-02-18 11:49:43 -08001710 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001711 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001712 async_schedule_domain(dapm_pre_sequence_async, d,
1713 &async_domain);
1714 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001715
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001716 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001717 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001718 }
1719
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001720 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001721 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001722 }
1723
Mark Brown6d3ddc82009-05-16 17:47:29 +01001724 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001725 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001726
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001727 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001728
Mark Brown6d3ddc82009-05-16 17:47:29 +01001729 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001730 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001731
Mark Brown9d0624a2011-02-18 11:49:43 -08001732 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001733 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001734 async_schedule_domain(dapm_post_sequence_async, d,
1735 &async_domain);
1736 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001737
Liam Girdwood8078d872012-02-15 15:15:35 +00001738 /* do we need to notify any clients that DAPM event is complete */
1739 list_for_each_entry(d, &card->dapm_list, list) {
1740 if (d->stream_event)
1741 d->stream_event(d, event);
1742 }
1743
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001744 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001745 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001746 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001747
Mark Brown84e90932010-11-04 00:07:02 -04001748 trace_snd_soc_dapm_done(card);
1749
Mark Brown42aa3412009-03-01 19:21:10 +00001750 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001751}
1752
Mark Brown79fb9382009-08-21 16:38:13 +01001753#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001754static ssize_t dapm_widget_power_read_file(struct file *file,
1755 char __user *user_buf,
1756 size_t count, loff_t *ppos)
1757{
1758 struct snd_soc_dapm_widget *w = file->private_data;
1759 char *buf;
1760 int in, out;
1761 ssize_t ret;
1762 struct snd_soc_dapm_path *p = NULL;
1763
1764 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1765 if (!buf)
1766 return -ENOMEM;
1767
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001768 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001769 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001770 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001771 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001772
Mark Brownf13ebad2012-03-03 18:01:01 +00001773 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1774 w->name, w->power ? "On" : "Off",
1775 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001776
Mark Brownd033c362009-12-04 15:25:56 +00001777 if (w->reg >= 0)
1778 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1779 " - R%d(0x%x) bit %d",
1780 w->reg, w->reg, w->shift);
1781
1782 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1783
Mark Brown3eef08b2009-09-14 16:49:00 +01001784 if (w->sname)
1785 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1786 w->sname,
1787 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001788
1789 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001790 if (p->connected && !p->connected(w, p->sink))
1791 continue;
1792
Mark Brown79fb9382009-08-21 16:38:13 +01001793 if (p->connect)
1794 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001795 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001796 p->name ? p->name : "static",
1797 p->source->name);
1798 }
1799 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001800 if (p->connected && !p->connected(w, p->sink))
1801 continue;
1802
Mark Brown79fb9382009-08-21 16:38:13 +01001803 if (p->connect)
1804 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001805 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001806 p->name ? p->name : "static",
1807 p->sink->name);
1808 }
1809
1810 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1811
1812 kfree(buf);
1813 return ret;
1814}
1815
1816static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001817 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001818 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001819 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001820};
1821
Mark Brownef49e4f2011-04-04 20:48:13 +09001822static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1823 size_t count, loff_t *ppos)
1824{
1825 struct snd_soc_dapm_context *dapm = file->private_data;
1826 char *level;
1827
1828 switch (dapm->bias_level) {
1829 case SND_SOC_BIAS_ON:
1830 level = "On\n";
1831 break;
1832 case SND_SOC_BIAS_PREPARE:
1833 level = "Prepare\n";
1834 break;
1835 case SND_SOC_BIAS_STANDBY:
1836 level = "Standby\n";
1837 break;
1838 case SND_SOC_BIAS_OFF:
1839 level = "Off\n";
1840 break;
1841 default:
1842 BUG();
1843 level = "Unknown\n";
1844 break;
1845 }
1846
1847 return simple_read_from_buffer(user_buf, count, ppos, level,
1848 strlen(level));
1849}
1850
1851static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001852 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001853 .read = dapm_bias_read_file,
1854 .llseek = default_llseek,
1855};
1856
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001857void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1858 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001859{
Mark Brown79fb9382009-08-21 16:38:13 +01001860 struct dentry *d;
1861
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001862 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1863
1864 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001865 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001866 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001867 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001868 }
Mark Brown79fb9382009-08-21 16:38:13 +01001869
Mark Brownef49e4f2011-04-04 20:48:13 +09001870 d = debugfs_create_file("bias_level", 0444,
1871 dapm->debugfs_dapm, dapm,
1872 &dapm_bias_fops);
1873 if (!d)
1874 dev_warn(dapm->dev,
1875 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001876}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001877
1878static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1879{
1880 struct snd_soc_dapm_context *dapm = w->dapm;
1881 struct dentry *d;
1882
1883 if (!dapm->debugfs_dapm || !w->name)
1884 return;
1885
1886 d = debugfs_create_file(w->name, 0444,
1887 dapm->debugfs_dapm, w,
1888 &dapm_widget_power_fops);
1889 if (!d)
1890 dev_warn(w->dapm->dev,
1891 "ASoC: Failed to create %s debugfs file\n",
1892 w->name);
1893}
1894
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001895static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1896{
1897 debugfs_remove_recursive(dapm->debugfs_dapm);
1898}
1899
Mark Brown79fb9382009-08-21 16:38:13 +01001900#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001901void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1902 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001903{
1904}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001905
1906static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1907{
1908}
1909
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001910static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1911{
1912}
1913
Mark Brown79fb9382009-08-21 16:38:13 +01001914#endif
1915
Richard Purdie2b97eab2006-10-06 18:32:18 +02001916/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001917static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001918 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001919{
1920 struct snd_soc_dapm_path *path;
1921 int found = 0;
1922
Richard Purdie2b97eab2006-10-06 18:32:18 +02001923 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001924 list_for_each_entry(path, &card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001925 if (path->kcontrol != kcontrol)
1926 continue;
1927
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001928 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001929 continue;
1930
1931 found = 1;
1932 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001933 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001934 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001935 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001936 } else {
1937 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001938 dapm_mark_dirty(path->source,
1939 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001940 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001941 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001942 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001943 }
1944
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001945 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001946 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001947
Liam Girdwood618dae12012-04-25 12:12:51 +01001948 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001949}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001950
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001951int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02001952 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
1953 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001954{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001955 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001956 int ret;
1957
Liam Girdwood3cd04342012-03-09 12:02:08 +00001958 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02001959 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001960 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02001961 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001962 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001963 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001964 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001965 return ret;
1966}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001967EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001968
Milan plzik1b075e32008-01-10 14:39:46 +01001969/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001970static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00001971 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001972{
1973 struct snd_soc_dapm_path *path;
1974 int found = 0;
1975
Richard Purdie2b97eab2006-10-06 18:32:18 +02001976 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001977 list_for_each_entry(path, &card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001978 if (path->kcontrol != kcontrol)
1979 continue;
1980
1981 /* found, now check type */
1982 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001983 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001984 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001985 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001986 }
1987
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001988 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001989 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001990
Liam Girdwood618dae12012-04-25 12:12:51 +01001991 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001992}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001993
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001994int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02001995 struct snd_kcontrol *kcontrol, int connect,
1996 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001997{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001998 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001999 int ret;
2000
Liam Girdwood3cd04342012-03-09 12:02:08 +00002001 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002002 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002003 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002004 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002005 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002006 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002007 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002008 return ret;
2009}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002010EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002011
2012/* show dapm widget status in sys fs */
2013static ssize_t dapm_widget_show(struct device *dev,
2014 struct device_attribute *attr, char *buf)
2015{
Mark Brown36ae1a92012-01-06 17:12:45 -08002016 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002017 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002018 struct snd_soc_dapm_widget *w;
2019 int count = 0;
2020 char *state = "not set";
2021
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002022 list_for_each_entry(w, &codec->card->widgets, list) {
2023 if (w->dapm != &codec->dapm)
2024 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002025
2026 /* only display widgets that burnm power */
2027 switch (w->id) {
2028 case snd_soc_dapm_hp:
2029 case snd_soc_dapm_mic:
2030 case snd_soc_dapm_spk:
2031 case snd_soc_dapm_line:
2032 case snd_soc_dapm_micbias:
2033 case snd_soc_dapm_dac:
2034 case snd_soc_dapm_adc:
2035 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002036 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002037 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002038 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002039 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002040 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002041 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002042 if (w->name)
2043 count += sprintf(buf + count, "%s: %s\n",
2044 w->name, w->power ? "On":"Off");
2045 break;
2046 default:
2047 break;
2048 }
2049 }
2050
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002051 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002052 case SND_SOC_BIAS_ON:
2053 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002054 break;
Mark Brown0be98982008-05-19 12:31:28 +02002055 case SND_SOC_BIAS_PREPARE:
2056 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002057 break;
Mark Brown0be98982008-05-19 12:31:28 +02002058 case SND_SOC_BIAS_STANDBY:
2059 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002060 break;
Mark Brown0be98982008-05-19 12:31:28 +02002061 case SND_SOC_BIAS_OFF:
2062 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002063 break;
2064 }
2065 count += sprintf(buf + count, "PM State: %s\n", state);
2066
2067 return count;
2068}
2069
2070static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2071
2072int snd_soc_dapm_sys_add(struct device *dev)
2073{
Troy Kisky12ef1932008-10-13 17:42:14 -07002074 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002075}
2076
2077static void snd_soc_dapm_sys_remove(struct device *dev)
2078{
Mark Brownaef90842009-05-16 17:53:16 +01002079 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002080}
2081
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002082static void dapm_free_path(struct snd_soc_dapm_path *path)
2083{
2084 list_del(&path->list_sink);
2085 list_del(&path->list_source);
2086 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002087 kfree(path);
2088}
2089
Richard Purdie2b97eab2006-10-06 18:32:18 +02002090/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002091static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002092{
2093 struct snd_soc_dapm_widget *w, *next_w;
2094 struct snd_soc_dapm_path *p, *next_p;
2095
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002096 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2097 if (w->dapm != dapm)
2098 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002099 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002100 /*
2101 * remove source and sink paths associated to this widget.
2102 * While removing the path, remove reference to it from both
2103 * source and sink widgets so that path is removed only once.
2104 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002105 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2106 dapm_free_path(p);
2107
2108 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2109 dapm_free_path(p);
2110
Stephen Warrenfad59882011-04-28 17:37:59 -06002111 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002112 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002113 kfree(w);
2114 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002115}
2116
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002117static struct snd_soc_dapm_widget *dapm_find_widget(
2118 struct snd_soc_dapm_context *dapm, const char *pin,
2119 bool search_other_contexts)
2120{
2121 struct snd_soc_dapm_widget *w;
2122 struct snd_soc_dapm_widget *fallback = NULL;
2123
2124 list_for_each_entry(w, &dapm->card->widgets, list) {
2125 if (!strcmp(w->name, pin)) {
2126 if (w->dapm == dapm)
2127 return w;
2128 else
2129 fallback = w;
2130 }
2131 }
2132
2133 if (search_other_contexts)
2134 return fallback;
2135
2136 return NULL;
2137}
2138
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002139static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002140 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002141{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002142 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002143
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002144 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002145 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002146 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002147 }
2148
Mark Brown1a8b2d92012-02-16 11:50:07 -08002149 if (w->connected != status)
2150 dapm_mark_dirty(w, "pin configuration");
2151
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002152 w->connected = status;
2153 if (status == 0)
2154 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002155
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002156 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002157}
2158
Richard Purdie2b97eab2006-10-06 18:32:18 +02002159/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002160 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002161 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002162 *
2163 * Walks all dapm audio paths and powers widgets according to their
2164 * stream or path usage.
2165 *
2166 * Returns 0 for success.
2167 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002168int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002169{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002170 int ret;
2171
Mark Brown4f4c0072011-10-07 14:29:19 +01002172 /*
2173 * Suppress early reports (eg, jacks syncing their state) to avoid
2174 * silly DAPM runs during card startup.
2175 */
2176 if (!dapm->card || !dapm->card->instantiated)
2177 return 0;
2178
Liam Girdwood3cd04342012-03-09 12:02:08 +00002179 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002180 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002181 mutex_unlock(&dapm->card->dapm_mutex);
2182 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002183}
Liam Girdwooda5302182008-07-07 13:35:17 +01002184EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002185
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002186static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002187 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002188{
2189 struct snd_soc_dapm_path *path;
2190 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002191 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002192 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002193 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002194 const char *source;
2195 char prefixed_sink[80];
2196 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002197 int ret = 0;
2198
Mark Brown88e8b9a2011-03-02 18:18:24 +00002199 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002200 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2201 dapm->codec->name_prefix, route->sink);
2202 sink = prefixed_sink;
2203 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2204 dapm->codec->name_prefix, route->source);
2205 source = prefixed_source;
2206 } else {
2207 sink = route->sink;
2208 source = route->source;
2209 }
2210
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002211 /*
2212 * find src and dest widgets over all widgets but favor a widget from
2213 * current DAPM context
2214 */
2215 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002216 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002217 wtsink = w;
2218 if (w->dapm == dapm)
2219 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002220 continue;
2221 }
2222 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002223 wtsource = w;
2224 if (w->dapm == dapm)
2225 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002226 }
2227 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002228 /* use widget from another DAPM context if not found from this */
2229 if (!wsink)
2230 wsink = wtsink;
2231 if (!wsource)
2232 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002233
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002234 if (wsource == NULL) {
2235 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2236 route->source);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002237 return -ENODEV;
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002238 }
2239 if (wsink == NULL) {
2240 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2241 route->sink);
2242 return -ENODEV;
2243 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244
2245 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2246 if (!path)
2247 return -ENOMEM;
2248
2249 path->source = wsource;
2250 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002251 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002252 INIT_LIST_HEAD(&path->list);
2253 INIT_LIST_HEAD(&path->list_source);
2254 INIT_LIST_HEAD(&path->list_sink);
2255
2256 /* check for external widgets */
2257 if (wsink->id == snd_soc_dapm_input) {
2258 if (wsource->id == snd_soc_dapm_micbias ||
2259 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002260 wsource->id == snd_soc_dapm_line ||
2261 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002262 wsink->ext = 1;
2263 }
2264 if (wsource->id == snd_soc_dapm_output) {
2265 if (wsink->id == snd_soc_dapm_spk ||
2266 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002267 wsink->id == snd_soc_dapm_line ||
2268 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002269 wsource->ext = 1;
2270 }
2271
2272 /* connect static paths */
2273 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002274 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002275 list_add(&path->list_sink, &wsink->sources);
2276 list_add(&path->list_source, &wsource->sinks);
2277 path->connect = 1;
2278 return 0;
2279 }
2280
2281 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002282 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002283 case snd_soc_dapm_adc:
2284 case snd_soc_dapm_dac:
2285 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002286 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002287 case snd_soc_dapm_input:
2288 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002289 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002290 case snd_soc_dapm_micbias:
2291 case snd_soc_dapm_vmid:
2292 case snd_soc_dapm_pre:
2293 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002294 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002295 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002296 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002297 case snd_soc_dapm_aif_in:
2298 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002299 case snd_soc_dapm_dai_in:
2300 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002301 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002302 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002303 list_add(&path->list_sink, &wsink->sources);
2304 list_add(&path->list_source, &wsource->sinks);
2305 path->connect = 1;
2306 return 0;
2307 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002308 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002309 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002310 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002311 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002312 if (ret != 0)
2313 goto err;
2314 break;
2315 case snd_soc_dapm_switch:
2316 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002317 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002318 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002319 if (ret != 0)
2320 goto err;
2321 break;
2322 case snd_soc_dapm_hp:
2323 case snd_soc_dapm_mic:
2324 case snd_soc_dapm_line:
2325 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002326 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002327 list_add(&path->list_sink, &wsink->sources);
2328 list_add(&path->list_source, &wsource->sinks);
2329 path->connect = 0;
2330 return 0;
2331 }
Mark Brownfabd0382012-07-05 17:20:06 +01002332
2333 dapm_mark_dirty(wsource, "Route added");
2334 dapm_mark_dirty(wsink, "Route added");
2335
Richard Purdie2b97eab2006-10-06 18:32:18 +02002336 return 0;
2337
2338err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002339 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002340 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002341 kfree(path);
2342 return ret;
2343}
Mark Brown105f1c22008-05-13 14:52:19 +02002344
Mark Brownefcc3c62012-07-05 17:24:19 +01002345static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2346 const struct snd_soc_dapm_route *route)
2347{
2348 struct snd_soc_dapm_path *path, *p;
2349 const char *sink;
2350 const char *source;
2351 char prefixed_sink[80];
2352 char prefixed_source[80];
2353
2354 if (route->control) {
2355 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002356 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002357 return -EINVAL;
2358 }
2359
2360 if (dapm->codec && dapm->codec->name_prefix) {
2361 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2362 dapm->codec->name_prefix, route->sink);
2363 sink = prefixed_sink;
2364 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2365 dapm->codec->name_prefix, route->source);
2366 source = prefixed_source;
2367 } else {
2368 sink = route->sink;
2369 source = route->source;
2370 }
2371
2372 path = NULL;
2373 list_for_each_entry(p, &dapm->card->paths, list) {
2374 if (strcmp(p->source->name, source) != 0)
2375 continue;
2376 if (strcmp(p->sink->name, sink) != 0)
2377 continue;
2378 path = p;
2379 break;
2380 }
2381
2382 if (path) {
2383 dapm_mark_dirty(path->source, "Route removed");
2384 dapm_mark_dirty(path->sink, "Route removed");
2385
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002386 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002387 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002388 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002389 source, sink);
2390 }
2391
2392 return 0;
2393}
2394
Mark Brown105f1c22008-05-13 14:52:19 +02002395/**
Mark Brown105f1c22008-05-13 14:52:19 +02002396 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002397 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002398 * @route: audio routes
2399 * @num: number of routes
2400 *
2401 * Connects 2 dapm widgets together via a named audio path. The sink is
2402 * the widget receiving the audio signal, whilst the source is the sender
2403 * of the audio signal.
2404 *
2405 * Returns 0 for success else error. On error all resources can be freed
2406 * with a call to snd_soc_card_free().
2407 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002408int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002409 const struct snd_soc_dapm_route *route, int num)
2410{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002411 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002412
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002413 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002414 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002415 r = snd_soc_dapm_add_route(dapm, route);
2416 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002417 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2418 route->source,
2419 route->control ? route->control : "direct",
2420 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002421 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002422 }
2423 route++;
2424 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002425 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002426
Dan Carpenter60884c22012-04-13 22:25:43 +03002427 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002428}
2429EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2430
Mark Brownefcc3c62012-07-05 17:24:19 +01002431/**
2432 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2433 * @dapm: DAPM context
2434 * @route: audio routes
2435 * @num: number of routes
2436 *
2437 * Removes routes from the DAPM context.
2438 */
2439int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2440 const struct snd_soc_dapm_route *route, int num)
2441{
2442 int i, ret = 0;
2443
2444 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2445 for (i = 0; i < num; i++) {
2446 snd_soc_dapm_del_route(dapm, route);
2447 route++;
2448 }
2449 mutex_unlock(&dapm->card->dapm_mutex);
2450
2451 return ret;
2452}
2453EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2454
Mark Brownbf3a9e12011-06-13 16:42:29 +01002455static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2456 const struct snd_soc_dapm_route *route)
2457{
2458 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2459 route->source,
2460 true);
2461 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2462 route->sink,
2463 true);
2464 struct snd_soc_dapm_path *path;
2465 int count = 0;
2466
2467 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002468 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002469 route->source);
2470 return -ENODEV;
2471 }
2472
2473 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002474 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002475 route->sink);
2476 return -ENODEV;
2477 }
2478
2479 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002480 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002481 route->source, route->sink);
2482
2483 list_for_each_entry(path, &source->sinks, list_source) {
2484 if (path->sink == sink) {
2485 path->weak = 1;
2486 count++;
2487 }
2488 }
2489
2490 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002491 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002492 route->source, route->sink);
2493 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002494 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002495 count, route->source, route->sink);
2496
2497 return 0;
2498}
2499
2500/**
2501 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2502 * @dapm: DAPM context
2503 * @route: audio routes
2504 * @num: number of routes
2505 *
2506 * Mark existing routes matching those specified in the passed array
2507 * as being weak, meaning that they are ignored for the purpose of
2508 * power decisions. The main intended use case is for sidetone paths
2509 * which couple audio between other independent paths if they are both
2510 * active in order to make the combination work better at the user
2511 * level but which aren't intended to be "used".
2512 *
2513 * Note that CODEC drivers should not use this as sidetone type paths
2514 * can frequently also be used as bypass paths.
2515 */
2516int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2517 const struct snd_soc_dapm_route *route, int num)
2518{
2519 int i, err;
2520 int ret = 0;
2521
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002522 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002523 for (i = 0; i < num; i++) {
2524 err = snd_soc_dapm_weak_route(dapm, route);
2525 if (err)
2526 ret = err;
2527 route++;
2528 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002529 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002530
2531 return ret;
2532}
2533EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2534
Mark Brown105f1c22008-05-13 14:52:19 +02002535/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002536 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002537 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002538 *
2539 * Checks the codec for any new dapm widgets and creates them if found.
2540 *
2541 * Returns 0 for success.
2542 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002543int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002544{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002545 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002546 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002547 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002548
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002549 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002550
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002551 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002552 {
2553 if (w->new)
2554 continue;
2555
Stephen Warrenfad59882011-04-28 17:37:59 -06002556 if (w->num_kcontrols) {
2557 w->kcontrols = kzalloc(w->num_kcontrols *
2558 sizeof(struct snd_kcontrol *),
2559 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002560 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002561 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002562 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002563 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002564 }
2565
Richard Purdie2b97eab2006-10-06 18:32:18 +02002566 switch(w->id) {
2567 case snd_soc_dapm_switch:
2568 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002569 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002570 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002571 break;
2572 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002573 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002574 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002575 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002576 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002577 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002578 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002579 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002580 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002581 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002582 break;
2583 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002584
2585 /* Read the initial power state from the device */
2586 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002587 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002588 val &= 1 << w->shift;
2589 if (w->invert)
2590 val = !val;
2591
2592 if (val)
2593 w->power = 1;
2594 }
2595
Richard Purdie2b97eab2006-10-06 18:32:18 +02002596 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002597
Mark Brown7508b122011-10-05 12:09:12 +01002598 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002599 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002600 }
2601
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002602 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2603 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002604 return 0;
2605}
2606EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2607
2608/**
2609 * snd_soc_dapm_get_volsw - dapm mixer get callback
2610 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002611 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002612 *
2613 * Callback to get the value of a dapm mixer control.
2614 *
2615 * Returns 0 for success.
2616 */
2617int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2618 struct snd_ctl_elem_value *ucontrol)
2619{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002620 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2621 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002622 struct soc_mixer_control *mc =
2623 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002624 unsigned int reg = mc->reg;
2625 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002626 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002627 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002628 unsigned int invert = mc->invert;
2629
2630 if (snd_soc_volsw_is_stereo(mc))
2631 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002632 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002633 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002634
Richard Purdie2b97eab2006-10-06 18:32:18 +02002635 ucontrol->value.integer.value[0] =
2636 (snd_soc_read(widget->codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002637 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002638 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002639 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002640
2641 return 0;
2642}
2643EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2644
2645/**
2646 * snd_soc_dapm_put_volsw - dapm mixer set callback
2647 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002648 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002649 *
2650 * Callback to set the value of a dapm mixer control.
2651 *
2652 * Returns 0 for success.
2653 */
2654int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2655 struct snd_ctl_elem_value *ucontrol)
2656{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002657 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2658 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2659 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002660 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002661 struct soc_mixer_control *mc =
2662 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002663 unsigned int reg = mc->reg;
2664 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002665 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002666 unsigned int mask = (1 << fls(max)) - 1;
2667 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002668 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002669 int connect, change;
2670 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002671
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002672 if (snd_soc_volsw_is_stereo(mc))
2673 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002674 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002675 kcontrol->id.name);
2676
Richard Purdie2b97eab2006-10-06 18:32:18 +02002677 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002678 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002679
2680 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002681 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002682 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002683 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002684
Liam Girdwood3cd04342012-03-09 12:02:08 +00002685 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002686
Stephen Warrene9cf7042011-01-27 14:54:05 -07002687 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002688 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002689 update.kcontrol = kcontrol;
2690 update.reg = reg;
2691 update.mask = mask;
2692 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002693
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002694 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002695
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002696 soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002697
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002698 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002699 }
2700
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002701 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002702 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002703}
2704EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2705
2706/**
2707 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2708 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002709 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002710 *
2711 * Callback to get the value of a dapm enumerated double mixer control.
2712 *
2713 * Returns 0 for success.
2714 */
2715int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2716 struct snd_ctl_elem_value *ucontrol)
2717{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002718 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2719 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002720 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002721 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002722
Richard Purdie2b97eab2006-10-06 18:32:18 +02002723 val = snd_soc_read(widget->codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002724 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002725 if (e->shift_l != e->shift_r)
2726 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002727 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002728
2729 return 0;
2730}
2731EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2732
2733/**
2734 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2735 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002736 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002737 *
2738 * Callback to set the value of a dapm enumerated double mixer control.
2739 *
2740 * Returns 0 for success.
2741 */
2742int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2743 struct snd_ctl_elem_value *ucontrol)
2744{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002745 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2746 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2747 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002748 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002749 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002750 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002751 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002752 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002753
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002754 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002755 return -EINVAL;
2756 mux = ucontrol->value.enumerated.item[0];
2757 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002758 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002759 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002760 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002761 return -EINVAL;
2762 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002763 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002764 }
2765
Liam Girdwood3cd04342012-03-09 12:02:08 +00002766 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002767
Mark Brown3a655772009-10-05 17:23:30 +01002768 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002769 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002770 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002771
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002772 update.kcontrol = kcontrol;
2773 update.reg = e->reg;
2774 update.mask = mask;
2775 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002776 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002777
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002778 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002779
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002780 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002781 }
2782
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002783 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002784 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002785}
2786EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2787
2788/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002789 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2790 * @kcontrol: mixer control
2791 * @ucontrol: control element information
2792 *
2793 * Returns 0 for success.
2794 */
2795int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2796 struct snd_ctl_elem_value *ucontrol)
2797{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002798 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2799 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002800
2801 ucontrol->value.enumerated.item[0] = widget->value;
2802
2803 return 0;
2804}
2805EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2806
2807/**
2808 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2809 * @kcontrol: mixer control
2810 * @ucontrol: control element information
2811 *
2812 * Returns 0 for success.
2813 */
2814int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2815 struct snd_ctl_elem_value *ucontrol)
2816{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002817 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2818 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2819 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002820 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002821 struct soc_enum *e =
2822 (struct soc_enum *)kcontrol->private_value;
2823 int change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002824
2825 if (ucontrol->value.enumerated.item[0] >= e->max)
2826 return -EINVAL;
2827
Liam Girdwood3cd04342012-03-09 12:02:08 +00002828 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002829
2830 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002831 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002832 widget->value = ucontrol->value.enumerated.item[0];
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002833 soc_dapm_mux_update_power(card, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002834 }
2835
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002836 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002837 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002838}
2839EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2840
2841/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002842 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2843 * callback
2844 * @kcontrol: mixer control
2845 * @ucontrol: control element information
2846 *
2847 * Callback to get the value of a dapm semi enumerated double mixer control.
2848 *
2849 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2850 * used for handling bitfield coded enumeration for example.
2851 *
2852 * Returns 0 for success.
2853 */
2854int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2855 struct snd_ctl_elem_value *ucontrol)
2856{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002857 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2858 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002859 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002860 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002861
2862 reg_val = snd_soc_read(widget->codec, e->reg);
2863 val = (reg_val >> e->shift_l) & e->mask;
2864 for (mux = 0; mux < e->max; mux++) {
2865 if (val == e->values[mux])
2866 break;
2867 }
2868 ucontrol->value.enumerated.item[0] = mux;
2869 if (e->shift_l != e->shift_r) {
2870 val = (reg_val >> e->shift_r) & e->mask;
2871 for (mux = 0; mux < e->max; mux++) {
2872 if (val == e->values[mux])
2873 break;
2874 }
2875 ucontrol->value.enumerated.item[1] = mux;
2876 }
2877
2878 return 0;
2879}
2880EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2881
2882/**
2883 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2884 * callback
2885 * @kcontrol: mixer control
2886 * @ucontrol: control element information
2887 *
2888 * Callback to set the value of a dapm semi enumerated double mixer control.
2889 *
2890 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2891 * used for handling bitfield coded enumeration for example.
2892 *
2893 * Returns 0 for success.
2894 */
2895int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2896 struct snd_ctl_elem_value *ucontrol)
2897{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002898 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2899 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2900 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002901 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002902 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002903 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002904 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002905 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002906
2907 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2908 return -EINVAL;
2909 mux = ucontrol->value.enumerated.item[0];
2910 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2911 mask = e->mask << e->shift_l;
2912 if (e->shift_l != e->shift_r) {
2913 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2914 return -EINVAL;
2915 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2916 mask |= e->mask << e->shift_r;
2917 }
2918
Liam Girdwood3cd04342012-03-09 12:02:08 +00002919 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002920
Mark Brown3a655772009-10-05 17:23:30 +01002921 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002922 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002923 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002924
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002925 update.kcontrol = kcontrol;
2926 update.reg = e->reg;
2927 update.mask = mask;
2928 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002929 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002930
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002931 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002932
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002933 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002934 }
2935
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002936 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002937 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002938}
2939EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2940
2941/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002942 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2943 *
2944 * @kcontrol: mixer control
2945 * @uinfo: control element information
2946 *
2947 * Callback to provide information about a pin switch control.
2948 */
2949int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2950 struct snd_ctl_elem_info *uinfo)
2951{
2952 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2953 uinfo->count = 1;
2954 uinfo->value.integer.min = 0;
2955 uinfo->value.integer.max = 1;
2956
2957 return 0;
2958}
2959EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2960
2961/**
2962 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2963 *
2964 * @kcontrol: mixer control
2965 * @ucontrol: Value
2966 */
2967int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2968 struct snd_ctl_elem_value *ucontrol)
2969{
Mark Brown48a8c392012-02-14 17:11:15 -08002970 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002971 const char *pin = (const char *)kcontrol->private_value;
2972
Liam Girdwood3cd04342012-03-09 12:02:08 +00002973 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002974
2975 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002976 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002977
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002978 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002979
2980 return 0;
2981}
2982EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2983
2984/**
2985 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2986 *
2987 * @kcontrol: mixer control
2988 * @ucontrol: Value
2989 */
2990int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2991 struct snd_ctl_elem_value *ucontrol)
2992{
Mark Brown48a8c392012-02-14 17:11:15 -08002993 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002994 const char *pin = (const char *)kcontrol->private_value;
2995
Liam Girdwood3cd04342012-03-09 12:02:08 +00002996 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002997
2998 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002999 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003000 else
Mark Brown48a8c392012-02-14 17:11:15 -08003001 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003002
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003003 mutex_unlock(&card->dapm_mutex);
3004
Mark Brown48a8c392012-02-14 17:11:15 -08003005 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003006 return 0;
3007}
3008EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3009
Mark Brown5ba06fc2012-02-16 11:07:13 -08003010static struct snd_soc_dapm_widget *
3011snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3012 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003013{
3014 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003015 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003016
3017 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003018 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003019
Mark Brown62ea8742012-01-21 21:14:48 +00003020 switch (w->id) {
3021 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003022 w->regulator = devm_regulator_get(dapm->dev, w->name);
3023 if (IS_ERR(w->regulator)) {
3024 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003025 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003026 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003027 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003028 }
Mark Brown8784c772013-01-10 19:33:47 +00003029
3030 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
3031 ret = regulator_allow_bypass(w->regulator, true);
3032 if (ret != 0)
3033 dev_warn(w->dapm->dev,
3034 "ASoC: Failed to unbypass %s: %d\n",
3035 w->name, ret);
3036 }
Mark Brown62ea8742012-01-21 21:14:48 +00003037 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003038 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003039#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003040 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003041 if (IS_ERR(w->clk)) {
3042 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003043 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003044 w->name, ret);
3045 return NULL;
3046 }
Mark Brownec029952012-06-04 08:16:20 +01003047#else
3048 return NULL;
3049#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003050 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003051 default:
3052 break;
3053 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003054
Mark Brown88e8b9a2011-03-02 18:18:24 +00003055 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003056 w->name = kasprintf(GFP_KERNEL, "%s %s",
3057 dapm->codec->name_prefix, widget->name);
3058 else
3059 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3060
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003061 if (w->name == NULL) {
3062 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003063 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003064 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003065
Mark Brown7ca3a182011-10-08 14:04:50 +01003066 switch (w->id) {
3067 case snd_soc_dapm_switch:
3068 case snd_soc_dapm_mixer:
3069 case snd_soc_dapm_mixer_named_ctl:
3070 w->power_check = dapm_generic_check_power;
3071 break;
3072 case snd_soc_dapm_mux:
3073 case snd_soc_dapm_virt_mux:
3074 case snd_soc_dapm_value_mux:
3075 w->power_check = dapm_generic_check_power;
3076 break;
Mark Brown46162742013-06-05 19:36:11 +01003077 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003078 w->power_check = dapm_adc_check_power;
3079 break;
Mark Brown46162742013-06-05 19:36:11 +01003080 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003081 w->power_check = dapm_dac_check_power;
3082 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003083 case snd_soc_dapm_adc:
3084 case snd_soc_dapm_aif_out:
3085 case snd_soc_dapm_dac:
3086 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003087 case snd_soc_dapm_pga:
3088 case snd_soc_dapm_out_drv:
3089 case snd_soc_dapm_input:
3090 case snd_soc_dapm_output:
3091 case snd_soc_dapm_micbias:
3092 case snd_soc_dapm_spk:
3093 case snd_soc_dapm_hp:
3094 case snd_soc_dapm_mic:
3095 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003096 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003097 w->power_check = dapm_generic_check_power;
3098 break;
3099 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003100 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003101 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003102 w->power_check = dapm_supply_check_power;
3103 break;
3104 default:
3105 w->power_check = dapm_always_on_check_power;
3106 break;
3107 }
3108
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003109 w->dapm = dapm;
3110 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003111 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003112 INIT_LIST_HEAD(&w->sources);
3113 INIT_LIST_HEAD(&w->sinks);
3114 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003115 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003116 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003117
3118 /* machine layer set ups unconnected pins and insertions */
3119 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003120 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003121}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003122
3123/**
Mark Brown4ba13272008-05-13 14:51:19 +02003124 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003125 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003126 * @widget: widget array
3127 * @num: number of widgets
3128 *
3129 * Creates new DAPM controls based upon the templates.
3130 *
3131 * Returns 0 for success else error.
3132 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003133int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003134 const struct snd_soc_dapm_widget *widget,
3135 int num)
3136{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003137 struct snd_soc_dapm_widget *w;
3138 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003139 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003140
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003141 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003142 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003143 w = snd_soc_dapm_new_control(dapm, widget);
3144 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003145 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003146 "ASoC: Failed to create DAPM control %s\n",
3147 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003148 ret = -ENOMEM;
3149 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003150 }
Mark Brown4ba13272008-05-13 14:51:19 +02003151 widget++;
3152 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003153 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003154 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003155}
3156EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3157
Mark Brownc74184e2012-04-04 22:12:09 +01003158static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3159 struct snd_kcontrol *kcontrol, int event)
3160{
3161 struct snd_soc_dapm_path *source_p, *sink_p;
3162 struct snd_soc_dai *source, *sink;
3163 const struct snd_soc_pcm_stream *config = w->params;
3164 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003165 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003166 u64 fmt;
3167 int ret;
3168
3169 BUG_ON(!config);
3170 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3171
3172 /* We only support a single source and sink, pick the first */
3173 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3174 list_sink);
3175 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3176 list_source);
3177
3178 BUG_ON(!source_p || !sink_p);
3179 BUG_ON(!sink_p->source || !source_p->sink);
3180 BUG_ON(!source_p->source || !sink_p->sink);
3181
3182 source = source_p->source->priv;
3183 sink = sink_p->sink->priv;
3184
3185 /* Be a little careful as we don't want to overflow the mask array */
3186 if (config->formats) {
3187 fmt = ffs(config->formats) - 1;
3188 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003189 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003190 config->formats);
3191 fmt = 0;
3192 }
3193
3194 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003195 params = kzalloc(sizeof(*params), GFP_KERNEL);
3196 if (!params) {
3197 ret = -ENOMEM;
3198 goto out;
3199 }
3200 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003201
Mark Brown9747cec2012-04-26 19:12:21 +01003202 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003203 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003204 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003205 config->rate_max;
3206
Mark Brown9747cec2012-04-26 19:12:21 +01003207 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003208 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003209 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003210 = config->channels_max;
3211
3212 memset(&substream, 0, sizeof(substream));
3213
3214 switch (event) {
3215 case SND_SOC_DAPM_PRE_PMU:
3216 if (source->driver->ops && source->driver->ops->hw_params) {
3217 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3218 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003219 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003220 if (ret != 0) {
3221 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003222 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003223 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003224 }
3225 }
3226
3227 if (sink->driver->ops && sink->driver->ops->hw_params) {
3228 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003229 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003230 sink);
3231 if (ret != 0) {
3232 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003233 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003234 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003235 }
3236 }
3237 break;
3238
3239 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003240 ret = snd_soc_dai_digital_mute(sink, 0,
3241 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003242 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003243 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003244 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003245 break;
3246
3247 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003248 ret = snd_soc_dai_digital_mute(sink, 1,
3249 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003250 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003251 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003252 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003253 break;
3254
3255 default:
3256 BUG();
3257 return -EINVAL;
3258 }
3259
Mark Brown9747cec2012-04-26 19:12:21 +01003260out:
3261 kfree(params);
3262 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003263}
3264
3265int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3266 const struct snd_soc_pcm_stream *params,
3267 struct snd_soc_dapm_widget *source,
3268 struct snd_soc_dapm_widget *sink)
3269{
3270 struct snd_soc_dapm_route routes[2];
3271 struct snd_soc_dapm_widget template;
3272 struct snd_soc_dapm_widget *w;
3273 size_t len;
3274 char *link_name;
3275
3276 len = strlen(source->name) + strlen(sink->name) + 2;
3277 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3278 if (!link_name)
3279 return -ENOMEM;
3280 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3281
3282 memset(&template, 0, sizeof(template));
3283 template.reg = SND_SOC_NOPM;
3284 template.id = snd_soc_dapm_dai_link;
3285 template.name = link_name;
3286 template.event = snd_soc_dai_link_event;
3287 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3288 SND_SOC_DAPM_PRE_PMD;
3289
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003290 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003291
3292 w = snd_soc_dapm_new_control(&card->dapm, &template);
3293 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003294 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003295 link_name);
3296 return -ENOMEM;
3297 }
3298
3299 w->params = params;
3300
3301 memset(&routes, 0, sizeof(routes));
3302
3303 routes[0].source = source->name;
3304 routes[0].sink = link_name;
3305 routes[1].source = link_name;
3306 routes[1].sink = sink->name;
3307
3308 return snd_soc_dapm_add_routes(&card->dapm, routes,
3309 ARRAY_SIZE(routes));
3310}
3311
Mark Brown888df392012-02-16 19:37:51 -08003312int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3313 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003314{
Mark Brown888df392012-02-16 19:37:51 -08003315 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003316 struct snd_soc_dapm_widget *w;
3317
Mark Brown888df392012-02-16 19:37:51 -08003318 WARN_ON(dapm->dev != dai->dev);
3319
3320 memset(&template, 0, sizeof(template));
3321 template.reg = SND_SOC_NOPM;
3322
3323 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003324 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003325 template.name = dai->driver->playback.stream_name;
3326 template.sname = dai->driver->playback.stream_name;
3327
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003328 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003329 template.name);
3330
3331 w = snd_soc_dapm_new_control(dapm, &template);
3332 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003333 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003334 dai->driver->playback.stream_name);
3335 }
3336
3337 w->priv = dai;
3338 dai->playback_widget = w;
3339 }
3340
3341 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003342 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003343 template.name = dai->driver->capture.stream_name;
3344 template.sname = dai->driver->capture.stream_name;
3345
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003346 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003347 template.name);
3348
3349 w = snd_soc_dapm_new_control(dapm, &template);
3350 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003351 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003352 dai->driver->capture.stream_name);
3353 }
3354
3355 w->priv = dai;
3356 dai->capture_widget = w;
3357 }
3358
3359 return 0;
3360}
3361
3362int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3363{
3364 struct snd_soc_dapm_widget *dai_w, *w;
3365 struct snd_soc_dai *dai;
3366 struct snd_soc_dapm_route r;
3367
3368 memset(&r, 0, sizeof(r));
3369
3370 /* For each DAI widget... */
3371 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003372 switch (dai_w->id) {
3373 case snd_soc_dapm_dai_in:
3374 case snd_soc_dapm_dai_out:
3375 break;
3376 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003377 continue;
Mark Brown46162742013-06-05 19:36:11 +01003378 }
Mark Brown888df392012-02-16 19:37:51 -08003379
3380 dai = dai_w->priv;
3381
3382 /* ...find all widgets with the same stream and link them */
3383 list_for_each_entry(w, &card->widgets, list) {
3384 if (w->dapm != dai_w->dapm)
3385 continue;
3386
Mark Brown46162742013-06-05 19:36:11 +01003387 switch (w->id) {
3388 case snd_soc_dapm_dai_in:
3389 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003390 continue;
Mark Brown46162742013-06-05 19:36:11 +01003391 default:
3392 break;
3393 }
Mark Brown888df392012-02-16 19:37:51 -08003394
3395 if (!w->sname)
3396 continue;
3397
3398 if (dai->driver->playback.stream_name &&
3399 strstr(w->sname,
3400 dai->driver->playback.stream_name)) {
3401 r.source = dai->playback_widget->name;
3402 r.sink = w->name;
3403 dev_dbg(dai->dev, "%s -> %s\n",
3404 r.source, r.sink);
3405
3406 snd_soc_dapm_add_route(w->dapm, &r);
3407 }
3408
3409 if (dai->driver->capture.stream_name &&
3410 strstr(w->sname,
3411 dai->driver->capture.stream_name)) {
3412 r.source = w->name;
3413 r.sink = dai->capture_widget->name;
3414 dev_dbg(dai->dev, "%s -> %s\n",
3415 r.source, r.sink);
3416
3417 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003418 }
3419 }
3420 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003421
Mark Brown888df392012-02-16 19:37:51 -08003422 return 0;
3423}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003424
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003425static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3426 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003427{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003428
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003429 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3430 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3431 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003432
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003433 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3434 w_cpu = cpu_dai->playback_widget;
3435 w_codec = codec_dai->playback_widget;
3436 } else {
3437 w_cpu = cpu_dai->capture_widget;
3438 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003439 }
3440
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003441 if (w_cpu) {
3442
3443 dapm_mark_dirty(w_cpu, "stream event");
3444
3445 switch (event) {
3446 case SND_SOC_DAPM_STREAM_START:
3447 w_cpu->active = 1;
3448 break;
3449 case SND_SOC_DAPM_STREAM_STOP:
3450 w_cpu->active = 0;
3451 break;
3452 case SND_SOC_DAPM_STREAM_SUSPEND:
3453 case SND_SOC_DAPM_STREAM_RESUME:
3454 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3455 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3456 break;
3457 }
3458 }
3459
3460 if (w_codec) {
3461
3462 dapm_mark_dirty(w_codec, "stream event");
3463
3464 switch (event) {
3465 case SND_SOC_DAPM_STREAM_START:
3466 w_codec->active = 1;
3467 break;
3468 case SND_SOC_DAPM_STREAM_STOP:
3469 w_codec->active = 0;
3470 break;
3471 case SND_SOC_DAPM_STREAM_SUSPEND:
3472 case SND_SOC_DAPM_STREAM_RESUME:
3473 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3474 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3475 break;
3476 }
3477 }
3478
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003479 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003480}
3481
3482/**
3483 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3484 * @rtd: PCM runtime data
3485 * @stream: stream name
3486 * @event: stream event
3487 *
3488 * Sends a stream event to the dapm core. The core then makes any
3489 * necessary widget power changes.
3490 *
3491 * Returns 0 for success else error.
3492 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003493void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3494 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003495{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003496 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003497
Liam Girdwood3cd04342012-03-09 12:02:08 +00003498 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003499 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003500 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003501}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003502
3503/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003504 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003505 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003506 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003507 *
Mark Brown74b8f952009-06-06 11:26:15 +01003508 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003509 * a valid audio route and active audio stream.
3510 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3511 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003512 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003513int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003514{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003515 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003516}
Liam Girdwooda5302182008-07-07 13:35:17 +01003517EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003518
3519/**
Mark Brownda341832010-03-15 19:23:37 +00003520 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003521 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003522 * @pin: pin name
3523 *
3524 * Enables input/output pin regardless of any other state. This is
3525 * intended for use with microphone bias supplies used in microphone
3526 * jack detection.
3527 *
3528 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3529 * do any widget power switching.
3530 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003531int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3532 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003533{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003534 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003535
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003536 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003537 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003538 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003539 }
3540
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003541 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003542 w->connected = 1;
3543 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003544 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003545
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003546 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003547}
3548EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3549
3550/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003551 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003552 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003553 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003554 *
Mark Brown74b8f952009-06-06 11:26:15 +01003555 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003556 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3557 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003558 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003559int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3560 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003561{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003562 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003563}
3564EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3565
3566/**
Mark Brown5817b522008-09-24 11:23:11 +01003567 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003568 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003569 * @pin: pin name
3570 *
3571 * Marks the specified pin as being not connected, disabling it along
3572 * any parent or child widgets. At present this is identical to
3573 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3574 * additional things such as disabling controls which only affect
3575 * paths through the pin.
3576 *
3577 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3578 * do any widget power switching.
3579 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003580int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003581{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003582 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003583}
3584EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3585
3586/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003587 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003588 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003589 * @pin: audio signal pin endpoint (or start point)
3590 *
3591 * Get audio pin status - connected or disconnected.
3592 *
3593 * Returns 1 for connected otherwise 0.
3594 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003595int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3596 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003597{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003598 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003599
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003600 if (w)
3601 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003602
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003603 return 0;
3604}
Liam Girdwooda5302182008-07-07 13:35:17 +01003605EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003606
3607/**
Mark Brown1547aba2010-05-07 21:11:40 +01003608 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003609 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003610 * @pin: audio signal pin endpoint (or start point)
3611 *
3612 * Mark the given endpoint or pin as ignoring suspend. When the
3613 * system is disabled a path between two endpoints flagged as ignoring
3614 * suspend will not be disabled. The path must already be enabled via
3615 * normal means at suspend time, it will not be turned on if it was not
3616 * already enabled.
3617 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003618int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3619 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003620{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003621 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003622
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003623 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003624 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003625 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003626 }
3627
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003628 w->ignore_suspend = 1;
3629
3630 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003631}
3632EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3633
Stephen Warren16332812011-11-23 12:42:04 -07003634static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3635 struct snd_soc_dapm_widget *w)
3636{
3637 struct snd_soc_dapm_path *p;
3638
3639 list_for_each_entry(p, &card->paths, list) {
3640 if ((p->source == w) || (p->sink == w)) {
3641 dev_dbg(card->dev,
3642 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3643 p->source->name, p->source->id, p->source->dapm,
3644 p->sink->name, p->sink->id, p->sink->dapm);
3645
3646 /* Connected to something other than the codec */
3647 if (p->source->dapm != p->sink->dapm)
3648 return true;
3649 /*
3650 * Loopback connection from codec external pin to
3651 * codec external pin
3652 */
3653 if (p->sink->id == snd_soc_dapm_input) {
3654 switch (p->source->id) {
3655 case snd_soc_dapm_output:
3656 case snd_soc_dapm_micbias:
3657 return true;
3658 default:
3659 break;
3660 }
3661 }
3662 }
3663 }
3664
3665 return false;
3666}
3667
3668/**
3669 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3670 * @codec: The codec whose pins should be processed
3671 *
3672 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3673 * which are unused. Pins are used if they are connected externally to the
3674 * codec, whether that be to some other device, or a loop-back connection to
3675 * the codec itself.
3676 */
3677void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3678{
3679 struct snd_soc_card *card = codec->card;
3680 struct snd_soc_dapm_context *dapm = &codec->dapm;
3681 struct snd_soc_dapm_widget *w;
3682
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003683 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003684 &card->dapm, &codec->dapm);
3685
3686 list_for_each_entry(w, &card->widgets, list) {
3687 if (w->dapm != dapm)
3688 continue;
3689 switch (w->id) {
3690 case snd_soc_dapm_input:
3691 case snd_soc_dapm_output:
3692 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003693 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003694 w->name);
3695 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003696 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003697 "... Not in map; disabling\n");
3698 snd_soc_dapm_nc_pin(dapm, w->name);
3699 }
3700 break;
3701 default:
3702 break;
3703 }
3704 }
3705}
3706
Mark Brown1547aba2010-05-07 21:11:40 +01003707/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003708 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003709 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003710 *
3711 * Free all dapm widgets and resources.
3712 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003713void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003714{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003715 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003716 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003717 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003718 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003719}
3720EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3721
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003722static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003723{
Liam Girdwood01005a72012-07-06 16:57:05 +01003724 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003725 struct snd_soc_dapm_widget *w;
3726 LIST_HEAD(down_list);
3727 int powerdown = 0;
3728
Liam Girdwood01005a72012-07-06 16:57:05 +01003729 mutex_lock(&card->dapm_mutex);
3730
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003731 list_for_each_entry(w, &dapm->card->widgets, list) {
3732 if (w->dapm != dapm)
3733 continue;
Mark Brown51737472009-06-22 13:16:51 +01003734 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003735 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003736 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003737 powerdown = 1;
3738 }
3739 }
3740
3741 /* If there were no widgets to power down we're already in
3742 * standby.
3743 */
3744 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003745 if (dapm->bias_level == SND_SOC_BIAS_ON)
3746 snd_soc_dapm_set_bias_level(dapm,
3747 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003748 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003749 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3750 snd_soc_dapm_set_bias_level(dapm,
3751 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003752 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003753
3754 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003755}
Mark Brown51737472009-06-22 13:16:51 +01003756
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003757/*
3758 * snd_soc_dapm_shutdown - callback for system shutdown
3759 */
3760void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3761{
3762 struct snd_soc_codec *codec;
3763
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003764 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003765 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003766 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3767 snd_soc_dapm_set_bias_level(&codec->dapm,
3768 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003769 }
Mark Brown51737472009-06-22 13:16:51 +01003770}
3771
Richard Purdie2b97eab2006-10-06 18:32:18 +02003772/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003773MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003774MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3775MODULE_LICENSE("GPL");