blob: 7449e27bf13381564cb8e44db6a8bf82dd31a2ef [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
Mark Brown68f89ad2010-11-03 23:51:49 -04001216static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1217 struct snd_soc_dapm_widget *w, int event)
1218{
1219 struct snd_soc_card *card = dapm->card;
1220 const char *ev_name;
1221 int power, ret;
1222
1223 switch (event) {
1224 case SND_SOC_DAPM_PRE_PMU:
1225 ev_name = "PRE_PMU";
1226 power = 1;
1227 break;
1228 case SND_SOC_DAPM_POST_PMU:
1229 ev_name = "POST_PMU";
1230 power = 1;
1231 break;
1232 case SND_SOC_DAPM_PRE_PMD:
1233 ev_name = "PRE_PMD";
1234 power = 0;
1235 break;
1236 case SND_SOC_DAPM_POST_PMD:
1237 ev_name = "POST_PMD";
1238 power = 0;
1239 break;
Mark Brown80114122013-02-25 15:14:19 +00001240 case SND_SOC_DAPM_WILL_PMU:
1241 ev_name = "WILL_PMU";
1242 power = 1;
1243 break;
1244 case SND_SOC_DAPM_WILL_PMD:
1245 ev_name = "WILL_PMD";
1246 power = 0;
1247 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001248 default:
1249 BUG();
1250 return;
1251 }
1252
1253 if (w->power != power)
1254 return;
1255
1256 if (w->event && (w->event_flags & event)) {
1257 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1258 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001259 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001260 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001261 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001262 if (ret < 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001263 dev_err(dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001264 ev_name, w->name, ret);
1265 }
1266}
1267
Mark Brownb22ead22009-06-07 12:51:26 +01001268/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001269static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001270 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001271{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001272 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001273 struct snd_soc_dapm_widget *w;
1274 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001275 unsigned int value = 0;
1276 unsigned int mask = 0;
1277 unsigned int cur_mask;
1278
1279 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1280 power_list)->reg;
1281
1282 list_for_each_entry(w, pending, power_list) {
1283 cur_mask = 1 << w->shift;
1284 BUG_ON(reg != w->reg);
1285
1286 if (w->invert)
1287 power = !w->power;
1288 else
1289 power = w->power;
1290
1291 mask |= cur_mask;
1292 if (power)
1293 value |= cur_mask;
1294
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001295 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001296 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1297 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001298
Mark Brown68f89ad2010-11-03 23:51:49 -04001299 /* Check for events */
1300 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1301 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001302 }
1303
Mark Brown81628102009-06-07 13:21:24 +01001304 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001305 /* Any widget will do, they should all be updating the
1306 * same register.
1307 */
1308 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1309 power_list);
1310
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001311 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001312 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001313 value, mask, reg, card->pop_time);
1314 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001315 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001316 }
1317
1318 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001319 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1320 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001321 }
Mark Brown42aa3412009-03-01 19:21:10 +00001322}
1323
Mark Brownb22ead22009-06-07 12:51:26 +01001324/* Apply a DAPM power sequence.
1325 *
1326 * We walk over a pre-sorted list of widgets to apply power to. In
1327 * order to minimise the number of writes to the device required
1328 * multiple widgets will be updated in a single write where possible.
1329 * Currently anything that requires more than a single write is not
1330 * handled.
1331 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001332static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001333 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001334{
1335 struct snd_soc_dapm_widget *w, *n;
1336 LIST_HEAD(pending);
1337 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001338 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001339 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001340 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001341 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001342 int *sort;
1343
1344 if (power_up)
1345 sort = dapm_up_seq;
1346 else
1347 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001348
Mark Brownb22ead22009-06-07 12:51:26 +01001349 list_for_each_entry_safe(w, n, list, power_list) {
1350 ret = 0;
1351
1352 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001353 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001354 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001355 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001356 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001357
Mark Brown474b62d2011-01-18 16:14:44 +00001358 if (cur_dapm && cur_dapm->seq_notifier) {
1359 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1360 if (sort[i] == cur_sort)
1361 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001362 i,
1363 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001364 }
1365
Mark Brownb22ead22009-06-07 12:51:26 +01001366 INIT_LIST_HEAD(&pending);
1367 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001368 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001369 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001370 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001371 }
1372
Mark Brown163cac02009-06-07 10:12:52 +01001373 switch (w->id) {
1374 case snd_soc_dapm_pre:
1375 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001376 list_for_each_entry_safe_continue(w, n, list,
1377 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001378
Mark Brownb22ead22009-06-07 12:51:26 +01001379 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001380 ret = w->event(w,
1381 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001382 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001383 ret = w->event(w,
1384 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001385 break;
1386
1387 case snd_soc_dapm_post:
1388 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001389 list_for_each_entry_safe_continue(w, n, list,
1390 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001391
Mark Brownb22ead22009-06-07 12:51:26 +01001392 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001393 ret = w->event(w,
1394 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001395 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001396 ret = w->event(w,
1397 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001398 break;
1399
Mark Brown163cac02009-06-07 10:12:52 +01001400 default:
Mark Brown81628102009-06-07 13:21:24 +01001401 /* Queue it up for application */
1402 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001403 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001404 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001405 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001406 list_move(&w->power_list, &pending);
1407 break;
Mark Brown163cac02009-06-07 10:12:52 +01001408 }
Mark Brownb22ead22009-06-07 12:51:26 +01001409
1410 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001411 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001412 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001413 }
Mark Brownb22ead22009-06-07 12:51:26 +01001414
1415 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001416 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001417
1418 if (cur_dapm && cur_dapm->seq_notifier) {
1419 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1420 if (sort[i] == cur_sort)
1421 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001422 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001423 }
Mark Brown163cac02009-06-07 10:12:52 +01001424}
1425
Mark Brown97404f22010-12-14 16:13:57 +00001426static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1427{
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02001428 struct snd_soc_dapm_update *update = dapm->card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001429 struct snd_soc_dapm_widget_list *wlist;
1430 struct snd_soc_dapm_widget *w = NULL;
1431 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001432 int ret;
1433
1434 if (!update)
1435 return;
1436
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001437 wlist = snd_kcontrol_chip(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001438
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001439 for (wi = 0; wi < wlist->num_widgets; wi++) {
1440 w = wlist->widgets[wi];
1441
1442 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1443 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1444 if (ret != 0)
1445 dev_err(dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1446 w->name, ret);
1447 }
Mark Brown97404f22010-12-14 16:13:57 +00001448 }
1449
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001450 if (!w)
1451 return;
1452
Liam Girdwood49575fb52012-03-06 18:16:19 +00001453 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001454 update->val);
1455 if (ret < 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001456 dev_err(dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1457 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001458
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001459 for (wi = 0; wi < wlist->num_widgets; wi++) {
1460 w = wlist->widgets[wi];
1461
1462 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1463 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1464 if (ret != 0)
1465 dev_err(dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1466 w->name, ret);
1467 }
Mark Brown97404f22010-12-14 16:13:57 +00001468 }
1469}
1470
Mark Brown9d0624a2011-02-18 11:49:43 -08001471/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1472 * they're changing state.
1473 */
1474static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1475{
1476 struct snd_soc_dapm_context *d = data;
1477 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001478
Mark Brown56fba412011-06-04 11:25:10 +01001479 /* If we're off and we're not supposed to be go into STANDBY */
1480 if (d->bias_level == SND_SOC_BIAS_OFF &&
1481 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001482 if (d->dev)
1483 pm_runtime_get_sync(d->dev);
1484
Mark Brown9d0624a2011-02-18 11:49:43 -08001485 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1486 if (ret != 0)
1487 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001488 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001489 }
1490
Mark Brown56fba412011-06-04 11:25:10 +01001491 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1492 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001493 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1494 if (ret != 0)
1495 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001496 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001497 }
1498}
1499
1500/* Async callback run prior to DAPM sequences - brings to their final
1501 * state.
1502 */
1503static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1504{
1505 struct snd_soc_dapm_context *d = data;
1506 int ret;
1507
1508 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001509 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1510 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1511 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001512 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1513 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001514 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001515 ret);
1516 }
1517
1518 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001519 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1520 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001521 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1522 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001523 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1524 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001525
1526 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001527 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001528 }
1529
1530 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001531 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1532 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001533 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1534 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001535 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001536 ret);
1537 }
1538}
Mark Brown97404f22010-12-14 16:13:57 +00001539
Mark Brownfe4fda52011-10-03 22:36:57 +01001540static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1541 bool power, bool connect)
1542{
1543 /* If a connection is being made or broken then that update
1544 * will have marked the peer dirty, otherwise the widgets are
1545 * not connected and this update has no impact. */
1546 if (!connect)
1547 return;
1548
1549 /* If the peer is already in the state we're moving to then we
1550 * won't have an impact on it. */
1551 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001552 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001553}
1554
Mark Brown05623c42011-09-28 17:02:31 +01001555static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1556 struct list_head *up_list,
1557 struct list_head *down_list)
1558{
Mark Browndb432b42011-10-03 21:06:40 +01001559 struct snd_soc_dapm_path *path;
1560
Mark Brown05623c42011-09-28 17:02:31 +01001561 if (w->power == power)
1562 return;
1563
1564 trace_snd_soc_dapm_widget_power(w, power);
1565
Mark Browndb432b42011-10-03 21:06:40 +01001566 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001567 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001568 */
1569 list_for_each_entry(path, &w->sources, list_sink) {
1570 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001571 dapm_widget_set_peer_power(path->source, power,
1572 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001573 }
1574 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001575 switch (w->id) {
1576 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001577 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001578 case snd_soc_dapm_clock_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001579 /* Supplies can't affect their outputs, only their inputs */
1580 break;
1581 default:
1582 list_for_each_entry(path, &w->sinks, list_source) {
1583 if (path->sink) {
1584 dapm_widget_set_peer_power(path->sink, power,
1585 path->connect);
1586 }
Mark Browndb432b42011-10-03 21:06:40 +01001587 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001588 break;
Mark Browndb432b42011-10-03 21:06:40 +01001589 }
1590
Mark Brown05623c42011-09-28 17:02:31 +01001591 if (power)
1592 dapm_seq_insert(w, up_list, true);
1593 else
1594 dapm_seq_insert(w, down_list, false);
1595
1596 w->power = power;
1597}
1598
Mark Brown7c81beb2011-09-20 22:22:32 +01001599static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1600 struct list_head *up_list,
1601 struct list_head *down_list)
1602{
Mark Brown7c81beb2011-09-20 22:22:32 +01001603 int power;
1604
1605 switch (w->id) {
1606 case snd_soc_dapm_pre:
1607 dapm_seq_insert(w, down_list, false);
1608 break;
1609 case snd_soc_dapm_post:
1610 dapm_seq_insert(w, up_list, true);
1611 break;
1612
1613 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001614 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001615
Mark Brown05623c42011-09-28 17:02:31 +01001616 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001617 break;
1618 }
1619}
1620
Mark Brown42aa3412009-03-01 19:21:10 +00001621/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001622 * Scan each dapm widget for complete audio path.
1623 * A complete path is a route that has valid endpoints i.e.:-
1624 *
1625 * o DAC to output pin.
1626 * o Input Pin to ADC.
1627 * o Input pin to Output pin (bypass, sidetone)
1628 * o DAC to ADC (loopback).
1629 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001630static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001631{
Mark Brown12ea2c72011-03-02 18:17:32 +00001632 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001633 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001634 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001635 LIST_HEAD(up_list);
1636 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001637 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001638 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001639
Mark Brown84e90932010-11-04 00:07:02 -04001640 trace_snd_soc_dapm_start(card);
1641
Mark Brown56fba412011-06-04 11:25:10 +01001642 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001643 if (d->idle_bias_off)
1644 d->target_bias_level = SND_SOC_BIAS_OFF;
1645 else
1646 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001647 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001648
Liam Girdwood6c120e12012-02-15 15:15:34 +00001649 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001650
Mark Brown6d3ddc82009-05-16 17:47:29 +01001651 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001652 * lists indicating if they should be powered up or down. We
1653 * only check widgets that have been flagged as dirty but note
1654 * that new widgets may be added to the dirty list while we
1655 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001656 */
Mark Browndb432b42011-10-03 21:06:40 +01001657 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001658 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001659 }
1660
Mark Brownf9de6d72011-09-28 17:19:47 +01001661 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001662 switch (w->id) {
1663 case snd_soc_dapm_pre:
1664 case snd_soc_dapm_post:
1665 /* These widgets always need to be powered */
1666 break;
1667 default:
1668 list_del_init(&w->dirty);
1669 break;
1670 }
Mark Browndb432b42011-10-03 21:06:40 +01001671
Mark Brownf9de6d72011-09-28 17:19:47 +01001672 if (w->power) {
1673 d = w->dapm;
1674
1675 /* Supplies and micbiases only bring the
1676 * context up to STANDBY as unless something
1677 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001678 * generally don't require full power. Signal
1679 * generators are virtual pins and have no
1680 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001681 */
1682 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001683 case snd_soc_dapm_siggen:
1684 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001685 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001686 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001687 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001688 case snd_soc_dapm_micbias:
1689 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1690 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1691 break;
1692 default:
1693 d->target_bias_level = SND_SOC_BIAS_ON;
1694 break;
1695 }
1696 }
1697
1698 }
1699
Mark Brown85a843c2011-09-21 21:29:47 +01001700 /* Force all contexts in the card to the same bias state if
1701 * they're not ground referenced.
1702 */
Mark Brown56fba412011-06-04 11:25:10 +01001703 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001704 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001705 if (d->target_bias_level > bias)
1706 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001707 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001708 if (!d->idle_bias_off)
1709 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001710
Mark Brownde02d072011-09-20 21:43:24 +01001711 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001712
Mark Brown9d0624a2011-02-18 11:49:43 -08001713 /* Run all the bias changes in parallel */
1714 list_for_each_entry(d, &dapm->card->dapm_list, list)
1715 async_schedule_domain(dapm_pre_sequence_async, d,
1716 &async_domain);
1717 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001718
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001719 list_for_each_entry(w, &down_list, power_list) {
Mark Brown80114122013-02-25 15:14:19 +00001720 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_WILL_PMD);
1721 }
1722
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001723 list_for_each_entry(w, &up_list, power_list) {
Mark Brown80114122013-02-25 15:14:19 +00001724 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_WILL_PMU);
1725 }
1726
Mark Brown6d3ddc82009-05-16 17:47:29 +01001727 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001728 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001729
Mark Brown97404f22010-12-14 16:13:57 +00001730 dapm_widget_update(dapm);
1731
Mark Brown6d3ddc82009-05-16 17:47:29 +01001732 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001733 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001734
Mark Brown9d0624a2011-02-18 11:49:43 -08001735 /* Run all the bias changes in parallel */
1736 list_for_each_entry(d, &dapm->card->dapm_list, list)
1737 async_schedule_domain(dapm_post_sequence_async, d,
1738 &async_domain);
1739 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001740
Liam Girdwood8078d872012-02-15 15:15:35 +00001741 /* do we need to notify any clients that DAPM event is complete */
1742 list_for_each_entry(d, &card->dapm_list, list) {
1743 if (d->stream_event)
1744 d->stream_event(d, event);
1745 }
1746
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001747 pop_dbg(dapm->dev, card->pop_time,
1748 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001749 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001750
Mark Brown84e90932010-11-04 00:07:02 -04001751 trace_snd_soc_dapm_done(card);
1752
Mark Brown42aa3412009-03-01 19:21:10 +00001753 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001754}
1755
Mark Brown79fb9382009-08-21 16:38:13 +01001756#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001757static ssize_t dapm_widget_power_read_file(struct file *file,
1758 char __user *user_buf,
1759 size_t count, loff_t *ppos)
1760{
1761 struct snd_soc_dapm_widget *w = file->private_data;
1762 char *buf;
1763 int in, out;
1764 ssize_t ret;
1765 struct snd_soc_dapm_path *p = NULL;
1766
1767 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1768 if (!buf)
1769 return -ENOMEM;
1770
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001771 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001772 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001773 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001774 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001775
Mark Brownf13ebad2012-03-03 18:01:01 +00001776 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1777 w->name, w->power ? "On" : "Off",
1778 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001779
Mark Brownd033c362009-12-04 15:25:56 +00001780 if (w->reg >= 0)
1781 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1782 " - R%d(0x%x) bit %d",
1783 w->reg, w->reg, w->shift);
1784
1785 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1786
Mark Brown3eef08b2009-09-14 16:49:00 +01001787 if (w->sname)
1788 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1789 w->sname,
1790 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001791
1792 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001793 if (p->connected && !p->connected(w, p->sink))
1794 continue;
1795
Mark Brown79fb9382009-08-21 16:38:13 +01001796 if (p->connect)
1797 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001798 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001799 p->name ? p->name : "static",
1800 p->source->name);
1801 }
1802 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001803 if (p->connected && !p->connected(w, p->sink))
1804 continue;
1805
Mark Brown79fb9382009-08-21 16:38:13 +01001806 if (p->connect)
1807 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001808 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001809 p->name ? p->name : "static",
1810 p->sink->name);
1811 }
1812
1813 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1814
1815 kfree(buf);
1816 return ret;
1817}
1818
1819static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001820 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001821 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001822 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001823};
1824
Mark Brownef49e4f2011-04-04 20:48:13 +09001825static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1826 size_t count, loff_t *ppos)
1827{
1828 struct snd_soc_dapm_context *dapm = file->private_data;
1829 char *level;
1830
1831 switch (dapm->bias_level) {
1832 case SND_SOC_BIAS_ON:
1833 level = "On\n";
1834 break;
1835 case SND_SOC_BIAS_PREPARE:
1836 level = "Prepare\n";
1837 break;
1838 case SND_SOC_BIAS_STANDBY:
1839 level = "Standby\n";
1840 break;
1841 case SND_SOC_BIAS_OFF:
1842 level = "Off\n";
1843 break;
1844 default:
1845 BUG();
1846 level = "Unknown\n";
1847 break;
1848 }
1849
1850 return simple_read_from_buffer(user_buf, count, ppos, level,
1851 strlen(level));
1852}
1853
1854static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001855 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001856 .read = dapm_bias_read_file,
1857 .llseek = default_llseek,
1858};
1859
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001860void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1861 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001862{
Mark Brown79fb9382009-08-21 16:38:13 +01001863 struct dentry *d;
1864
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001865 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1866
1867 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001868 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001869 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001870 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001871 }
Mark Brown79fb9382009-08-21 16:38:13 +01001872
Mark Brownef49e4f2011-04-04 20:48:13 +09001873 d = debugfs_create_file("bias_level", 0444,
1874 dapm->debugfs_dapm, dapm,
1875 &dapm_bias_fops);
1876 if (!d)
1877 dev_warn(dapm->dev,
1878 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001879}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001880
1881static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1882{
1883 struct snd_soc_dapm_context *dapm = w->dapm;
1884 struct dentry *d;
1885
1886 if (!dapm->debugfs_dapm || !w->name)
1887 return;
1888
1889 d = debugfs_create_file(w->name, 0444,
1890 dapm->debugfs_dapm, w,
1891 &dapm_widget_power_fops);
1892 if (!d)
1893 dev_warn(w->dapm->dev,
1894 "ASoC: Failed to create %s debugfs file\n",
1895 w->name);
1896}
1897
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001898static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1899{
1900 debugfs_remove_recursive(dapm->debugfs_dapm);
1901}
1902
Mark Brown79fb9382009-08-21 16:38:13 +01001903#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001904void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1905 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001906{
1907}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001908
1909static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1910{
1911}
1912
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001913static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1914{
1915}
1916
Mark Brown79fb9382009-08-21 16:38:13 +01001917#endif
1918
Richard Purdie2b97eab2006-10-06 18:32:18 +02001919/* test and update the power status of a mux widget */
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001920static int soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001921 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001922{
1923 struct snd_soc_dapm_path *path;
1924 int found = 0;
1925
Richard Purdie2b97eab2006-10-06 18:32:18 +02001926 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001927 list_for_each_entry(path, &dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001928 if (path->kcontrol != kcontrol)
1929 continue;
1930
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001931 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001932 continue;
1933
1934 found = 1;
1935 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001936 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001937 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001938 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001939 } else {
1940 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001941 dapm_mark_dirty(path->source,
1942 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001943 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001944 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001945 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001946 }
1947
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001948 if (found)
1949 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001950
Liam Girdwood618dae12012-04-25 12:12:51 +01001951 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001952}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001953
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001954int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02001955 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
1956 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001957{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001958 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001959 int ret;
1960
Liam Girdwood3cd04342012-03-09 12:02:08 +00001961 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02001962 card->update = update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001963 ret = soc_dapm_mux_update_power(dapm, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02001964 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001965 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001966 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001967 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001968 return ret;
1969}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001970EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001971
Milan plzik1b075e32008-01-10 14:39:46 +01001972/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001973static int soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Mark Brown283375c2009-12-07 18:09:03 +00001974 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001975{
1976 struct snd_soc_dapm_path *path;
1977 int found = 0;
1978
Richard Purdie2b97eab2006-10-06 18:32:18 +02001979 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001980 list_for_each_entry(path, &dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001981 if (path->kcontrol != kcontrol)
1982 continue;
1983
1984 /* found, now check type */
1985 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001986 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001987 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001988 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001989 }
1990
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001991 if (found)
1992 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001993
Liam Girdwood618dae12012-04-25 12:12:51 +01001994 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001995}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001996
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001997int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02001998 struct snd_kcontrol *kcontrol, int connect,
1999 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002000{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002001 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002002 int ret;
2003
Liam Girdwood3cd04342012-03-09 12:02:08 +00002004 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002005 card->update = update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002006 ret = soc_dapm_mixer_update_power(dapm, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002007 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002008 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002009 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002010 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002011 return ret;
2012}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002013EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002014
2015/* show dapm widget status in sys fs */
2016static ssize_t dapm_widget_show(struct device *dev,
2017 struct device_attribute *attr, char *buf)
2018{
Mark Brown36ae1a92012-01-06 17:12:45 -08002019 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002020 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002021 struct snd_soc_dapm_widget *w;
2022 int count = 0;
2023 char *state = "not set";
2024
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002025 list_for_each_entry(w, &codec->card->widgets, list) {
2026 if (w->dapm != &codec->dapm)
2027 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002028
2029 /* only display widgets that burnm power */
2030 switch (w->id) {
2031 case snd_soc_dapm_hp:
2032 case snd_soc_dapm_mic:
2033 case snd_soc_dapm_spk:
2034 case snd_soc_dapm_line:
2035 case snd_soc_dapm_micbias:
2036 case snd_soc_dapm_dac:
2037 case snd_soc_dapm_adc:
2038 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002039 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002040 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002041 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002042 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002043 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002044 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002045 if (w->name)
2046 count += sprintf(buf + count, "%s: %s\n",
2047 w->name, w->power ? "On":"Off");
2048 break;
2049 default:
2050 break;
2051 }
2052 }
2053
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002054 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002055 case SND_SOC_BIAS_ON:
2056 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002057 break;
Mark Brown0be98982008-05-19 12:31:28 +02002058 case SND_SOC_BIAS_PREPARE:
2059 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002060 break;
Mark Brown0be98982008-05-19 12:31:28 +02002061 case SND_SOC_BIAS_STANDBY:
2062 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002063 break;
Mark Brown0be98982008-05-19 12:31:28 +02002064 case SND_SOC_BIAS_OFF:
2065 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002066 break;
2067 }
2068 count += sprintf(buf + count, "PM State: %s\n", state);
2069
2070 return count;
2071}
2072
2073static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2074
2075int snd_soc_dapm_sys_add(struct device *dev)
2076{
Troy Kisky12ef1932008-10-13 17:42:14 -07002077 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002078}
2079
2080static void snd_soc_dapm_sys_remove(struct device *dev)
2081{
Mark Brownaef90842009-05-16 17:53:16 +01002082 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002083}
2084
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002085static void dapm_free_path(struct snd_soc_dapm_path *path)
2086{
2087 list_del(&path->list_sink);
2088 list_del(&path->list_source);
2089 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002090 kfree(path);
2091}
2092
Richard Purdie2b97eab2006-10-06 18:32:18 +02002093/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002094static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002095{
2096 struct snd_soc_dapm_widget *w, *next_w;
2097 struct snd_soc_dapm_path *p, *next_p;
2098
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002099 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2100 if (w->dapm != dapm)
2101 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002102 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002103 /*
2104 * remove source and sink paths associated to this widget.
2105 * While removing the path, remove reference to it from both
2106 * source and sink widgets so that path is removed only once.
2107 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002108 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2109 dapm_free_path(p);
2110
2111 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2112 dapm_free_path(p);
2113
Stephen Warrenfad59882011-04-28 17:37:59 -06002114 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002115 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002116 kfree(w);
2117 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002118}
2119
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002120static struct snd_soc_dapm_widget *dapm_find_widget(
2121 struct snd_soc_dapm_context *dapm, const char *pin,
2122 bool search_other_contexts)
2123{
2124 struct snd_soc_dapm_widget *w;
2125 struct snd_soc_dapm_widget *fallback = NULL;
2126
2127 list_for_each_entry(w, &dapm->card->widgets, list) {
2128 if (!strcmp(w->name, pin)) {
2129 if (w->dapm == dapm)
2130 return w;
2131 else
2132 fallback = w;
2133 }
2134 }
2135
2136 if (search_other_contexts)
2137 return fallback;
2138
2139 return NULL;
2140}
2141
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002142static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002143 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002144{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002145 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002146
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002147 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002148 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002149 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002150 }
2151
Mark Brown1a8b2d92012-02-16 11:50:07 -08002152 if (w->connected != status)
2153 dapm_mark_dirty(w, "pin configuration");
2154
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002155 w->connected = status;
2156 if (status == 0)
2157 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002158
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002159 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002160}
2161
Richard Purdie2b97eab2006-10-06 18:32:18 +02002162/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002163 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002164 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002165 *
2166 * Walks all dapm audio paths and powers widgets according to their
2167 * stream or path usage.
2168 *
2169 * Returns 0 for success.
2170 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002171int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002172{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002173 int ret;
2174
Mark Brown4f4c0072011-10-07 14:29:19 +01002175 /*
2176 * Suppress early reports (eg, jacks syncing their state) to avoid
2177 * silly DAPM runs during card startup.
2178 */
2179 if (!dapm->card || !dapm->card->instantiated)
2180 return 0;
2181
Liam Girdwood3cd04342012-03-09 12:02:08 +00002182 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002183 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2184 mutex_unlock(&dapm->card->dapm_mutex);
2185 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002186}
Liam Girdwooda5302182008-07-07 13:35:17 +01002187EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002188
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002189static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002190 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002191{
2192 struct snd_soc_dapm_path *path;
2193 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002194 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002195 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002196 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002197 const char *source;
2198 char prefixed_sink[80];
2199 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002200 int ret = 0;
2201
Mark Brown88e8b9a2011-03-02 18:18:24 +00002202 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002203 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2204 dapm->codec->name_prefix, route->sink);
2205 sink = prefixed_sink;
2206 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2207 dapm->codec->name_prefix, route->source);
2208 source = prefixed_source;
2209 } else {
2210 sink = route->sink;
2211 source = route->source;
2212 }
2213
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002214 /*
2215 * find src and dest widgets over all widgets but favor a widget from
2216 * current DAPM context
2217 */
2218 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002219 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002220 wtsink = w;
2221 if (w->dapm == dapm)
2222 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002223 continue;
2224 }
2225 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002226 wtsource = w;
2227 if (w->dapm == dapm)
2228 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002229 }
2230 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002231 /* use widget from another DAPM context if not found from this */
2232 if (!wsink)
2233 wsink = wtsink;
2234 if (!wsource)
2235 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002236
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002237 if (wsource == NULL) {
2238 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2239 route->source);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002240 return -ENODEV;
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002241 }
2242 if (wsink == NULL) {
2243 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2244 route->sink);
2245 return -ENODEV;
2246 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002247
2248 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2249 if (!path)
2250 return -ENOMEM;
2251
2252 path->source = wsource;
2253 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002254 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002255 INIT_LIST_HEAD(&path->list);
2256 INIT_LIST_HEAD(&path->list_source);
2257 INIT_LIST_HEAD(&path->list_sink);
2258
2259 /* check for external widgets */
2260 if (wsink->id == snd_soc_dapm_input) {
2261 if (wsource->id == snd_soc_dapm_micbias ||
2262 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002263 wsource->id == snd_soc_dapm_line ||
2264 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002265 wsink->ext = 1;
2266 }
2267 if (wsource->id == snd_soc_dapm_output) {
2268 if (wsink->id == snd_soc_dapm_spk ||
2269 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002270 wsink->id == snd_soc_dapm_line ||
2271 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002272 wsource->ext = 1;
2273 }
2274
2275 /* connect static paths */
2276 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002277 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002278 list_add(&path->list_sink, &wsink->sources);
2279 list_add(&path->list_source, &wsource->sinks);
2280 path->connect = 1;
2281 return 0;
2282 }
2283
2284 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002285 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002286 case snd_soc_dapm_adc:
2287 case snd_soc_dapm_dac:
2288 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002289 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002290 case snd_soc_dapm_input:
2291 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002292 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002293 case snd_soc_dapm_micbias:
2294 case snd_soc_dapm_vmid:
2295 case snd_soc_dapm_pre:
2296 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002297 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002298 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002299 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002300 case snd_soc_dapm_aif_in:
2301 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002302 case snd_soc_dapm_dai_in:
2303 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002304 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002305 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002306 list_add(&path->list_sink, &wsink->sources);
2307 list_add(&path->list_source, &wsource->sinks);
2308 path->connect = 1;
2309 return 0;
2310 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002311 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002312 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002313 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002314 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002315 if (ret != 0)
2316 goto err;
2317 break;
2318 case snd_soc_dapm_switch:
2319 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002320 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002321 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002322 if (ret != 0)
2323 goto err;
2324 break;
2325 case snd_soc_dapm_hp:
2326 case snd_soc_dapm_mic:
2327 case snd_soc_dapm_line:
2328 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002329 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002330 list_add(&path->list_sink, &wsink->sources);
2331 list_add(&path->list_source, &wsource->sinks);
2332 path->connect = 0;
2333 return 0;
2334 }
Mark Brownfabd0382012-07-05 17:20:06 +01002335
2336 dapm_mark_dirty(wsource, "Route added");
2337 dapm_mark_dirty(wsink, "Route added");
2338
Richard Purdie2b97eab2006-10-06 18:32:18 +02002339 return 0;
2340
2341err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002342 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002343 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002344 kfree(path);
2345 return ret;
2346}
Mark Brown105f1c22008-05-13 14:52:19 +02002347
Mark Brownefcc3c62012-07-05 17:24:19 +01002348static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2349 const struct snd_soc_dapm_route *route)
2350{
2351 struct snd_soc_dapm_path *path, *p;
2352 const char *sink;
2353 const char *source;
2354 char prefixed_sink[80];
2355 char prefixed_source[80];
2356
2357 if (route->control) {
2358 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002359 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002360 return -EINVAL;
2361 }
2362
2363 if (dapm->codec && dapm->codec->name_prefix) {
2364 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2365 dapm->codec->name_prefix, route->sink);
2366 sink = prefixed_sink;
2367 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2368 dapm->codec->name_prefix, route->source);
2369 source = prefixed_source;
2370 } else {
2371 sink = route->sink;
2372 source = route->source;
2373 }
2374
2375 path = NULL;
2376 list_for_each_entry(p, &dapm->card->paths, list) {
2377 if (strcmp(p->source->name, source) != 0)
2378 continue;
2379 if (strcmp(p->sink->name, sink) != 0)
2380 continue;
2381 path = p;
2382 break;
2383 }
2384
2385 if (path) {
2386 dapm_mark_dirty(path->source, "Route removed");
2387 dapm_mark_dirty(path->sink, "Route removed");
2388
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002389 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002390 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002391 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002392 source, sink);
2393 }
2394
2395 return 0;
2396}
2397
Mark Brown105f1c22008-05-13 14:52:19 +02002398/**
Mark Brown105f1c22008-05-13 14:52:19 +02002399 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002400 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002401 * @route: audio routes
2402 * @num: number of routes
2403 *
2404 * Connects 2 dapm widgets together via a named audio path. The sink is
2405 * the widget receiving the audio signal, whilst the source is the sender
2406 * of the audio signal.
2407 *
2408 * Returns 0 for success else error. On error all resources can be freed
2409 * with a call to snd_soc_card_free().
2410 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002411int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002412 const struct snd_soc_dapm_route *route, int num)
2413{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002414 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002415
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002416 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002417 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002418 r = snd_soc_dapm_add_route(dapm, route);
2419 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002420 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2421 route->source,
2422 route->control ? route->control : "direct",
2423 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002424 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002425 }
2426 route++;
2427 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002428 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002429
Dan Carpenter60884c22012-04-13 22:25:43 +03002430 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002431}
2432EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2433
Mark Brownefcc3c62012-07-05 17:24:19 +01002434/**
2435 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2436 * @dapm: DAPM context
2437 * @route: audio routes
2438 * @num: number of routes
2439 *
2440 * Removes routes from the DAPM context.
2441 */
2442int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2443 const struct snd_soc_dapm_route *route, int num)
2444{
2445 int i, ret = 0;
2446
2447 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2448 for (i = 0; i < num; i++) {
2449 snd_soc_dapm_del_route(dapm, route);
2450 route++;
2451 }
2452 mutex_unlock(&dapm->card->dapm_mutex);
2453
2454 return ret;
2455}
2456EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2457
Mark Brownbf3a9e12011-06-13 16:42:29 +01002458static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2459 const struct snd_soc_dapm_route *route)
2460{
2461 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2462 route->source,
2463 true);
2464 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2465 route->sink,
2466 true);
2467 struct snd_soc_dapm_path *path;
2468 int count = 0;
2469
2470 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002471 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002472 route->source);
2473 return -ENODEV;
2474 }
2475
2476 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002477 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002478 route->sink);
2479 return -ENODEV;
2480 }
2481
2482 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002483 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002484 route->source, route->sink);
2485
2486 list_for_each_entry(path, &source->sinks, list_source) {
2487 if (path->sink == sink) {
2488 path->weak = 1;
2489 count++;
2490 }
2491 }
2492
2493 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002494 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002495 route->source, route->sink);
2496 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002497 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002498 count, route->source, route->sink);
2499
2500 return 0;
2501}
2502
2503/**
2504 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2505 * @dapm: DAPM context
2506 * @route: audio routes
2507 * @num: number of routes
2508 *
2509 * Mark existing routes matching those specified in the passed array
2510 * as being weak, meaning that they are ignored for the purpose of
2511 * power decisions. The main intended use case is for sidetone paths
2512 * which couple audio between other independent paths if they are both
2513 * active in order to make the combination work better at the user
2514 * level but which aren't intended to be "used".
2515 *
2516 * Note that CODEC drivers should not use this as sidetone type paths
2517 * can frequently also be used as bypass paths.
2518 */
2519int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2520 const struct snd_soc_dapm_route *route, int num)
2521{
2522 int i, err;
2523 int ret = 0;
2524
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002525 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002526 for (i = 0; i < num; i++) {
2527 err = snd_soc_dapm_weak_route(dapm, route);
2528 if (err)
2529 ret = err;
2530 route++;
2531 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002532 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002533
2534 return ret;
2535}
2536EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2537
Mark Brown105f1c22008-05-13 14:52:19 +02002538/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002539 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002540 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002541 *
2542 * Checks the codec for any new dapm widgets and creates them if found.
2543 *
2544 * Returns 0 for success.
2545 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002546int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002547{
2548 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002549 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002550
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002551 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2552
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002553 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002554 {
2555 if (w->new)
2556 continue;
2557
Stephen Warrenfad59882011-04-28 17:37:59 -06002558 if (w->num_kcontrols) {
2559 w->kcontrols = kzalloc(w->num_kcontrols *
2560 sizeof(struct snd_kcontrol *),
2561 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002562 if (!w->kcontrols) {
2563 mutex_unlock(&dapm->card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002564 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002565 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002566 }
2567
Richard Purdie2b97eab2006-10-06 18:32:18 +02002568 switch(w->id) {
2569 case snd_soc_dapm_switch:
2570 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002571 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002572 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002573 break;
2574 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002575 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002576 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002577 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002578 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002579 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002580 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002581 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002582 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002583 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002584 break;
2585 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002586
2587 /* Read the initial power state from the device */
2588 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002589 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002590 val &= 1 << w->shift;
2591 if (w->invert)
2592 val = !val;
2593
2594 if (val)
2595 w->power = 1;
2596 }
2597
Richard Purdie2b97eab2006-10-06 18:32:18 +02002598 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002599
Mark Brown7508b122011-10-05 12:09:12 +01002600 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002601 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002602 }
2603
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002604 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002605 mutex_unlock(&dapm->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002606 return 0;
2607}
2608EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2609
2610/**
2611 * snd_soc_dapm_get_volsw - dapm mixer get callback
2612 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002613 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002614 *
2615 * Callback to get the value of a dapm mixer control.
2616 *
2617 * Returns 0 for success.
2618 */
2619int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2620 struct snd_ctl_elem_value *ucontrol)
2621{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002622 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2623 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002624 struct soc_mixer_control *mc =
2625 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002626 unsigned int reg = mc->reg;
2627 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002628 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002629 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002630 unsigned int invert = mc->invert;
2631
2632 if (snd_soc_volsw_is_stereo(mc))
2633 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002634 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002635 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002636
Richard Purdie2b97eab2006-10-06 18:32:18 +02002637 ucontrol->value.integer.value[0] =
2638 (snd_soc_read(widget->codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002639 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002640 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002641 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002642
2643 return 0;
2644}
2645EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2646
2647/**
2648 * snd_soc_dapm_put_volsw - dapm mixer set callback
2649 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002650 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002651 *
2652 * Callback to set the value of a dapm mixer control.
2653 *
2654 * Returns 0 for success.
2655 */
2656int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2657 struct snd_ctl_elem_value *ucontrol)
2658{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002659 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2660 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2661 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002662 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002663 struct soc_mixer_control *mc =
2664 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002665 unsigned int reg = mc->reg;
2666 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002667 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002668 unsigned int mask = (1 << fls(max)) - 1;
2669 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002670 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002671 int connect, change;
2672 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002673
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002674 if (snd_soc_volsw_is_stereo(mc))
2675 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002676 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002677 kcontrol->id.name);
2678
Richard Purdie2b97eab2006-10-06 18:32:18 +02002679 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002680 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002681
2682 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002683 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002684 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002685 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002686
Liam Girdwood3cd04342012-03-09 12:02:08 +00002687 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002688
Stephen Warrene9cf7042011-01-27 14:54:05 -07002689 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002690 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002691 update.kcontrol = kcontrol;
2692 update.reg = reg;
2693 update.mask = mask;
2694 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002695
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002696 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002697
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002698 soc_dapm_mixer_update_power(widget->dapm, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002699
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002700 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002701 }
2702
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002703 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002704 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002705}
2706EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2707
2708/**
2709 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2710 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002711 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002712 *
2713 * Callback to get the value of a dapm enumerated double mixer control.
2714 *
2715 * Returns 0 for success.
2716 */
2717int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2718 struct snd_ctl_elem_value *ucontrol)
2719{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002720 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2721 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002722 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002723 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002724
Richard Purdie2b97eab2006-10-06 18:32:18 +02002725 val = snd_soc_read(widget->codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002726 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002727 if (e->shift_l != e->shift_r)
2728 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002729 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002730
2731 return 0;
2732}
2733EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2734
2735/**
2736 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2737 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002738 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002739 *
2740 * Callback to set the value of a dapm enumerated double mixer control.
2741 *
2742 * Returns 0 for success.
2743 */
2744int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2745 struct snd_ctl_elem_value *ucontrol)
2746{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002747 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2748 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2749 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002750 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002751 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002752 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002753 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002754 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002755
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002756 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002757 return -EINVAL;
2758 mux = ucontrol->value.enumerated.item[0];
2759 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002760 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002761 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002762 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002763 return -EINVAL;
2764 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002765 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002766 }
2767
Liam Girdwood3cd04342012-03-09 12:02:08 +00002768 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002769
Mark Brown3a655772009-10-05 17:23:30 +01002770 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002771 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002772 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002773
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002774 update.kcontrol = kcontrol;
2775 update.reg = e->reg;
2776 update.mask = mask;
2777 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002778 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002779
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002780 soc_dapm_mux_update_power(widget->dapm, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002781
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002782 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002783 }
2784
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002785 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002786 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002787}
2788EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2789
2790/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002791 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2792 * @kcontrol: mixer control
2793 * @ucontrol: control element information
2794 *
2795 * Returns 0 for success.
2796 */
2797int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2798 struct snd_ctl_elem_value *ucontrol)
2799{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002800 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2801 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002802
2803 ucontrol->value.enumerated.item[0] = widget->value;
2804
2805 return 0;
2806}
2807EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2808
2809/**
2810 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2811 * @kcontrol: mixer control
2812 * @ucontrol: control element information
2813 *
2814 * Returns 0 for success.
2815 */
2816int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2817 struct snd_ctl_elem_value *ucontrol)
2818{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002819 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2820 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2821 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002822 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002823 struct soc_enum *e =
2824 (struct soc_enum *)kcontrol->private_value;
2825 int change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002826
2827 if (ucontrol->value.enumerated.item[0] >= e->max)
2828 return -EINVAL;
2829
Liam Girdwood3cd04342012-03-09 12:02:08 +00002830 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002831
2832 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002833 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002834 widget->value = ucontrol->value.enumerated.item[0];
2835 soc_dapm_mux_update_power(widget->dapm, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002836 }
2837
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002838 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002839 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002840}
2841EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2842
2843/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002844 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2845 * callback
2846 * @kcontrol: mixer control
2847 * @ucontrol: control element information
2848 *
2849 * Callback to get the value of a dapm semi enumerated double mixer control.
2850 *
2851 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2852 * used for handling bitfield coded enumeration for example.
2853 *
2854 * Returns 0 for success.
2855 */
2856int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2857 struct snd_ctl_elem_value *ucontrol)
2858{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002859 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2860 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002861 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002862 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002863
2864 reg_val = snd_soc_read(widget->codec, e->reg);
2865 val = (reg_val >> e->shift_l) & e->mask;
2866 for (mux = 0; mux < e->max; mux++) {
2867 if (val == e->values[mux])
2868 break;
2869 }
2870 ucontrol->value.enumerated.item[0] = mux;
2871 if (e->shift_l != e->shift_r) {
2872 val = (reg_val >> e->shift_r) & e->mask;
2873 for (mux = 0; mux < e->max; mux++) {
2874 if (val == e->values[mux])
2875 break;
2876 }
2877 ucontrol->value.enumerated.item[1] = mux;
2878 }
2879
2880 return 0;
2881}
2882EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2883
2884/**
2885 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2886 * callback
2887 * @kcontrol: mixer control
2888 * @ucontrol: control element information
2889 *
2890 * Callback to set the value of a dapm semi enumerated double mixer control.
2891 *
2892 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2893 * used for handling bitfield coded enumeration for example.
2894 *
2895 * Returns 0 for success.
2896 */
2897int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2898 struct snd_ctl_elem_value *ucontrol)
2899{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002900 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2901 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2902 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002903 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002904 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002905 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002906 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002907 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002908
2909 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2910 return -EINVAL;
2911 mux = ucontrol->value.enumerated.item[0];
2912 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2913 mask = e->mask << e->shift_l;
2914 if (e->shift_l != e->shift_r) {
2915 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2916 return -EINVAL;
2917 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2918 mask |= e->mask << e->shift_r;
2919 }
2920
Liam Girdwood3cd04342012-03-09 12:02:08 +00002921 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002922
Mark Brown3a655772009-10-05 17:23:30 +01002923 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002924 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002925 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002926
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002927 update.kcontrol = kcontrol;
2928 update.reg = e->reg;
2929 update.mask = mask;
2930 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002931 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002932
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002933 soc_dapm_mux_update_power(widget->dapm, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002934
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002935 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002936 }
2937
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002938 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002939 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002940}
2941EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2942
2943/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002944 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2945 *
2946 * @kcontrol: mixer control
2947 * @uinfo: control element information
2948 *
2949 * Callback to provide information about a pin switch control.
2950 */
2951int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2952 struct snd_ctl_elem_info *uinfo)
2953{
2954 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2955 uinfo->count = 1;
2956 uinfo->value.integer.min = 0;
2957 uinfo->value.integer.max = 1;
2958
2959 return 0;
2960}
2961EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2962
2963/**
2964 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2965 *
2966 * @kcontrol: mixer control
2967 * @ucontrol: Value
2968 */
2969int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2970 struct snd_ctl_elem_value *ucontrol)
2971{
Mark Brown48a8c392012-02-14 17:11:15 -08002972 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002973 const char *pin = (const char *)kcontrol->private_value;
2974
Liam Girdwood3cd04342012-03-09 12:02:08 +00002975 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002976
2977 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002978 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002979
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002980 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002981
2982 return 0;
2983}
2984EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2985
2986/**
2987 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2988 *
2989 * @kcontrol: mixer control
2990 * @ucontrol: Value
2991 */
2992int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2993 struct snd_ctl_elem_value *ucontrol)
2994{
Mark Brown48a8c392012-02-14 17:11:15 -08002995 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002996 const char *pin = (const char *)kcontrol->private_value;
2997
Liam Girdwood3cd04342012-03-09 12:02:08 +00002998 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002999
3000 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003001 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003002 else
Mark Brown48a8c392012-02-14 17:11:15 -08003003 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003004
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003005 mutex_unlock(&card->dapm_mutex);
3006
Mark Brown48a8c392012-02-14 17:11:15 -08003007 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003008 return 0;
3009}
3010EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3011
Mark Brown5ba06fc2012-02-16 11:07:13 -08003012static struct snd_soc_dapm_widget *
3013snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3014 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003015{
3016 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003017 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003018
3019 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003020 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003021
Mark Brown62ea8742012-01-21 21:14:48 +00003022 switch (w->id) {
3023 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003024 w->regulator = devm_regulator_get(dapm->dev, w->name);
3025 if (IS_ERR(w->regulator)) {
3026 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003027 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003028 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003029 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003030 }
Mark Brown8784c772013-01-10 19:33:47 +00003031
3032 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
3033 ret = regulator_allow_bypass(w->regulator, true);
3034 if (ret != 0)
3035 dev_warn(w->dapm->dev,
3036 "ASoC: Failed to unbypass %s: %d\n",
3037 w->name, ret);
3038 }
Mark Brown62ea8742012-01-21 21:14:48 +00003039 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003040 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003041#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003042 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003043 if (IS_ERR(w->clk)) {
3044 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003045 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003046 w->name, ret);
3047 return NULL;
3048 }
Mark Brownec029952012-06-04 08:16:20 +01003049#else
3050 return NULL;
3051#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003052 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003053 default:
3054 break;
3055 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003056
Mark Brown88e8b9a2011-03-02 18:18:24 +00003057 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003058 w->name = kasprintf(GFP_KERNEL, "%s %s",
3059 dapm->codec->name_prefix, widget->name);
3060 else
3061 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3062
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003063 if (w->name == NULL) {
3064 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003065 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003066 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003067
Mark Brown7ca3a182011-10-08 14:04:50 +01003068 switch (w->id) {
3069 case snd_soc_dapm_switch:
3070 case snd_soc_dapm_mixer:
3071 case snd_soc_dapm_mixer_named_ctl:
3072 w->power_check = dapm_generic_check_power;
3073 break;
3074 case snd_soc_dapm_mux:
3075 case snd_soc_dapm_virt_mux:
3076 case snd_soc_dapm_value_mux:
3077 w->power_check = dapm_generic_check_power;
3078 break;
Mark Brown46162742013-06-05 19:36:11 +01003079 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003080 w->power_check = dapm_adc_check_power;
3081 break;
Mark Brown46162742013-06-05 19:36:11 +01003082 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003083 w->power_check = dapm_dac_check_power;
3084 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003085 case snd_soc_dapm_adc:
3086 case snd_soc_dapm_aif_out:
3087 case snd_soc_dapm_dac:
3088 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003089 case snd_soc_dapm_pga:
3090 case snd_soc_dapm_out_drv:
3091 case snd_soc_dapm_input:
3092 case snd_soc_dapm_output:
3093 case snd_soc_dapm_micbias:
3094 case snd_soc_dapm_spk:
3095 case snd_soc_dapm_hp:
3096 case snd_soc_dapm_mic:
3097 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003098 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003099 w->power_check = dapm_generic_check_power;
3100 break;
3101 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003102 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003103 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003104 w->power_check = dapm_supply_check_power;
3105 break;
3106 default:
3107 w->power_check = dapm_always_on_check_power;
3108 break;
3109 }
3110
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003111 w->dapm = dapm;
3112 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003113 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003114 INIT_LIST_HEAD(&w->sources);
3115 INIT_LIST_HEAD(&w->sinks);
3116 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003117 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003118 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003119
3120 /* machine layer set ups unconnected pins and insertions */
3121 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003122 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003123}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003124
3125/**
Mark Brown4ba13272008-05-13 14:51:19 +02003126 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003127 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003128 * @widget: widget array
3129 * @num: number of widgets
3130 *
3131 * Creates new DAPM controls based upon the templates.
3132 *
3133 * Returns 0 for success else error.
3134 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003135int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003136 const struct snd_soc_dapm_widget *widget,
3137 int num)
3138{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003139 struct snd_soc_dapm_widget *w;
3140 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003141 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003142
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003143 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003144 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003145 w = snd_soc_dapm_new_control(dapm, widget);
3146 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003147 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003148 "ASoC: Failed to create DAPM control %s\n",
3149 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003150 ret = -ENOMEM;
3151 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003152 }
Mark Brown4ba13272008-05-13 14:51:19 +02003153 widget++;
3154 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003155 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003156 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003157}
3158EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3159
Mark Brownc74184e2012-04-04 22:12:09 +01003160static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3161 struct snd_kcontrol *kcontrol, int event)
3162{
3163 struct snd_soc_dapm_path *source_p, *sink_p;
3164 struct snd_soc_dai *source, *sink;
3165 const struct snd_soc_pcm_stream *config = w->params;
3166 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003167 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003168 u64 fmt;
3169 int ret;
3170
3171 BUG_ON(!config);
3172 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3173
3174 /* We only support a single source and sink, pick the first */
3175 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3176 list_sink);
3177 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3178 list_source);
3179
3180 BUG_ON(!source_p || !sink_p);
3181 BUG_ON(!sink_p->source || !source_p->sink);
3182 BUG_ON(!source_p->source || !sink_p->sink);
3183
3184 source = source_p->source->priv;
3185 sink = sink_p->sink->priv;
3186
3187 /* Be a little careful as we don't want to overflow the mask array */
3188 if (config->formats) {
3189 fmt = ffs(config->formats) - 1;
3190 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003191 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003192 config->formats);
3193 fmt = 0;
3194 }
3195
3196 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003197 params = kzalloc(sizeof(*params), GFP_KERNEL);
3198 if (!params) {
3199 ret = -ENOMEM;
3200 goto out;
3201 }
3202 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003203
Mark Brown9747cec2012-04-26 19:12:21 +01003204 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003205 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003206 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003207 config->rate_max;
3208
Mark Brown9747cec2012-04-26 19:12:21 +01003209 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003210 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003211 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003212 = config->channels_max;
3213
3214 memset(&substream, 0, sizeof(substream));
3215
3216 switch (event) {
3217 case SND_SOC_DAPM_PRE_PMU:
3218 if (source->driver->ops && source->driver->ops->hw_params) {
3219 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3220 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003221 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003222 if (ret != 0) {
3223 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003224 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003225 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003226 }
3227 }
3228
3229 if (sink->driver->ops && sink->driver->ops->hw_params) {
3230 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003231 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003232 sink);
3233 if (ret != 0) {
3234 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003235 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003236 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003237 }
3238 }
3239 break;
3240
3241 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003242 ret = snd_soc_dai_digital_mute(sink, 0,
3243 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003244 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003245 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003246 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003247 break;
3248
3249 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003250 ret = snd_soc_dai_digital_mute(sink, 1,
3251 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003252 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003253 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003254 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003255 break;
3256
3257 default:
3258 BUG();
3259 return -EINVAL;
3260 }
3261
Mark Brown9747cec2012-04-26 19:12:21 +01003262out:
3263 kfree(params);
3264 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003265}
3266
3267int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3268 const struct snd_soc_pcm_stream *params,
3269 struct snd_soc_dapm_widget *source,
3270 struct snd_soc_dapm_widget *sink)
3271{
3272 struct snd_soc_dapm_route routes[2];
3273 struct snd_soc_dapm_widget template;
3274 struct snd_soc_dapm_widget *w;
3275 size_t len;
3276 char *link_name;
3277
3278 len = strlen(source->name) + strlen(sink->name) + 2;
3279 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3280 if (!link_name)
3281 return -ENOMEM;
3282 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3283
3284 memset(&template, 0, sizeof(template));
3285 template.reg = SND_SOC_NOPM;
3286 template.id = snd_soc_dapm_dai_link;
3287 template.name = link_name;
3288 template.event = snd_soc_dai_link_event;
3289 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3290 SND_SOC_DAPM_PRE_PMD;
3291
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003292 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003293
3294 w = snd_soc_dapm_new_control(&card->dapm, &template);
3295 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003296 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003297 link_name);
3298 return -ENOMEM;
3299 }
3300
3301 w->params = params;
3302
3303 memset(&routes, 0, sizeof(routes));
3304
3305 routes[0].source = source->name;
3306 routes[0].sink = link_name;
3307 routes[1].source = link_name;
3308 routes[1].sink = sink->name;
3309
3310 return snd_soc_dapm_add_routes(&card->dapm, routes,
3311 ARRAY_SIZE(routes));
3312}
3313
Mark Brown888df392012-02-16 19:37:51 -08003314int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3315 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003316{
Mark Brown888df392012-02-16 19:37:51 -08003317 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003318 struct snd_soc_dapm_widget *w;
3319
Mark Brown888df392012-02-16 19:37:51 -08003320 WARN_ON(dapm->dev != dai->dev);
3321
3322 memset(&template, 0, sizeof(template));
3323 template.reg = SND_SOC_NOPM;
3324
3325 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003326 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003327 template.name = dai->driver->playback.stream_name;
3328 template.sname = dai->driver->playback.stream_name;
3329
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003330 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003331 template.name);
3332
3333 w = snd_soc_dapm_new_control(dapm, &template);
3334 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003335 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003336 dai->driver->playback.stream_name);
3337 }
3338
3339 w->priv = dai;
3340 dai->playback_widget = w;
3341 }
3342
3343 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003344 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003345 template.name = dai->driver->capture.stream_name;
3346 template.sname = dai->driver->capture.stream_name;
3347
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003348 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003349 template.name);
3350
3351 w = snd_soc_dapm_new_control(dapm, &template);
3352 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003353 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003354 dai->driver->capture.stream_name);
3355 }
3356
3357 w->priv = dai;
3358 dai->capture_widget = w;
3359 }
3360
3361 return 0;
3362}
3363
3364int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3365{
3366 struct snd_soc_dapm_widget *dai_w, *w;
3367 struct snd_soc_dai *dai;
3368 struct snd_soc_dapm_route r;
3369
3370 memset(&r, 0, sizeof(r));
3371
3372 /* For each DAI widget... */
3373 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003374 switch (dai_w->id) {
3375 case snd_soc_dapm_dai_in:
3376 case snd_soc_dapm_dai_out:
3377 break;
3378 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003379 continue;
Mark Brown46162742013-06-05 19:36:11 +01003380 }
Mark Brown888df392012-02-16 19:37:51 -08003381
3382 dai = dai_w->priv;
3383
3384 /* ...find all widgets with the same stream and link them */
3385 list_for_each_entry(w, &card->widgets, list) {
3386 if (w->dapm != dai_w->dapm)
3387 continue;
3388
Mark Brown46162742013-06-05 19:36:11 +01003389 switch (w->id) {
3390 case snd_soc_dapm_dai_in:
3391 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003392 continue;
Mark Brown46162742013-06-05 19:36:11 +01003393 default:
3394 break;
3395 }
Mark Brown888df392012-02-16 19:37:51 -08003396
3397 if (!w->sname)
3398 continue;
3399
3400 if (dai->driver->playback.stream_name &&
3401 strstr(w->sname,
3402 dai->driver->playback.stream_name)) {
3403 r.source = dai->playback_widget->name;
3404 r.sink = w->name;
3405 dev_dbg(dai->dev, "%s -> %s\n",
3406 r.source, r.sink);
3407
3408 snd_soc_dapm_add_route(w->dapm, &r);
3409 }
3410
3411 if (dai->driver->capture.stream_name &&
3412 strstr(w->sname,
3413 dai->driver->capture.stream_name)) {
3414 r.source = w->name;
3415 r.sink = dai->capture_widget->name;
3416 dev_dbg(dai->dev, "%s -> %s\n",
3417 r.source, r.sink);
3418
3419 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003420 }
3421 }
3422 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003423
Mark Brown888df392012-02-16 19:37:51 -08003424 return 0;
3425}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003426
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003427static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3428 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003429{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003430
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003431 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3432 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3433 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003434
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003435 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3436 w_cpu = cpu_dai->playback_widget;
3437 w_codec = codec_dai->playback_widget;
3438 } else {
3439 w_cpu = cpu_dai->capture_widget;
3440 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003441 }
3442
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003443 if (w_cpu) {
3444
3445 dapm_mark_dirty(w_cpu, "stream event");
3446
3447 switch (event) {
3448 case SND_SOC_DAPM_STREAM_START:
3449 w_cpu->active = 1;
3450 break;
3451 case SND_SOC_DAPM_STREAM_STOP:
3452 w_cpu->active = 0;
3453 break;
3454 case SND_SOC_DAPM_STREAM_SUSPEND:
3455 case SND_SOC_DAPM_STREAM_RESUME:
3456 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3457 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3458 break;
3459 }
3460 }
3461
3462 if (w_codec) {
3463
3464 dapm_mark_dirty(w_codec, "stream event");
3465
3466 switch (event) {
3467 case SND_SOC_DAPM_STREAM_START:
3468 w_codec->active = 1;
3469 break;
3470 case SND_SOC_DAPM_STREAM_STOP:
3471 w_codec->active = 0;
3472 break;
3473 case SND_SOC_DAPM_STREAM_SUSPEND:
3474 case SND_SOC_DAPM_STREAM_RESUME:
3475 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3476 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3477 break;
3478 }
3479 }
3480
3481 dapm_power_widgets(&rtd->card->dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003482}
3483
3484/**
3485 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3486 * @rtd: PCM runtime data
3487 * @stream: stream name
3488 * @event: stream event
3489 *
3490 * Sends a stream event to the dapm core. The core then makes any
3491 * necessary widget power changes.
3492 *
3493 * Returns 0 for success else error.
3494 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003495void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3496 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003497{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003498 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003499
Liam Girdwood3cd04342012-03-09 12:02:08 +00003500 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003501 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003502 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003503}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003504
3505/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003506 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003507 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003508 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003509 *
Mark Brown74b8f952009-06-06 11:26:15 +01003510 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003511 * a valid audio route and active audio stream.
3512 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3513 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003514 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003515int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003516{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003517 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003518}
Liam Girdwooda5302182008-07-07 13:35:17 +01003519EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003520
3521/**
Mark Brownda341832010-03-15 19:23:37 +00003522 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003523 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003524 * @pin: pin name
3525 *
3526 * Enables input/output pin regardless of any other state. This is
3527 * intended for use with microphone bias supplies used in microphone
3528 * jack detection.
3529 *
3530 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3531 * do any widget power switching.
3532 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003533int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3534 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003535{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003536 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003537
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003538 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003539 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003540 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003541 }
3542
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003543 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003544 w->connected = 1;
3545 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003546 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003547
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003548 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003549}
3550EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3551
3552/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003553 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003554 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003555 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003556 *
Mark Brown74b8f952009-06-06 11:26:15 +01003557 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003558 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3559 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003560 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003561int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3562 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003563{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003564 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003565}
3566EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3567
3568/**
Mark Brown5817b522008-09-24 11:23:11 +01003569 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003570 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003571 * @pin: pin name
3572 *
3573 * Marks the specified pin as being not connected, disabling it along
3574 * any parent or child widgets. At present this is identical to
3575 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3576 * additional things such as disabling controls which only affect
3577 * paths through the pin.
3578 *
3579 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3580 * do any widget power switching.
3581 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003582int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003583{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003584 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003585}
3586EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3587
3588/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003589 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003590 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003591 * @pin: audio signal pin endpoint (or start point)
3592 *
3593 * Get audio pin status - connected or disconnected.
3594 *
3595 * Returns 1 for connected otherwise 0.
3596 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003597int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3598 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003599{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003600 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003601
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003602 if (w)
3603 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003604
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003605 return 0;
3606}
Liam Girdwooda5302182008-07-07 13:35:17 +01003607EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003608
3609/**
Mark Brown1547aba2010-05-07 21:11:40 +01003610 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003611 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003612 * @pin: audio signal pin endpoint (or start point)
3613 *
3614 * Mark the given endpoint or pin as ignoring suspend. When the
3615 * system is disabled a path between two endpoints flagged as ignoring
3616 * suspend will not be disabled. The path must already be enabled via
3617 * normal means at suspend time, it will not be turned on if it was not
3618 * already enabled.
3619 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003620int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3621 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003622{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003623 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003624
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003625 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003626 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003627 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003628 }
3629
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003630 w->ignore_suspend = 1;
3631
3632 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003633}
3634EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3635
Stephen Warren16332812011-11-23 12:42:04 -07003636static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3637 struct snd_soc_dapm_widget *w)
3638{
3639 struct snd_soc_dapm_path *p;
3640
3641 list_for_each_entry(p, &card->paths, list) {
3642 if ((p->source == w) || (p->sink == w)) {
3643 dev_dbg(card->dev,
3644 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3645 p->source->name, p->source->id, p->source->dapm,
3646 p->sink->name, p->sink->id, p->sink->dapm);
3647
3648 /* Connected to something other than the codec */
3649 if (p->source->dapm != p->sink->dapm)
3650 return true;
3651 /*
3652 * Loopback connection from codec external pin to
3653 * codec external pin
3654 */
3655 if (p->sink->id == snd_soc_dapm_input) {
3656 switch (p->source->id) {
3657 case snd_soc_dapm_output:
3658 case snd_soc_dapm_micbias:
3659 return true;
3660 default:
3661 break;
3662 }
3663 }
3664 }
3665 }
3666
3667 return false;
3668}
3669
3670/**
3671 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3672 * @codec: The codec whose pins should be processed
3673 *
3674 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3675 * which are unused. Pins are used if they are connected externally to the
3676 * codec, whether that be to some other device, or a loop-back connection to
3677 * the codec itself.
3678 */
3679void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3680{
3681 struct snd_soc_card *card = codec->card;
3682 struct snd_soc_dapm_context *dapm = &codec->dapm;
3683 struct snd_soc_dapm_widget *w;
3684
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003685 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003686 &card->dapm, &codec->dapm);
3687
3688 list_for_each_entry(w, &card->widgets, list) {
3689 if (w->dapm != dapm)
3690 continue;
3691 switch (w->id) {
3692 case snd_soc_dapm_input:
3693 case snd_soc_dapm_output:
3694 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003695 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003696 w->name);
3697 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003698 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003699 "... Not in map; disabling\n");
3700 snd_soc_dapm_nc_pin(dapm, w->name);
3701 }
3702 break;
3703 default:
3704 break;
3705 }
3706 }
3707}
3708
Mark Brown1547aba2010-05-07 21:11:40 +01003709/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003710 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003711 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003712 *
3713 * Free all dapm widgets and resources.
3714 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003715void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003716{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003717 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003718 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003719 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003720 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003721}
3722EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3723
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003724static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003725{
Liam Girdwood01005a72012-07-06 16:57:05 +01003726 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003727 struct snd_soc_dapm_widget *w;
3728 LIST_HEAD(down_list);
3729 int powerdown = 0;
3730
Liam Girdwood01005a72012-07-06 16:57:05 +01003731 mutex_lock(&card->dapm_mutex);
3732
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003733 list_for_each_entry(w, &dapm->card->widgets, list) {
3734 if (w->dapm != dapm)
3735 continue;
Mark Brown51737472009-06-22 13:16:51 +01003736 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003737 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003738 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003739 powerdown = 1;
3740 }
3741 }
3742
3743 /* If there were no widgets to power down we're already in
3744 * standby.
3745 */
3746 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003747 if (dapm->bias_level == SND_SOC_BIAS_ON)
3748 snd_soc_dapm_set_bias_level(dapm,
3749 SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003750 dapm_seq_run(dapm, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003751 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3752 snd_soc_dapm_set_bias_level(dapm,
3753 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003754 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003755
3756 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003757}
Mark Brown51737472009-06-22 13:16:51 +01003758
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003759/*
3760 * snd_soc_dapm_shutdown - callback for system shutdown
3761 */
3762void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3763{
3764 struct snd_soc_codec *codec;
3765
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003766 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003767 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003768 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3769 snd_soc_dapm_set_bias_level(&codec->dapm,
3770 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003771 }
Mark Brown51737472009-06-22 13:16:51 +01003772}
3773
Richard Purdie2b97eab2006-10-06 18:32:18 +02003774/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003775MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003776MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3777MODULE_LICENSE("GPL");