blob: 6f8a01bf6ca8d991a0e306de51c1e8edcbb73dac [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 Brown15e4c722008-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 Brown15e4c722008-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 Brown15e4c722008-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 Brown15e4c722008-07-02 11:51:20 +0100128 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200129
130 kfree(buf);
Mark Brown15e4c722008-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 Girdwood48056082011-07-20 12:23:33 +0100177/* get snd_card from DAPM context */
178static inline struct snd_card *dapm_get_snd_card(
179 struct snd_soc_dapm_context *dapm)
180{
181 if (dapm->codec)
182 return dapm->codec->card->snd_card;
183 else if (dapm->platform)
184 return dapm->platform->card->snd_card;
185 else
186 BUG();
187
188 /* unreachable */
189 return NULL;
190}
191
192/* get soc_card from DAPM context */
193static inline struct snd_soc_card *dapm_get_soc_card(
194 struct snd_soc_dapm_context *dapm)
195{
196 if (dapm->codec)
197 return dapm->codec->card;
198 else if (dapm->platform)
199 return dapm->platform->card;
200 else
201 BUG();
202
203 /* unreachable */
204 return NULL;
205}
206
Liam Girdwood6c120e12012-02-15 15:15:34 +0000207static void dapm_reset(struct snd_soc_card *card)
208{
209 struct snd_soc_dapm_widget *w;
210
211 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
212
213 list_for_each_entry(w, &card->widgets, list) {
214 w->power_checked = false;
215 w->inputs = -1;
216 w->outputs = -1;
217 }
218}
219
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100220static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
221{
222 if (w->codec)
223 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100224 else if (w->platform)
225 return snd_soc_platform_read(w->platform, reg);
226
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000227 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100228 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100229}
230
231static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
232{
233 if (w->codec)
234 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100235 else if (w->platform)
236 return snd_soc_platform_write(w->platform, reg, val);
237
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000238 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100239 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100240}
241
Liam Girdwood49575fb52012-03-06 18:16:19 +0000242static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
243{
Mark Browne06ab3b2012-03-06 23:58:22 +0000244 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000245 mutex_lock(&w->codec->mutex);
246 else if (w->platform)
247 mutex_lock(&w->platform->mutex);
248}
249
250static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
251{
Mark Browne06ab3b2012-03-06 23:58:22 +0000252 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000253 mutex_unlock(&w->codec->mutex);
254 else if (w->platform)
255 mutex_unlock(&w->platform->mutex);
256}
257
258static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100259 unsigned short reg, unsigned int mask, unsigned int value)
260{
Mark Brown8a713da2011-12-03 12:33:55 +0000261 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100262 unsigned int old, new;
263 int ret;
264
Mark Brown8a713da2011-12-03 12:33:55 +0000265 if (w->codec && w->codec->using_regmap) {
266 ret = regmap_update_bits_check(w->codec->control_data,
267 reg, mask, value, &change);
268 if (ret != 0)
269 return ret;
270 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000271 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000272 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000273 if (ret < 0) {
274 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100275 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000276 }
Mark Brown8a713da2011-12-03 12:33:55 +0000277
278 old = ret;
279 new = (old & ~mask) | (value & mask);
280 change = old != new;
281 if (change) {
282 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000283 if (ret < 0) {
284 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000285 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000286 }
Mark Brown8a713da2011-12-03 12:33:55 +0000287 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000288 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100289 }
290
291 return change;
292}
293
Mark Brown452c5ea2009-05-17 21:41:23 +0100294/**
295 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800296 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100297 * @level: level to configure
298 *
299 * Configure the bias (power) levels for the SoC audio device.
300 *
301 * Returns 0 for success else error.
302 */
Mark Browned5a4c42011-02-18 11:12:42 -0800303static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200304 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100305{
Mark Browned5a4c42011-02-18 11:12:42 -0800306 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100307 int ret = 0;
308
Mark Brown84e90932010-11-04 00:07:02 -0400309 trace_snd_soc_bias_level_start(card, level);
310
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000311 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100312 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100313 if (ret != 0)
314 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100315
Mark Browncc4c6702011-06-06 19:03:34 +0100316 if (dapm->codec) {
317 if (dapm->codec->driver->set_bias_level)
318 ret = dapm->codec->driver->set_bias_level(dapm->codec,
319 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100320 else
321 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100322 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100323 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100324 }
Liam Girdwood41231282012-07-06 16:56:16 +0100325
Mark Brown171ec6b2011-06-06 18:15:19 +0100326 if (ret != 0)
327 goto out;
328
329 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100330 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100331out:
Mark Brown84e90932010-11-04 00:07:02 -0400332 trace_snd_soc_bias_level_done(card, level);
333
Mark Brown452c5ea2009-05-17 21:41:23 +0100334 return ret;
335}
336
Richard Purdie2b97eab2006-10-06 18:32:18 +0200337/* set up initial codec paths */
338static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
339 struct snd_soc_dapm_path *p, int i)
340{
341 switch (w->id) {
342 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000343 case snd_soc_dapm_mixer:
344 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200345 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100346 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600347 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400348 unsigned int reg = mc->reg;
349 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100350 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400351 unsigned int mask = (1 << fls(max)) - 1;
352 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200353
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100354 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200355 val = (val >> shift) & mask;
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200356 if (invert)
357 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200358
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200359 p->connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200360 }
361 break;
362 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600363 struct soc_enum *e = (struct soc_enum *)
364 w->kcontrol_news[i].private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200365 int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200366
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100367 val = soc_widget_read(w, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200368 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200369
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200370 if (item < e->max && !strcmp(p->name, e->texts[item]))
371 p->connect = 1;
372 else
373 p->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200374 }
375 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000376 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600377 struct soc_enum *e = (struct soc_enum *)
378 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000379
380 p->connect = 0;
381 /* since a virtual mux has no backing registers to
382 * decide which path to connect, it will try to match
383 * with the first enumeration. This is to ensure
384 * that the default mux choice (the first) will be
385 * correctly powered up during initialization.
386 */
387 if (!strcmp(p->name, e->texts[0]))
388 p->connect = 1;
389 }
390 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200391 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200392 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600393 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200394 int val, item;
395
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100396 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200397 val = (val >> e->shift_l) & e->mask;
398 for (item = 0; item < e->max; item++) {
399 if (val == e->values[item])
400 break;
401 }
402
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200403 if (item < e->max && !strcmp(p->name, e->texts[item]))
404 p->connect = 1;
405 else
406 p->connect = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200407 }
408 break;
Mark Brown56563102011-10-03 22:41:09 +0100409 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200410 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600411 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200412 case snd_soc_dapm_output:
413 case snd_soc_dapm_adc:
414 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000415 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200416 case snd_soc_dapm_dac:
417 case snd_soc_dapm_micbias:
418 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100419 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000420 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200421 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100422 case snd_soc_dapm_aif_in:
423 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100424 case snd_soc_dapm_dai_in:
425 case snd_soc_dapm_dai_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200426 case snd_soc_dapm_hp:
427 case snd_soc_dapm_mic:
428 case snd_soc_dapm_spk:
429 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100430 case snd_soc_dapm_dai_link:
Mark Brown56563102011-10-03 22:41:09 +0100431 p->connect = 1;
432 break;
433 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200434 case snd_soc_dapm_pre:
435 case snd_soc_dapm_post:
436 p->connect = 0;
437 break;
438 }
439}
440
Mark Brown74b8f952009-06-06 11:26:15 +0100441/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200442static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200443 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
444 struct snd_soc_dapm_path *path, const char *control_name,
445 const struct snd_kcontrol_new *kcontrol)
446{
447 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
448 int i;
449
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100450 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200451 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200452 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200453 list_add(&path->list_sink, &dest->sources);
454 list_add(&path->list_source, &src->sinks);
455 path->name = (char*)e->texts[i];
456 dapm_set_path_status(dest, path, 0);
457 return 0;
458 }
459 }
460
461 return -ENODEV;
462}
463
Mark Brown74b8f952009-06-06 11:26:15 +0100464/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200465static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200466 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
467 struct snd_soc_dapm_path *path, const char *control_name)
468{
469 int i;
470
471 /* search for mixer kcontrol */
472 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600473 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200474 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200475 list_add(&path->list_sink, &dest->sources);
476 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600477 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200478 dapm_set_path_status(dest, path, i);
479 return 0;
480 }
481 }
482 return -ENODEV;
483}
484
Stephen Warrenaf468002011-04-28 17:38:01 -0600485static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600486 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600487 const struct snd_kcontrol_new *kcontrol_new,
488 struct snd_kcontrol **kcontrol)
489{
490 struct snd_soc_dapm_widget *w;
491 int i;
492
493 *kcontrol = NULL;
494
495 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600496 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
497 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600498 for (i = 0; i < w->num_kcontrols; i++) {
499 if (&w->kcontrol_news[i] == kcontrol_new) {
500 if (w->kcontrols)
501 *kcontrol = w->kcontrols[i];
502 return 1;
503 }
504 }
505 }
506
507 return 0;
508}
509
Lars-Peter Clausen6b75bf02013-06-14 13:16:51 +0200510static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
511{
512 kfree(kctl->private_data);
513}
514
Stephen Warren85762e72013-03-29 15:40:10 -0600515/*
516 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
517 * create it. Either way, add the widget into the control's widget list
518 */
519static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
520 int kci, struct snd_soc_dapm_path *path)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200521{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200522 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000523 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000524 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600525 size_t prefix_len;
526 int shared;
527 struct snd_kcontrol *kcontrol;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600528 struct snd_soc_dapm_widget_list *wlist;
Stephen Warren85762e72013-03-29 15:40:10 -0600529 int wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600530 size_t wlistsize;
Stephen Warren85762e72013-03-29 15:40:10 -0600531 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600532 char *long_name;
533 const char *name;
534 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000535
536 if (dapm->codec)
537 prefix = dapm->codec->name_prefix;
538 else
539 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200540
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000541 if (prefix)
542 prefix_len = strlen(prefix) + 1;
543 else
544 prefix_len = 0;
545
Stephen Warren85762e72013-03-29 15:40:10 -0600546 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
547 &kcontrol);
548
549 if (kcontrol) {
550 wlist = kcontrol->private_data;
551 wlistentries = wlist->num_widgets + 1;
552 } else {
553 wlist = NULL;
554 wlistentries = 1;
555 }
556
557 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
558 wlistentries * sizeof(struct snd_soc_dapm_widget *);
559 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
560 if (wlist == NULL) {
561 dev_err(dapm->dev, "ASoC: can't allocate widget list for %s\n",
562 w->name);
563 return -ENOMEM;
564 }
565 wlist->num_widgets = wlistentries;
566 wlist->widgets[wlistentries - 1] = w;
567
568 if (!kcontrol) {
569 if (shared) {
570 wname_in_long_name = false;
571 kcname_in_long_name = true;
572 } else {
573 switch (w->id) {
574 case snd_soc_dapm_switch:
575 case snd_soc_dapm_mixer:
576 wname_in_long_name = true;
577 kcname_in_long_name = true;
578 break;
579 case snd_soc_dapm_mixer_named_ctl:
580 wname_in_long_name = false;
581 kcname_in_long_name = true;
582 break;
583 case snd_soc_dapm_mux:
584 case snd_soc_dapm_virt_mux:
585 case snd_soc_dapm_value_mux:
586 wname_in_long_name = true;
587 kcname_in_long_name = false;
588 break;
589 default:
590 kfree(wlist);
591 return -EINVAL;
592 }
593 }
594
595 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600596 /*
597 * The control will get a prefix from the control
598 * creation process but we're also using the same
599 * prefix for widgets so cut the prefix off the
600 * front of the widget name.
601 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200602 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600603 w->name + prefix_len,
604 w->kcontrol_news[kci].name);
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200605 if (long_name == NULL) {
606 kfree(wlist);
607 return -ENOMEM;
608 }
Stephen Warren85762e72013-03-29 15:40:10 -0600609
610 name = long_name;
611 } else if (wname_in_long_name) {
612 long_name = NULL;
613 name = w->name + prefix_len;
614 } else {
615 long_name = NULL;
616 name = w->kcontrol_news[kci].name;
617 }
618
619 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], wlist, name,
620 prefix);
Lars-Peter Clausen6b75bf02013-06-14 13:16:51 +0200621 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200622 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600623 ret = snd_ctl_add(card, kcontrol);
624 if (ret < 0) {
625 dev_err(dapm->dev,
626 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
627 w->name, name, ret);
628 kfree(wlist);
Stephen Warren85762e72013-03-29 15:40:10 -0600629 return ret;
630 }
Stephen Warren85762e72013-03-29 15:40:10 -0600631 }
632
633 kcontrol->private_data = wlist;
634 w->kcontrols[kci] = kcontrol;
635 path->kcontrol = kcontrol;
636
637 return 0;
638}
639
640/* create new dapm mixer control */
641static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
642{
643 int i, ret;
644 struct snd_soc_dapm_path *path;
645
Richard Purdie2b97eab2006-10-06 18:32:18 +0200646 /* add kcontrol */
647 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200648 /* match name */
649 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200650 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600651 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200652 continue;
653
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200654 if (w->kcontrols[i]) {
655 path->kcontrol = w->kcontrols[i];
656 continue;
657 }
658
Stephen Warren85762e72013-03-29 15:40:10 -0600659 ret = dapm_create_or_share_mixmux_kcontrol(w, i, path);
660 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200661 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200662 }
663 }
Stephen Warren85762e72013-03-29 15:40:10 -0600664
665 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200666}
667
668/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200669static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200670{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200671 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600672 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600673 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200674
Stephen Warrenaf468002011-04-28 17:38:01 -0600675 if (w->num_kcontrols != 1) {
676 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000677 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600678 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200679 return -EINVAL;
680 }
681
Stephen Warren85762e72013-03-29 15:40:10 -0600682 path = list_first_entry(&w->sources, struct snd_soc_dapm_path,
683 list_sink);
684 if (!path) {
685 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
686 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600687 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200688
Stephen Warren85762e72013-03-29 15:40:10 -0600689 ret = dapm_create_or_share_mixmux_kcontrol(w, 0, path);
690 if (ret < 0)
691 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600692
Richard Purdie2b97eab2006-10-06 18:32:18 +0200693 list_for_each_entry(path, &w->sources, list_sink)
Stephen Warren85762e72013-03-29 15:40:10 -0600694 path->kcontrol = w->kcontrols[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200695
Stephen Warrenaf468002011-04-28 17:38:01 -0600696 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200697}
698
699/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200700static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200701{
Mark Browna6c65732010-03-03 17:45:21 +0000702 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200703 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000704 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200705
Mark Browna6c65732010-03-03 17:45:21 +0000706 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200707}
708
709/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100710static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
711 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200712{
713 struct snd_soc_dapm_path *p;
714
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100715 list_for_each_entry(p, sink, list_source) {
716 if (p->walked) {
717 p->walked = 0;
718 dapm_clear_walk_output(dapm, &p->sink->sinks);
719 }
720 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200721}
722
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100723static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
724 struct list_head *source)
725{
726 struct snd_soc_dapm_path *p;
727
728 list_for_each_entry(p, source, list_sink) {
729 if (p->walked) {
730 p->walked = 0;
731 dapm_clear_walk_input(dapm, &p->source->sources);
732 }
733 }
734}
735
736
Mark Brown99497882010-05-07 20:24:05 +0100737/* We implement power down on suspend by checking the power state of
738 * the ALSA card - when we are suspending the ALSA state for the card
739 * is set to D3.
740 */
741static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
742{
Mark Brown12ea2c72011-03-02 18:17:32 +0000743 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100744
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000745 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100746 case SNDRV_CTL_POWER_D3hot:
747 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100748 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000749 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200750 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100751 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100752 default:
753 return 1;
754 }
755}
756
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100757/* add widget to list if it's not already in the list */
758static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
759 struct snd_soc_dapm_widget *w)
760{
761 struct snd_soc_dapm_widget_list *wlist;
762 int wlistsize, wlistentries, i;
763
764 if (*list == NULL)
765 return -EINVAL;
766
767 wlist = *list;
768
769 /* is this widget already in the list */
770 for (i = 0; i < wlist->num_widgets; i++) {
771 if (wlist->widgets[i] == w)
772 return 0;
773 }
774
775 /* allocate some new space */
776 wlistentries = wlist->num_widgets + 1;
777 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
778 wlistentries * sizeof(struct snd_soc_dapm_widget *);
779 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
780 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000781 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100782 w->name);
783 return -ENOMEM;
784 }
785 wlist = *list;
786
787 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000788 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100789 w->name, wlist->num_widgets);
790
791 wlist->widgets[wlist->num_widgets] = w;
792 wlist->num_widgets++;
793 return 1;
794}
795
Richard Purdie2b97eab2006-10-06 18:32:18 +0200796/*
797 * Recursively check for a completed path to an active or physically connected
798 * output widget. Returns number of complete paths.
799 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100800static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
801 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200802{
803 struct snd_soc_dapm_path *path;
804 int con = 0;
805
Mark Brown024dc072011-10-09 11:52:05 +0100806 if (widget->outputs >= 0)
807 return widget->outputs;
808
Mark Brownde02d072011-09-20 21:43:24 +0100809 DAPM_UPDATE_STAT(widget, path_checks);
810
Mark Brown62ea8742012-01-21 21:14:48 +0000811 switch (widget->id) {
812 case snd_soc_dapm_supply:
813 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200814 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100815 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000816 default:
817 break;
818 }
Mark Brown246d0a12009-04-22 18:24:55 +0100819
Mark Brown010ff262009-08-17 17:39:22 +0100820 switch (widget->id) {
821 case snd_soc_dapm_adc:
822 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100823 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100824 if (widget->active) {
825 widget->outputs = snd_soc_dapm_suspend_check(widget);
826 return widget->outputs;
827 }
Mark Brown010ff262009-08-17 17:39:22 +0100828 default:
829 break;
830 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200831
832 if (widget->connected) {
833 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100834 if (widget->id == snd_soc_dapm_output && !widget->ext) {
835 widget->outputs = snd_soc_dapm_suspend_check(widget);
836 return widget->outputs;
837 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200838
839 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100840 if (widget->id == snd_soc_dapm_hp ||
841 widget->id == snd_soc_dapm_spk ||
842 (widget->id == snd_soc_dapm_line &&
843 !list_empty(&widget->sources))) {
844 widget->outputs = snd_soc_dapm_suspend_check(widget);
845 return widget->outputs;
846 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200847 }
848
849 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100850 DAPM_UPDATE_STAT(widget, neighbour_checks);
851
Mark Brownbf3a9e12011-06-13 16:42:29 +0100852 if (path->weak)
853 continue;
854
Mark Brown8af294b2013-02-22 17:48:15 +0000855 if (path->walking)
856 return 1;
857
Richard Purdie2b97eab2006-10-06 18:32:18 +0200858 if (path->walked)
859 continue;
860
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100861 trace_snd_soc_dapm_output_path(widget, path);
862
Richard Purdie2b97eab2006-10-06 18:32:18 +0200863 if (path->sink && path->connect) {
864 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000865 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100866
867 /* do we need to add this widget to the list ? */
868 if (list) {
869 int err;
870 err = dapm_list_add_widget(list, path->sink);
871 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000872 dev_err(widget->dapm->dev,
873 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100874 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000875 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100876 return con;
877 }
878 }
879
880 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000881
882 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200883 }
884 }
885
Mark Brown024dc072011-10-09 11:52:05 +0100886 widget->outputs = con;
887
Richard Purdie2b97eab2006-10-06 18:32:18 +0200888 return con;
889}
890
891/*
892 * Recursively check for a completed path to an active or physically connected
893 * input widget. Returns number of complete paths.
894 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100895static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
896 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200897{
898 struct snd_soc_dapm_path *path;
899 int con = 0;
900
Mark Brown024dc072011-10-09 11:52:05 +0100901 if (widget->inputs >= 0)
902 return widget->inputs;
903
Mark Brownde02d072011-09-20 21:43:24 +0100904 DAPM_UPDATE_STAT(widget, path_checks);
905
Mark Brown62ea8742012-01-21 21:14:48 +0000906 switch (widget->id) {
907 case snd_soc_dapm_supply:
908 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200909 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100910 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000911 default:
912 break;
913 }
Mark Brown246d0a12009-04-22 18:24:55 +0100914
Richard Purdie2b97eab2006-10-06 18:32:18 +0200915 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100916 switch (widget->id) {
917 case snd_soc_dapm_dac:
918 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +0100919 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +0100920 if (widget->active) {
921 widget->inputs = snd_soc_dapm_suspend_check(widget);
922 return widget->inputs;
923 }
Mark Brown010ff262009-08-17 17:39:22 +0100924 default:
925 break;
926 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200927
928 if (widget->connected) {
929 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100930 if (widget->id == snd_soc_dapm_input && !widget->ext) {
931 widget->inputs = snd_soc_dapm_suspend_check(widget);
932 return widget->inputs;
933 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200934
935 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100936 if (widget->id == snd_soc_dapm_vmid) {
937 widget->inputs = snd_soc_dapm_suspend_check(widget);
938 return widget->inputs;
939 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200940
941 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300942 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100943 (widget->id == snd_soc_dapm_line &&
944 !list_empty(&widget->sinks))) {
945 widget->inputs = snd_soc_dapm_suspend_check(widget);
946 return widget->inputs;
947 }
948
Mark Brown1ab97c82011-11-27 16:21:51 +0000949 /* signal generator */
950 if (widget->id == snd_soc_dapm_siggen) {
951 widget->inputs = snd_soc_dapm_suspend_check(widget);
952 return widget->inputs;
953 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200954 }
955
956 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +0100957 DAPM_UPDATE_STAT(widget, neighbour_checks);
958
Mark Brownbf3a9e12011-06-13 16:42:29 +0100959 if (path->weak)
960 continue;
961
Mark Brown8af294b2013-02-22 17:48:15 +0000962 if (path->walking)
963 return 1;
964
Richard Purdie2b97eab2006-10-06 18:32:18 +0200965 if (path->walked)
966 continue;
967
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100968 trace_snd_soc_dapm_input_path(widget, path);
969
Richard Purdie2b97eab2006-10-06 18:32:18 +0200970 if (path->source && path->connect) {
971 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000972 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100973
974 /* do we need to add this widget to the list ? */
975 if (list) {
976 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +0100977 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100978 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000979 dev_err(widget->dapm->dev,
980 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100981 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000982 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100983 return con;
984 }
985 }
986
987 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000988
989 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200990 }
991 }
992
Mark Brown024dc072011-10-09 11:52:05 +0100993 widget->inputs = con;
994
Richard Purdie2b97eab2006-10-06 18:32:18 +0200995 return con;
996}
997
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100998/**
999 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1000 * @dai: the soc DAI.
1001 * @stream: stream direction.
1002 * @list: list of active widgets for this stream.
1003 *
1004 * Queries DAPM graph as to whether an valid audio stream path exists for
1005 * the initial stream specified by name. This takes into account
1006 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1007 *
1008 * Returns the number of valid paths or negative error.
1009 */
1010int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1011 struct snd_soc_dapm_widget_list **list)
1012{
1013 struct snd_soc_card *card = dai->card;
1014 int paths;
1015
1016 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1017 dapm_reset(card);
1018
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001019 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001020 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001021 dapm_clear_walk_output(&card->dapm,
1022 &dai->playback_widget->sinks);
1023 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001024 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001025 dapm_clear_walk_input(&card->dapm,
1026 &dai->capture_widget->sources);
1027 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001028
1029 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001030 mutex_unlock(&card->dapm_mutex);
1031
1032 return paths;
1033}
1034
Richard Purdie2b97eab2006-10-06 18:32:18 +02001035/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001036 * Handler for generic register modifier widget.
1037 */
1038int dapm_reg_event(struct snd_soc_dapm_widget *w,
1039 struct snd_kcontrol *kcontrol, int event)
1040{
1041 unsigned int val;
1042
1043 if (SND_SOC_DAPM_EVENT_ON(event))
1044 val = w->on_val;
1045 else
1046 val = w->off_val;
1047
Liam Girdwood49575fb52012-03-06 18:16:19 +00001048 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001049 w->mask << w->shift, val << w->shift);
1050
1051 return 0;
1052}
Mark Brown11589412008-07-29 11:42:23 +01001053EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001054
Mark Brown62ea8742012-01-21 21:14:48 +00001055/*
1056 * Handler for regulator supply widget.
1057 */
1058int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1059 struct snd_kcontrol *kcontrol, int event)
1060{
Mark Brownc05b84d2012-09-07 12:57:11 +08001061 int ret;
1062
1063 if (SND_SOC_DAPM_EVENT_ON(event)) {
1064 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001065 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001066 if (ret != 0)
1067 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001068 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001069 w->name, ret);
1070 }
1071
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001072 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001073 } else {
1074 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001075 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001076 if (ret != 0)
1077 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001078 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001079 w->name, ret);
1080 }
1081
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001082 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001083 }
Mark Brown62ea8742012-01-21 21:14:48 +00001084}
1085EXPORT_SYMBOL_GPL(dapm_regulator_event);
1086
Ola Liljad7e7eb92012-05-24 15:26:25 +02001087/*
1088 * Handler for clock supply widget.
1089 */
1090int dapm_clock_event(struct snd_soc_dapm_widget *w,
1091 struct snd_kcontrol *kcontrol, int event)
1092{
1093 if (!w->clk)
1094 return -EIO;
1095
Mark Brownec029952012-06-04 08:16:20 +01001096#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001097 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001098 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001099 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001100 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001101 return 0;
1102 }
Mark Brownec029952012-06-04 08:16:20 +01001103#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001104 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001105}
1106EXPORT_SYMBOL_GPL(dapm_clock_event);
1107
Mark Brownd8050022011-09-28 18:28:23 +01001108static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1109{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001110 if (w->power_checked)
1111 return w->new_power;
1112
Mark Brownd8050022011-09-28 18:28:23 +01001113 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001114 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001115 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001116 w->new_power = w->power_check(w);
1117
1118 w->power_checked = true;
1119
1120 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001121}
1122
Mark Browncd0f2d42009-04-20 16:56:59 +01001123/* Generic check to see if a widget should be powered.
1124 */
1125static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1126{
1127 int in, out;
1128
Mark Brownde02d072011-09-20 21:43:24 +01001129 DAPM_UPDATE_STAT(w, power_checks);
1130
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001131 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001132 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001133 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001134 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001135 return out != 0 && in != 0;
1136}
1137
Mark Brown6ea31b92009-04-20 17:15:41 +01001138/* Check to see if an ADC has power */
1139static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1140{
1141 int in;
1142
Mark Brownde02d072011-09-20 21:43:24 +01001143 DAPM_UPDATE_STAT(w, power_checks);
1144
Mark Brown6ea31b92009-04-20 17:15:41 +01001145 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001146 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001147 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001148 return in != 0;
1149 } else {
1150 return dapm_generic_check_power(w);
1151 }
1152}
1153
1154/* Check to see if a DAC has power */
1155static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1156{
1157 int out;
1158
Mark Brownde02d072011-09-20 21:43:24 +01001159 DAPM_UPDATE_STAT(w, power_checks);
1160
Mark Brown6ea31b92009-04-20 17:15:41 +01001161 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001162 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001163 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001164 return out != 0;
1165 } else {
1166 return dapm_generic_check_power(w);
1167 }
1168}
1169
Mark Brown246d0a12009-04-22 18:24:55 +01001170/* Check to see if a power supply is needed */
1171static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1172{
1173 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001174
Mark Brownde02d072011-09-20 21:43:24 +01001175 DAPM_UPDATE_STAT(w, power_checks);
1176
Mark Brown246d0a12009-04-22 18:24:55 +01001177 /* Check if one of our outputs is connected */
1178 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001179 DAPM_UPDATE_STAT(w, neighbour_checks);
1180
Mark Brownbf3a9e12011-06-13 16:42:29 +01001181 if (path->weak)
1182 continue;
1183
Mark Brown215edda2009-09-08 18:59:05 +01001184 if (path->connected &&
1185 !path->connected(path->source, path->sink))
1186 continue;
1187
Mark Brown30173582011-02-11 11:42:19 +00001188 if (!path->sink)
1189 continue;
1190
Mark Brownf68d7e12011-10-04 22:57:50 +01001191 if (dapm_widget_power_check(path->sink))
1192 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001193 }
1194
Mark Brownf68d7e12011-10-04 22:57:50 +01001195 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001196}
1197
Mark Brown35c64bc2011-09-28 18:23:53 +01001198static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1199{
1200 return 1;
1201}
1202
Mark Brown38357ab2009-06-06 19:03:23 +01001203static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1204 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001205 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001206{
Mark Brown828a8422011-01-15 13:14:30 +00001207 int *sort;
1208
1209 if (power_up)
1210 sort = dapm_up_seq;
1211 else
1212 sort = dapm_down_seq;
1213
Mark Brown38357ab2009-06-06 19:03:23 +01001214 if (sort[a->id] != sort[b->id])
1215 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001216 if (a->subseq != b->subseq) {
1217 if (power_up)
1218 return a->subseq - b->subseq;
1219 else
1220 return b->subseq - a->subseq;
1221 }
Mark Brownb22ead22009-06-07 12:51:26 +01001222 if (a->reg != b->reg)
1223 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001224 if (a->dapm != b->dapm)
1225 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001226
Mark Brown38357ab2009-06-06 19:03:23 +01001227 return 0;
1228}
Mark Brown42aa3412009-03-01 19:21:10 +00001229
Mark Brown38357ab2009-06-06 19:03:23 +01001230/* Insert a widget in order into a DAPM power sequence. */
1231static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1232 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001233 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001234{
1235 struct snd_soc_dapm_widget *w;
1236
1237 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001238 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001239 list_add_tail(&new_widget->power_list, &w->power_list);
1240 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001241 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001242
Mark Brown38357ab2009-06-06 19:03:23 +01001243 list_add_tail(&new_widget->power_list, list);
1244}
Mark Brown42aa3412009-03-01 19:21:10 +00001245
Mark Brown68f89ad2010-11-03 23:51:49 -04001246static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1247 struct snd_soc_dapm_widget *w, int event)
1248{
1249 struct snd_soc_card *card = dapm->card;
1250 const char *ev_name;
1251 int power, ret;
1252
1253 switch (event) {
1254 case SND_SOC_DAPM_PRE_PMU:
1255 ev_name = "PRE_PMU";
1256 power = 1;
1257 break;
1258 case SND_SOC_DAPM_POST_PMU:
1259 ev_name = "POST_PMU";
1260 power = 1;
1261 break;
1262 case SND_SOC_DAPM_PRE_PMD:
1263 ev_name = "PRE_PMD";
1264 power = 0;
1265 break;
1266 case SND_SOC_DAPM_POST_PMD:
1267 ev_name = "POST_PMD";
1268 power = 0;
1269 break;
Mark Brown80114122013-02-25 15:14:19 +00001270 case SND_SOC_DAPM_WILL_PMU:
1271 ev_name = "WILL_PMU";
1272 power = 1;
1273 break;
1274 case SND_SOC_DAPM_WILL_PMD:
1275 ev_name = "WILL_PMD";
1276 power = 0;
1277 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001278 default:
1279 BUG();
1280 return;
1281 }
1282
1283 if (w->power != power)
1284 return;
1285
1286 if (w->event && (w->event_flags & event)) {
1287 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1288 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001289 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001290 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001291 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001292 if (ret < 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001293 dev_err(dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001294 ev_name, w->name, ret);
1295 }
1296}
1297
Mark Brownb22ead22009-06-07 12:51:26 +01001298/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001299static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001300 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001301{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001302 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001303 struct snd_soc_dapm_widget *w;
1304 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001305 unsigned int value = 0;
1306 unsigned int mask = 0;
1307 unsigned int cur_mask;
1308
1309 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1310 power_list)->reg;
1311
1312 list_for_each_entry(w, pending, power_list) {
1313 cur_mask = 1 << w->shift;
1314 BUG_ON(reg != w->reg);
1315
1316 if (w->invert)
1317 power = !w->power;
1318 else
1319 power = w->power;
1320
1321 mask |= cur_mask;
1322 if (power)
1323 value |= cur_mask;
1324
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001325 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001326 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1327 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001328
Mark Brown68f89ad2010-11-03 23:51:49 -04001329 /* Check for events */
1330 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1331 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001332 }
1333
Mark Brown81628102009-06-07 13:21:24 +01001334 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001335 /* Any widget will do, they should all be updating the
1336 * same register.
1337 */
1338 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1339 power_list);
1340
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001341 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001342 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001343 value, mask, reg, card->pop_time);
1344 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001345 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001346 }
1347
1348 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001349 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1350 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001351 }
Mark Brown42aa3412009-03-01 19:21:10 +00001352}
1353
Mark Brownb22ead22009-06-07 12:51:26 +01001354/* Apply a DAPM power sequence.
1355 *
1356 * We walk over a pre-sorted list of widgets to apply power to. In
1357 * order to minimise the number of writes to the device required
1358 * multiple widgets will be updated in a single write where possible.
1359 * Currently anything that requires more than a single write is not
1360 * handled.
1361 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001362static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001363 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001364{
1365 struct snd_soc_dapm_widget *w, *n;
1366 LIST_HEAD(pending);
1367 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001368 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001369 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001370 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001371 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001372 int *sort;
1373
1374 if (power_up)
1375 sort = dapm_up_seq;
1376 else
1377 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001378
Mark Brownb22ead22009-06-07 12:51:26 +01001379 list_for_each_entry_safe(w, n, list, power_list) {
1380 ret = 0;
1381
1382 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001383 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001384 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001385 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001386 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001387
Mark Brown474b62d2011-01-18 16:14:44 +00001388 if (cur_dapm && cur_dapm->seq_notifier) {
1389 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1390 if (sort[i] == cur_sort)
1391 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001392 i,
1393 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001394 }
1395
Mark Brownb22ead22009-06-07 12:51:26 +01001396 INIT_LIST_HEAD(&pending);
1397 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001398 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001399 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001400 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001401 }
1402
Mark Brown163cac02009-06-07 10:12:52 +01001403 switch (w->id) {
1404 case snd_soc_dapm_pre:
1405 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001406 list_for_each_entry_safe_continue(w, n, list,
1407 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001408
Mark Brownb22ead22009-06-07 12:51:26 +01001409 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001410 ret = w->event(w,
1411 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001412 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001413 ret = w->event(w,
1414 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001415 break;
1416
1417 case snd_soc_dapm_post:
1418 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001419 list_for_each_entry_safe_continue(w, n, list,
1420 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001421
Mark Brownb22ead22009-06-07 12:51:26 +01001422 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001423 ret = w->event(w,
1424 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001425 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001426 ret = w->event(w,
1427 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001428 break;
1429
Mark Brown163cac02009-06-07 10:12:52 +01001430 default:
Mark Brown81628102009-06-07 13:21:24 +01001431 /* Queue it up for application */
1432 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001433 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001434 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001435 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001436 list_move(&w->power_list, &pending);
1437 break;
Mark Brown163cac02009-06-07 10:12:52 +01001438 }
Mark Brownb22ead22009-06-07 12:51:26 +01001439
1440 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001441 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001442 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001443 }
Mark Brownb22ead22009-06-07 12:51:26 +01001444
1445 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001446 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001447
1448 if (cur_dapm && cur_dapm->seq_notifier) {
1449 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1450 if (sort[i] == cur_sort)
1451 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001452 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001453 }
Mark Brown163cac02009-06-07 10:12:52 +01001454}
1455
Mark Brown97404f22010-12-14 16:13:57 +00001456static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1457{
1458 struct snd_soc_dapm_update *update = dapm->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001459 struct snd_soc_dapm_widget_list *wlist;
1460 struct snd_soc_dapm_widget *w = NULL;
1461 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001462 int ret;
1463
1464 if (!update)
1465 return;
1466
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001467 wlist = snd_kcontrol_chip(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001468
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001469 for (wi = 0; wi < wlist->num_widgets; wi++) {
1470 w = wlist->widgets[wi];
1471
1472 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1473 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1474 if (ret != 0)
1475 dev_err(dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1476 w->name, ret);
1477 }
Mark Brown97404f22010-12-14 16:13:57 +00001478 }
1479
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001480 if (!w)
1481 return;
1482
Liam Girdwood49575fb52012-03-06 18:16:19 +00001483 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001484 update->val);
1485 if (ret < 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001486 dev_err(dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1487 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001488
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001489 for (wi = 0; wi < wlist->num_widgets; wi++) {
1490 w = wlist->widgets[wi];
1491
1492 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1493 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1494 if (ret != 0)
1495 dev_err(dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1496 w->name, ret);
1497 }
Mark Brown97404f22010-12-14 16:13:57 +00001498 }
1499}
1500
Mark Brown9d0624a2011-02-18 11:49:43 -08001501/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1502 * they're changing state.
1503 */
1504static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1505{
1506 struct snd_soc_dapm_context *d = data;
1507 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001508
Mark Brown56fba412011-06-04 11:25:10 +01001509 /* If we're off and we're not supposed to be go into STANDBY */
1510 if (d->bias_level == SND_SOC_BIAS_OFF &&
1511 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001512 if (d->dev)
1513 pm_runtime_get_sync(d->dev);
1514
Mark Brown9d0624a2011-02-18 11:49:43 -08001515 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1516 if (ret != 0)
1517 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001518 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001519 }
1520
Mark Brown56fba412011-06-04 11:25:10 +01001521 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1522 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001523 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1524 if (ret != 0)
1525 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001526 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001527 }
1528}
1529
1530/* Async callback run prior to DAPM sequences - brings to their final
1531 * state.
1532 */
1533static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1534{
1535 struct snd_soc_dapm_context *d = data;
1536 int ret;
1537
1538 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001539 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1540 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1541 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001542 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1543 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001544 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001545 ret);
1546 }
1547
1548 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001549 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1550 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001551 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1552 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001553 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1554 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001555
1556 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001557 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001558 }
1559
1560 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001561 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1562 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001563 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1564 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001565 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001566 ret);
1567 }
1568}
Mark Brown97404f22010-12-14 16:13:57 +00001569
Mark Brownfe4fda52011-10-03 22:36:57 +01001570static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1571 bool power, bool connect)
1572{
1573 /* If a connection is being made or broken then that update
1574 * will have marked the peer dirty, otherwise the widgets are
1575 * not connected and this update has no impact. */
1576 if (!connect)
1577 return;
1578
1579 /* If the peer is already in the state we're moving to then we
1580 * won't have an impact on it. */
1581 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001582 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001583}
1584
Mark Brown05623c42011-09-28 17:02:31 +01001585static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1586 struct list_head *up_list,
1587 struct list_head *down_list)
1588{
Mark Browndb432b42011-10-03 21:06:40 +01001589 struct snd_soc_dapm_path *path;
1590
Mark Brown05623c42011-09-28 17:02:31 +01001591 if (w->power == power)
1592 return;
1593
1594 trace_snd_soc_dapm_widget_power(w, power);
1595
Mark Browndb432b42011-10-03 21:06:40 +01001596 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001597 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001598 */
1599 list_for_each_entry(path, &w->sources, list_sink) {
1600 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001601 dapm_widget_set_peer_power(path->source, power,
1602 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001603 }
1604 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001605 switch (w->id) {
1606 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001607 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001608 case snd_soc_dapm_clock_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001609 /* Supplies can't affect their outputs, only their inputs */
1610 break;
1611 default:
1612 list_for_each_entry(path, &w->sinks, list_source) {
1613 if (path->sink) {
1614 dapm_widget_set_peer_power(path->sink, power,
1615 path->connect);
1616 }
Mark Browndb432b42011-10-03 21:06:40 +01001617 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001618 break;
Mark Browndb432b42011-10-03 21:06:40 +01001619 }
1620
Mark Brown05623c42011-09-28 17:02:31 +01001621 if (power)
1622 dapm_seq_insert(w, up_list, true);
1623 else
1624 dapm_seq_insert(w, down_list, false);
1625
1626 w->power = power;
1627}
1628
Mark Brown7c81beb2011-09-20 22:22:32 +01001629static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1630 struct list_head *up_list,
1631 struct list_head *down_list)
1632{
Mark Brown7c81beb2011-09-20 22:22:32 +01001633 int power;
1634
1635 switch (w->id) {
1636 case snd_soc_dapm_pre:
1637 dapm_seq_insert(w, down_list, false);
1638 break;
1639 case snd_soc_dapm_post:
1640 dapm_seq_insert(w, up_list, true);
1641 break;
1642
1643 default:
Mark Brownd8050022011-09-28 18:28:23 +01001644 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001645
Mark Brown05623c42011-09-28 17:02:31 +01001646 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001647 break;
1648 }
1649}
1650
Mark Brown42aa3412009-03-01 19:21:10 +00001651/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001652 * Scan each dapm widget for complete audio path.
1653 * A complete path is a route that has valid endpoints i.e.:-
1654 *
1655 * o DAC to output pin.
1656 * o Input Pin to ADC.
1657 * o Input pin to Output pin (bypass, sidetone)
1658 * o DAC to ADC (loopback).
1659 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001660static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001661{
Mark Brown12ea2c72011-03-02 18:17:32 +00001662 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001663 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001664 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001665 LIST_HEAD(up_list);
1666 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001667 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001668 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001669
Mark Brown84e90932010-11-04 00:07:02 -04001670 trace_snd_soc_dapm_start(card);
1671
Mark Brown56fba412011-06-04 11:25:10 +01001672 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001673 if (d->idle_bias_off)
1674 d->target_bias_level = SND_SOC_BIAS_OFF;
1675 else
1676 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001677 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001678
Liam Girdwood6c120e12012-02-15 15:15:34 +00001679 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001680
Mark Brown6d3ddc82009-05-16 17:47:29 +01001681 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001682 * lists indicating if they should be powered up or down. We
1683 * only check widgets that have been flagged as dirty but note
1684 * that new widgets may be added to the dirty list while we
1685 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001686 */
Mark Browndb432b42011-10-03 21:06:40 +01001687 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001688 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001689 }
1690
Mark Brownf9de6d72011-09-28 17:19:47 +01001691 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001692 switch (w->id) {
1693 case snd_soc_dapm_pre:
1694 case snd_soc_dapm_post:
1695 /* These widgets always need to be powered */
1696 break;
1697 default:
1698 list_del_init(&w->dirty);
1699 break;
1700 }
Mark Browndb432b42011-10-03 21:06:40 +01001701
Mark Brownf9de6d72011-09-28 17:19:47 +01001702 if (w->power) {
1703 d = w->dapm;
1704
1705 /* Supplies and micbiases only bring the
1706 * context up to STANDBY as unless something
1707 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001708 * generally don't require full power. Signal
1709 * generators are virtual pins and have no
1710 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001711 */
1712 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001713 case snd_soc_dapm_siggen:
1714 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001715 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001716 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001717 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001718 case snd_soc_dapm_micbias:
1719 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1720 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1721 break;
1722 default:
1723 d->target_bias_level = SND_SOC_BIAS_ON;
1724 break;
1725 }
1726 }
1727
1728 }
1729
Mark Brown85a843c2011-09-21 21:29:47 +01001730 /* Force all contexts in the card to the same bias state if
1731 * they're not ground referenced.
1732 */
Mark Brown56fba412011-06-04 11:25:10 +01001733 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001734 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001735 if (d->target_bias_level > bias)
1736 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001737 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001738 if (!d->idle_bias_off)
1739 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001740
Mark Brownde02d072011-09-20 21:43:24 +01001741 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001742
Mark Brown9d0624a2011-02-18 11:49:43 -08001743 /* Run all the bias changes in parallel */
1744 list_for_each_entry(d, &dapm->card->dapm_list, list)
1745 async_schedule_domain(dapm_pre_sequence_async, d,
1746 &async_domain);
1747 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001748
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001749 list_for_each_entry(w, &down_list, power_list) {
Mark Brown80114122013-02-25 15:14:19 +00001750 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_WILL_PMD);
1751 }
1752
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001753 list_for_each_entry(w, &up_list, power_list) {
Mark Brown80114122013-02-25 15:14:19 +00001754 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_WILL_PMU);
1755 }
1756
Mark Brown6d3ddc82009-05-16 17:47:29 +01001757 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001758 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001759
Mark Brown97404f22010-12-14 16:13:57 +00001760 dapm_widget_update(dapm);
1761
Mark Brown6d3ddc82009-05-16 17:47:29 +01001762 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001763 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001764
Mark Brown9d0624a2011-02-18 11:49:43 -08001765 /* Run all the bias changes in parallel */
1766 list_for_each_entry(d, &dapm->card->dapm_list, list)
1767 async_schedule_domain(dapm_post_sequence_async, d,
1768 &async_domain);
1769 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001770
Liam Girdwood8078d872012-02-15 15:15:35 +00001771 /* do we need to notify any clients that DAPM event is complete */
1772 list_for_each_entry(d, &card->dapm_list, list) {
1773 if (d->stream_event)
1774 d->stream_event(d, event);
1775 }
1776
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001777 pop_dbg(dapm->dev, card->pop_time,
1778 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001779 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001780
Mark Brown84e90932010-11-04 00:07:02 -04001781 trace_snd_soc_dapm_done(card);
1782
Mark Brown42aa3412009-03-01 19:21:10 +00001783 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001784}
1785
Mark Brown79fb9382009-08-21 16:38:13 +01001786#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001787static ssize_t dapm_widget_power_read_file(struct file *file,
1788 char __user *user_buf,
1789 size_t count, loff_t *ppos)
1790{
1791 struct snd_soc_dapm_widget *w = file->private_data;
1792 char *buf;
1793 int in, out;
1794 ssize_t ret;
1795 struct snd_soc_dapm_path *p = NULL;
1796
1797 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1798 if (!buf)
1799 return -ENOMEM;
1800
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001801 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001802 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001803 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001804 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001805
Mark Brownf13ebad2012-03-03 18:01:01 +00001806 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1807 w->name, w->power ? "On" : "Off",
1808 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001809
Mark Brownd033c362009-12-04 15:25:56 +00001810 if (w->reg >= 0)
1811 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1812 " - R%d(0x%x) bit %d",
1813 w->reg, w->reg, w->shift);
1814
1815 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1816
Mark Brown3eef08b2009-09-14 16:49:00 +01001817 if (w->sname)
1818 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1819 w->sname,
1820 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001821
1822 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001823 if (p->connected && !p->connected(w, p->sink))
1824 continue;
1825
Mark Brown79fb9382009-08-21 16:38:13 +01001826 if (p->connect)
1827 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001828 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001829 p->name ? p->name : "static",
1830 p->source->name);
1831 }
1832 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001833 if (p->connected && !p->connected(w, p->sink))
1834 continue;
1835
Mark Brown79fb9382009-08-21 16:38:13 +01001836 if (p->connect)
1837 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001838 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001839 p->name ? p->name : "static",
1840 p->sink->name);
1841 }
1842
1843 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1844
1845 kfree(buf);
1846 return ret;
1847}
1848
1849static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001850 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001851 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001852 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001853};
1854
Mark Brownef49e4f2011-04-04 20:48:13 +09001855static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1856 size_t count, loff_t *ppos)
1857{
1858 struct snd_soc_dapm_context *dapm = file->private_data;
1859 char *level;
1860
1861 switch (dapm->bias_level) {
1862 case SND_SOC_BIAS_ON:
1863 level = "On\n";
1864 break;
1865 case SND_SOC_BIAS_PREPARE:
1866 level = "Prepare\n";
1867 break;
1868 case SND_SOC_BIAS_STANDBY:
1869 level = "Standby\n";
1870 break;
1871 case SND_SOC_BIAS_OFF:
1872 level = "Off\n";
1873 break;
1874 default:
1875 BUG();
1876 level = "Unknown\n";
1877 break;
1878 }
1879
1880 return simple_read_from_buffer(user_buf, count, ppos, level,
1881 strlen(level));
1882}
1883
1884static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001885 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001886 .read = dapm_bias_read_file,
1887 .llseek = default_llseek,
1888};
1889
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001890void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1891 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001892{
Mark Brown79fb9382009-08-21 16:38:13 +01001893 struct dentry *d;
1894
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001895 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1896
1897 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001898 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001899 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001900 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001901 }
Mark Brown79fb9382009-08-21 16:38:13 +01001902
Mark Brownef49e4f2011-04-04 20:48:13 +09001903 d = debugfs_create_file("bias_level", 0444,
1904 dapm->debugfs_dapm, dapm,
1905 &dapm_bias_fops);
1906 if (!d)
1907 dev_warn(dapm->dev,
1908 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001909}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001910
1911static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1912{
1913 struct snd_soc_dapm_context *dapm = w->dapm;
1914 struct dentry *d;
1915
1916 if (!dapm->debugfs_dapm || !w->name)
1917 return;
1918
1919 d = debugfs_create_file(w->name, 0444,
1920 dapm->debugfs_dapm, w,
1921 &dapm_widget_power_fops);
1922 if (!d)
1923 dev_warn(w->dapm->dev,
1924 "ASoC: Failed to create %s debugfs file\n",
1925 w->name);
1926}
1927
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001928static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1929{
1930 debugfs_remove_recursive(dapm->debugfs_dapm);
1931}
1932
Mark Brown79fb9382009-08-21 16:38:13 +01001933#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001934void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1935 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001936{
1937}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001938
1939static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1940{
1941}
1942
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001943static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1944{
1945}
1946
Mark Brown79fb9382009-08-21 16:38:13 +01001947#endif
1948
Richard Purdie2b97eab2006-10-06 18:32:18 +02001949/* test and update the power status of a mux widget */
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001950static int soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001951 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001952{
1953 struct snd_soc_dapm_path *path;
1954 int found = 0;
1955
Richard Purdie2b97eab2006-10-06 18:32:18 +02001956 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001957 list_for_each_entry(path, &dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001958 if (path->kcontrol != kcontrol)
1959 continue;
1960
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001961 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001962 continue;
1963
1964 found = 1;
1965 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001966 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001967 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001968 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001969 } else {
1970 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001971 dapm_mark_dirty(path->source,
1972 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001973 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001974 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001975 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001976 }
1977
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001978 if (found)
1979 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001980
Liam Girdwood618dae12012-04-25 12:12:51 +01001981 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001982}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001983
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001984int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
1985 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001986{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001987 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001988 int ret;
1989
Liam Girdwood3cd04342012-03-09 12:02:08 +00001990 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001991 ret = soc_dapm_mux_update_power(dapm, kcontrol, mux, e);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001992 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001993 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001994 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001995 return ret;
1996}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001997EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001998
Milan plzik1b075e32008-01-10 14:39:46 +01001999/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002000static int soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Mark Brown283375c2009-12-07 18:09:03 +00002001 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002002{
2003 struct snd_soc_dapm_path *path;
2004 int found = 0;
2005
Richard Purdie2b97eab2006-10-06 18:32:18 +02002006 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002007 list_for_each_entry(path, &dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002008 if (path->kcontrol != kcontrol)
2009 continue;
2010
2011 /* found, now check type */
2012 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002013 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002014 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002015 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002016 }
2017
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002018 if (found)
2019 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002020
Liam Girdwood618dae12012-04-25 12:12:51 +01002021 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002022}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002023
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002024int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2025 struct snd_kcontrol *kcontrol, int connect)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002026{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002027 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002028 int ret;
2029
Liam Girdwood3cd04342012-03-09 12:02:08 +00002030 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002031 ret = soc_dapm_mixer_update_power(dapm, kcontrol, connect);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002032 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002033 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002034 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002035 return ret;
2036}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002037EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002038
2039/* show dapm widget status in sys fs */
2040static ssize_t dapm_widget_show(struct device *dev,
2041 struct device_attribute *attr, char *buf)
2042{
Mark Brown36ae1a92012-01-06 17:12:45 -08002043 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002044 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002045 struct snd_soc_dapm_widget *w;
2046 int count = 0;
2047 char *state = "not set";
2048
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002049 list_for_each_entry(w, &codec->card->widgets, list) {
2050 if (w->dapm != &codec->dapm)
2051 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002052
2053 /* only display widgets that burnm power */
2054 switch (w->id) {
2055 case snd_soc_dapm_hp:
2056 case snd_soc_dapm_mic:
2057 case snd_soc_dapm_spk:
2058 case snd_soc_dapm_line:
2059 case snd_soc_dapm_micbias:
2060 case snd_soc_dapm_dac:
2061 case snd_soc_dapm_adc:
2062 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002063 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002064 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002065 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002066 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002067 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002068 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002069 if (w->name)
2070 count += sprintf(buf + count, "%s: %s\n",
2071 w->name, w->power ? "On":"Off");
2072 break;
2073 default:
2074 break;
2075 }
2076 }
2077
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002078 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002079 case SND_SOC_BIAS_ON:
2080 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002081 break;
Mark Brown0be98982008-05-19 12:31:28 +02002082 case SND_SOC_BIAS_PREPARE:
2083 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002084 break;
Mark Brown0be98982008-05-19 12:31:28 +02002085 case SND_SOC_BIAS_STANDBY:
2086 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002087 break;
Mark Brown0be98982008-05-19 12:31:28 +02002088 case SND_SOC_BIAS_OFF:
2089 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002090 break;
2091 }
2092 count += sprintf(buf + count, "PM State: %s\n", state);
2093
2094 return count;
2095}
2096
2097static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2098
2099int snd_soc_dapm_sys_add(struct device *dev)
2100{
Troy Kisky12ef1932008-10-13 17:42:14 -07002101 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002102}
2103
2104static void snd_soc_dapm_sys_remove(struct device *dev)
2105{
Mark Brownaef90842009-05-16 17:53:16 +01002106 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002107}
2108
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002109static void dapm_free_path(struct snd_soc_dapm_path *path)
2110{
2111 list_del(&path->list_sink);
2112 list_del(&path->list_source);
2113 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002114 kfree(path);
2115}
2116
Richard Purdie2b97eab2006-10-06 18:32:18 +02002117/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002118static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002119{
2120 struct snd_soc_dapm_widget *w, *next_w;
2121 struct snd_soc_dapm_path *p, *next_p;
2122
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002123 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2124 if (w->dapm != dapm)
2125 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002126 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002127 /*
2128 * remove source and sink paths associated to this widget.
2129 * While removing the path, remove reference to it from both
2130 * source and sink widgets so that path is removed only once.
2131 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002132 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2133 dapm_free_path(p);
2134
2135 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2136 dapm_free_path(p);
2137
Stephen Warrenfad59882011-04-28 17:37:59 -06002138 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002139 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002140 kfree(w);
2141 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002142}
2143
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002144static struct snd_soc_dapm_widget *dapm_find_widget(
2145 struct snd_soc_dapm_context *dapm, const char *pin,
2146 bool search_other_contexts)
2147{
2148 struct snd_soc_dapm_widget *w;
2149 struct snd_soc_dapm_widget *fallback = NULL;
2150
2151 list_for_each_entry(w, &dapm->card->widgets, list) {
2152 if (!strcmp(w->name, pin)) {
2153 if (w->dapm == dapm)
2154 return w;
2155 else
2156 fallback = w;
2157 }
2158 }
2159
2160 if (search_other_contexts)
2161 return fallback;
2162
2163 return NULL;
2164}
2165
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002166static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002167 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002168{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002169 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002170
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002171 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002172 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002173 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002174 }
2175
Mark Brown1a8b2d92012-02-16 11:50:07 -08002176 if (w->connected != status)
2177 dapm_mark_dirty(w, "pin configuration");
2178
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002179 w->connected = status;
2180 if (status == 0)
2181 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002182
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002183 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002184}
2185
Richard Purdie2b97eab2006-10-06 18:32:18 +02002186/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002187 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002188 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002189 *
2190 * Walks all dapm audio paths and powers widgets according to their
2191 * stream or path usage.
2192 *
2193 * Returns 0 for success.
2194 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002195int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002196{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002197 int ret;
2198
Mark Brown4f4c0072011-10-07 14:29:19 +01002199 /*
2200 * Suppress early reports (eg, jacks syncing their state) to avoid
2201 * silly DAPM runs during card startup.
2202 */
2203 if (!dapm->card || !dapm->card->instantiated)
2204 return 0;
2205
Liam Girdwood3cd04342012-03-09 12:02:08 +00002206 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002207 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2208 mutex_unlock(&dapm->card->dapm_mutex);
2209 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002210}
Liam Girdwooda5302182008-07-07 13:35:17 +01002211EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002212
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002213static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002214 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002215{
2216 struct snd_soc_dapm_path *path;
2217 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002218 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002219 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002220 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002221 const char *source;
2222 char prefixed_sink[80];
2223 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002224 int ret = 0;
2225
Mark Brown88e8b9a2011-03-02 18:18:24 +00002226 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002227 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2228 dapm->codec->name_prefix, route->sink);
2229 sink = prefixed_sink;
2230 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2231 dapm->codec->name_prefix, route->source);
2232 source = prefixed_source;
2233 } else {
2234 sink = route->sink;
2235 source = route->source;
2236 }
2237
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002238 /*
2239 * find src and dest widgets over all widgets but favor a widget from
2240 * current DAPM context
2241 */
2242 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002243 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002244 wtsink = w;
2245 if (w->dapm == dapm)
2246 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002247 continue;
2248 }
2249 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002250 wtsource = w;
2251 if (w->dapm == dapm)
2252 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002253 }
2254 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002255 /* use widget from another DAPM context if not found from this */
2256 if (!wsink)
2257 wsink = wtsink;
2258 if (!wsource)
2259 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002260
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002261 if (wsource == NULL) {
2262 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2263 route->source);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002264 return -ENODEV;
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002265 }
2266 if (wsink == NULL) {
2267 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2268 route->sink);
2269 return -ENODEV;
2270 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002271
2272 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2273 if (!path)
2274 return -ENOMEM;
2275
2276 path->source = wsource;
2277 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002278 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002279 INIT_LIST_HEAD(&path->list);
2280 INIT_LIST_HEAD(&path->list_source);
2281 INIT_LIST_HEAD(&path->list_sink);
2282
2283 /* check for external widgets */
2284 if (wsink->id == snd_soc_dapm_input) {
2285 if (wsource->id == snd_soc_dapm_micbias ||
2286 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002287 wsource->id == snd_soc_dapm_line ||
2288 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002289 wsink->ext = 1;
2290 }
2291 if (wsource->id == snd_soc_dapm_output) {
2292 if (wsink->id == snd_soc_dapm_spk ||
2293 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002294 wsink->id == snd_soc_dapm_line ||
2295 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002296 wsource->ext = 1;
2297 }
2298
2299 /* connect static paths */
2300 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002301 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002302 list_add(&path->list_sink, &wsink->sources);
2303 list_add(&path->list_source, &wsource->sinks);
2304 path->connect = 1;
2305 return 0;
2306 }
2307
2308 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002309 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002310 case snd_soc_dapm_adc:
2311 case snd_soc_dapm_dac:
2312 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002313 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002314 case snd_soc_dapm_input:
2315 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002316 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002317 case snd_soc_dapm_micbias:
2318 case snd_soc_dapm_vmid:
2319 case snd_soc_dapm_pre:
2320 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002321 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002322 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002323 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002324 case snd_soc_dapm_aif_in:
2325 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002326 case snd_soc_dapm_dai_in:
2327 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002328 case snd_soc_dapm_dai_link:
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 = 1;
2333 return 0;
2334 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002335 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002336 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002337 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002338 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002339 if (ret != 0)
2340 goto err;
2341 break;
2342 case snd_soc_dapm_switch:
2343 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002344 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002345 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002346 if (ret != 0)
2347 goto err;
2348 break;
2349 case snd_soc_dapm_hp:
2350 case snd_soc_dapm_mic:
2351 case snd_soc_dapm_line:
2352 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002353 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002354 list_add(&path->list_sink, &wsink->sources);
2355 list_add(&path->list_source, &wsource->sinks);
2356 path->connect = 0;
2357 return 0;
2358 }
Mark Brownfabd0382012-07-05 17:20:06 +01002359
2360 dapm_mark_dirty(wsource, "Route added");
2361 dapm_mark_dirty(wsink, "Route added");
2362
Richard Purdie2b97eab2006-10-06 18:32:18 +02002363 return 0;
2364
2365err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002366 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002367 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002368 kfree(path);
2369 return ret;
2370}
Mark Brown105f1c22008-05-13 14:52:19 +02002371
Mark Brownefcc3c62012-07-05 17:24:19 +01002372static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2373 const struct snd_soc_dapm_route *route)
2374{
2375 struct snd_soc_dapm_path *path, *p;
2376 const char *sink;
2377 const char *source;
2378 char prefixed_sink[80];
2379 char prefixed_source[80];
2380
2381 if (route->control) {
2382 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002383 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002384 return -EINVAL;
2385 }
2386
2387 if (dapm->codec && dapm->codec->name_prefix) {
2388 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2389 dapm->codec->name_prefix, route->sink);
2390 sink = prefixed_sink;
2391 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2392 dapm->codec->name_prefix, route->source);
2393 source = prefixed_source;
2394 } else {
2395 sink = route->sink;
2396 source = route->source;
2397 }
2398
2399 path = NULL;
2400 list_for_each_entry(p, &dapm->card->paths, list) {
2401 if (strcmp(p->source->name, source) != 0)
2402 continue;
2403 if (strcmp(p->sink->name, sink) != 0)
2404 continue;
2405 path = p;
2406 break;
2407 }
2408
2409 if (path) {
2410 dapm_mark_dirty(path->source, "Route removed");
2411 dapm_mark_dirty(path->sink, "Route removed");
2412
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002413 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002414 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002415 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002416 source, sink);
2417 }
2418
2419 return 0;
2420}
2421
Mark Brown105f1c22008-05-13 14:52:19 +02002422/**
Mark Brown105f1c22008-05-13 14:52:19 +02002423 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002424 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002425 * @route: audio routes
2426 * @num: number of routes
2427 *
2428 * Connects 2 dapm widgets together via a named audio path. The sink is
2429 * the widget receiving the audio signal, whilst the source is the sender
2430 * of the audio signal.
2431 *
2432 * Returns 0 for success else error. On error all resources can be freed
2433 * with a call to snd_soc_card_free().
2434 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002435int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002436 const struct snd_soc_dapm_route *route, int num)
2437{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002438 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002439
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002440 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002441 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002442 r = snd_soc_dapm_add_route(dapm, route);
2443 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002444 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2445 route->source,
2446 route->control ? route->control : "direct",
2447 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002448 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002449 }
2450 route++;
2451 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002452 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002453
Dan Carpenter60884c22012-04-13 22:25:43 +03002454 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002455}
2456EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2457
Mark Brownefcc3c62012-07-05 17:24:19 +01002458/**
2459 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2460 * @dapm: DAPM context
2461 * @route: audio routes
2462 * @num: number of routes
2463 *
2464 * Removes routes from the DAPM context.
2465 */
2466int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2467 const struct snd_soc_dapm_route *route, int num)
2468{
2469 int i, ret = 0;
2470
2471 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2472 for (i = 0; i < num; i++) {
2473 snd_soc_dapm_del_route(dapm, route);
2474 route++;
2475 }
2476 mutex_unlock(&dapm->card->dapm_mutex);
2477
2478 return ret;
2479}
2480EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2481
Mark Brownbf3a9e12011-06-13 16:42:29 +01002482static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2483 const struct snd_soc_dapm_route *route)
2484{
2485 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2486 route->source,
2487 true);
2488 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2489 route->sink,
2490 true);
2491 struct snd_soc_dapm_path *path;
2492 int count = 0;
2493
2494 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002495 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002496 route->source);
2497 return -ENODEV;
2498 }
2499
2500 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002501 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002502 route->sink);
2503 return -ENODEV;
2504 }
2505
2506 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002507 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002508 route->source, route->sink);
2509
2510 list_for_each_entry(path, &source->sinks, list_source) {
2511 if (path->sink == sink) {
2512 path->weak = 1;
2513 count++;
2514 }
2515 }
2516
2517 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002518 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002519 route->source, route->sink);
2520 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002521 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002522 count, route->source, route->sink);
2523
2524 return 0;
2525}
2526
2527/**
2528 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2529 * @dapm: DAPM context
2530 * @route: audio routes
2531 * @num: number of routes
2532 *
2533 * Mark existing routes matching those specified in the passed array
2534 * as being weak, meaning that they are ignored for the purpose of
2535 * power decisions. The main intended use case is for sidetone paths
2536 * which couple audio between other independent paths if they are both
2537 * active in order to make the combination work better at the user
2538 * level but which aren't intended to be "used".
2539 *
2540 * Note that CODEC drivers should not use this as sidetone type paths
2541 * can frequently also be used as bypass paths.
2542 */
2543int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2544 const struct snd_soc_dapm_route *route, int num)
2545{
2546 int i, err;
2547 int ret = 0;
2548
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002549 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002550 for (i = 0; i < num; i++) {
2551 err = snd_soc_dapm_weak_route(dapm, route);
2552 if (err)
2553 ret = err;
2554 route++;
2555 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002556 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002557
2558 return ret;
2559}
2560EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2561
Mark Brown105f1c22008-05-13 14:52:19 +02002562/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002563 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002564 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002565 *
2566 * Checks the codec for any new dapm widgets and creates them if found.
2567 *
2568 * Returns 0 for success.
2569 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002570int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002571{
2572 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002573 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002574
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002575 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2576
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002577 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002578 {
2579 if (w->new)
2580 continue;
2581
Stephen Warrenfad59882011-04-28 17:37:59 -06002582 if (w->num_kcontrols) {
2583 w->kcontrols = kzalloc(w->num_kcontrols *
2584 sizeof(struct snd_kcontrol *),
2585 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002586 if (!w->kcontrols) {
2587 mutex_unlock(&dapm->card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002588 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002589 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002590 }
2591
Richard Purdie2b97eab2006-10-06 18:32:18 +02002592 switch(w->id) {
2593 case snd_soc_dapm_switch:
2594 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002595 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002596 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002597 break;
2598 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002599 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002600 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002601 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002602 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002603 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002604 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002605 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002606 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002607 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002608 break;
2609 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002610
2611 /* Read the initial power state from the device */
2612 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002613 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002614 val &= 1 << w->shift;
2615 if (w->invert)
2616 val = !val;
2617
2618 if (val)
2619 w->power = 1;
2620 }
2621
Richard Purdie2b97eab2006-10-06 18:32:18 +02002622 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002623
Mark Brown7508b122011-10-05 12:09:12 +01002624 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002625 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002626 }
2627
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002628 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002629 mutex_unlock(&dapm->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002630 return 0;
2631}
2632EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2633
2634/**
2635 * snd_soc_dapm_get_volsw - dapm mixer get callback
2636 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002637 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002638 *
2639 * Callback to get the value of a dapm mixer control.
2640 *
2641 * Returns 0 for success.
2642 */
2643int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2644 struct snd_ctl_elem_value *ucontrol)
2645{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002646 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2647 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002648 struct soc_mixer_control *mc =
2649 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002650 unsigned int reg = mc->reg;
2651 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002652 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002653 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002654 unsigned int invert = mc->invert;
2655
2656 if (snd_soc_volsw_is_stereo(mc))
2657 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002658 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002659 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002660
Richard Purdie2b97eab2006-10-06 18:32:18 +02002661 ucontrol->value.integer.value[0] =
2662 (snd_soc_read(widget->codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002663 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002664 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002665 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002666
2667 return 0;
2668}
2669EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2670
2671/**
2672 * snd_soc_dapm_put_volsw - dapm mixer set callback
2673 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002674 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002675 *
2676 * Callback to set the value of a dapm mixer control.
2677 *
2678 * Returns 0 for success.
2679 */
2680int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2681 struct snd_ctl_elem_value *ucontrol)
2682{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002683 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2684 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2685 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002686 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002687 struct soc_mixer_control *mc =
2688 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002689 unsigned int reg = mc->reg;
2690 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002691 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002692 unsigned int mask = (1 << fls(max)) - 1;
2693 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002694 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002695 int connect, change;
2696 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002697
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002698 if (snd_soc_volsw_is_stereo(mc))
2699 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002700 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002701 kcontrol->id.name);
2702
Richard Purdie2b97eab2006-10-06 18:32:18 +02002703 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002704 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002705
2706 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002707 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002708 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002709 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002710
Liam Girdwood3cd04342012-03-09 12:02:08 +00002711 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002712
Stephen Warrene9cf7042011-01-27 14:54:05 -07002713 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002714 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002715 update.kcontrol = kcontrol;
2716 update.reg = reg;
2717 update.mask = mask;
2718 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002719
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002720 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002721
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002722 soc_dapm_mixer_update_power(widget->dapm, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002723
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002724 widget->dapm->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002725 }
2726
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002727 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002728 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002729}
2730EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2731
2732/**
2733 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2734 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002735 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002736 *
2737 * Callback to get the value of a dapm enumerated double mixer control.
2738 *
2739 * Returns 0 for success.
2740 */
2741int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2742 struct snd_ctl_elem_value *ucontrol)
2743{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002744 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2745 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002746 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002747 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002748
Richard Purdie2b97eab2006-10-06 18:32:18 +02002749 val = snd_soc_read(widget->codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002750 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002751 if (e->shift_l != e->shift_r)
2752 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002753 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002754
2755 return 0;
2756}
2757EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2758
2759/**
2760 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2761 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002762 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002763 *
2764 * Callback to set the value of a dapm enumerated double mixer control.
2765 *
2766 * Returns 0 for success.
2767 */
2768int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2769 struct snd_ctl_elem_value *ucontrol)
2770{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002771 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2772 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2773 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002774 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002775 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002776 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002777 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002778 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002779
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002780 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002781 return -EINVAL;
2782 mux = ucontrol->value.enumerated.item[0];
2783 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002784 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002785 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002786 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002787 return -EINVAL;
2788 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002789 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002790 }
2791
Liam Girdwood3cd04342012-03-09 12:02:08 +00002792 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002793
Mark Brown3a655772009-10-05 17:23:30 +01002794 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002795 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002796 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002797
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002798 update.kcontrol = kcontrol;
2799 update.reg = e->reg;
2800 update.mask = mask;
2801 update.val = val;
2802 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002803
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002804 soc_dapm_mux_update_power(widget->dapm, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002805
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002806 widget->dapm->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002807 }
2808
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002809 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002810 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002811}
2812EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2813
2814/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002815 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2816 * @kcontrol: mixer control
2817 * @ucontrol: control element information
2818 *
2819 * Returns 0 for success.
2820 */
2821int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2822 struct snd_ctl_elem_value *ucontrol)
2823{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002824 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2825 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002826
2827 ucontrol->value.enumerated.item[0] = widget->value;
2828
2829 return 0;
2830}
2831EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2832
2833/**
2834 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2835 * @kcontrol: mixer control
2836 * @ucontrol: control element information
2837 *
2838 * Returns 0 for success.
2839 */
2840int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2841 struct snd_ctl_elem_value *ucontrol)
2842{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002843 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2844 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2845 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002846 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002847 struct soc_enum *e =
2848 (struct soc_enum *)kcontrol->private_value;
2849 int change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002850
2851 if (ucontrol->value.enumerated.item[0] >= e->max)
2852 return -EINVAL;
2853
Liam Girdwood3cd04342012-03-09 12:02:08 +00002854 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002855
2856 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002857 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002858 widget->value = ucontrol->value.enumerated.item[0];
2859 soc_dapm_mux_update_power(widget->dapm, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002860 }
2861
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002862 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002863 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002864}
2865EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2866
2867/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002868 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2869 * callback
2870 * @kcontrol: mixer control
2871 * @ucontrol: control element information
2872 *
2873 * Callback to get the value of a dapm semi enumerated double mixer control.
2874 *
2875 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2876 * used for handling bitfield coded enumeration for example.
2877 *
2878 * Returns 0 for success.
2879 */
2880int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2881 struct snd_ctl_elem_value *ucontrol)
2882{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002883 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2884 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002885 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002886 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002887
2888 reg_val = snd_soc_read(widget->codec, e->reg);
2889 val = (reg_val >> e->shift_l) & e->mask;
2890 for (mux = 0; mux < e->max; mux++) {
2891 if (val == e->values[mux])
2892 break;
2893 }
2894 ucontrol->value.enumerated.item[0] = mux;
2895 if (e->shift_l != e->shift_r) {
2896 val = (reg_val >> e->shift_r) & e->mask;
2897 for (mux = 0; mux < e->max; mux++) {
2898 if (val == e->values[mux])
2899 break;
2900 }
2901 ucontrol->value.enumerated.item[1] = mux;
2902 }
2903
2904 return 0;
2905}
2906EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2907
2908/**
2909 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2910 * callback
2911 * @kcontrol: mixer control
2912 * @ucontrol: control element information
2913 *
2914 * Callback to set the value of a dapm semi enumerated double mixer control.
2915 *
2916 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2917 * used for handling bitfield coded enumeration for example.
2918 *
2919 * Returns 0 for success.
2920 */
2921int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2922 struct snd_ctl_elem_value *ucontrol)
2923{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002924 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2925 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2926 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002927 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002928 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002929 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002930 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002931 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002932
2933 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2934 return -EINVAL;
2935 mux = ucontrol->value.enumerated.item[0];
2936 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2937 mask = e->mask << e->shift_l;
2938 if (e->shift_l != e->shift_r) {
2939 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2940 return -EINVAL;
2941 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2942 mask |= e->mask << e->shift_r;
2943 }
2944
Liam Girdwood3cd04342012-03-09 12:02:08 +00002945 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002946
Mark Brown3a655772009-10-05 17:23:30 +01002947 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002948 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002949 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002950
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002951 update.kcontrol = kcontrol;
2952 update.reg = e->reg;
2953 update.mask = mask;
2954 update.val = val;
2955 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002956
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002957 soc_dapm_mux_update_power(widget->dapm, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002958
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002959 widget->dapm->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002960 }
2961
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002962 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002963 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002964}
2965EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2966
2967/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002968 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2969 *
2970 * @kcontrol: mixer control
2971 * @uinfo: control element information
2972 *
2973 * Callback to provide information about a pin switch control.
2974 */
2975int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2976 struct snd_ctl_elem_info *uinfo)
2977{
2978 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2979 uinfo->count = 1;
2980 uinfo->value.integer.min = 0;
2981 uinfo->value.integer.max = 1;
2982
2983 return 0;
2984}
2985EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2986
2987/**
2988 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2989 *
2990 * @kcontrol: mixer control
2991 * @ucontrol: Value
2992 */
2993int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2994 struct snd_ctl_elem_value *ucontrol)
2995{
Mark Brown48a8c392012-02-14 17:11:15 -08002996 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002997 const char *pin = (const char *)kcontrol->private_value;
2998
Liam Girdwood3cd04342012-03-09 12:02:08 +00002999 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003000
3001 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003002 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003003
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003004 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003005
3006 return 0;
3007}
3008EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3009
3010/**
3011 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3012 *
3013 * @kcontrol: mixer control
3014 * @ucontrol: Value
3015 */
3016int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3017 struct snd_ctl_elem_value *ucontrol)
3018{
Mark Brown48a8c392012-02-14 17:11:15 -08003019 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003020 const char *pin = (const char *)kcontrol->private_value;
3021
Liam Girdwood3cd04342012-03-09 12:02:08 +00003022 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003023
3024 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003025 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003026 else
Mark Brown48a8c392012-02-14 17:11:15 -08003027 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003028
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003029 mutex_unlock(&card->dapm_mutex);
3030
Mark Brown48a8c392012-02-14 17:11:15 -08003031 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003032 return 0;
3033}
3034EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3035
Mark Brown5ba06fc2012-02-16 11:07:13 -08003036static struct snd_soc_dapm_widget *
3037snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3038 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003039{
3040 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003041 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003042
3043 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003044 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003045
Mark Brown62ea8742012-01-21 21:14:48 +00003046 switch (w->id) {
3047 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003048 w->regulator = devm_regulator_get(dapm->dev, w->name);
3049 if (IS_ERR(w->regulator)) {
3050 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003051 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003052 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003053 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003054 }
Mark Brown8784c772013-01-10 19:33:47 +00003055
3056 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
3057 ret = regulator_allow_bypass(w->regulator, true);
3058 if (ret != 0)
3059 dev_warn(w->dapm->dev,
3060 "ASoC: Failed to unbypass %s: %d\n",
3061 w->name, ret);
3062 }
Mark Brown62ea8742012-01-21 21:14:48 +00003063 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003064 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003065#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003066 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003067 if (IS_ERR(w->clk)) {
3068 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003069 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003070 w->name, ret);
3071 return NULL;
3072 }
Mark Brownec029952012-06-04 08:16:20 +01003073#else
3074 return NULL;
3075#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003076 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003077 default:
3078 break;
3079 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003080
Mark Brown88e8b9a2011-03-02 18:18:24 +00003081 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003082 w->name = kasprintf(GFP_KERNEL, "%s %s",
3083 dapm->codec->name_prefix, widget->name);
3084 else
3085 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3086
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003087 if (w->name == NULL) {
3088 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003089 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003090 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003091
Mark Brown7ca3a182011-10-08 14:04:50 +01003092 switch (w->id) {
3093 case snd_soc_dapm_switch:
3094 case snd_soc_dapm_mixer:
3095 case snd_soc_dapm_mixer_named_ctl:
3096 w->power_check = dapm_generic_check_power;
3097 break;
3098 case snd_soc_dapm_mux:
3099 case snd_soc_dapm_virt_mux:
3100 case snd_soc_dapm_value_mux:
3101 w->power_check = dapm_generic_check_power;
3102 break;
Mark Brown46162742013-06-05 19:36:11 +01003103 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003104 w->power_check = dapm_adc_check_power;
3105 break;
Mark Brown46162742013-06-05 19:36:11 +01003106 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003107 w->power_check = dapm_dac_check_power;
3108 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003109 case snd_soc_dapm_adc:
3110 case snd_soc_dapm_aif_out:
3111 case snd_soc_dapm_dac:
3112 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003113 case snd_soc_dapm_pga:
3114 case snd_soc_dapm_out_drv:
3115 case snd_soc_dapm_input:
3116 case snd_soc_dapm_output:
3117 case snd_soc_dapm_micbias:
3118 case snd_soc_dapm_spk:
3119 case snd_soc_dapm_hp:
3120 case snd_soc_dapm_mic:
3121 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003122 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003123 w->power_check = dapm_generic_check_power;
3124 break;
3125 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003126 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003127 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003128 w->power_check = dapm_supply_check_power;
3129 break;
3130 default:
3131 w->power_check = dapm_always_on_check_power;
3132 break;
3133 }
3134
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003135 w->dapm = dapm;
3136 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003137 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003138 INIT_LIST_HEAD(&w->sources);
3139 INIT_LIST_HEAD(&w->sinks);
3140 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003141 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003142 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003143
3144 /* machine layer set ups unconnected pins and insertions */
3145 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003146 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003147}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003148
3149/**
Mark Brown4ba13272008-05-13 14:51:19 +02003150 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003151 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003152 * @widget: widget array
3153 * @num: number of widgets
3154 *
3155 * Creates new DAPM controls based upon the templates.
3156 *
3157 * Returns 0 for success else error.
3158 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003159int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003160 const struct snd_soc_dapm_widget *widget,
3161 int num)
3162{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003163 struct snd_soc_dapm_widget *w;
3164 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003165 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003166
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003167 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003168 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003169 w = snd_soc_dapm_new_control(dapm, widget);
3170 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003171 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003172 "ASoC: Failed to create DAPM control %s\n",
3173 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003174 ret = -ENOMEM;
3175 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003176 }
Mark Brown4ba13272008-05-13 14:51:19 +02003177 widget++;
3178 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003179 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003180 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003181}
3182EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3183
Mark Brownc74184e2012-04-04 22:12:09 +01003184static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3185 struct snd_kcontrol *kcontrol, int event)
3186{
3187 struct snd_soc_dapm_path *source_p, *sink_p;
3188 struct snd_soc_dai *source, *sink;
3189 const struct snd_soc_pcm_stream *config = w->params;
3190 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003191 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003192 u64 fmt;
3193 int ret;
3194
3195 BUG_ON(!config);
3196 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3197
3198 /* We only support a single source and sink, pick the first */
3199 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3200 list_sink);
3201 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3202 list_source);
3203
3204 BUG_ON(!source_p || !sink_p);
3205 BUG_ON(!sink_p->source || !source_p->sink);
3206 BUG_ON(!source_p->source || !sink_p->sink);
3207
3208 source = source_p->source->priv;
3209 sink = sink_p->sink->priv;
3210
3211 /* Be a little careful as we don't want to overflow the mask array */
3212 if (config->formats) {
3213 fmt = ffs(config->formats) - 1;
3214 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003215 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003216 config->formats);
3217 fmt = 0;
3218 }
3219
3220 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003221 params = kzalloc(sizeof(*params), GFP_KERNEL);
3222 if (!params) {
3223 ret = -ENOMEM;
3224 goto out;
3225 }
3226 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003227
Mark Brown9747cec2012-04-26 19:12:21 +01003228 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003229 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003230 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003231 config->rate_max;
3232
Mark Brown9747cec2012-04-26 19:12:21 +01003233 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003234 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003235 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003236 = config->channels_max;
3237
3238 memset(&substream, 0, sizeof(substream));
3239
3240 switch (event) {
3241 case SND_SOC_DAPM_PRE_PMU:
3242 if (source->driver->ops && source->driver->ops->hw_params) {
3243 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3244 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003245 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003246 if (ret != 0) {
3247 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003248 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003249 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003250 }
3251 }
3252
3253 if (sink->driver->ops && sink->driver->ops->hw_params) {
3254 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003255 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003256 sink);
3257 if (ret != 0) {
3258 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003259 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003260 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003261 }
3262 }
3263 break;
3264
3265 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003266 ret = snd_soc_dai_digital_mute(sink, 0,
3267 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003268 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003269 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003270 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003271 break;
3272
3273 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003274 ret = snd_soc_dai_digital_mute(sink, 1,
3275 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003276 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003277 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003278 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003279 break;
3280
3281 default:
3282 BUG();
3283 return -EINVAL;
3284 }
3285
Mark Brown9747cec2012-04-26 19:12:21 +01003286out:
3287 kfree(params);
3288 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003289}
3290
3291int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3292 const struct snd_soc_pcm_stream *params,
3293 struct snd_soc_dapm_widget *source,
3294 struct snd_soc_dapm_widget *sink)
3295{
3296 struct snd_soc_dapm_route routes[2];
3297 struct snd_soc_dapm_widget template;
3298 struct snd_soc_dapm_widget *w;
3299 size_t len;
3300 char *link_name;
3301
3302 len = strlen(source->name) + strlen(sink->name) + 2;
3303 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3304 if (!link_name)
3305 return -ENOMEM;
3306 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3307
3308 memset(&template, 0, sizeof(template));
3309 template.reg = SND_SOC_NOPM;
3310 template.id = snd_soc_dapm_dai_link;
3311 template.name = link_name;
3312 template.event = snd_soc_dai_link_event;
3313 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3314 SND_SOC_DAPM_PRE_PMD;
3315
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003316 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003317
3318 w = snd_soc_dapm_new_control(&card->dapm, &template);
3319 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003320 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003321 link_name);
3322 return -ENOMEM;
3323 }
3324
3325 w->params = params;
3326
3327 memset(&routes, 0, sizeof(routes));
3328
3329 routes[0].source = source->name;
3330 routes[0].sink = link_name;
3331 routes[1].source = link_name;
3332 routes[1].sink = sink->name;
3333
3334 return snd_soc_dapm_add_routes(&card->dapm, routes,
3335 ARRAY_SIZE(routes));
3336}
3337
Mark Brown888df392012-02-16 19:37:51 -08003338int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3339 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003340{
Mark Brown888df392012-02-16 19:37:51 -08003341 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003342 struct snd_soc_dapm_widget *w;
3343
Mark Brown888df392012-02-16 19:37:51 -08003344 WARN_ON(dapm->dev != dai->dev);
3345
3346 memset(&template, 0, sizeof(template));
3347 template.reg = SND_SOC_NOPM;
3348
3349 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003350 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003351 template.name = dai->driver->playback.stream_name;
3352 template.sname = dai->driver->playback.stream_name;
3353
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003354 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003355 template.name);
3356
3357 w = snd_soc_dapm_new_control(dapm, &template);
3358 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003359 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003360 dai->driver->playback.stream_name);
3361 }
3362
3363 w->priv = dai;
3364 dai->playback_widget = w;
3365 }
3366
3367 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003368 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003369 template.name = dai->driver->capture.stream_name;
3370 template.sname = dai->driver->capture.stream_name;
3371
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003372 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003373 template.name);
3374
3375 w = snd_soc_dapm_new_control(dapm, &template);
3376 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003377 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003378 dai->driver->capture.stream_name);
3379 }
3380
3381 w->priv = dai;
3382 dai->capture_widget = w;
3383 }
3384
3385 return 0;
3386}
3387
3388int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3389{
3390 struct snd_soc_dapm_widget *dai_w, *w;
3391 struct snd_soc_dai *dai;
3392 struct snd_soc_dapm_route r;
3393
3394 memset(&r, 0, sizeof(r));
3395
3396 /* For each DAI widget... */
3397 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003398 switch (dai_w->id) {
3399 case snd_soc_dapm_dai_in:
3400 case snd_soc_dapm_dai_out:
3401 break;
3402 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003403 continue;
Mark Brown46162742013-06-05 19:36:11 +01003404 }
Mark Brown888df392012-02-16 19:37:51 -08003405
3406 dai = dai_w->priv;
3407
3408 /* ...find all widgets with the same stream and link them */
3409 list_for_each_entry(w, &card->widgets, list) {
3410 if (w->dapm != dai_w->dapm)
3411 continue;
3412
Mark Brown46162742013-06-05 19:36:11 +01003413 switch (w->id) {
3414 case snd_soc_dapm_dai_in:
3415 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003416 continue;
Mark Brown46162742013-06-05 19:36:11 +01003417 default:
3418 break;
3419 }
Mark Brown888df392012-02-16 19:37:51 -08003420
3421 if (!w->sname)
3422 continue;
3423
3424 if (dai->driver->playback.stream_name &&
3425 strstr(w->sname,
3426 dai->driver->playback.stream_name)) {
3427 r.source = dai->playback_widget->name;
3428 r.sink = w->name;
3429 dev_dbg(dai->dev, "%s -> %s\n",
3430 r.source, r.sink);
3431
3432 snd_soc_dapm_add_route(w->dapm, &r);
3433 }
3434
3435 if (dai->driver->capture.stream_name &&
3436 strstr(w->sname,
3437 dai->driver->capture.stream_name)) {
3438 r.source = w->name;
3439 r.sink = dai->capture_widget->name;
3440 dev_dbg(dai->dev, "%s -> %s\n",
3441 r.source, r.sink);
3442
3443 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003444 }
3445 }
3446 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003447
Mark Brown888df392012-02-16 19:37:51 -08003448 return 0;
3449}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003450
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003451static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3452 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003453{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003454
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003455 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3456 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3457 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003458
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003459 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3460 w_cpu = cpu_dai->playback_widget;
3461 w_codec = codec_dai->playback_widget;
3462 } else {
3463 w_cpu = cpu_dai->capture_widget;
3464 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003465 }
3466
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003467 if (w_cpu) {
3468
3469 dapm_mark_dirty(w_cpu, "stream event");
3470
3471 switch (event) {
3472 case SND_SOC_DAPM_STREAM_START:
3473 w_cpu->active = 1;
3474 break;
3475 case SND_SOC_DAPM_STREAM_STOP:
3476 w_cpu->active = 0;
3477 break;
3478 case SND_SOC_DAPM_STREAM_SUSPEND:
3479 case SND_SOC_DAPM_STREAM_RESUME:
3480 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3481 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3482 break;
3483 }
3484 }
3485
3486 if (w_codec) {
3487
3488 dapm_mark_dirty(w_codec, "stream event");
3489
3490 switch (event) {
3491 case SND_SOC_DAPM_STREAM_START:
3492 w_codec->active = 1;
3493 break;
3494 case SND_SOC_DAPM_STREAM_STOP:
3495 w_codec->active = 0;
3496 break;
3497 case SND_SOC_DAPM_STREAM_SUSPEND:
3498 case SND_SOC_DAPM_STREAM_RESUME:
3499 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3500 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3501 break;
3502 }
3503 }
3504
3505 dapm_power_widgets(&rtd->card->dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003506}
3507
3508/**
3509 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3510 * @rtd: PCM runtime data
3511 * @stream: stream name
3512 * @event: stream event
3513 *
3514 * Sends a stream event to the dapm core. The core then makes any
3515 * necessary widget power changes.
3516 *
3517 * Returns 0 for success else error.
3518 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003519void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3520 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003521{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003522 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003523
Liam Girdwood3cd04342012-03-09 12:02:08 +00003524 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003525 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003526 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003527}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003528
3529/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003530 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003531 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003532 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003533 *
Mark Brown74b8f952009-06-06 11:26:15 +01003534 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003535 * a valid audio route and active audio stream.
3536 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3537 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003538 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003539int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003540{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003541 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003542}
Liam Girdwooda5302182008-07-07 13:35:17 +01003543EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003544
3545/**
Mark Brownda341832010-03-15 19:23:37 +00003546 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003547 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003548 * @pin: pin name
3549 *
3550 * Enables input/output pin regardless of any other state. This is
3551 * intended for use with microphone bias supplies used in microphone
3552 * jack detection.
3553 *
3554 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3555 * do any widget power switching.
3556 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003557int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3558 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003559{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003560 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003561
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003562 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003563 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003564 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003565 }
3566
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003567 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003568 w->connected = 1;
3569 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003570 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003571
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003572 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003573}
3574EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3575
3576/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003577 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003578 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003579 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003580 *
Mark Brown74b8f952009-06-06 11:26:15 +01003581 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003582 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3583 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003584 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003585int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3586 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003587{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003588 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003589}
3590EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3591
3592/**
Mark Brown5817b522008-09-24 11:23:11 +01003593 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003594 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003595 * @pin: pin name
3596 *
3597 * Marks the specified pin as being not connected, disabling it along
3598 * any parent or child widgets. At present this is identical to
3599 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3600 * additional things such as disabling controls which only affect
3601 * paths through the pin.
3602 *
3603 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3604 * do any widget power switching.
3605 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003606int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003607{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003608 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003609}
3610EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3611
3612/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003613 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003614 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003615 * @pin: audio signal pin endpoint (or start point)
3616 *
3617 * Get audio pin status - connected or disconnected.
3618 *
3619 * Returns 1 for connected otherwise 0.
3620 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003621int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3622 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003623{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003624 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003625
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003626 if (w)
3627 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003628
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003629 return 0;
3630}
Liam Girdwooda5302182008-07-07 13:35:17 +01003631EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003632
3633/**
Mark Brown1547aba2010-05-07 21:11:40 +01003634 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003635 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003636 * @pin: audio signal pin endpoint (or start point)
3637 *
3638 * Mark the given endpoint or pin as ignoring suspend. When the
3639 * system is disabled a path between two endpoints flagged as ignoring
3640 * suspend will not be disabled. The path must already be enabled via
3641 * normal means at suspend time, it will not be turned on if it was not
3642 * already enabled.
3643 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003644int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3645 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003646{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003647 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003648
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003649 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003650 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003651 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003652 }
3653
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003654 w->ignore_suspend = 1;
3655
3656 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003657}
3658EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3659
Stephen Warren16332812011-11-23 12:42:04 -07003660static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3661 struct snd_soc_dapm_widget *w)
3662{
3663 struct snd_soc_dapm_path *p;
3664
3665 list_for_each_entry(p, &card->paths, list) {
3666 if ((p->source == w) || (p->sink == w)) {
3667 dev_dbg(card->dev,
3668 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3669 p->source->name, p->source->id, p->source->dapm,
3670 p->sink->name, p->sink->id, p->sink->dapm);
3671
3672 /* Connected to something other than the codec */
3673 if (p->source->dapm != p->sink->dapm)
3674 return true;
3675 /*
3676 * Loopback connection from codec external pin to
3677 * codec external pin
3678 */
3679 if (p->sink->id == snd_soc_dapm_input) {
3680 switch (p->source->id) {
3681 case snd_soc_dapm_output:
3682 case snd_soc_dapm_micbias:
3683 return true;
3684 default:
3685 break;
3686 }
3687 }
3688 }
3689 }
3690
3691 return false;
3692}
3693
3694/**
3695 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3696 * @codec: The codec whose pins should be processed
3697 *
3698 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3699 * which are unused. Pins are used if they are connected externally to the
3700 * codec, whether that be to some other device, or a loop-back connection to
3701 * the codec itself.
3702 */
3703void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3704{
3705 struct snd_soc_card *card = codec->card;
3706 struct snd_soc_dapm_context *dapm = &codec->dapm;
3707 struct snd_soc_dapm_widget *w;
3708
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003709 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003710 &card->dapm, &codec->dapm);
3711
3712 list_for_each_entry(w, &card->widgets, list) {
3713 if (w->dapm != dapm)
3714 continue;
3715 switch (w->id) {
3716 case snd_soc_dapm_input:
3717 case snd_soc_dapm_output:
3718 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003719 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003720 w->name);
3721 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003722 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003723 "... Not in map; disabling\n");
3724 snd_soc_dapm_nc_pin(dapm, w->name);
3725 }
3726 break;
3727 default:
3728 break;
3729 }
3730 }
3731}
3732
Mark Brown1547aba2010-05-07 21:11:40 +01003733/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003734 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003735 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003736 *
3737 * Free all dapm widgets and resources.
3738 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003739void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003740{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003741 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003742 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003743 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003744 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003745}
3746EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3747
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003748static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003749{
Liam Girdwood01005a72012-07-06 16:57:05 +01003750 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003751 struct snd_soc_dapm_widget *w;
3752 LIST_HEAD(down_list);
3753 int powerdown = 0;
3754
Liam Girdwood01005a72012-07-06 16:57:05 +01003755 mutex_lock(&card->dapm_mutex);
3756
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003757 list_for_each_entry(w, &dapm->card->widgets, list) {
3758 if (w->dapm != dapm)
3759 continue;
Mark Brown51737472009-06-22 13:16:51 +01003760 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003761 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003762 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003763 powerdown = 1;
3764 }
3765 }
3766
3767 /* If there were no widgets to power down we're already in
3768 * standby.
3769 */
3770 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003771 if (dapm->bias_level == SND_SOC_BIAS_ON)
3772 snd_soc_dapm_set_bias_level(dapm,
3773 SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003774 dapm_seq_run(dapm, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003775 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3776 snd_soc_dapm_set_bias_level(dapm,
3777 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003778 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003779
3780 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003781}
Mark Brown51737472009-06-22 13:16:51 +01003782
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003783/*
3784 * snd_soc_dapm_shutdown - callback for system shutdown
3785 */
3786void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3787{
3788 struct snd_soc_codec *codec;
3789
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003790 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003791 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003792 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3793 snd_soc_dapm_set_bias_level(&codec->dapm,
3794 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003795 }
Mark Brown51737472009-06-22 13:16:51 +01003796}
3797
Richard Purdie2b97eab2006-10-06 18:32:18 +02003798/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003799MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003800MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3801MODULE_LICENSE("GPL");