blob: 758761146a42c59da5e8fa92386378fd88c35163 [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 Brown9949788b2010-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 Brown9949788b2010-05-07 20:24:05 +0100744
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000745 switch (level) {
Mark Brown9949788b2010-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 Brown9949788b2010-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 Browne56235e02011-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 Browne56235e02011-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,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02001985 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
1986 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001987{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001988 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001989 int ret;
1990
Liam Girdwood3cd04342012-03-09 12:02:08 +00001991 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02001992 dapm->update = update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001993 ret = soc_dapm_mux_update_power(dapm, kcontrol, mux, e);
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02001994 dapm->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001995 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001996 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001997 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001998 return ret;
1999}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002000EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002001
Milan plzik1b075e32008-01-10 14:39:46 +01002002/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002003static int soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Mark Brown283375c2009-12-07 18:09:03 +00002004 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002005{
2006 struct snd_soc_dapm_path *path;
2007 int found = 0;
2008
Richard Purdie2b97eab2006-10-06 18:32:18 +02002009 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002010 list_for_each_entry(path, &dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002011 if (path->kcontrol != kcontrol)
2012 continue;
2013
2014 /* found, now check type */
2015 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002016 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002017 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002018 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002019 }
2020
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002021 if (found)
2022 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002023
Liam Girdwood618dae12012-04-25 12:12:51 +01002024 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002025}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002026
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002027int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002028 struct snd_kcontrol *kcontrol, int connect,
2029 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002030{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002031 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002032 int ret;
2033
Liam Girdwood3cd04342012-03-09 12:02:08 +00002034 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002035 dapm->update = update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002036 ret = soc_dapm_mixer_update_power(dapm, kcontrol, connect);
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002037 dapm->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002038 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002039 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002040 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002041 return ret;
2042}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002043EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002044
2045/* show dapm widget status in sys fs */
2046static ssize_t dapm_widget_show(struct device *dev,
2047 struct device_attribute *attr, char *buf)
2048{
Mark Brown36ae1a92012-01-06 17:12:45 -08002049 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002050 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002051 struct snd_soc_dapm_widget *w;
2052 int count = 0;
2053 char *state = "not set";
2054
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002055 list_for_each_entry(w, &codec->card->widgets, list) {
2056 if (w->dapm != &codec->dapm)
2057 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002058
2059 /* only display widgets that burnm power */
2060 switch (w->id) {
2061 case snd_soc_dapm_hp:
2062 case snd_soc_dapm_mic:
2063 case snd_soc_dapm_spk:
2064 case snd_soc_dapm_line:
2065 case snd_soc_dapm_micbias:
2066 case snd_soc_dapm_dac:
2067 case snd_soc_dapm_adc:
2068 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002069 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002070 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002071 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002072 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002073 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002074 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002075 if (w->name)
2076 count += sprintf(buf + count, "%s: %s\n",
2077 w->name, w->power ? "On":"Off");
2078 break;
2079 default:
2080 break;
2081 }
2082 }
2083
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002084 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002085 case SND_SOC_BIAS_ON:
2086 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002087 break;
Mark Brown0be98982008-05-19 12:31:28 +02002088 case SND_SOC_BIAS_PREPARE:
2089 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002090 break;
Mark Brown0be98982008-05-19 12:31:28 +02002091 case SND_SOC_BIAS_STANDBY:
2092 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002093 break;
Mark Brown0be98982008-05-19 12:31:28 +02002094 case SND_SOC_BIAS_OFF:
2095 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002096 break;
2097 }
2098 count += sprintf(buf + count, "PM State: %s\n", state);
2099
2100 return count;
2101}
2102
2103static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2104
2105int snd_soc_dapm_sys_add(struct device *dev)
2106{
Troy Kisky12ef1932008-10-13 17:42:14 -07002107 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002108}
2109
2110static void snd_soc_dapm_sys_remove(struct device *dev)
2111{
Mark Brownaef90842009-05-16 17:53:16 +01002112 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002113}
2114
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002115static void dapm_free_path(struct snd_soc_dapm_path *path)
2116{
2117 list_del(&path->list_sink);
2118 list_del(&path->list_source);
2119 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002120 kfree(path);
2121}
2122
Richard Purdie2b97eab2006-10-06 18:32:18 +02002123/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002124static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002125{
2126 struct snd_soc_dapm_widget *w, *next_w;
2127 struct snd_soc_dapm_path *p, *next_p;
2128
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002129 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2130 if (w->dapm != dapm)
2131 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002132 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002133 /*
2134 * remove source and sink paths associated to this widget.
2135 * While removing the path, remove reference to it from both
2136 * source and sink widgets so that path is removed only once.
2137 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002138 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2139 dapm_free_path(p);
2140
2141 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2142 dapm_free_path(p);
2143
Stephen Warrenfad59882011-04-28 17:37:59 -06002144 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002145 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002146 kfree(w);
2147 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002148}
2149
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002150static struct snd_soc_dapm_widget *dapm_find_widget(
2151 struct snd_soc_dapm_context *dapm, const char *pin,
2152 bool search_other_contexts)
2153{
2154 struct snd_soc_dapm_widget *w;
2155 struct snd_soc_dapm_widget *fallback = NULL;
2156
2157 list_for_each_entry(w, &dapm->card->widgets, list) {
2158 if (!strcmp(w->name, pin)) {
2159 if (w->dapm == dapm)
2160 return w;
2161 else
2162 fallback = w;
2163 }
2164 }
2165
2166 if (search_other_contexts)
2167 return fallback;
2168
2169 return NULL;
2170}
2171
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002172static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002173 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002174{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002175 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002176
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002177 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002178 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002179 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002180 }
2181
Mark Brown1a8b2d92012-02-16 11:50:07 -08002182 if (w->connected != status)
2183 dapm_mark_dirty(w, "pin configuration");
2184
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002185 w->connected = status;
2186 if (status == 0)
2187 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002188
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002189 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002190}
2191
Richard Purdie2b97eab2006-10-06 18:32:18 +02002192/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002193 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002194 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002195 *
2196 * Walks all dapm audio paths and powers widgets according to their
2197 * stream or path usage.
2198 *
2199 * Returns 0 for success.
2200 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002201int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002202{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002203 int ret;
2204
Mark Brown4f4c0072011-10-07 14:29:19 +01002205 /*
2206 * Suppress early reports (eg, jacks syncing their state) to avoid
2207 * silly DAPM runs during card startup.
2208 */
2209 if (!dapm->card || !dapm->card->instantiated)
2210 return 0;
2211
Liam Girdwood3cd04342012-03-09 12:02:08 +00002212 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002213 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2214 mutex_unlock(&dapm->card->dapm_mutex);
2215 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002216}
Liam Girdwooda5302182008-07-07 13:35:17 +01002217EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002218
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002219static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002220 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002221{
2222 struct snd_soc_dapm_path *path;
2223 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002224 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002225 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002226 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002227 const char *source;
2228 char prefixed_sink[80];
2229 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002230 int ret = 0;
2231
Mark Brown88e8b9a2011-03-02 18:18:24 +00002232 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002233 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2234 dapm->codec->name_prefix, route->sink);
2235 sink = prefixed_sink;
2236 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2237 dapm->codec->name_prefix, route->source);
2238 source = prefixed_source;
2239 } else {
2240 sink = route->sink;
2241 source = route->source;
2242 }
2243
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002244 /*
2245 * find src and dest widgets over all widgets but favor a widget from
2246 * current DAPM context
2247 */
2248 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002249 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002250 wtsink = w;
2251 if (w->dapm == dapm)
2252 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002253 continue;
2254 }
2255 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002256 wtsource = w;
2257 if (w->dapm == dapm)
2258 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002259 }
2260 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002261 /* use widget from another DAPM context if not found from this */
2262 if (!wsink)
2263 wsink = wtsink;
2264 if (!wsource)
2265 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002266
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002267 if (wsource == NULL) {
2268 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2269 route->source);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002270 return -ENODEV;
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002271 }
2272 if (wsink == NULL) {
2273 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2274 route->sink);
2275 return -ENODEV;
2276 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002277
2278 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2279 if (!path)
2280 return -ENOMEM;
2281
2282 path->source = wsource;
2283 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002284 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002285 INIT_LIST_HEAD(&path->list);
2286 INIT_LIST_HEAD(&path->list_source);
2287 INIT_LIST_HEAD(&path->list_sink);
2288
2289 /* check for external widgets */
2290 if (wsink->id == snd_soc_dapm_input) {
2291 if (wsource->id == snd_soc_dapm_micbias ||
2292 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002293 wsource->id == snd_soc_dapm_line ||
2294 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002295 wsink->ext = 1;
2296 }
2297 if (wsource->id == snd_soc_dapm_output) {
2298 if (wsink->id == snd_soc_dapm_spk ||
2299 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002300 wsink->id == snd_soc_dapm_line ||
2301 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002302 wsource->ext = 1;
2303 }
2304
2305 /* connect static paths */
2306 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002307 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002308 list_add(&path->list_sink, &wsink->sources);
2309 list_add(&path->list_source, &wsource->sinks);
2310 path->connect = 1;
2311 return 0;
2312 }
2313
2314 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002315 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002316 case snd_soc_dapm_adc:
2317 case snd_soc_dapm_dac:
2318 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002319 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002320 case snd_soc_dapm_input:
2321 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002322 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002323 case snd_soc_dapm_micbias:
2324 case snd_soc_dapm_vmid:
2325 case snd_soc_dapm_pre:
2326 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002327 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002328 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002329 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002330 case snd_soc_dapm_aif_in:
2331 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002332 case snd_soc_dapm_dai_in:
2333 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002334 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002335 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002336 list_add(&path->list_sink, &wsink->sources);
2337 list_add(&path->list_source, &wsource->sinks);
2338 path->connect = 1;
2339 return 0;
2340 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002341 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002342 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002343 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002344 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002345 if (ret != 0)
2346 goto err;
2347 break;
2348 case snd_soc_dapm_switch:
2349 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002350 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002351 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002352 if (ret != 0)
2353 goto err;
2354 break;
2355 case snd_soc_dapm_hp:
2356 case snd_soc_dapm_mic:
2357 case snd_soc_dapm_line:
2358 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002359 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002360 list_add(&path->list_sink, &wsink->sources);
2361 list_add(&path->list_source, &wsource->sinks);
2362 path->connect = 0;
2363 return 0;
2364 }
Mark Brownfabd0382012-07-05 17:20:06 +01002365
2366 dapm_mark_dirty(wsource, "Route added");
2367 dapm_mark_dirty(wsink, "Route added");
2368
Richard Purdie2b97eab2006-10-06 18:32:18 +02002369 return 0;
2370
2371err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002372 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002373 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002374 kfree(path);
2375 return ret;
2376}
Mark Brown105f1c22008-05-13 14:52:19 +02002377
Mark Brownefcc3c62012-07-05 17:24:19 +01002378static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2379 const struct snd_soc_dapm_route *route)
2380{
2381 struct snd_soc_dapm_path *path, *p;
2382 const char *sink;
2383 const char *source;
2384 char prefixed_sink[80];
2385 char prefixed_source[80];
2386
2387 if (route->control) {
2388 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002389 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002390 return -EINVAL;
2391 }
2392
2393 if (dapm->codec && dapm->codec->name_prefix) {
2394 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2395 dapm->codec->name_prefix, route->sink);
2396 sink = prefixed_sink;
2397 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2398 dapm->codec->name_prefix, route->source);
2399 source = prefixed_source;
2400 } else {
2401 sink = route->sink;
2402 source = route->source;
2403 }
2404
2405 path = NULL;
2406 list_for_each_entry(p, &dapm->card->paths, list) {
2407 if (strcmp(p->source->name, source) != 0)
2408 continue;
2409 if (strcmp(p->sink->name, sink) != 0)
2410 continue;
2411 path = p;
2412 break;
2413 }
2414
2415 if (path) {
2416 dapm_mark_dirty(path->source, "Route removed");
2417 dapm_mark_dirty(path->sink, "Route removed");
2418
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002419 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002420 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002421 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002422 source, sink);
2423 }
2424
2425 return 0;
2426}
2427
Mark Brown105f1c22008-05-13 14:52:19 +02002428/**
Mark Brown105f1c22008-05-13 14:52:19 +02002429 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002430 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002431 * @route: audio routes
2432 * @num: number of routes
2433 *
2434 * Connects 2 dapm widgets together via a named audio path. The sink is
2435 * the widget receiving the audio signal, whilst the source is the sender
2436 * of the audio signal.
2437 *
2438 * Returns 0 for success else error. On error all resources can be freed
2439 * with a call to snd_soc_card_free().
2440 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002441int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002442 const struct snd_soc_dapm_route *route, int num)
2443{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002444 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002445
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002446 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002447 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002448 r = snd_soc_dapm_add_route(dapm, route);
2449 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002450 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2451 route->source,
2452 route->control ? route->control : "direct",
2453 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002454 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002455 }
2456 route++;
2457 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002458 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002459
Dan Carpenter60884c22012-04-13 22:25:43 +03002460 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002461}
2462EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2463
Mark Brownefcc3c62012-07-05 17:24:19 +01002464/**
2465 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2466 * @dapm: DAPM context
2467 * @route: audio routes
2468 * @num: number of routes
2469 *
2470 * Removes routes from the DAPM context.
2471 */
2472int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2473 const struct snd_soc_dapm_route *route, int num)
2474{
2475 int i, ret = 0;
2476
2477 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2478 for (i = 0; i < num; i++) {
2479 snd_soc_dapm_del_route(dapm, route);
2480 route++;
2481 }
2482 mutex_unlock(&dapm->card->dapm_mutex);
2483
2484 return ret;
2485}
2486EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2487
Mark Brownbf3a9e12011-06-13 16:42:29 +01002488static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2489 const struct snd_soc_dapm_route *route)
2490{
2491 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2492 route->source,
2493 true);
2494 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2495 route->sink,
2496 true);
2497 struct snd_soc_dapm_path *path;
2498 int count = 0;
2499
2500 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002501 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002502 route->source);
2503 return -ENODEV;
2504 }
2505
2506 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002507 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002508 route->sink);
2509 return -ENODEV;
2510 }
2511
2512 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002513 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002514 route->source, route->sink);
2515
2516 list_for_each_entry(path, &source->sinks, list_source) {
2517 if (path->sink == sink) {
2518 path->weak = 1;
2519 count++;
2520 }
2521 }
2522
2523 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002524 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002525 route->source, route->sink);
2526 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002527 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002528 count, route->source, route->sink);
2529
2530 return 0;
2531}
2532
2533/**
2534 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2535 * @dapm: DAPM context
2536 * @route: audio routes
2537 * @num: number of routes
2538 *
2539 * Mark existing routes matching those specified in the passed array
2540 * as being weak, meaning that they are ignored for the purpose of
2541 * power decisions. The main intended use case is for sidetone paths
2542 * which couple audio between other independent paths if they are both
2543 * active in order to make the combination work better at the user
2544 * level but which aren't intended to be "used".
2545 *
2546 * Note that CODEC drivers should not use this as sidetone type paths
2547 * can frequently also be used as bypass paths.
2548 */
2549int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2550 const struct snd_soc_dapm_route *route, int num)
2551{
2552 int i, err;
2553 int ret = 0;
2554
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002555 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002556 for (i = 0; i < num; i++) {
2557 err = snd_soc_dapm_weak_route(dapm, route);
2558 if (err)
2559 ret = err;
2560 route++;
2561 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002562 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002563
2564 return ret;
2565}
2566EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2567
Mark Brown105f1c22008-05-13 14:52:19 +02002568/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002569 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002570 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002571 *
2572 * Checks the codec for any new dapm widgets and creates them if found.
2573 *
2574 * Returns 0 for success.
2575 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002576int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002577{
2578 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002579 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002580
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002581 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2582
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002583 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002584 {
2585 if (w->new)
2586 continue;
2587
Stephen Warrenfad59882011-04-28 17:37:59 -06002588 if (w->num_kcontrols) {
2589 w->kcontrols = kzalloc(w->num_kcontrols *
2590 sizeof(struct snd_kcontrol *),
2591 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002592 if (!w->kcontrols) {
2593 mutex_unlock(&dapm->card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002594 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002595 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002596 }
2597
Richard Purdie2b97eab2006-10-06 18:32:18 +02002598 switch(w->id) {
2599 case snd_soc_dapm_switch:
2600 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002601 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002602 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002603 break;
2604 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002605 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002606 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002607 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002608 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002609 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002610 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002611 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002612 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002613 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002614 break;
2615 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002616
2617 /* Read the initial power state from the device */
2618 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002619 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002620 val &= 1 << w->shift;
2621 if (w->invert)
2622 val = !val;
2623
2624 if (val)
2625 w->power = 1;
2626 }
2627
Richard Purdie2b97eab2006-10-06 18:32:18 +02002628 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002629
Mark Brown7508b122011-10-05 12:09:12 +01002630 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002631 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002632 }
2633
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002634 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002635 mutex_unlock(&dapm->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002636 return 0;
2637}
2638EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2639
2640/**
2641 * snd_soc_dapm_get_volsw - dapm mixer get callback
2642 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002643 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002644 *
2645 * Callback to get the value of a dapm mixer control.
2646 *
2647 * Returns 0 for success.
2648 */
2649int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_value *ucontrol)
2651{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002652 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2653 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002654 struct soc_mixer_control *mc =
2655 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002656 unsigned int reg = mc->reg;
2657 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002658 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002659 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002660 unsigned int invert = mc->invert;
2661
2662 if (snd_soc_volsw_is_stereo(mc))
2663 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002664 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002665 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002666
Richard Purdie2b97eab2006-10-06 18:32:18 +02002667 ucontrol->value.integer.value[0] =
2668 (snd_soc_read(widget->codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002669 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002670 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002671 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002672
2673 return 0;
2674}
2675EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2676
2677/**
2678 * snd_soc_dapm_put_volsw - dapm mixer set callback
2679 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002680 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002681 *
2682 * Callback to set the value of a dapm mixer control.
2683 *
2684 * Returns 0 for success.
2685 */
2686int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2687 struct snd_ctl_elem_value *ucontrol)
2688{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002689 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2690 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2691 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002692 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002693 struct soc_mixer_control *mc =
2694 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002695 unsigned int reg = mc->reg;
2696 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002697 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002698 unsigned int mask = (1 << fls(max)) - 1;
2699 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002700 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002701 int connect, change;
2702 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002703
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002704 if (snd_soc_volsw_is_stereo(mc))
2705 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002706 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002707 kcontrol->id.name);
2708
Richard Purdie2b97eab2006-10-06 18:32:18 +02002709 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002710 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002711
2712 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002713 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002714 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002715 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002716
Liam Girdwood3cd04342012-03-09 12:02:08 +00002717 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002718
Stephen Warrene9cf7042011-01-27 14:54:05 -07002719 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002720 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002721 update.kcontrol = kcontrol;
2722 update.reg = reg;
2723 update.mask = mask;
2724 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002725
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002726 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002727
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002728 soc_dapm_mixer_update_power(widget->dapm, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002729
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002730 widget->dapm->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002731 }
2732
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002733 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002734 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002735}
2736EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2737
2738/**
2739 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2740 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002741 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002742 *
2743 * Callback to get the value of a dapm enumerated double mixer control.
2744 *
2745 * Returns 0 for success.
2746 */
2747int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2748 struct snd_ctl_elem_value *ucontrol)
2749{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002750 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2751 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002752 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002753 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002754
Richard Purdie2b97eab2006-10-06 18:32:18 +02002755 val = snd_soc_read(widget->codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002756 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002757 if (e->shift_l != e->shift_r)
2758 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002759 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002760
2761 return 0;
2762}
2763EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2764
2765/**
2766 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2767 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002768 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002769 *
2770 * Callback to set the value of a dapm enumerated double mixer control.
2771 *
2772 * Returns 0 for success.
2773 */
2774int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2775 struct snd_ctl_elem_value *ucontrol)
2776{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002777 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2778 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2779 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002780 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002781 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002782 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002783 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002784 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002785
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002786 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002787 return -EINVAL;
2788 mux = ucontrol->value.enumerated.item[0];
2789 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002790 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002791 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002792 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002793 return -EINVAL;
2794 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002795 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002796 }
2797
Liam Girdwood3cd04342012-03-09 12:02:08 +00002798 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002799
Mark Brown3a655772009-10-05 17:23:30 +01002800 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002801 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002802 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002803
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002804 update.kcontrol = kcontrol;
2805 update.reg = e->reg;
2806 update.mask = mask;
2807 update.val = val;
2808 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002809
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002810 soc_dapm_mux_update_power(widget->dapm, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002811
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002812 widget->dapm->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002813 }
2814
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002815 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002816 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002817}
2818EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2819
2820/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002821 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2822 * @kcontrol: mixer control
2823 * @ucontrol: control element information
2824 *
2825 * Returns 0 for success.
2826 */
2827int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2828 struct snd_ctl_elem_value *ucontrol)
2829{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002830 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2831 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002832
2833 ucontrol->value.enumerated.item[0] = widget->value;
2834
2835 return 0;
2836}
2837EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2838
2839/**
2840 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2841 * @kcontrol: mixer control
2842 * @ucontrol: control element information
2843 *
2844 * Returns 0 for success.
2845 */
2846int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2847 struct snd_ctl_elem_value *ucontrol)
2848{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002849 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2850 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2851 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002852 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002853 struct soc_enum *e =
2854 (struct soc_enum *)kcontrol->private_value;
2855 int change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002856
2857 if (ucontrol->value.enumerated.item[0] >= e->max)
2858 return -EINVAL;
2859
Liam Girdwood3cd04342012-03-09 12:02:08 +00002860 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002861
2862 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002863 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002864 widget->value = ucontrol->value.enumerated.item[0];
2865 soc_dapm_mux_update_power(widget->dapm, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002866 }
2867
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002868 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002869 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002870}
2871EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2872
2873/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002874 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2875 * callback
2876 * @kcontrol: mixer control
2877 * @ucontrol: control element information
2878 *
2879 * Callback to get the value of a dapm semi enumerated double mixer control.
2880 *
2881 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2882 * used for handling bitfield coded enumeration for example.
2883 *
2884 * Returns 0 for success.
2885 */
2886int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2887 struct snd_ctl_elem_value *ucontrol)
2888{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002889 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2890 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002891 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002892 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002893
2894 reg_val = snd_soc_read(widget->codec, e->reg);
2895 val = (reg_val >> e->shift_l) & e->mask;
2896 for (mux = 0; mux < e->max; mux++) {
2897 if (val == e->values[mux])
2898 break;
2899 }
2900 ucontrol->value.enumerated.item[0] = mux;
2901 if (e->shift_l != e->shift_r) {
2902 val = (reg_val >> e->shift_r) & e->mask;
2903 for (mux = 0; mux < e->max; mux++) {
2904 if (val == e->values[mux])
2905 break;
2906 }
2907 ucontrol->value.enumerated.item[1] = mux;
2908 }
2909
2910 return 0;
2911}
2912EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2913
2914/**
2915 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2916 * callback
2917 * @kcontrol: mixer control
2918 * @ucontrol: control element information
2919 *
2920 * Callback to set the value of a dapm semi enumerated double mixer control.
2921 *
2922 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2923 * used for handling bitfield coded enumeration for example.
2924 *
2925 * Returns 0 for success.
2926 */
2927int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2928 struct snd_ctl_elem_value *ucontrol)
2929{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002930 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2931 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2932 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002933 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002934 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002935 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002936 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002937 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002938
2939 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2940 return -EINVAL;
2941 mux = ucontrol->value.enumerated.item[0];
2942 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2943 mask = e->mask << e->shift_l;
2944 if (e->shift_l != e->shift_r) {
2945 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2946 return -EINVAL;
2947 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2948 mask |= e->mask << e->shift_r;
2949 }
2950
Liam Girdwood3cd04342012-03-09 12:02:08 +00002951 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002952
Mark Brown3a655772009-10-05 17:23:30 +01002953 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002954 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002955 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002956
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002957 update.kcontrol = kcontrol;
2958 update.reg = e->reg;
2959 update.mask = mask;
2960 update.val = val;
2961 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002962
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002963 soc_dapm_mux_update_power(widget->dapm, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002964
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002965 widget->dapm->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002966 }
2967
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002968 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002969 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002970}
2971EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2972
2973/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002974 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2975 *
2976 * @kcontrol: mixer control
2977 * @uinfo: control element information
2978 *
2979 * Callback to provide information about a pin switch control.
2980 */
2981int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2982 struct snd_ctl_elem_info *uinfo)
2983{
2984 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2985 uinfo->count = 1;
2986 uinfo->value.integer.min = 0;
2987 uinfo->value.integer.max = 1;
2988
2989 return 0;
2990}
2991EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2992
2993/**
2994 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2995 *
2996 * @kcontrol: mixer control
2997 * @ucontrol: Value
2998 */
2999int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3000 struct snd_ctl_elem_value *ucontrol)
3001{
Mark Brown48a8c392012-02-14 17:11:15 -08003002 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003003 const char *pin = (const char *)kcontrol->private_value;
3004
Liam Girdwood3cd04342012-03-09 12:02:08 +00003005 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003006
3007 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003008 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003009
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003010 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003011
3012 return 0;
3013}
3014EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3015
3016/**
3017 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3018 *
3019 * @kcontrol: mixer control
3020 * @ucontrol: Value
3021 */
3022int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3023 struct snd_ctl_elem_value *ucontrol)
3024{
Mark Brown48a8c392012-02-14 17:11:15 -08003025 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003026 const char *pin = (const char *)kcontrol->private_value;
3027
Liam Girdwood3cd04342012-03-09 12:02:08 +00003028 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003029
3030 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003031 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003032 else
Mark Brown48a8c392012-02-14 17:11:15 -08003033 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003034
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003035 mutex_unlock(&card->dapm_mutex);
3036
Mark Brown48a8c392012-02-14 17:11:15 -08003037 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003038 return 0;
3039}
3040EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3041
Mark Brown5ba06fc2012-02-16 11:07:13 -08003042static struct snd_soc_dapm_widget *
3043snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3044 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003045{
3046 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003047 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003048
3049 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003050 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003051
Mark Brown62ea8742012-01-21 21:14:48 +00003052 switch (w->id) {
3053 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003054 w->regulator = devm_regulator_get(dapm->dev, w->name);
3055 if (IS_ERR(w->regulator)) {
3056 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003057 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003058 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003059 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003060 }
Mark Brown8784c772013-01-10 19:33:47 +00003061
3062 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
3063 ret = regulator_allow_bypass(w->regulator, true);
3064 if (ret != 0)
3065 dev_warn(w->dapm->dev,
3066 "ASoC: Failed to unbypass %s: %d\n",
3067 w->name, ret);
3068 }
Mark Brown62ea8742012-01-21 21:14:48 +00003069 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003070 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003071#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003072 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003073 if (IS_ERR(w->clk)) {
3074 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003075 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003076 w->name, ret);
3077 return NULL;
3078 }
Mark Brownec029952012-06-04 08:16:20 +01003079#else
3080 return NULL;
3081#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003082 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003083 default:
3084 break;
3085 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003086
Mark Brown88e8b9a2011-03-02 18:18:24 +00003087 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003088 w->name = kasprintf(GFP_KERNEL, "%s %s",
3089 dapm->codec->name_prefix, widget->name);
3090 else
3091 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3092
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003093 if (w->name == NULL) {
3094 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003095 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003096 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003097
Mark Brown7ca3a182011-10-08 14:04:50 +01003098 switch (w->id) {
3099 case snd_soc_dapm_switch:
3100 case snd_soc_dapm_mixer:
3101 case snd_soc_dapm_mixer_named_ctl:
3102 w->power_check = dapm_generic_check_power;
3103 break;
3104 case snd_soc_dapm_mux:
3105 case snd_soc_dapm_virt_mux:
3106 case snd_soc_dapm_value_mux:
3107 w->power_check = dapm_generic_check_power;
3108 break;
Mark Brown46162742013-06-05 19:36:11 +01003109 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003110 w->power_check = dapm_adc_check_power;
3111 break;
Mark Brown46162742013-06-05 19:36:11 +01003112 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003113 w->power_check = dapm_dac_check_power;
3114 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003115 case snd_soc_dapm_adc:
3116 case snd_soc_dapm_aif_out:
3117 case snd_soc_dapm_dac:
3118 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003119 case snd_soc_dapm_pga:
3120 case snd_soc_dapm_out_drv:
3121 case snd_soc_dapm_input:
3122 case snd_soc_dapm_output:
3123 case snd_soc_dapm_micbias:
3124 case snd_soc_dapm_spk:
3125 case snd_soc_dapm_hp:
3126 case snd_soc_dapm_mic:
3127 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003128 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003129 w->power_check = dapm_generic_check_power;
3130 break;
3131 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003132 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003133 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003134 w->power_check = dapm_supply_check_power;
3135 break;
3136 default:
3137 w->power_check = dapm_always_on_check_power;
3138 break;
3139 }
3140
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003141 w->dapm = dapm;
3142 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003143 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003144 INIT_LIST_HEAD(&w->sources);
3145 INIT_LIST_HEAD(&w->sinks);
3146 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003147 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003148 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003149
3150 /* machine layer set ups unconnected pins and insertions */
3151 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003152 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003153}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003154
3155/**
Mark Brown4ba13272008-05-13 14:51:19 +02003156 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003157 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003158 * @widget: widget array
3159 * @num: number of widgets
3160 *
3161 * Creates new DAPM controls based upon the templates.
3162 *
3163 * Returns 0 for success else error.
3164 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003165int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003166 const struct snd_soc_dapm_widget *widget,
3167 int num)
3168{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003169 struct snd_soc_dapm_widget *w;
3170 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003171 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003172
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003173 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003174 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003175 w = snd_soc_dapm_new_control(dapm, widget);
3176 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003177 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003178 "ASoC: Failed to create DAPM control %s\n",
3179 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003180 ret = -ENOMEM;
3181 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003182 }
Mark Brown4ba13272008-05-13 14:51:19 +02003183 widget++;
3184 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003185 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003186 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003187}
3188EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3189
Mark Brownc74184e2012-04-04 22:12:09 +01003190static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3191 struct snd_kcontrol *kcontrol, int event)
3192{
3193 struct snd_soc_dapm_path *source_p, *sink_p;
3194 struct snd_soc_dai *source, *sink;
3195 const struct snd_soc_pcm_stream *config = w->params;
3196 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003197 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003198 u64 fmt;
3199 int ret;
3200
3201 BUG_ON(!config);
3202 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3203
3204 /* We only support a single source and sink, pick the first */
3205 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3206 list_sink);
3207 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3208 list_source);
3209
3210 BUG_ON(!source_p || !sink_p);
3211 BUG_ON(!sink_p->source || !source_p->sink);
3212 BUG_ON(!source_p->source || !sink_p->sink);
3213
3214 source = source_p->source->priv;
3215 sink = sink_p->sink->priv;
3216
3217 /* Be a little careful as we don't want to overflow the mask array */
3218 if (config->formats) {
3219 fmt = ffs(config->formats) - 1;
3220 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003221 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003222 config->formats);
3223 fmt = 0;
3224 }
3225
3226 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003227 params = kzalloc(sizeof(*params), GFP_KERNEL);
3228 if (!params) {
3229 ret = -ENOMEM;
3230 goto out;
3231 }
3232 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003233
Mark Brown9747cec2012-04-26 19:12:21 +01003234 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003235 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003236 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003237 config->rate_max;
3238
Mark Brown9747cec2012-04-26 19:12:21 +01003239 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003240 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003241 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003242 = config->channels_max;
3243
3244 memset(&substream, 0, sizeof(substream));
3245
3246 switch (event) {
3247 case SND_SOC_DAPM_PRE_PMU:
3248 if (source->driver->ops && source->driver->ops->hw_params) {
3249 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3250 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003251 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003252 if (ret != 0) {
3253 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003254 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003255 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003256 }
3257 }
3258
3259 if (sink->driver->ops && sink->driver->ops->hw_params) {
3260 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003261 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003262 sink);
3263 if (ret != 0) {
3264 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003265 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003266 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003267 }
3268 }
3269 break;
3270
3271 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003272 ret = snd_soc_dai_digital_mute(sink, 0,
3273 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003274 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003275 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003276 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003277 break;
3278
3279 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003280 ret = snd_soc_dai_digital_mute(sink, 1,
3281 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003282 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003283 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003284 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003285 break;
3286
3287 default:
3288 BUG();
3289 return -EINVAL;
3290 }
3291
Mark Brown9747cec2012-04-26 19:12:21 +01003292out:
3293 kfree(params);
3294 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003295}
3296
3297int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3298 const struct snd_soc_pcm_stream *params,
3299 struct snd_soc_dapm_widget *source,
3300 struct snd_soc_dapm_widget *sink)
3301{
3302 struct snd_soc_dapm_route routes[2];
3303 struct snd_soc_dapm_widget template;
3304 struct snd_soc_dapm_widget *w;
3305 size_t len;
3306 char *link_name;
3307
3308 len = strlen(source->name) + strlen(sink->name) + 2;
3309 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3310 if (!link_name)
3311 return -ENOMEM;
3312 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3313
3314 memset(&template, 0, sizeof(template));
3315 template.reg = SND_SOC_NOPM;
3316 template.id = snd_soc_dapm_dai_link;
3317 template.name = link_name;
3318 template.event = snd_soc_dai_link_event;
3319 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3320 SND_SOC_DAPM_PRE_PMD;
3321
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003322 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003323
3324 w = snd_soc_dapm_new_control(&card->dapm, &template);
3325 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003326 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003327 link_name);
3328 return -ENOMEM;
3329 }
3330
3331 w->params = params;
3332
3333 memset(&routes, 0, sizeof(routes));
3334
3335 routes[0].source = source->name;
3336 routes[0].sink = link_name;
3337 routes[1].source = link_name;
3338 routes[1].sink = sink->name;
3339
3340 return snd_soc_dapm_add_routes(&card->dapm, routes,
3341 ARRAY_SIZE(routes));
3342}
3343
Mark Brown888df392012-02-16 19:37:51 -08003344int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3345 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003346{
Mark Brown888df392012-02-16 19:37:51 -08003347 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003348 struct snd_soc_dapm_widget *w;
3349
Mark Brown888df392012-02-16 19:37:51 -08003350 WARN_ON(dapm->dev != dai->dev);
3351
3352 memset(&template, 0, sizeof(template));
3353 template.reg = SND_SOC_NOPM;
3354
3355 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003356 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003357 template.name = dai->driver->playback.stream_name;
3358 template.sname = dai->driver->playback.stream_name;
3359
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003360 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003361 template.name);
3362
3363 w = snd_soc_dapm_new_control(dapm, &template);
3364 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003365 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003366 dai->driver->playback.stream_name);
3367 }
3368
3369 w->priv = dai;
3370 dai->playback_widget = w;
3371 }
3372
3373 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003374 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003375 template.name = dai->driver->capture.stream_name;
3376 template.sname = dai->driver->capture.stream_name;
3377
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003378 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003379 template.name);
3380
3381 w = snd_soc_dapm_new_control(dapm, &template);
3382 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003383 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003384 dai->driver->capture.stream_name);
3385 }
3386
3387 w->priv = dai;
3388 dai->capture_widget = w;
3389 }
3390
3391 return 0;
3392}
3393
3394int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3395{
3396 struct snd_soc_dapm_widget *dai_w, *w;
3397 struct snd_soc_dai *dai;
3398 struct snd_soc_dapm_route r;
3399
3400 memset(&r, 0, sizeof(r));
3401
3402 /* For each DAI widget... */
3403 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003404 switch (dai_w->id) {
3405 case snd_soc_dapm_dai_in:
3406 case snd_soc_dapm_dai_out:
3407 break;
3408 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003409 continue;
Mark Brown46162742013-06-05 19:36:11 +01003410 }
Mark Brown888df392012-02-16 19:37:51 -08003411
3412 dai = dai_w->priv;
3413
3414 /* ...find all widgets with the same stream and link them */
3415 list_for_each_entry(w, &card->widgets, list) {
3416 if (w->dapm != dai_w->dapm)
3417 continue;
3418
Mark Brown46162742013-06-05 19:36:11 +01003419 switch (w->id) {
3420 case snd_soc_dapm_dai_in:
3421 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003422 continue;
Mark Brown46162742013-06-05 19:36:11 +01003423 default:
3424 break;
3425 }
Mark Brown888df392012-02-16 19:37:51 -08003426
3427 if (!w->sname)
3428 continue;
3429
3430 if (dai->driver->playback.stream_name &&
3431 strstr(w->sname,
3432 dai->driver->playback.stream_name)) {
3433 r.source = dai->playback_widget->name;
3434 r.sink = w->name;
3435 dev_dbg(dai->dev, "%s -> %s\n",
3436 r.source, r.sink);
3437
3438 snd_soc_dapm_add_route(w->dapm, &r);
3439 }
3440
3441 if (dai->driver->capture.stream_name &&
3442 strstr(w->sname,
3443 dai->driver->capture.stream_name)) {
3444 r.source = w->name;
3445 r.sink = dai->capture_widget->name;
3446 dev_dbg(dai->dev, "%s -> %s\n",
3447 r.source, r.sink);
3448
3449 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003450 }
3451 }
3452 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003453
Mark Brown888df392012-02-16 19:37:51 -08003454 return 0;
3455}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003456
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003457static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3458 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003459{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003460
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003461 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3462 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3463 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003464
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003465 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3466 w_cpu = cpu_dai->playback_widget;
3467 w_codec = codec_dai->playback_widget;
3468 } else {
3469 w_cpu = cpu_dai->capture_widget;
3470 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003471 }
3472
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003473 if (w_cpu) {
3474
3475 dapm_mark_dirty(w_cpu, "stream event");
3476
3477 switch (event) {
3478 case SND_SOC_DAPM_STREAM_START:
3479 w_cpu->active = 1;
3480 break;
3481 case SND_SOC_DAPM_STREAM_STOP:
3482 w_cpu->active = 0;
3483 break;
3484 case SND_SOC_DAPM_STREAM_SUSPEND:
3485 case SND_SOC_DAPM_STREAM_RESUME:
3486 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3487 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3488 break;
3489 }
3490 }
3491
3492 if (w_codec) {
3493
3494 dapm_mark_dirty(w_codec, "stream event");
3495
3496 switch (event) {
3497 case SND_SOC_DAPM_STREAM_START:
3498 w_codec->active = 1;
3499 break;
3500 case SND_SOC_DAPM_STREAM_STOP:
3501 w_codec->active = 0;
3502 break;
3503 case SND_SOC_DAPM_STREAM_SUSPEND:
3504 case SND_SOC_DAPM_STREAM_RESUME:
3505 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3506 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3507 break;
3508 }
3509 }
3510
3511 dapm_power_widgets(&rtd->card->dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003512}
3513
3514/**
3515 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3516 * @rtd: PCM runtime data
3517 * @stream: stream name
3518 * @event: stream event
3519 *
3520 * Sends a stream event to the dapm core. The core then makes any
3521 * necessary widget power changes.
3522 *
3523 * Returns 0 for success else error.
3524 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003525void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3526 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003527{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003528 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003529
Liam Girdwood3cd04342012-03-09 12:02:08 +00003530 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003531 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003532 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003533}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003534
3535/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003536 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003537 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003538 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003539 *
Mark Brown74b8f952009-06-06 11:26:15 +01003540 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003541 * a valid audio route and active audio stream.
3542 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3543 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003544 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003545int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003546{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003547 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003548}
Liam Girdwooda5302182008-07-07 13:35:17 +01003549EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003550
3551/**
Mark Brownda341832010-03-15 19:23:37 +00003552 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003553 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003554 * @pin: pin name
3555 *
3556 * Enables input/output pin regardless of any other state. This is
3557 * intended for use with microphone bias supplies used in microphone
3558 * jack detection.
3559 *
3560 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3561 * do any widget power switching.
3562 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003563int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3564 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003565{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003566 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003567
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003568 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003569 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003570 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003571 }
3572
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003573 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003574 w->connected = 1;
3575 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003576 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003577
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003578 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003579}
3580EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3581
3582/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003583 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003584 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003585 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003586 *
Mark Brown74b8f952009-06-06 11:26:15 +01003587 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003588 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3589 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003590 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003591int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3592 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003593{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003594 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003595}
3596EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3597
3598/**
Mark Brown5817b522008-09-24 11:23:11 +01003599 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003600 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003601 * @pin: pin name
3602 *
3603 * Marks the specified pin as being not connected, disabling it along
3604 * any parent or child widgets. At present this is identical to
3605 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3606 * additional things such as disabling controls which only affect
3607 * paths through the pin.
3608 *
3609 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3610 * do any widget power switching.
3611 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003612int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003613{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003614 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003615}
3616EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3617
3618/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003619 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003620 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003621 * @pin: audio signal pin endpoint (or start point)
3622 *
3623 * Get audio pin status - connected or disconnected.
3624 *
3625 * Returns 1 for connected otherwise 0.
3626 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003627int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3628 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003629{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003630 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003631
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003632 if (w)
3633 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003634
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003635 return 0;
3636}
Liam Girdwooda5302182008-07-07 13:35:17 +01003637EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003638
3639/**
Mark Brown1547aba2010-05-07 21:11:40 +01003640 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003641 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003642 * @pin: audio signal pin endpoint (or start point)
3643 *
3644 * Mark the given endpoint or pin as ignoring suspend. When the
3645 * system is disabled a path between two endpoints flagged as ignoring
3646 * suspend will not be disabled. The path must already be enabled via
3647 * normal means at suspend time, it will not be turned on if it was not
3648 * already enabled.
3649 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003650int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3651 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003652{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003653 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003654
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003655 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003656 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003657 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003658 }
3659
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003660 w->ignore_suspend = 1;
3661
3662 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003663}
3664EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3665
Stephen Warren16332812011-11-23 12:42:04 -07003666static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3667 struct snd_soc_dapm_widget *w)
3668{
3669 struct snd_soc_dapm_path *p;
3670
3671 list_for_each_entry(p, &card->paths, list) {
3672 if ((p->source == w) || (p->sink == w)) {
3673 dev_dbg(card->dev,
3674 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3675 p->source->name, p->source->id, p->source->dapm,
3676 p->sink->name, p->sink->id, p->sink->dapm);
3677
3678 /* Connected to something other than the codec */
3679 if (p->source->dapm != p->sink->dapm)
3680 return true;
3681 /*
3682 * Loopback connection from codec external pin to
3683 * codec external pin
3684 */
3685 if (p->sink->id == snd_soc_dapm_input) {
3686 switch (p->source->id) {
3687 case snd_soc_dapm_output:
3688 case snd_soc_dapm_micbias:
3689 return true;
3690 default:
3691 break;
3692 }
3693 }
3694 }
3695 }
3696
3697 return false;
3698}
3699
3700/**
3701 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3702 * @codec: The codec whose pins should be processed
3703 *
3704 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3705 * which are unused. Pins are used if they are connected externally to the
3706 * codec, whether that be to some other device, or a loop-back connection to
3707 * the codec itself.
3708 */
3709void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3710{
3711 struct snd_soc_card *card = codec->card;
3712 struct snd_soc_dapm_context *dapm = &codec->dapm;
3713 struct snd_soc_dapm_widget *w;
3714
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003715 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003716 &card->dapm, &codec->dapm);
3717
3718 list_for_each_entry(w, &card->widgets, list) {
3719 if (w->dapm != dapm)
3720 continue;
3721 switch (w->id) {
3722 case snd_soc_dapm_input:
3723 case snd_soc_dapm_output:
3724 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003725 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003726 w->name);
3727 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003728 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003729 "... Not in map; disabling\n");
3730 snd_soc_dapm_nc_pin(dapm, w->name);
3731 }
3732 break;
3733 default:
3734 break;
3735 }
3736 }
3737}
3738
Mark Brown1547aba2010-05-07 21:11:40 +01003739/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003740 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003741 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003742 *
3743 * Free all dapm widgets and resources.
3744 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003745void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003746{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003747 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003748 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003749 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003750 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003751}
3752EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3753
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003754static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003755{
Liam Girdwood01005a72012-07-06 16:57:05 +01003756 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003757 struct snd_soc_dapm_widget *w;
3758 LIST_HEAD(down_list);
3759 int powerdown = 0;
3760
Liam Girdwood01005a72012-07-06 16:57:05 +01003761 mutex_lock(&card->dapm_mutex);
3762
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003763 list_for_each_entry(w, &dapm->card->widgets, list) {
3764 if (w->dapm != dapm)
3765 continue;
Mark Brown51737472009-06-22 13:16:51 +01003766 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003767 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003768 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003769 powerdown = 1;
3770 }
3771 }
3772
3773 /* If there were no widgets to power down we're already in
3774 * standby.
3775 */
3776 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003777 if (dapm->bias_level == SND_SOC_BIAS_ON)
3778 snd_soc_dapm_set_bias_level(dapm,
3779 SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003780 dapm_seq_run(dapm, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003781 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3782 snd_soc_dapm_set_bias_level(dapm,
3783 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003784 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003785
3786 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003787}
Mark Brown51737472009-06-22 13:16:51 +01003788
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003789/*
3790 * snd_soc_dapm_shutdown - callback for system shutdown
3791 */
3792void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3793{
3794 struct snd_soc_codec *codec;
3795
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003796 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003797 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003798 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3799 snd_soc_dapm_set_bias_level(&codec->dapm,
3800 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003801 }
Mark Brown51737472009-06-22 13:16:51 +01003802}
3803
Richard Purdie2b97eab2006-10-06 18:32:18 +02003804/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003805MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003806MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3807MODULE_LICENSE("GPL");