blob: 8d9c09b266fd04f59915a7a7b59e392f892ecf54 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
Liam Girdwood612a3fe2012-02-06 16:05:29 +000017 * mic/headphone insertion events.
Richard Purdie2b97eab2006-10-06 18:32:18 +020018 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Liam Girdwood612a3fe2012-02-06 16:05:29 +000021 * o Delayed power down of audio subsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020024 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080029#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020030#include <linux/delay.h>
31#include <linux/pm.h>
32#include <linux/bitops.h>
33#include <linux/platform_device.h>
34#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020035#include <linux/debugfs.h>
Mark Brownf1aac482011-12-05 15:17:06 +000036#include <linux/pm_runtime.h>
Mark Brown62ea8742012-01-21 21:14:48 +000037#include <linux/regulator/consumer.h>
Ola Liljad7e7eb92012-05-24 15:26:25 +020038#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020040#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020043#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020044#include <sound/initval.h>
45
Mark Brown84e90932010-11-04 00:07:02 -040046#include <trace/events/asoc.h>
47
Mark Brownde02d072011-09-20 21:43:24 +010048#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49
Richard Purdie2b97eab2006-10-06 18:32:18 +020050/* dapm power sequences - make this per codec in the future */
51static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010052 [snd_soc_dapm_pre] = 0,
53 [snd_soc_dapm_supply] = 1,
Mark Brown62ea8742012-01-21 21:14:48 +000054 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020055 [snd_soc_dapm_clock_supply] = 1,
Mark Brown38357ab2009-06-06 19:03:23 +010056 [snd_soc_dapm_micbias] = 2,
Mark Brownc74184e2012-04-04 22:12:09 +010057 [snd_soc_dapm_dai_link] = 2,
Mark Brown46162742013-06-05 19:36:11 +010058 [snd_soc_dapm_dai_in] = 3,
59 [snd_soc_dapm_dai_out] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010060 [snd_soc_dapm_aif_in] = 3,
61 [snd_soc_dapm_aif_out] = 3,
62 [snd_soc_dapm_mic] = 4,
63 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000064 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010065 [snd_soc_dapm_value_mux] = 5,
66 [snd_soc_dapm_dac] = 6,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020067 [snd_soc_dapm_switch] = 7,
Mark Brown010ff262009-08-17 17:39:22 +010068 [snd_soc_dapm_mixer] = 7,
69 [snd_soc_dapm_mixer_named_ctl] = 7,
70 [snd_soc_dapm_pga] = 8,
71 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060072 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010073 [snd_soc_dapm_hp] = 10,
74 [snd_soc_dapm_spk] = 10,
Mark Brown7e1f7c82012-04-12 17:29:36 +010075 [snd_soc_dapm_line] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010076 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020077};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000078
Richard Purdie2b97eab2006-10-06 18:32:18 +020079static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010080 [snd_soc_dapm_pre] = 0,
81 [snd_soc_dapm_adc] = 1,
82 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010083 [snd_soc_dapm_spk] = 2,
Mark Brown7e1f7c82012-04-12 17:29:36 +010084 [snd_soc_dapm_line] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060085 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010086 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020087 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +010088 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010089 [snd_soc_dapm_mixer] = 5,
90 [snd_soc_dapm_dac] = 6,
91 [snd_soc_dapm_mic] = 7,
92 [snd_soc_dapm_micbias] = 8,
93 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000094 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010095 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010096 [snd_soc_dapm_aif_in] = 10,
97 [snd_soc_dapm_aif_out] = 10,
Mark Brown46162742013-06-05 19:36:11 +010098 [snd_soc_dapm_dai_in] = 10,
99 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100100 [snd_soc_dapm_dai_link] = 11,
Ola Liljad7e7eb92012-05-24 15:26:25 +0200101 [snd_soc_dapm_clock_supply] = 12,
Mark Brownc74184e2012-04-04 22:12:09 +0100102 [snd_soc_dapm_regulator_supply] = 12,
103 [snd_soc_dapm_supply] = 12,
104 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200105};
106
Troy Kisky12ef1932008-10-13 17:42:14 -0700107static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100108{
109 if (pop_time)
110 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
111}
112
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200113static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100114{
115 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200116 char *buf;
117
118 if (!pop_time)
119 return;
120
121 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
122 if (buf == NULL)
123 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100124
125 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200126 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100127 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100128 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200129
130 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100131}
132
Mark Browndb432b42011-10-03 21:06:40 +0100133static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
134{
135 return !list_empty(&w->dirty);
136}
137
Mark Brown25c77c52011-10-08 13:36:03 +0100138void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100139{
Mark Brown75c1f892011-10-04 22:28:08 +0100140 if (!dapm_dirty_widget(w)) {
141 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
142 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100143 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100144 }
Mark Browndb432b42011-10-03 21:06:40 +0100145}
Mark Brown25c77c52011-10-08 13:36:03 +0100146EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100147
Mark Browne2d32ff2012-08-31 17:38:32 -0700148void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
149{
150 struct snd_soc_card *card = dapm->card;
151 struct snd_soc_dapm_widget *w;
152
153 mutex_lock(&card->dapm_mutex);
154
155 list_for_each_entry(w, &card->widgets, list) {
156 switch (w->id) {
157 case snd_soc_dapm_input:
158 case snd_soc_dapm_output:
159 dapm_mark_dirty(w, "Rechecking inputs and outputs");
160 break;
161 default:
162 break;
163 }
164 }
165
166 mutex_unlock(&card->dapm_mutex);
167}
168EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
169
Richard Purdie2b97eab2006-10-06 18:32:18 +0200170/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100171static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200172 const struct snd_soc_dapm_widget *_widget)
173{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100174 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200175}
176
Liam Girdwood48056082011-07-20 12:23:33 +0100177/* get snd_card from DAPM context */
178static inline struct snd_card *dapm_get_snd_card(
179 struct snd_soc_dapm_context *dapm)
180{
181 if (dapm->codec)
182 return dapm->codec->card->snd_card;
183 else if (dapm->platform)
184 return dapm->platform->card->snd_card;
185 else
186 BUG();
187
188 /* unreachable */
189 return NULL;
190}
191
192/* get soc_card from DAPM context */
193static inline struct snd_soc_card *dapm_get_soc_card(
194 struct snd_soc_dapm_context *dapm)
195{
196 if (dapm->codec)
197 return dapm->codec->card;
198 else if (dapm->platform)
199 return dapm->platform->card;
200 else
201 BUG();
202
203 /* unreachable */
204 return NULL;
205}
206
Liam Girdwood6c120e12012-02-15 15:15:34 +0000207static void dapm_reset(struct snd_soc_card *card)
208{
209 struct snd_soc_dapm_widget *w;
210
211 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
212
213 list_for_each_entry(w, &card->widgets, list) {
214 w->power_checked = false;
215 w->inputs = -1;
216 w->outputs = -1;
217 }
218}
219
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100220static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
221{
222 if (w->codec)
223 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100224 else if (w->platform)
225 return snd_soc_platform_read(w->platform, reg);
226
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000227 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100228 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100229}
230
231static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
232{
233 if (w->codec)
234 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100235 else if (w->platform)
236 return snd_soc_platform_write(w->platform, reg, val);
237
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000238 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100239 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100240}
241
Liam Girdwood49575fb52012-03-06 18:16:19 +0000242static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
243{
Mark Browne06ab3b2012-03-06 23:58:22 +0000244 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000245 mutex_lock(&w->codec->mutex);
246 else if (w->platform)
247 mutex_lock(&w->platform->mutex);
248}
249
250static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
251{
Mark Browne06ab3b2012-03-06 23:58:22 +0000252 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000253 mutex_unlock(&w->codec->mutex);
254 else if (w->platform)
255 mutex_unlock(&w->platform->mutex);
256}
257
258static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100259 unsigned short reg, unsigned int mask, unsigned int value)
260{
Mark Brown8a713da2011-12-03 12:33:55 +0000261 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100262 unsigned int old, new;
263 int ret;
264
Mark Brown8a713da2011-12-03 12:33:55 +0000265 if (w->codec && w->codec->using_regmap) {
266 ret = regmap_update_bits_check(w->codec->control_data,
267 reg, mask, value, &change);
268 if (ret != 0)
269 return ret;
270 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000271 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000272 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000273 if (ret < 0) {
274 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100275 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000276 }
Mark Brown8a713da2011-12-03 12:33:55 +0000277
278 old = ret;
279 new = (old & ~mask) | (value & mask);
280 change = old != new;
281 if (change) {
282 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000283 if (ret < 0) {
284 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000285 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000286 }
Mark Brown8a713da2011-12-03 12:33:55 +0000287 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000288 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100289 }
290
291 return change;
292}
293
Mark Brown452c5ea2009-05-17 21:41:23 +0100294/**
295 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800296 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100297 * @level: level to configure
298 *
299 * Configure the bias (power) levels for the SoC audio device.
300 *
301 * Returns 0 for success else error.
302 */
Mark Browned5a4c42011-02-18 11:12:42 -0800303static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200304 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100305{
Mark Browned5a4c42011-02-18 11:12:42 -0800306 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100307 int ret = 0;
308
Mark Brown84e90932010-11-04 00:07:02 -0400309 trace_snd_soc_bias_level_start(card, level);
310
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000311 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100312 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100313 if (ret != 0)
314 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100315
Mark Browncc4c6702011-06-06 19:03:34 +0100316 if (dapm->codec) {
317 if (dapm->codec->driver->set_bias_level)
318 ret = dapm->codec->driver->set_bias_level(dapm->codec,
319 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100320 else
321 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100322 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100323 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100324 }
Liam Girdwood41231282012-07-06 16:56:16 +0100325
Mark Brown171ec6b2011-06-06 18:15:19 +0100326 if (ret != 0)
327 goto out;
328
329 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100330 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100331out:
Mark Brown84e90932010-11-04 00:07:02 -0400332 trace_snd_soc_bias_level_done(card, level);
333
Mark Brown452c5ea2009-05-17 21:41:23 +0100334 return ret;
335}
336
Richard Purdie2b97eab2006-10-06 18:32:18 +0200337/* set up initial codec paths */
338static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
339 struct snd_soc_dapm_path *p, int i)
340{
341 switch (w->id) {
342 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000343 case snd_soc_dapm_mixer:
344 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200345 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100346 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600347 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400348 unsigned int reg = mc->reg;
349 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100350 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400351 unsigned int mask = (1 << fls(max)) - 1;
352 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200353
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100354 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200355 val = (val >> shift) & mask;
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200356 if (invert)
357 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200358
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200359 p->connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200360 }
361 break;
362 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600363 struct soc_enum *e = (struct soc_enum *)
364 w->kcontrol_news[i].private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200365 int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200366
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100367 val = soc_widget_read(w, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200368 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200369
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200370 if (item < e->max && !strcmp(p->name, e->texts[item]))
371 p->connect = 1;
372 else
373 p->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200374 }
375 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000376 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600377 struct soc_enum *e = (struct soc_enum *)
378 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000379
380 p->connect = 0;
381 /* since a virtual mux has no backing registers to
382 * decide which path to connect, it will try to match
383 * with the first enumeration. This is to ensure
384 * that the default mux choice (the first) will be
385 * correctly powered up during initialization.
386 */
387 if (!strcmp(p->name, e->texts[0]))
388 p->connect = 1;
389 }
390 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200391 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200392 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600393 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200394 int val, item;
395
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100396 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200397 val = (val >> e->shift_l) & e->mask;
398 for (item = 0; item < e->max; item++) {
399 if (val == e->values[item])
400 break;
401 }
402
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200403 if (item < e->max && !strcmp(p->name, e->texts[item]))
404 p->connect = 1;
405 else
406 p->connect = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200407 }
408 break;
Mark Brown56563102011-10-03 22:41:09 +0100409 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200410 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600411 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200412 case snd_soc_dapm_output:
413 case snd_soc_dapm_adc:
414 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000415 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200416 case snd_soc_dapm_dac:
417 case snd_soc_dapm_micbias:
418 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100419 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000420 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200421 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100422 case snd_soc_dapm_aif_in:
423 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100424 case snd_soc_dapm_dai_in:
425 case snd_soc_dapm_dai_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200426 case snd_soc_dapm_hp:
427 case snd_soc_dapm_mic:
428 case snd_soc_dapm_spk:
429 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100430 case snd_soc_dapm_dai_link:
Mark Brown56563102011-10-03 22:41:09 +0100431 p->connect = 1;
432 break;
433 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200434 case snd_soc_dapm_pre:
435 case snd_soc_dapm_post:
436 p->connect = 0;
437 break;
438 }
439}
440
Mark Brown74b8f952009-06-06 11:26:15 +0100441/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200442static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200443 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
444 struct snd_soc_dapm_path *path, const char *control_name,
445 const struct snd_kcontrol_new *kcontrol)
446{
447 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
448 int i;
449
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100450 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200451 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200452 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200453 list_add(&path->list_sink, &dest->sources);
454 list_add(&path->list_source, &src->sinks);
455 path->name = (char*)e->texts[i];
456 dapm_set_path_status(dest, path, 0);
457 return 0;
458 }
459 }
460
461 return -ENODEV;
462}
463
Mark Brown74b8f952009-06-06 11:26:15 +0100464/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200465static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200466 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
467 struct snd_soc_dapm_path *path, const char *control_name)
468{
469 int i;
470
471 /* search for mixer kcontrol */
472 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600473 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200474 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200475 list_add(&path->list_sink, &dest->sources);
476 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600477 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200478 dapm_set_path_status(dest, path, i);
479 return 0;
480 }
481 }
482 return -ENODEV;
483}
484
Stephen Warrenaf468002011-04-28 17:38:01 -0600485static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600486 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600487 const struct snd_kcontrol_new *kcontrol_new,
488 struct snd_kcontrol **kcontrol)
489{
490 struct snd_soc_dapm_widget *w;
491 int i;
492
493 *kcontrol = NULL;
494
495 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600496 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
497 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600498 for (i = 0; i < w->num_kcontrols; i++) {
499 if (&w->kcontrol_news[i] == kcontrol_new) {
500 if (w->kcontrols)
501 *kcontrol = w->kcontrols[i];
502 return 1;
503 }
504 }
505 }
506
507 return 0;
508}
509
Lars-Peter Clausen6b75bf02013-06-14 13:16:51 +0200510static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
511{
512 kfree(kctl->private_data);
513}
514
Stephen Warren85762e72013-03-29 15:40:10 -0600515/*
516 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
517 * create it. Either way, add the widget into the control's widget list
518 */
519static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
520 int kci, struct snd_soc_dapm_path *path)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200521{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200522 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000523 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000524 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600525 size_t prefix_len;
526 int shared;
527 struct snd_kcontrol *kcontrol;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600528 struct snd_soc_dapm_widget_list *wlist;
Stephen Warren85762e72013-03-29 15:40:10 -0600529 int wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600530 size_t wlistsize;
Stephen Warren85762e72013-03-29 15:40:10 -0600531 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600532 char *long_name;
533 const char *name;
534 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000535
536 if (dapm->codec)
537 prefix = dapm->codec->name_prefix;
538 else
539 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200540
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000541 if (prefix)
542 prefix_len = strlen(prefix) + 1;
543 else
544 prefix_len = 0;
545
Stephen Warren85762e72013-03-29 15:40:10 -0600546 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
547 &kcontrol);
548
549 if (kcontrol) {
550 wlist = kcontrol->private_data;
551 wlistentries = wlist->num_widgets + 1;
552 } else {
553 wlist = NULL;
554 wlistentries = 1;
555 }
556
557 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
558 wlistentries * sizeof(struct snd_soc_dapm_widget *);
559 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
560 if (wlist == NULL) {
561 dev_err(dapm->dev, "ASoC: can't allocate widget list for %s\n",
562 w->name);
563 return -ENOMEM;
564 }
565 wlist->num_widgets = wlistentries;
566 wlist->widgets[wlistentries - 1] = w;
567
568 if (!kcontrol) {
569 if (shared) {
570 wname_in_long_name = false;
571 kcname_in_long_name = true;
572 } else {
573 switch (w->id) {
574 case snd_soc_dapm_switch:
575 case snd_soc_dapm_mixer:
576 wname_in_long_name = true;
577 kcname_in_long_name = true;
578 break;
579 case snd_soc_dapm_mixer_named_ctl:
580 wname_in_long_name = false;
581 kcname_in_long_name = true;
582 break;
583 case snd_soc_dapm_mux:
584 case snd_soc_dapm_virt_mux:
585 case snd_soc_dapm_value_mux:
586 wname_in_long_name = true;
587 kcname_in_long_name = false;
588 break;
589 default:
590 kfree(wlist);
591 return -EINVAL;
592 }
593 }
594
595 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600596 /*
597 * The control will get a prefix from the control
598 * creation process but we're also using the same
599 * prefix for widgets so cut the prefix off the
600 * front of the widget name.
601 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200602 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600603 w->name + prefix_len,
604 w->kcontrol_news[kci].name);
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200605 if (long_name == NULL) {
606 kfree(wlist);
607 return -ENOMEM;
608 }
Stephen Warren85762e72013-03-29 15:40:10 -0600609
610 name = long_name;
611 } else if (wname_in_long_name) {
612 long_name = NULL;
613 name = w->name + prefix_len;
614 } else {
615 long_name = NULL;
616 name = w->kcontrol_news[kci].name;
617 }
618
619 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], wlist, name,
620 prefix);
Lars-Peter Clausen6b75bf02013-06-14 13:16:51 +0200621 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200622 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600623 ret = snd_ctl_add(card, kcontrol);
624 if (ret < 0) {
625 dev_err(dapm->dev,
626 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
627 w->name, name, ret);
628 kfree(wlist);
Stephen Warren85762e72013-03-29 15:40:10 -0600629 return ret;
630 }
Stephen Warren85762e72013-03-29 15:40:10 -0600631 }
632
633 kcontrol->private_data = wlist;
634 w->kcontrols[kci] = kcontrol;
635 path->kcontrol = kcontrol;
636
637 return 0;
638}
639
640/* create new dapm mixer control */
641static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
642{
643 int i, ret;
644 struct snd_soc_dapm_path *path;
645
Richard Purdie2b97eab2006-10-06 18:32:18 +0200646 /* add kcontrol */
647 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200648 /* match name */
649 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200650 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600651 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200652 continue;
653
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200654 if (w->kcontrols[i]) {
655 path->kcontrol = w->kcontrols[i];
656 continue;
657 }
658
Stephen Warren85762e72013-03-29 15:40:10 -0600659 ret = dapm_create_or_share_mixmux_kcontrol(w, i, path);
660 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200661 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200662 }
663 }
Stephen Warren85762e72013-03-29 15:40:10 -0600664
665 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200666}
667
668/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200669static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200670{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200671 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600672 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600673 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200674
Stephen Warrenaf468002011-04-28 17:38:01 -0600675 if (w->num_kcontrols != 1) {
676 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000677 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600678 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200679 return -EINVAL;
680 }
681
Stephen Warren85762e72013-03-29 15:40:10 -0600682 path = list_first_entry(&w->sources, struct snd_soc_dapm_path,
683 list_sink);
684 if (!path) {
685 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
686 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600687 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200688
Stephen Warren85762e72013-03-29 15:40:10 -0600689 ret = dapm_create_or_share_mixmux_kcontrol(w, 0, path);
690 if (ret < 0)
691 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600692
Richard Purdie2b97eab2006-10-06 18:32:18 +0200693 list_for_each_entry(path, &w->sources, list_sink)
Stephen Warren85762e72013-03-29 15:40:10 -0600694 path->kcontrol = w->kcontrols[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200695
Stephen Warrenaf468002011-04-28 17:38:01 -0600696 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200697}
698
699/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200700static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200701{
Mark Browna6c65732010-03-03 17:45:21 +0000702 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200703 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000704 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200705
Mark Browna6c65732010-03-03 17:45:21 +0000706 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200707}
708
709/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100710static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
711 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200712{
713 struct snd_soc_dapm_path *p;
714
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100715 list_for_each_entry(p, sink, list_source) {
716 if (p->walked) {
717 p->walked = 0;
718 dapm_clear_walk_output(dapm, &p->sink->sinks);
719 }
720 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200721}
722
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100723static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
724 struct list_head *source)
725{
726 struct snd_soc_dapm_path *p;
727
728 list_for_each_entry(p, source, list_sink) {
729 if (p->walked) {
730 p->walked = 0;
731 dapm_clear_walk_input(dapm, &p->source->sources);
732 }
733 }
734}
735
736
Mark Brown99497882010-05-07 20:24:05 +0100737/* We implement power down on suspend by checking the power state of
738 * the ALSA card - when we are suspending the ALSA state for the card
739 * is set to D3.
740 */
741static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
742{
Mark Brown12ea2c72011-03-02 18:17:32 +0000743 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100744
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000745 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100746 case SNDRV_CTL_POWER_D3hot:
747 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100748 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000749 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200750 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100751 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100752 default:
753 return 1;
754 }
755}
756
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100757/* add widget to list if it's not already in the list */
758static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
759 struct snd_soc_dapm_widget *w)
760{
761 struct snd_soc_dapm_widget_list *wlist;
762 int wlistsize, wlistentries, i;
763
764 if (*list == NULL)
765 return -EINVAL;
766
767 wlist = *list;
768
769 /* is this widget already in the list */
770 for (i = 0; i < wlist->num_widgets; i++) {
771 if (wlist->widgets[i] == w)
772 return 0;
773 }
774
775 /* allocate some new space */
776 wlistentries = wlist->num_widgets + 1;
777 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
778 wlistentries * sizeof(struct snd_soc_dapm_widget *);
779 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
780 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000781 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100782 w->name);
783 return -ENOMEM;
784 }
785 wlist = *list;
786
787 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000788 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100789 w->name, wlist->num_widgets);
790
791 wlist->widgets[wlist->num_widgets] = w;
792 wlist->num_widgets++;
793 return 1;
794}
795
Richard Purdie2b97eab2006-10-06 18:32:18 +0200796/*
797 * Recursively check for a completed path to an active or physically connected
798 * output widget. Returns number of complete paths.
799 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100800static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
801 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200802{
803 struct snd_soc_dapm_path *path;
804 int con = 0;
805
Mark Brown024dc072011-10-09 11:52:05 +0100806 if (widget->outputs >= 0)
807 return widget->outputs;
808
Mark Brownde02d072011-09-20 21:43:24 +0100809 DAPM_UPDATE_STAT(widget, path_checks);
810
Mark Brown62ea8742012-01-21 21:14:48 +0000811 switch (widget->id) {
812 case snd_soc_dapm_supply:
813 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200814 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100815 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000816 default:
817 break;
818 }
Mark Brown246d0a12009-04-22 18:24:55 +0100819
Mark Brown010ff262009-08-17 17:39:22 +0100820 switch (widget->id) {
821 case snd_soc_dapm_adc:
822 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100823 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100824 if (widget->active) {
825 widget->outputs = snd_soc_dapm_suspend_check(widget);
826 return widget->outputs;
827 }
Mark Brown010ff262009-08-17 17:39:22 +0100828 default:
829 break;
830 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200831
832 if (widget->connected) {
833 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100834 if (widget->id == snd_soc_dapm_output && !widget->ext) {
835 widget->outputs = snd_soc_dapm_suspend_check(widget);
836 return widget->outputs;
837 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200838
839 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100840 if (widget->id == snd_soc_dapm_hp ||
841 widget->id == snd_soc_dapm_spk ||
842 (widget->id == snd_soc_dapm_line &&
843 !list_empty(&widget->sources))) {
844 widget->outputs = snd_soc_dapm_suspend_check(widget);
845 return widget->outputs;
846 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200847 }
848
849 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100850 DAPM_UPDATE_STAT(widget, neighbour_checks);
851
Mark Brownbf3a9e12011-06-13 16:42:29 +0100852 if (path->weak)
853 continue;
854
Mark Brown8af294b2013-02-22 17:48:15 +0000855 if (path->walking)
856 return 1;
857
Richard Purdie2b97eab2006-10-06 18:32:18 +0200858 if (path->walked)
859 continue;
860
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100861 trace_snd_soc_dapm_output_path(widget, path);
862
Richard Purdie2b97eab2006-10-06 18:32:18 +0200863 if (path->sink && path->connect) {
864 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000865 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100866
867 /* do we need to add this widget to the list ? */
868 if (list) {
869 int err;
870 err = dapm_list_add_widget(list, path->sink);
871 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000872 dev_err(widget->dapm->dev,
873 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100874 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000875 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100876 return con;
877 }
878 }
879
880 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000881
882 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200883 }
884 }
885
Mark Brown024dc072011-10-09 11:52:05 +0100886 widget->outputs = con;
887
Richard Purdie2b97eab2006-10-06 18:32:18 +0200888 return con;
889}
890
891/*
892 * Recursively check for a completed path to an active or physically connected
893 * input widget. Returns number of complete paths.
894 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100895static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
896 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200897{
898 struct snd_soc_dapm_path *path;
899 int con = 0;
900
Mark Brown024dc072011-10-09 11:52:05 +0100901 if (widget->inputs >= 0)
902 return widget->inputs;
903
Mark Brownde02d072011-09-20 21:43:24 +0100904 DAPM_UPDATE_STAT(widget, path_checks);
905
Mark Brown62ea8742012-01-21 21:14:48 +0000906 switch (widget->id) {
907 case snd_soc_dapm_supply:
908 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200909 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100910 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000911 default:
912 break;
913 }
Mark Brown246d0a12009-04-22 18:24:55 +0100914
Richard Purdie2b97eab2006-10-06 18:32:18 +0200915 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100916 switch (widget->id) {
917 case snd_soc_dapm_dac:
918 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +0100919 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +0100920 if (widget->active) {
921 widget->inputs = snd_soc_dapm_suspend_check(widget);
922 return widget->inputs;
923 }
Mark Brown010ff262009-08-17 17:39:22 +0100924 default:
925 break;
926 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200927
928 if (widget->connected) {
929 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100930 if (widget->id == snd_soc_dapm_input && !widget->ext) {
931 widget->inputs = snd_soc_dapm_suspend_check(widget);
932 return widget->inputs;
933 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200934
935 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100936 if (widget->id == snd_soc_dapm_vmid) {
937 widget->inputs = snd_soc_dapm_suspend_check(widget);
938 return widget->inputs;
939 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200940
941 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300942 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100943 (widget->id == snd_soc_dapm_line &&
944 !list_empty(&widget->sinks))) {
945 widget->inputs = snd_soc_dapm_suspend_check(widget);
946 return widget->inputs;
947 }
948
Mark Brown1ab97c82011-11-27 16:21:51 +0000949 /* signal generator */
950 if (widget->id == snd_soc_dapm_siggen) {
951 widget->inputs = snd_soc_dapm_suspend_check(widget);
952 return widget->inputs;
953 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200954 }
955
956 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +0100957 DAPM_UPDATE_STAT(widget, neighbour_checks);
958
Mark Brownbf3a9e12011-06-13 16:42:29 +0100959 if (path->weak)
960 continue;
961
Mark Brown8af294b2013-02-22 17:48:15 +0000962 if (path->walking)
963 return 1;
964
Richard Purdie2b97eab2006-10-06 18:32:18 +0200965 if (path->walked)
966 continue;
967
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100968 trace_snd_soc_dapm_input_path(widget, path);
969
Richard Purdie2b97eab2006-10-06 18:32:18 +0200970 if (path->source && path->connect) {
971 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000972 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100973
974 /* do we need to add this widget to the list ? */
975 if (list) {
976 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +0100977 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100978 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000979 dev_err(widget->dapm->dev,
980 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100981 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000982 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100983 return con;
984 }
985 }
986
987 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000988
989 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200990 }
991 }
992
Mark Brown024dc072011-10-09 11:52:05 +0100993 widget->inputs = con;
994
Richard Purdie2b97eab2006-10-06 18:32:18 +0200995 return con;
996}
997
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100998/**
999 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1000 * @dai: the soc DAI.
1001 * @stream: stream direction.
1002 * @list: list of active widgets for this stream.
1003 *
1004 * Queries DAPM graph as to whether an valid audio stream path exists for
1005 * the initial stream specified by name. This takes into account
1006 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1007 *
1008 * Returns the number of valid paths or negative error.
1009 */
1010int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1011 struct snd_soc_dapm_widget_list **list)
1012{
1013 struct snd_soc_card *card = dai->card;
1014 int paths;
1015
1016 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1017 dapm_reset(card);
1018
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001019 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001020 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001021 dapm_clear_walk_output(&card->dapm,
1022 &dai->playback_widget->sinks);
1023 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001024 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001025 dapm_clear_walk_input(&card->dapm,
1026 &dai->capture_widget->sources);
1027 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001028
1029 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001030 mutex_unlock(&card->dapm_mutex);
1031
1032 return paths;
1033}
1034
Richard Purdie2b97eab2006-10-06 18:32:18 +02001035/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001036 * Handler for generic register modifier widget.
1037 */
1038int dapm_reg_event(struct snd_soc_dapm_widget *w,
1039 struct snd_kcontrol *kcontrol, int event)
1040{
1041 unsigned int val;
1042
1043 if (SND_SOC_DAPM_EVENT_ON(event))
1044 val = w->on_val;
1045 else
1046 val = w->off_val;
1047
Liam Girdwood49575fb52012-03-06 18:16:19 +00001048 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001049 w->mask << w->shift, val << w->shift);
1050
1051 return 0;
1052}
Mark Brown11589412008-07-29 11:42:23 +01001053EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001054
Mark Brown62ea8742012-01-21 21:14:48 +00001055/*
1056 * Handler for regulator supply widget.
1057 */
1058int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1059 struct snd_kcontrol *kcontrol, int event)
1060{
Mark Brownc05b84d2012-09-07 12:57:11 +08001061 int ret;
1062
1063 if (SND_SOC_DAPM_EVENT_ON(event)) {
1064 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001065 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001066 if (ret != 0)
1067 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001068 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001069 w->name, ret);
1070 }
1071
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001072 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001073 } else {
1074 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001075 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001076 if (ret != 0)
1077 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001078 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001079 w->name, ret);
1080 }
1081
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001082 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001083 }
Mark Brown62ea8742012-01-21 21:14:48 +00001084}
1085EXPORT_SYMBOL_GPL(dapm_regulator_event);
1086
Ola Liljad7e7eb92012-05-24 15:26:25 +02001087/*
1088 * Handler for clock supply widget.
1089 */
1090int dapm_clock_event(struct snd_soc_dapm_widget *w,
1091 struct snd_kcontrol *kcontrol, int event)
1092{
1093 if (!w->clk)
1094 return -EIO;
1095
Mark Brownec029952012-06-04 08:16:20 +01001096#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001097 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001098 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001099 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001100 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001101 return 0;
1102 }
Mark Brownec029952012-06-04 08:16:20 +01001103#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001104 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001105}
1106EXPORT_SYMBOL_GPL(dapm_clock_event);
1107
Mark Brownd8050022011-09-28 18:28:23 +01001108static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1109{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001110 if (w->power_checked)
1111 return w->new_power;
1112
Mark Brownd8050022011-09-28 18:28:23 +01001113 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001114 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001115 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001116 w->new_power = w->power_check(w);
1117
1118 w->power_checked = true;
1119
1120 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001121}
1122
Mark Browncd0f2d42009-04-20 16:56:59 +01001123/* Generic check to see if a widget should be powered.
1124 */
1125static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1126{
1127 int in, out;
1128
Mark Brownde02d072011-09-20 21:43:24 +01001129 DAPM_UPDATE_STAT(w, power_checks);
1130
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001131 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001132 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001133 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001134 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001135 return out != 0 && in != 0;
1136}
1137
Mark Brown6ea31b92009-04-20 17:15:41 +01001138/* Check to see if an ADC has power */
1139static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1140{
1141 int in;
1142
Mark Brownde02d072011-09-20 21:43:24 +01001143 DAPM_UPDATE_STAT(w, power_checks);
1144
Mark Brown6ea31b92009-04-20 17:15:41 +01001145 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001146 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001147 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001148 return in != 0;
1149 } else {
1150 return dapm_generic_check_power(w);
1151 }
1152}
1153
1154/* Check to see if a DAC has power */
1155static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1156{
1157 int out;
1158
Mark Brownde02d072011-09-20 21:43:24 +01001159 DAPM_UPDATE_STAT(w, power_checks);
1160
Mark Brown6ea31b92009-04-20 17:15:41 +01001161 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001162 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001163 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001164 return out != 0;
1165 } else {
1166 return dapm_generic_check_power(w);
1167 }
1168}
1169
Mark Brown246d0a12009-04-22 18:24:55 +01001170/* Check to see if a power supply is needed */
1171static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1172{
1173 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001174
Mark Brownde02d072011-09-20 21:43:24 +01001175 DAPM_UPDATE_STAT(w, power_checks);
1176
Mark Brown246d0a12009-04-22 18:24:55 +01001177 /* Check if one of our outputs is connected */
1178 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001179 DAPM_UPDATE_STAT(w, neighbour_checks);
1180
Mark Brownbf3a9e12011-06-13 16:42:29 +01001181 if (path->weak)
1182 continue;
1183
Mark Brown215edda2009-09-08 18:59:05 +01001184 if (path->connected &&
1185 !path->connected(path->source, path->sink))
1186 continue;
1187
Mark Brown30173582011-02-11 11:42:19 +00001188 if (!path->sink)
1189 continue;
1190
Mark Brownf68d7e12011-10-04 22:57:50 +01001191 if (dapm_widget_power_check(path->sink))
1192 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001193 }
1194
Mark Brownf68d7e12011-10-04 22:57:50 +01001195 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001196}
1197
Mark Brown35c64bc2011-09-28 18:23:53 +01001198static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1199{
1200 return 1;
1201}
1202
Mark Brown38357ab2009-06-06 19:03:23 +01001203static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1204 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001205 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001206{
Mark Brown828a8422011-01-15 13:14:30 +00001207 int *sort;
1208
1209 if (power_up)
1210 sort = dapm_up_seq;
1211 else
1212 sort = dapm_down_seq;
1213
Mark Brown38357ab2009-06-06 19:03:23 +01001214 if (sort[a->id] != sort[b->id])
1215 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001216 if (a->subseq != b->subseq) {
1217 if (power_up)
1218 return a->subseq - b->subseq;
1219 else
1220 return b->subseq - a->subseq;
1221 }
Mark Brownb22ead22009-06-07 12:51:26 +01001222 if (a->reg != b->reg)
1223 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001224 if (a->dapm != b->dapm)
1225 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001226
Mark Brown38357ab2009-06-06 19:03:23 +01001227 return 0;
1228}
Mark Brown42aa3412009-03-01 19:21:10 +00001229
Mark Brown38357ab2009-06-06 19:03:23 +01001230/* Insert a widget in order into a DAPM power sequence. */
1231static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1232 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001233 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001234{
1235 struct snd_soc_dapm_widget *w;
1236
1237 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001238 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001239 list_add_tail(&new_widget->power_list, &w->power_list);
1240 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001241 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001242
Mark Brown38357ab2009-06-06 19:03:23 +01001243 list_add_tail(&new_widget->power_list, list);
1244}
Mark Brown42aa3412009-03-01 19:21:10 +00001245
Mark Brown68f89ad2010-11-03 23:51:49 -04001246static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1247 struct snd_soc_dapm_widget *w, int event)
1248{
1249 struct snd_soc_card *card = dapm->card;
1250 const char *ev_name;
1251 int power, ret;
1252
1253 switch (event) {
1254 case SND_SOC_DAPM_PRE_PMU:
1255 ev_name = "PRE_PMU";
1256 power = 1;
1257 break;
1258 case SND_SOC_DAPM_POST_PMU:
1259 ev_name = "POST_PMU";
1260 power = 1;
1261 break;
1262 case SND_SOC_DAPM_PRE_PMD:
1263 ev_name = "PRE_PMD";
1264 power = 0;
1265 break;
1266 case SND_SOC_DAPM_POST_PMD:
1267 ev_name = "POST_PMD";
1268 power = 0;
1269 break;
Mark Brown80114122013-02-25 15:14:19 +00001270 case SND_SOC_DAPM_WILL_PMU:
1271 ev_name = "WILL_PMU";
1272 power = 1;
1273 break;
1274 case SND_SOC_DAPM_WILL_PMD:
1275 ev_name = "WILL_PMD";
1276 power = 0;
1277 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001278 default:
1279 BUG();
1280 return;
1281 }
1282
1283 if (w->power != power)
1284 return;
1285
1286 if (w->event && (w->event_flags & event)) {
1287 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1288 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001289 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001290 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001291 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001292 if (ret < 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001293 dev_err(dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001294 ev_name, w->name, ret);
1295 }
1296}
1297
Mark Brownb22ead22009-06-07 12:51:26 +01001298/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001299static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001300 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001301{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001302 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001303 struct snd_soc_dapm_widget *w;
1304 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001305 unsigned int value = 0;
1306 unsigned int mask = 0;
1307 unsigned int cur_mask;
1308
1309 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1310 power_list)->reg;
1311
1312 list_for_each_entry(w, pending, power_list) {
1313 cur_mask = 1 << w->shift;
1314 BUG_ON(reg != w->reg);
1315
1316 if (w->invert)
1317 power = !w->power;
1318 else
1319 power = w->power;
1320
1321 mask |= cur_mask;
1322 if (power)
1323 value |= cur_mask;
1324
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001325 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001326 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1327 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001328
Mark Brown68f89ad2010-11-03 23:51:49 -04001329 /* Check for events */
1330 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1331 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001332 }
1333
Mark Brown81628102009-06-07 13:21:24 +01001334 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001335 /* Any widget will do, they should all be updating the
1336 * same register.
1337 */
1338 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1339 power_list);
1340
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001341 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001342 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001343 value, mask, reg, card->pop_time);
1344 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001345 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001346 }
1347
1348 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001349 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1350 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001351 }
Mark Brown42aa3412009-03-01 19:21:10 +00001352}
1353
Mark Brownb22ead22009-06-07 12:51:26 +01001354/* Apply a DAPM power sequence.
1355 *
1356 * We walk over a pre-sorted list of widgets to apply power to. In
1357 * order to minimise the number of writes to the device required
1358 * multiple widgets will be updated in a single write where possible.
1359 * Currently anything that requires more than a single write is not
1360 * handled.
1361 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001362static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001363 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001364{
1365 struct snd_soc_dapm_widget *w, *n;
1366 LIST_HEAD(pending);
1367 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001368 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001369 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001370 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001371 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001372 int *sort;
1373
1374 if (power_up)
1375 sort = dapm_up_seq;
1376 else
1377 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001378
Mark Brownb22ead22009-06-07 12:51:26 +01001379 list_for_each_entry_safe(w, n, list, power_list) {
1380 ret = 0;
1381
1382 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001383 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001384 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001385 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001386 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001387
Mark Brown474b62d2011-01-18 16:14:44 +00001388 if (cur_dapm && cur_dapm->seq_notifier) {
1389 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1390 if (sort[i] == cur_sort)
1391 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001392 i,
1393 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001394 }
1395
Mark Brownb22ead22009-06-07 12:51:26 +01001396 INIT_LIST_HEAD(&pending);
1397 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001398 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001399 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001400 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001401 }
1402
Mark Brown163cac02009-06-07 10:12:52 +01001403 switch (w->id) {
1404 case snd_soc_dapm_pre:
1405 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001406 list_for_each_entry_safe_continue(w, n, list,
1407 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001408
Mark Brownb22ead22009-06-07 12:51:26 +01001409 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001410 ret = w->event(w,
1411 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001412 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001413 ret = w->event(w,
1414 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001415 break;
1416
1417 case snd_soc_dapm_post:
1418 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001419 list_for_each_entry_safe_continue(w, n, list,
1420 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001421
Mark Brownb22ead22009-06-07 12:51:26 +01001422 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001423 ret = w->event(w,
1424 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001425 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001426 ret = w->event(w,
1427 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001428 break;
1429
Mark Brown163cac02009-06-07 10:12:52 +01001430 default:
Mark Brown81628102009-06-07 13:21:24 +01001431 /* Queue it up for application */
1432 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001433 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001434 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001435 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001436 list_move(&w->power_list, &pending);
1437 break;
Mark Brown163cac02009-06-07 10:12:52 +01001438 }
Mark Brownb22ead22009-06-07 12:51:26 +01001439
1440 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001441 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001442 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001443 }
Mark Brownb22ead22009-06-07 12:51:26 +01001444
1445 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001446 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001447
1448 if (cur_dapm && cur_dapm->seq_notifier) {
1449 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1450 if (sort[i] == cur_sort)
1451 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001452 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001453 }
Mark Brown163cac02009-06-07 10:12:52 +01001454}
1455
Mark Brown97404f22010-12-14 16:13:57 +00001456static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1457{
1458 struct snd_soc_dapm_update *update = dapm->update;
1459 struct snd_soc_dapm_widget *w;
1460 int ret;
1461
1462 if (!update)
1463 return;
1464
1465 w = update->widget;
1466
1467 if (w->event &&
1468 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1469 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1470 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001471 dev_err(dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Mark Brown97404f22010-12-14 16:13:57 +00001472 w->name, ret);
1473 }
1474
Liam Girdwood49575fb52012-03-06 18:16:19 +00001475 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001476 update->val);
1477 if (ret < 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001478 dev_err(dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1479 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001480
1481 if (w->event &&
1482 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1483 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1484 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001485 dev_err(dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Mark Brown97404f22010-12-14 16:13:57 +00001486 w->name, ret);
1487 }
1488}
1489
Mark Brown9d0624a2011-02-18 11:49:43 -08001490/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1491 * they're changing state.
1492 */
1493static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1494{
1495 struct snd_soc_dapm_context *d = data;
1496 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001497
Mark Brown56fba412011-06-04 11:25:10 +01001498 /* If we're off and we're not supposed to be go into STANDBY */
1499 if (d->bias_level == SND_SOC_BIAS_OFF &&
1500 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001501 if (d->dev)
1502 pm_runtime_get_sync(d->dev);
1503
Mark Brown9d0624a2011-02-18 11:49:43 -08001504 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1505 if (ret != 0)
1506 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001507 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001508 }
1509
Mark Brown56fba412011-06-04 11:25:10 +01001510 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1511 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001512 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1513 if (ret != 0)
1514 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001515 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001516 }
1517}
1518
1519/* Async callback run prior to DAPM sequences - brings to their final
1520 * state.
1521 */
1522static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1523{
1524 struct snd_soc_dapm_context *d = data;
1525 int ret;
1526
1527 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001528 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1529 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1530 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001531 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1532 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001533 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001534 ret);
1535 }
1536
1537 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001538 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1539 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001540 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1541 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001542 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1543 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001544
1545 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001546 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001547 }
1548
1549 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001550 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1551 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001552 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1553 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001554 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001555 ret);
1556 }
1557}
Mark Brown97404f22010-12-14 16:13:57 +00001558
Mark Brownfe4fda52011-10-03 22:36:57 +01001559static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1560 bool power, bool connect)
1561{
1562 /* If a connection is being made or broken then that update
1563 * will have marked the peer dirty, otherwise the widgets are
1564 * not connected and this update has no impact. */
1565 if (!connect)
1566 return;
1567
1568 /* If the peer is already in the state we're moving to then we
1569 * won't have an impact on it. */
1570 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001571 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001572}
1573
Mark Brown05623c42011-09-28 17:02:31 +01001574static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1575 struct list_head *up_list,
1576 struct list_head *down_list)
1577{
Mark Browndb432b42011-10-03 21:06:40 +01001578 struct snd_soc_dapm_path *path;
1579
Mark Brown05623c42011-09-28 17:02:31 +01001580 if (w->power == power)
1581 return;
1582
1583 trace_snd_soc_dapm_widget_power(w, power);
1584
Mark Browndb432b42011-10-03 21:06:40 +01001585 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001586 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001587 */
1588 list_for_each_entry(path, &w->sources, list_sink) {
1589 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001590 dapm_widget_set_peer_power(path->source, power,
1591 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001592 }
1593 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001594 switch (w->id) {
1595 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001596 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001597 case snd_soc_dapm_clock_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001598 /* Supplies can't affect their outputs, only their inputs */
1599 break;
1600 default:
1601 list_for_each_entry(path, &w->sinks, list_source) {
1602 if (path->sink) {
1603 dapm_widget_set_peer_power(path->sink, power,
1604 path->connect);
1605 }
Mark Browndb432b42011-10-03 21:06:40 +01001606 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001607 break;
Mark Browndb432b42011-10-03 21:06:40 +01001608 }
1609
Mark Brown05623c42011-09-28 17:02:31 +01001610 if (power)
1611 dapm_seq_insert(w, up_list, true);
1612 else
1613 dapm_seq_insert(w, down_list, false);
1614
1615 w->power = power;
1616}
1617
Mark Brown7c81beb2011-09-20 22:22:32 +01001618static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1619 struct list_head *up_list,
1620 struct list_head *down_list)
1621{
Mark Brown7c81beb2011-09-20 22:22:32 +01001622 int power;
1623
1624 switch (w->id) {
1625 case snd_soc_dapm_pre:
1626 dapm_seq_insert(w, down_list, false);
1627 break;
1628 case snd_soc_dapm_post:
1629 dapm_seq_insert(w, up_list, true);
1630 break;
1631
1632 default:
Mark Brownd8050022011-09-28 18:28:23 +01001633 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001634
Mark Brown05623c42011-09-28 17:02:31 +01001635 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001636 break;
1637 }
1638}
1639
Mark Brown42aa3412009-03-01 19:21:10 +00001640/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001641 * Scan each dapm widget for complete audio path.
1642 * A complete path is a route that has valid endpoints i.e.:-
1643 *
1644 * o DAC to output pin.
1645 * o Input Pin to ADC.
1646 * o Input pin to Output pin (bypass, sidetone)
1647 * o DAC to ADC (loopback).
1648 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001649static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001650{
Mark Brown12ea2c72011-03-02 18:17:32 +00001651 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001652 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001653 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001654 LIST_HEAD(up_list);
1655 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001656 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001657 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001658
Mark Brown84e90932010-11-04 00:07:02 -04001659 trace_snd_soc_dapm_start(card);
1660
Mark Brown56fba412011-06-04 11:25:10 +01001661 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001662 if (d->idle_bias_off)
1663 d->target_bias_level = SND_SOC_BIAS_OFF;
1664 else
1665 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001666 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001667
Liam Girdwood6c120e12012-02-15 15:15:34 +00001668 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001669
Mark Brown6d3ddc82009-05-16 17:47:29 +01001670 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001671 * lists indicating if they should be powered up or down. We
1672 * only check widgets that have been flagged as dirty but note
1673 * that new widgets may be added to the dirty list while we
1674 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001675 */
Mark Browndb432b42011-10-03 21:06:40 +01001676 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001677 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001678 }
1679
Mark Brownf9de6d72011-09-28 17:19:47 +01001680 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001681 switch (w->id) {
1682 case snd_soc_dapm_pre:
1683 case snd_soc_dapm_post:
1684 /* These widgets always need to be powered */
1685 break;
1686 default:
1687 list_del_init(&w->dirty);
1688 break;
1689 }
Mark Browndb432b42011-10-03 21:06:40 +01001690
Mark Brownf9de6d72011-09-28 17:19:47 +01001691 if (w->power) {
1692 d = w->dapm;
1693
1694 /* Supplies and micbiases only bring the
1695 * context up to STANDBY as unless something
1696 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001697 * generally don't require full power. Signal
1698 * generators are virtual pins and have no
1699 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001700 */
1701 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001702 case snd_soc_dapm_siggen:
1703 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001704 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001705 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001706 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001707 case snd_soc_dapm_micbias:
1708 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1709 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1710 break;
1711 default:
1712 d->target_bias_level = SND_SOC_BIAS_ON;
1713 break;
1714 }
1715 }
1716
1717 }
1718
Mark Brown85a843c2011-09-21 21:29:47 +01001719 /* Force all contexts in the card to the same bias state if
1720 * they're not ground referenced.
1721 */
Mark Brown56fba412011-06-04 11:25:10 +01001722 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001723 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001724 if (d->target_bias_level > bias)
1725 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001726 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001727 if (!d->idle_bias_off)
1728 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001729
Mark Brownde02d072011-09-20 21:43:24 +01001730 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001731
Mark Brown9d0624a2011-02-18 11:49:43 -08001732 /* Run all the bias changes in parallel */
1733 list_for_each_entry(d, &dapm->card->dapm_list, list)
1734 async_schedule_domain(dapm_pre_sequence_async, d,
1735 &async_domain);
1736 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001737
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001738 list_for_each_entry(w, &down_list, power_list) {
Mark Brown80114122013-02-25 15:14:19 +00001739 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_WILL_PMD);
1740 }
1741
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001742 list_for_each_entry(w, &up_list, power_list) {
Mark Brown80114122013-02-25 15:14:19 +00001743 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_WILL_PMU);
1744 }
1745
Mark Brown6d3ddc82009-05-16 17:47:29 +01001746 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001747 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001748
Mark Brown97404f22010-12-14 16:13:57 +00001749 dapm_widget_update(dapm);
1750
Mark Brown6d3ddc82009-05-16 17:47:29 +01001751 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001752 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001753
Mark Brown9d0624a2011-02-18 11:49:43 -08001754 /* Run all the bias changes in parallel */
1755 list_for_each_entry(d, &dapm->card->dapm_list, list)
1756 async_schedule_domain(dapm_post_sequence_async, d,
1757 &async_domain);
1758 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001759
Liam Girdwood8078d872012-02-15 15:15:35 +00001760 /* do we need to notify any clients that DAPM event is complete */
1761 list_for_each_entry(d, &card->dapm_list, list) {
1762 if (d->stream_event)
1763 d->stream_event(d, event);
1764 }
1765
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001766 pop_dbg(dapm->dev, card->pop_time,
1767 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001768 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001769
Mark Brown84e90932010-11-04 00:07:02 -04001770 trace_snd_soc_dapm_done(card);
1771
Mark Brown42aa3412009-03-01 19:21:10 +00001772 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001773}
1774
Mark Brown79fb9382009-08-21 16:38:13 +01001775#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001776static ssize_t dapm_widget_power_read_file(struct file *file,
1777 char __user *user_buf,
1778 size_t count, loff_t *ppos)
1779{
1780 struct snd_soc_dapm_widget *w = file->private_data;
1781 char *buf;
1782 int in, out;
1783 ssize_t ret;
1784 struct snd_soc_dapm_path *p = NULL;
1785
1786 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1787 if (!buf)
1788 return -ENOMEM;
1789
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001790 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001791 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001792 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001793 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001794
Mark Brownf13ebad2012-03-03 18:01:01 +00001795 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1796 w->name, w->power ? "On" : "Off",
1797 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001798
Mark Brownd033c362009-12-04 15:25:56 +00001799 if (w->reg >= 0)
1800 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1801 " - R%d(0x%x) bit %d",
1802 w->reg, w->reg, w->shift);
1803
1804 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1805
Mark Brown3eef08b2009-09-14 16:49:00 +01001806 if (w->sname)
1807 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1808 w->sname,
1809 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001810
1811 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001812 if (p->connected && !p->connected(w, p->sink))
1813 continue;
1814
Mark Brown79fb9382009-08-21 16:38:13 +01001815 if (p->connect)
1816 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001817 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001818 p->name ? p->name : "static",
1819 p->source->name);
1820 }
1821 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001822 if (p->connected && !p->connected(w, p->sink))
1823 continue;
1824
Mark Brown79fb9382009-08-21 16:38:13 +01001825 if (p->connect)
1826 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001827 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001828 p->name ? p->name : "static",
1829 p->sink->name);
1830 }
1831
1832 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1833
1834 kfree(buf);
1835 return ret;
1836}
1837
1838static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001839 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001840 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001841 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001842};
1843
Mark Brownef49e4f2011-04-04 20:48:13 +09001844static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1845 size_t count, loff_t *ppos)
1846{
1847 struct snd_soc_dapm_context *dapm = file->private_data;
1848 char *level;
1849
1850 switch (dapm->bias_level) {
1851 case SND_SOC_BIAS_ON:
1852 level = "On\n";
1853 break;
1854 case SND_SOC_BIAS_PREPARE:
1855 level = "Prepare\n";
1856 break;
1857 case SND_SOC_BIAS_STANDBY:
1858 level = "Standby\n";
1859 break;
1860 case SND_SOC_BIAS_OFF:
1861 level = "Off\n";
1862 break;
1863 default:
1864 BUG();
1865 level = "Unknown\n";
1866 break;
1867 }
1868
1869 return simple_read_from_buffer(user_buf, count, ppos, level,
1870 strlen(level));
1871}
1872
1873static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001874 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001875 .read = dapm_bias_read_file,
1876 .llseek = default_llseek,
1877};
1878
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001879void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1880 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001881{
Mark Brown79fb9382009-08-21 16:38:13 +01001882 struct dentry *d;
1883
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001884 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1885
1886 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001887 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001888 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001889 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001890 }
Mark Brown79fb9382009-08-21 16:38:13 +01001891
Mark Brownef49e4f2011-04-04 20:48:13 +09001892 d = debugfs_create_file("bias_level", 0444,
1893 dapm->debugfs_dapm, dapm,
1894 &dapm_bias_fops);
1895 if (!d)
1896 dev_warn(dapm->dev,
1897 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001898}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001899
1900static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1901{
1902 struct snd_soc_dapm_context *dapm = w->dapm;
1903 struct dentry *d;
1904
1905 if (!dapm->debugfs_dapm || !w->name)
1906 return;
1907
1908 d = debugfs_create_file(w->name, 0444,
1909 dapm->debugfs_dapm, w,
1910 &dapm_widget_power_fops);
1911 if (!d)
1912 dev_warn(w->dapm->dev,
1913 "ASoC: Failed to create %s debugfs file\n",
1914 w->name);
1915}
1916
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001917static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1918{
1919 debugfs_remove_recursive(dapm->debugfs_dapm);
1920}
1921
Mark Brown79fb9382009-08-21 16:38:13 +01001922#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001923void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1924 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001925{
1926}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001927
1928static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1929{
1930}
1931
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001932static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1933{
1934}
1935
Mark Brown79fb9382009-08-21 16:38:13 +01001936#endif
1937
Richard Purdie2b97eab2006-10-06 18:32:18 +02001938/* test and update the power status of a mux widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001939static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001940 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001941{
1942 struct snd_soc_dapm_path *path;
1943 int found = 0;
1944
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001945 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001946 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001947 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001948 return -ENODEV;
1949
Richard Purdie2b97eab2006-10-06 18:32:18 +02001950 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001951 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001952 if (path->kcontrol != kcontrol)
1953 continue;
1954
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001955 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001956 continue;
1957
1958 found = 1;
1959 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001960 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001961 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001962 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001963 } else {
1964 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001965 dapm_mark_dirty(path->source,
1966 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001967 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001968 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001969 }
1970
Mark Browndb432b42011-10-03 21:06:40 +01001971 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001972 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001973 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001974 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001975
Liam Girdwood618dae12012-04-25 12:12:51 +01001976 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001977}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001978
1979int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1980 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1981{
1982 struct snd_soc_card *card = widget->dapm->card;
1983 int ret;
1984
Liam Girdwood3cd04342012-03-09 12:02:08 +00001985 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001986 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1987 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001988 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001989 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001990 return ret;
1991}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001992EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001993
Milan plzik1b075e32008-01-10 14:39:46 +01001994/* test and update the power status of a mixer or switch widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001995static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001996 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001997{
1998 struct snd_soc_dapm_path *path;
1999 int found = 0;
2000
Milan plzik1b075e32008-01-10 14:39:46 +01002001 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002002 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01002003 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002004 return -ENODEV;
2005
Richard Purdie2b97eab2006-10-06 18:32:18 +02002006 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002007 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002008 if (path->kcontrol != kcontrol)
2009 continue;
2010
2011 /* found, now check type */
2012 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002013 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002014 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002015 }
2016
Mark Browndb432b42011-10-03 21:06:40 +01002017 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01002018 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002019 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01002020 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002021
Liam Girdwood618dae12012-04-25 12:12:51 +01002022 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002023}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002024
2025int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
2026 struct snd_kcontrol *kcontrol, int connect)
2027{
2028 struct snd_soc_card *card = widget->dapm->card;
2029 int ret;
2030
Liam Girdwood3cd04342012-03-09 12:02:08 +00002031 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002032 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
2033 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002034 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002035 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002036 return ret;
2037}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002038EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002039
2040/* show dapm widget status in sys fs */
2041static ssize_t dapm_widget_show(struct device *dev,
2042 struct device_attribute *attr, char *buf)
2043{
Mark Brown36ae1a92012-01-06 17:12:45 -08002044 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002045 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002046 struct snd_soc_dapm_widget *w;
2047 int count = 0;
2048 char *state = "not set";
2049
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002050 list_for_each_entry(w, &codec->card->widgets, list) {
2051 if (w->dapm != &codec->dapm)
2052 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002053
2054 /* only display widgets that burnm power */
2055 switch (w->id) {
2056 case snd_soc_dapm_hp:
2057 case snd_soc_dapm_mic:
2058 case snd_soc_dapm_spk:
2059 case snd_soc_dapm_line:
2060 case snd_soc_dapm_micbias:
2061 case snd_soc_dapm_dac:
2062 case snd_soc_dapm_adc:
2063 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002064 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002065 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002066 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002067 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002068 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002069 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002070 if (w->name)
2071 count += sprintf(buf + count, "%s: %s\n",
2072 w->name, w->power ? "On":"Off");
2073 break;
2074 default:
2075 break;
2076 }
2077 }
2078
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002079 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002080 case SND_SOC_BIAS_ON:
2081 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002082 break;
Mark Brown0be98982008-05-19 12:31:28 +02002083 case SND_SOC_BIAS_PREPARE:
2084 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002085 break;
Mark Brown0be98982008-05-19 12:31:28 +02002086 case SND_SOC_BIAS_STANDBY:
2087 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002088 break;
Mark Brown0be98982008-05-19 12:31:28 +02002089 case SND_SOC_BIAS_OFF:
2090 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002091 break;
2092 }
2093 count += sprintf(buf + count, "PM State: %s\n", state);
2094
2095 return count;
2096}
2097
2098static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2099
2100int snd_soc_dapm_sys_add(struct device *dev)
2101{
Troy Kisky12ef1932008-10-13 17:42:14 -07002102 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002103}
2104
2105static void snd_soc_dapm_sys_remove(struct device *dev)
2106{
Mark Brownaef90842009-05-16 17:53:16 +01002107 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002108}
2109
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002110static void dapm_free_path(struct snd_soc_dapm_path *path)
2111{
2112 list_del(&path->list_sink);
2113 list_del(&path->list_source);
2114 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002115 kfree(path);
2116}
2117
Richard Purdie2b97eab2006-10-06 18:32:18 +02002118/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002119static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002120{
2121 struct snd_soc_dapm_widget *w, *next_w;
2122 struct snd_soc_dapm_path *p, *next_p;
2123
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002124 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2125 if (w->dapm != dapm)
2126 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002127 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002128 /*
2129 * remove source and sink paths associated to this widget.
2130 * While removing the path, remove reference to it from both
2131 * source and sink widgets so that path is removed only once.
2132 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002133 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2134 dapm_free_path(p);
2135
2136 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2137 dapm_free_path(p);
2138
Stephen Warrenfad59882011-04-28 17:37:59 -06002139 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002140 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002141 kfree(w);
2142 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002143}
2144
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002145static struct snd_soc_dapm_widget *dapm_find_widget(
2146 struct snd_soc_dapm_context *dapm, const char *pin,
2147 bool search_other_contexts)
2148{
2149 struct snd_soc_dapm_widget *w;
2150 struct snd_soc_dapm_widget *fallback = NULL;
2151
2152 list_for_each_entry(w, &dapm->card->widgets, list) {
2153 if (!strcmp(w->name, pin)) {
2154 if (w->dapm == dapm)
2155 return w;
2156 else
2157 fallback = w;
2158 }
2159 }
2160
2161 if (search_other_contexts)
2162 return fallback;
2163
2164 return NULL;
2165}
2166
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002167static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002168 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002169{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002170 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002171
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002172 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002173 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002174 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002175 }
2176
Mark Brown1a8b2d92012-02-16 11:50:07 -08002177 if (w->connected != status)
2178 dapm_mark_dirty(w, "pin configuration");
2179
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002180 w->connected = status;
2181 if (status == 0)
2182 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002183
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002184 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002185}
2186
Richard Purdie2b97eab2006-10-06 18:32:18 +02002187/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002188 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002189 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002190 *
2191 * Walks all dapm audio paths and powers widgets according to their
2192 * stream or path usage.
2193 *
2194 * Returns 0 for success.
2195 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002196int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002197{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002198 int ret;
2199
Mark Brown4f4c0072011-10-07 14:29:19 +01002200 /*
2201 * Suppress early reports (eg, jacks syncing their state) to avoid
2202 * silly DAPM runs during card startup.
2203 */
2204 if (!dapm->card || !dapm->card->instantiated)
2205 return 0;
2206
Liam Girdwood3cd04342012-03-09 12:02:08 +00002207 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002208 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2209 mutex_unlock(&dapm->card->dapm_mutex);
2210 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002211}
Liam Girdwooda5302182008-07-07 13:35:17 +01002212EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002213
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002214static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002215 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002216{
2217 struct snd_soc_dapm_path *path;
2218 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002219 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002220 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002221 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002222 const char *source;
2223 char prefixed_sink[80];
2224 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002225 int ret = 0;
2226
Mark Brown88e8b9a2011-03-02 18:18:24 +00002227 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002228 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2229 dapm->codec->name_prefix, route->sink);
2230 sink = prefixed_sink;
2231 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2232 dapm->codec->name_prefix, route->source);
2233 source = prefixed_source;
2234 } else {
2235 sink = route->sink;
2236 source = route->source;
2237 }
2238
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002239 /*
2240 * find src and dest widgets over all widgets but favor a widget from
2241 * current DAPM context
2242 */
2243 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002245 wtsink = w;
2246 if (w->dapm == dapm)
2247 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002248 continue;
2249 }
2250 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002251 wtsource = w;
2252 if (w->dapm == dapm)
2253 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002254 }
2255 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002256 /* use widget from another DAPM context if not found from this */
2257 if (!wsink)
2258 wsink = wtsink;
2259 if (!wsource)
2260 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002261
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002262 if (wsource == NULL) {
2263 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2264 route->source);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002265 return -ENODEV;
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002266 }
2267 if (wsink == NULL) {
2268 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2269 route->sink);
2270 return -ENODEV;
2271 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002272
2273 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2274 if (!path)
2275 return -ENOMEM;
2276
2277 path->source = wsource;
2278 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002279 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002280 INIT_LIST_HEAD(&path->list);
2281 INIT_LIST_HEAD(&path->list_source);
2282 INIT_LIST_HEAD(&path->list_sink);
2283
2284 /* check for external widgets */
2285 if (wsink->id == snd_soc_dapm_input) {
2286 if (wsource->id == snd_soc_dapm_micbias ||
2287 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002288 wsource->id == snd_soc_dapm_line ||
2289 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002290 wsink->ext = 1;
2291 }
2292 if (wsource->id == snd_soc_dapm_output) {
2293 if (wsink->id == snd_soc_dapm_spk ||
2294 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002295 wsink->id == snd_soc_dapm_line ||
2296 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002297 wsource->ext = 1;
2298 }
2299
2300 /* connect static paths */
2301 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002302 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002303 list_add(&path->list_sink, &wsink->sources);
2304 list_add(&path->list_source, &wsource->sinks);
2305 path->connect = 1;
2306 return 0;
2307 }
2308
2309 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002310 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002311 case snd_soc_dapm_adc:
2312 case snd_soc_dapm_dac:
2313 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002314 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002315 case snd_soc_dapm_input:
2316 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002317 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002318 case snd_soc_dapm_micbias:
2319 case snd_soc_dapm_vmid:
2320 case snd_soc_dapm_pre:
2321 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002322 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002323 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002324 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002325 case snd_soc_dapm_aif_in:
2326 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002327 case snd_soc_dapm_dai_in:
2328 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002329 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002330 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002331 list_add(&path->list_sink, &wsink->sources);
2332 list_add(&path->list_source, &wsource->sinks);
2333 path->connect = 1;
2334 return 0;
2335 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002336 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002337 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002338 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002339 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002340 if (ret != 0)
2341 goto err;
2342 break;
2343 case snd_soc_dapm_switch:
2344 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002345 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002346 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002347 if (ret != 0)
2348 goto err;
2349 break;
2350 case snd_soc_dapm_hp:
2351 case snd_soc_dapm_mic:
2352 case snd_soc_dapm_line:
2353 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002354 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002355 list_add(&path->list_sink, &wsink->sources);
2356 list_add(&path->list_source, &wsource->sinks);
2357 path->connect = 0;
2358 return 0;
2359 }
Mark Brownfabd0382012-07-05 17:20:06 +01002360
2361 dapm_mark_dirty(wsource, "Route added");
2362 dapm_mark_dirty(wsink, "Route added");
2363
Richard Purdie2b97eab2006-10-06 18:32:18 +02002364 return 0;
2365
2366err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002367 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002368 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002369 kfree(path);
2370 return ret;
2371}
Mark Brown105f1c22008-05-13 14:52:19 +02002372
Mark Brownefcc3c62012-07-05 17:24:19 +01002373static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2374 const struct snd_soc_dapm_route *route)
2375{
2376 struct snd_soc_dapm_path *path, *p;
2377 const char *sink;
2378 const char *source;
2379 char prefixed_sink[80];
2380 char prefixed_source[80];
2381
2382 if (route->control) {
2383 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002384 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002385 return -EINVAL;
2386 }
2387
2388 if (dapm->codec && dapm->codec->name_prefix) {
2389 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2390 dapm->codec->name_prefix, route->sink);
2391 sink = prefixed_sink;
2392 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2393 dapm->codec->name_prefix, route->source);
2394 source = prefixed_source;
2395 } else {
2396 sink = route->sink;
2397 source = route->source;
2398 }
2399
2400 path = NULL;
2401 list_for_each_entry(p, &dapm->card->paths, list) {
2402 if (strcmp(p->source->name, source) != 0)
2403 continue;
2404 if (strcmp(p->sink->name, sink) != 0)
2405 continue;
2406 path = p;
2407 break;
2408 }
2409
2410 if (path) {
2411 dapm_mark_dirty(path->source, "Route removed");
2412 dapm_mark_dirty(path->sink, "Route removed");
2413
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002414 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002415 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002416 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002417 source, sink);
2418 }
2419
2420 return 0;
2421}
2422
Mark Brown105f1c22008-05-13 14:52:19 +02002423/**
Mark Brown105f1c22008-05-13 14:52:19 +02002424 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002425 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002426 * @route: audio routes
2427 * @num: number of routes
2428 *
2429 * Connects 2 dapm widgets together via a named audio path. The sink is
2430 * the widget receiving the audio signal, whilst the source is the sender
2431 * of the audio signal.
2432 *
2433 * Returns 0 for success else error. On error all resources can be freed
2434 * with a call to snd_soc_card_free().
2435 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002436int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002437 const struct snd_soc_dapm_route *route, int num)
2438{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002439 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002440
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002441 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002442 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002443 r = snd_soc_dapm_add_route(dapm, route);
2444 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002445 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2446 route->source,
2447 route->control ? route->control : "direct",
2448 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002449 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002450 }
2451 route++;
2452 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002453 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002454
Dan Carpenter60884c22012-04-13 22:25:43 +03002455 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002456}
2457EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2458
Mark Brownefcc3c62012-07-05 17:24:19 +01002459/**
2460 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2461 * @dapm: DAPM context
2462 * @route: audio routes
2463 * @num: number of routes
2464 *
2465 * Removes routes from the DAPM context.
2466 */
2467int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2468 const struct snd_soc_dapm_route *route, int num)
2469{
2470 int i, ret = 0;
2471
2472 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2473 for (i = 0; i < num; i++) {
2474 snd_soc_dapm_del_route(dapm, route);
2475 route++;
2476 }
2477 mutex_unlock(&dapm->card->dapm_mutex);
2478
2479 return ret;
2480}
2481EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2482
Mark Brownbf3a9e12011-06-13 16:42:29 +01002483static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2484 const struct snd_soc_dapm_route *route)
2485{
2486 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2487 route->source,
2488 true);
2489 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2490 route->sink,
2491 true);
2492 struct snd_soc_dapm_path *path;
2493 int count = 0;
2494
2495 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002496 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002497 route->source);
2498 return -ENODEV;
2499 }
2500
2501 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002502 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002503 route->sink);
2504 return -ENODEV;
2505 }
2506
2507 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002508 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002509 route->source, route->sink);
2510
2511 list_for_each_entry(path, &source->sinks, list_source) {
2512 if (path->sink == sink) {
2513 path->weak = 1;
2514 count++;
2515 }
2516 }
2517
2518 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002519 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002520 route->source, route->sink);
2521 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002522 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002523 count, route->source, route->sink);
2524
2525 return 0;
2526}
2527
2528/**
2529 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2530 * @dapm: DAPM context
2531 * @route: audio routes
2532 * @num: number of routes
2533 *
2534 * Mark existing routes matching those specified in the passed array
2535 * as being weak, meaning that they are ignored for the purpose of
2536 * power decisions. The main intended use case is for sidetone paths
2537 * which couple audio between other independent paths if they are both
2538 * active in order to make the combination work better at the user
2539 * level but which aren't intended to be "used".
2540 *
2541 * Note that CODEC drivers should not use this as sidetone type paths
2542 * can frequently also be used as bypass paths.
2543 */
2544int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2545 const struct snd_soc_dapm_route *route, int num)
2546{
2547 int i, err;
2548 int ret = 0;
2549
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002550 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002551 for (i = 0; i < num; i++) {
2552 err = snd_soc_dapm_weak_route(dapm, route);
2553 if (err)
2554 ret = err;
2555 route++;
2556 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002557 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002558
2559 return ret;
2560}
2561EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2562
Mark Brown105f1c22008-05-13 14:52:19 +02002563/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002564 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002565 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002566 *
2567 * Checks the codec for any new dapm widgets and creates them if found.
2568 *
2569 * Returns 0 for success.
2570 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002571int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002572{
2573 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002574 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002575
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002576 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2577
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002578 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002579 {
2580 if (w->new)
2581 continue;
2582
Stephen Warrenfad59882011-04-28 17:37:59 -06002583 if (w->num_kcontrols) {
2584 w->kcontrols = kzalloc(w->num_kcontrols *
2585 sizeof(struct snd_kcontrol *),
2586 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002587 if (!w->kcontrols) {
2588 mutex_unlock(&dapm->card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002589 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002590 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002591 }
2592
Richard Purdie2b97eab2006-10-06 18:32:18 +02002593 switch(w->id) {
2594 case snd_soc_dapm_switch:
2595 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002596 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002597 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002598 break;
2599 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002600 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002601 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002602 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002603 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002604 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002605 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002606 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002607 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002608 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002609 break;
2610 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002611
2612 /* Read the initial power state from the device */
2613 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002614 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002615 val &= 1 << w->shift;
2616 if (w->invert)
2617 val = !val;
2618
2619 if (val)
2620 w->power = 1;
2621 }
2622
Richard Purdie2b97eab2006-10-06 18:32:18 +02002623 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002624
Mark Brown7508b122011-10-05 12:09:12 +01002625 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002626 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002627 }
2628
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002629 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002630 mutex_unlock(&dapm->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002631 return 0;
2632}
2633EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2634
2635/**
2636 * snd_soc_dapm_get_volsw - dapm mixer get callback
2637 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002638 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002639 *
2640 * Callback to get the value of a dapm mixer control.
2641 *
2642 * Returns 0 for success.
2643 */
2644int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2645 struct snd_ctl_elem_value *ucontrol)
2646{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002647 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2648 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002649 struct soc_mixer_control *mc =
2650 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002651 unsigned int reg = mc->reg;
2652 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002653 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002654 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002655 unsigned int invert = mc->invert;
2656
2657 if (snd_soc_volsw_is_stereo(mc))
2658 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002659 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002660 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002661
Richard Purdie2b97eab2006-10-06 18:32:18 +02002662 ucontrol->value.integer.value[0] =
2663 (snd_soc_read(widget->codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002664 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002665 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002666 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002667
2668 return 0;
2669}
2670EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2671
2672/**
2673 * snd_soc_dapm_put_volsw - dapm mixer set callback
2674 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002675 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002676 *
2677 * Callback to set the value of a dapm mixer control.
2678 *
2679 * Returns 0 for success.
2680 */
2681int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2682 struct snd_ctl_elem_value *ucontrol)
2683{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002684 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2685 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2686 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002687 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002688 struct soc_mixer_control *mc =
2689 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002690 unsigned int reg = mc->reg;
2691 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002692 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002693 unsigned int mask = (1 << fls(max)) - 1;
2694 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002695 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002696 int connect, change;
2697 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002698 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002699
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002700 if (snd_soc_volsw_is_stereo(mc))
2701 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002702 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002703 kcontrol->id.name);
2704
Richard Purdie2b97eab2006-10-06 18:32:18 +02002705 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002706 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002707
2708 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002709 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002710 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002711 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002712
Liam Girdwood3cd04342012-03-09 12:02:08 +00002713 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002714
Stephen Warrene9cf7042011-01-27 14:54:05 -07002715 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002716 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002717 for (wi = 0; wi < wlist->num_widgets; wi++) {
2718 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002719
Stephen Warrenfafd2172011-04-28 17:38:00 -06002720 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002721
Stephen Warrenfafd2172011-04-28 17:38:00 -06002722 update.kcontrol = kcontrol;
2723 update.widget = widget;
2724 update.reg = reg;
2725 update.mask = mask;
2726 update.val = val;
2727 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002728
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002729 soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002730
2731 widget->dapm->update = NULL;
2732 }
Mark Brown283375c2009-12-07 18:09:03 +00002733 }
2734
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002735 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002736 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002737}
2738EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2739
2740/**
2741 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2742 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002743 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002744 *
2745 * Callback to get the value of a dapm enumerated double mixer control.
2746 *
2747 * Returns 0 for success.
2748 */
2749int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2750 struct snd_ctl_elem_value *ucontrol)
2751{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002752 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2753 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002754 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002755 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002756
Richard Purdie2b97eab2006-10-06 18:32:18 +02002757 val = snd_soc_read(widget->codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002758 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002759 if (e->shift_l != e->shift_r)
2760 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002761 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002762
2763 return 0;
2764}
2765EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2766
2767/**
2768 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2769 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002770 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002771 *
2772 * Callback to set the value of a dapm enumerated double mixer control.
2773 *
2774 * Returns 0 for success.
2775 */
2776int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2777 struct snd_ctl_elem_value *ucontrol)
2778{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002779 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2780 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2781 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002782 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002783 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002784 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002785 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002786 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002787 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002788
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002789 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002790 return -EINVAL;
2791 mux = ucontrol->value.enumerated.item[0];
2792 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002793 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002794 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002795 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002796 return -EINVAL;
2797 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002798 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002799 }
2800
Liam Girdwood3cd04342012-03-09 12:02:08 +00002801 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002802
Mark Brown3a655772009-10-05 17:23:30 +01002803 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002804 if (change) {
2805 for (wi = 0; wi < wlist->num_widgets; wi++) {
2806 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002807
Stephen Warrenfafd2172011-04-28 17:38:00 -06002808 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002809
Stephen Warrenfafd2172011-04-28 17:38:00 -06002810 update.kcontrol = kcontrol;
2811 update.widget = widget;
2812 update.reg = e->reg;
2813 update.mask = mask;
2814 update.val = val;
2815 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002816
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002817 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002818
Stephen Warrenfafd2172011-04-28 17:38:00 -06002819 widget->dapm->update = NULL;
2820 }
2821 }
2822
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002823 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002824 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002825}
2826EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2827
2828/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002829 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2830 * @kcontrol: mixer control
2831 * @ucontrol: control element information
2832 *
2833 * Returns 0 for success.
2834 */
2835int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2836 struct snd_ctl_elem_value *ucontrol)
2837{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002838 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2839 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002840
2841 ucontrol->value.enumerated.item[0] = widget->value;
2842
2843 return 0;
2844}
2845EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2846
2847/**
2848 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2849 * @kcontrol: mixer control
2850 * @ucontrol: control element information
2851 *
2852 * Returns 0 for success.
2853 */
2854int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2855 struct snd_ctl_elem_value *ucontrol)
2856{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002857 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2858 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2859 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002860 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002861 struct soc_enum *e =
2862 (struct soc_enum *)kcontrol->private_value;
2863 int change;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002864 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002865
2866 if (ucontrol->value.enumerated.item[0] >= e->max)
2867 return -EINVAL;
2868
Liam Girdwood3cd04342012-03-09 12:02:08 +00002869 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002870
2871 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002872 if (change) {
2873 for (wi = 0; wi < wlist->num_widgets; wi++) {
2874 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002875
Stephen Warrenfafd2172011-04-28 17:38:00 -06002876 widget->value = ucontrol->value.enumerated.item[0];
2877
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002878 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002879 }
2880 }
2881
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002882 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002883 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002884}
2885EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2886
2887/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002888 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2889 * callback
2890 * @kcontrol: mixer control
2891 * @ucontrol: control element information
2892 *
2893 * Callback to get the value of a dapm semi enumerated double mixer control.
2894 *
2895 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2896 * used for handling bitfield coded enumeration for example.
2897 *
2898 * Returns 0 for success.
2899 */
2900int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2901 struct snd_ctl_elem_value *ucontrol)
2902{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002903 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2904 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002905 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002906 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002907
2908 reg_val = snd_soc_read(widget->codec, e->reg);
2909 val = (reg_val >> e->shift_l) & e->mask;
2910 for (mux = 0; mux < e->max; mux++) {
2911 if (val == e->values[mux])
2912 break;
2913 }
2914 ucontrol->value.enumerated.item[0] = mux;
2915 if (e->shift_l != e->shift_r) {
2916 val = (reg_val >> e->shift_r) & e->mask;
2917 for (mux = 0; mux < e->max; mux++) {
2918 if (val == e->values[mux])
2919 break;
2920 }
2921 ucontrol->value.enumerated.item[1] = mux;
2922 }
2923
2924 return 0;
2925}
2926EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2927
2928/**
2929 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2930 * callback
2931 * @kcontrol: mixer control
2932 * @ucontrol: control element information
2933 *
2934 * Callback to set the value of a dapm semi enumerated double mixer control.
2935 *
2936 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2937 * used for handling bitfield coded enumeration for example.
2938 *
2939 * Returns 0 for success.
2940 */
2941int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2942 struct snd_ctl_elem_value *ucontrol)
2943{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002944 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2945 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2946 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002947 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002948 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002949 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002950 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002951 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002952 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002953
2954 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2955 return -EINVAL;
2956 mux = ucontrol->value.enumerated.item[0];
2957 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2958 mask = e->mask << e->shift_l;
2959 if (e->shift_l != e->shift_r) {
2960 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2961 return -EINVAL;
2962 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2963 mask |= e->mask << e->shift_r;
2964 }
2965
Liam Girdwood3cd04342012-03-09 12:02:08 +00002966 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002967
Mark Brown3a655772009-10-05 17:23:30 +01002968 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002969 if (change) {
2970 for (wi = 0; wi < wlist->num_widgets; wi++) {
2971 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002972
Stephen Warrenfafd2172011-04-28 17:38:00 -06002973 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002974
Stephen Warrenfafd2172011-04-28 17:38:00 -06002975 update.kcontrol = kcontrol;
2976 update.widget = widget;
2977 update.reg = e->reg;
2978 update.mask = mask;
2979 update.val = val;
2980 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002981
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002982 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002983
Stephen Warrenfafd2172011-04-28 17:38:00 -06002984 widget->dapm->update = NULL;
2985 }
2986 }
2987
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002988 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002989 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002990}
2991EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2992
2993/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002994 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2995 *
2996 * @kcontrol: mixer control
2997 * @uinfo: control element information
2998 *
2999 * Callback to provide information about a pin switch control.
3000 */
3001int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3002 struct snd_ctl_elem_info *uinfo)
3003{
3004 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3005 uinfo->count = 1;
3006 uinfo->value.integer.min = 0;
3007 uinfo->value.integer.max = 1;
3008
3009 return 0;
3010}
3011EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3012
3013/**
3014 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3015 *
3016 * @kcontrol: mixer control
3017 * @ucontrol: Value
3018 */
3019int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3020 struct snd_ctl_elem_value *ucontrol)
3021{
Mark Brown48a8c392012-02-14 17:11:15 -08003022 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003023 const char *pin = (const char *)kcontrol->private_value;
3024
Liam Girdwood3cd04342012-03-09 12:02:08 +00003025 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003026
3027 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003028 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003029
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003030 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003031
3032 return 0;
3033}
3034EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3035
3036/**
3037 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3038 *
3039 * @kcontrol: mixer control
3040 * @ucontrol: Value
3041 */
3042int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3043 struct snd_ctl_elem_value *ucontrol)
3044{
Mark Brown48a8c392012-02-14 17:11:15 -08003045 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003046 const char *pin = (const char *)kcontrol->private_value;
3047
Liam Girdwood3cd04342012-03-09 12:02:08 +00003048 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003049
3050 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003051 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003052 else
Mark Brown48a8c392012-02-14 17:11:15 -08003053 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003054
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003055 mutex_unlock(&card->dapm_mutex);
3056
Mark Brown48a8c392012-02-14 17:11:15 -08003057 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003058 return 0;
3059}
3060EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3061
Mark Brown5ba06fc2012-02-16 11:07:13 -08003062static struct snd_soc_dapm_widget *
3063snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3064 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003065{
3066 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003067 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003068
3069 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003070 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003071
Mark Brown62ea8742012-01-21 21:14:48 +00003072 switch (w->id) {
3073 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003074 w->regulator = devm_regulator_get(dapm->dev, w->name);
3075 if (IS_ERR(w->regulator)) {
3076 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003077 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003078 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003079 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003080 }
Mark Brown8784c772013-01-10 19:33:47 +00003081
3082 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
3083 ret = regulator_allow_bypass(w->regulator, true);
3084 if (ret != 0)
3085 dev_warn(w->dapm->dev,
3086 "ASoC: Failed to unbypass %s: %d\n",
3087 w->name, ret);
3088 }
Mark Brown62ea8742012-01-21 21:14:48 +00003089 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003090 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003091#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003092 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003093 if (IS_ERR(w->clk)) {
3094 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003095 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003096 w->name, ret);
3097 return NULL;
3098 }
Mark Brownec029952012-06-04 08:16:20 +01003099#else
3100 return NULL;
3101#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003102 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003103 default:
3104 break;
3105 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003106
Mark Brown88e8b9a2011-03-02 18:18:24 +00003107 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003108 w->name = kasprintf(GFP_KERNEL, "%s %s",
3109 dapm->codec->name_prefix, widget->name);
3110 else
3111 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3112
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003113 if (w->name == NULL) {
3114 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003115 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003116 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003117
Mark Brown7ca3a182011-10-08 14:04:50 +01003118 switch (w->id) {
3119 case snd_soc_dapm_switch:
3120 case snd_soc_dapm_mixer:
3121 case snd_soc_dapm_mixer_named_ctl:
3122 w->power_check = dapm_generic_check_power;
3123 break;
3124 case snd_soc_dapm_mux:
3125 case snd_soc_dapm_virt_mux:
3126 case snd_soc_dapm_value_mux:
3127 w->power_check = dapm_generic_check_power;
3128 break;
Mark Brown46162742013-06-05 19:36:11 +01003129 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003130 w->power_check = dapm_adc_check_power;
3131 break;
Mark Brown46162742013-06-05 19:36:11 +01003132 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003133 w->power_check = dapm_dac_check_power;
3134 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003135 case snd_soc_dapm_adc:
3136 case snd_soc_dapm_aif_out:
3137 case snd_soc_dapm_dac:
3138 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003139 case snd_soc_dapm_pga:
3140 case snd_soc_dapm_out_drv:
3141 case snd_soc_dapm_input:
3142 case snd_soc_dapm_output:
3143 case snd_soc_dapm_micbias:
3144 case snd_soc_dapm_spk:
3145 case snd_soc_dapm_hp:
3146 case snd_soc_dapm_mic:
3147 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003148 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003149 w->power_check = dapm_generic_check_power;
3150 break;
3151 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003152 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003153 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003154 w->power_check = dapm_supply_check_power;
3155 break;
3156 default:
3157 w->power_check = dapm_always_on_check_power;
3158 break;
3159 }
3160
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003161 w->dapm = dapm;
3162 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003163 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003164 INIT_LIST_HEAD(&w->sources);
3165 INIT_LIST_HEAD(&w->sinks);
3166 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003167 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003168 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003169
3170 /* machine layer set ups unconnected pins and insertions */
3171 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003172 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003173}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003174
3175/**
Mark Brown4ba13272008-05-13 14:51:19 +02003176 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003177 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003178 * @widget: widget array
3179 * @num: number of widgets
3180 *
3181 * Creates new DAPM controls based upon the templates.
3182 *
3183 * Returns 0 for success else error.
3184 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003185int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003186 const struct snd_soc_dapm_widget *widget,
3187 int num)
3188{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003189 struct snd_soc_dapm_widget *w;
3190 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003191 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003192
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003193 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003194 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003195 w = snd_soc_dapm_new_control(dapm, widget);
3196 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003197 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003198 "ASoC: Failed to create DAPM control %s\n",
3199 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003200 ret = -ENOMEM;
3201 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003202 }
Mark Brown4ba13272008-05-13 14:51:19 +02003203 widget++;
3204 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003205 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003206 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003207}
3208EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3209
Mark Brownc74184e2012-04-04 22:12:09 +01003210static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3211 struct snd_kcontrol *kcontrol, int event)
3212{
3213 struct snd_soc_dapm_path *source_p, *sink_p;
3214 struct snd_soc_dai *source, *sink;
3215 const struct snd_soc_pcm_stream *config = w->params;
3216 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003217 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003218 u64 fmt;
3219 int ret;
3220
3221 BUG_ON(!config);
3222 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3223
3224 /* We only support a single source and sink, pick the first */
3225 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3226 list_sink);
3227 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3228 list_source);
3229
3230 BUG_ON(!source_p || !sink_p);
3231 BUG_ON(!sink_p->source || !source_p->sink);
3232 BUG_ON(!source_p->source || !sink_p->sink);
3233
3234 source = source_p->source->priv;
3235 sink = sink_p->sink->priv;
3236
3237 /* Be a little careful as we don't want to overflow the mask array */
3238 if (config->formats) {
3239 fmt = ffs(config->formats) - 1;
3240 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003241 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003242 config->formats);
3243 fmt = 0;
3244 }
3245
3246 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003247 params = kzalloc(sizeof(*params), GFP_KERNEL);
3248 if (!params) {
3249 ret = -ENOMEM;
3250 goto out;
3251 }
3252 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003253
Mark Brown9747cec2012-04-26 19:12:21 +01003254 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003255 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003256 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003257 config->rate_max;
3258
Mark Brown9747cec2012-04-26 19:12:21 +01003259 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003260 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003261 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003262 = config->channels_max;
3263
3264 memset(&substream, 0, sizeof(substream));
3265
3266 switch (event) {
3267 case SND_SOC_DAPM_PRE_PMU:
3268 if (source->driver->ops && source->driver->ops->hw_params) {
3269 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3270 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003271 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003272 if (ret != 0) {
3273 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003274 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003275 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003276 }
3277 }
3278
3279 if (sink->driver->ops && sink->driver->ops->hw_params) {
3280 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003281 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003282 sink);
3283 if (ret != 0) {
3284 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003285 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003286 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003287 }
3288 }
3289 break;
3290
3291 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003292 ret = snd_soc_dai_digital_mute(sink, 0,
3293 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003294 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003295 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003296 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003297 break;
3298
3299 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003300 ret = snd_soc_dai_digital_mute(sink, 1,
3301 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003302 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003303 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003304 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003305 break;
3306
3307 default:
3308 BUG();
3309 return -EINVAL;
3310 }
3311
Mark Brown9747cec2012-04-26 19:12:21 +01003312out:
3313 kfree(params);
3314 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003315}
3316
3317int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3318 const struct snd_soc_pcm_stream *params,
3319 struct snd_soc_dapm_widget *source,
3320 struct snd_soc_dapm_widget *sink)
3321{
3322 struct snd_soc_dapm_route routes[2];
3323 struct snd_soc_dapm_widget template;
3324 struct snd_soc_dapm_widget *w;
3325 size_t len;
3326 char *link_name;
3327
3328 len = strlen(source->name) + strlen(sink->name) + 2;
3329 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3330 if (!link_name)
3331 return -ENOMEM;
3332 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3333
3334 memset(&template, 0, sizeof(template));
3335 template.reg = SND_SOC_NOPM;
3336 template.id = snd_soc_dapm_dai_link;
3337 template.name = link_name;
3338 template.event = snd_soc_dai_link_event;
3339 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3340 SND_SOC_DAPM_PRE_PMD;
3341
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003342 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003343
3344 w = snd_soc_dapm_new_control(&card->dapm, &template);
3345 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003346 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003347 link_name);
3348 return -ENOMEM;
3349 }
3350
3351 w->params = params;
3352
3353 memset(&routes, 0, sizeof(routes));
3354
3355 routes[0].source = source->name;
3356 routes[0].sink = link_name;
3357 routes[1].source = link_name;
3358 routes[1].sink = sink->name;
3359
3360 return snd_soc_dapm_add_routes(&card->dapm, routes,
3361 ARRAY_SIZE(routes));
3362}
3363
Mark Brown888df392012-02-16 19:37:51 -08003364int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3365 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003366{
Mark Brown888df392012-02-16 19:37:51 -08003367 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003368 struct snd_soc_dapm_widget *w;
3369
Mark Brown888df392012-02-16 19:37:51 -08003370 WARN_ON(dapm->dev != dai->dev);
3371
3372 memset(&template, 0, sizeof(template));
3373 template.reg = SND_SOC_NOPM;
3374
3375 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003376 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003377 template.name = dai->driver->playback.stream_name;
3378 template.sname = dai->driver->playback.stream_name;
3379
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003380 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003381 template.name);
3382
3383 w = snd_soc_dapm_new_control(dapm, &template);
3384 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003385 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003386 dai->driver->playback.stream_name);
3387 }
3388
3389 w->priv = dai;
3390 dai->playback_widget = w;
3391 }
3392
3393 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003394 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003395 template.name = dai->driver->capture.stream_name;
3396 template.sname = dai->driver->capture.stream_name;
3397
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003398 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003399 template.name);
3400
3401 w = snd_soc_dapm_new_control(dapm, &template);
3402 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003403 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003404 dai->driver->capture.stream_name);
3405 }
3406
3407 w->priv = dai;
3408 dai->capture_widget = w;
3409 }
3410
3411 return 0;
3412}
3413
3414int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3415{
3416 struct snd_soc_dapm_widget *dai_w, *w;
3417 struct snd_soc_dai *dai;
3418 struct snd_soc_dapm_route r;
3419
3420 memset(&r, 0, sizeof(r));
3421
3422 /* For each DAI widget... */
3423 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003424 switch (dai_w->id) {
3425 case snd_soc_dapm_dai_in:
3426 case snd_soc_dapm_dai_out:
3427 break;
3428 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003429 continue;
Mark Brown46162742013-06-05 19:36:11 +01003430 }
Mark Brown888df392012-02-16 19:37:51 -08003431
3432 dai = dai_w->priv;
3433
3434 /* ...find all widgets with the same stream and link them */
3435 list_for_each_entry(w, &card->widgets, list) {
3436 if (w->dapm != dai_w->dapm)
3437 continue;
3438
Mark Brown46162742013-06-05 19:36:11 +01003439 switch (w->id) {
3440 case snd_soc_dapm_dai_in:
3441 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003442 continue;
Mark Brown46162742013-06-05 19:36:11 +01003443 default:
3444 break;
3445 }
Mark Brown888df392012-02-16 19:37:51 -08003446
3447 if (!w->sname)
3448 continue;
3449
3450 if (dai->driver->playback.stream_name &&
3451 strstr(w->sname,
3452 dai->driver->playback.stream_name)) {
3453 r.source = dai->playback_widget->name;
3454 r.sink = w->name;
3455 dev_dbg(dai->dev, "%s -> %s\n",
3456 r.source, r.sink);
3457
3458 snd_soc_dapm_add_route(w->dapm, &r);
3459 }
3460
3461 if (dai->driver->capture.stream_name &&
3462 strstr(w->sname,
3463 dai->driver->capture.stream_name)) {
3464 r.source = w->name;
3465 r.sink = dai->capture_widget->name;
3466 dev_dbg(dai->dev, "%s -> %s\n",
3467 r.source, r.sink);
3468
3469 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003470 }
3471 }
3472 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003473
Mark Brown888df392012-02-16 19:37:51 -08003474 return 0;
3475}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003476
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003477static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3478 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003479{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003480
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003481 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3482 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3483 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003484
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003485 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3486 w_cpu = cpu_dai->playback_widget;
3487 w_codec = codec_dai->playback_widget;
3488 } else {
3489 w_cpu = cpu_dai->capture_widget;
3490 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003491 }
3492
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003493 if (w_cpu) {
3494
3495 dapm_mark_dirty(w_cpu, "stream event");
3496
3497 switch (event) {
3498 case SND_SOC_DAPM_STREAM_START:
3499 w_cpu->active = 1;
3500 break;
3501 case SND_SOC_DAPM_STREAM_STOP:
3502 w_cpu->active = 0;
3503 break;
3504 case SND_SOC_DAPM_STREAM_SUSPEND:
3505 case SND_SOC_DAPM_STREAM_RESUME:
3506 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3507 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3508 break;
3509 }
3510 }
3511
3512 if (w_codec) {
3513
3514 dapm_mark_dirty(w_codec, "stream event");
3515
3516 switch (event) {
3517 case SND_SOC_DAPM_STREAM_START:
3518 w_codec->active = 1;
3519 break;
3520 case SND_SOC_DAPM_STREAM_STOP:
3521 w_codec->active = 0;
3522 break;
3523 case SND_SOC_DAPM_STREAM_SUSPEND:
3524 case SND_SOC_DAPM_STREAM_RESUME:
3525 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3526 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3527 break;
3528 }
3529 }
3530
3531 dapm_power_widgets(&rtd->card->dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003532}
3533
3534/**
3535 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3536 * @rtd: PCM runtime data
3537 * @stream: stream name
3538 * @event: stream event
3539 *
3540 * Sends a stream event to the dapm core. The core then makes any
3541 * necessary widget power changes.
3542 *
3543 * Returns 0 for success else error.
3544 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003545void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3546 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003547{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003548 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003549
Liam Girdwood3cd04342012-03-09 12:02:08 +00003550 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003551 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003552 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003553}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003554
3555/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003556 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003557 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003558 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003559 *
Mark Brown74b8f952009-06-06 11:26:15 +01003560 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003561 * a valid audio route and active audio stream.
3562 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3563 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003564 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003565int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003566{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003567 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003568}
Liam Girdwooda5302182008-07-07 13:35:17 +01003569EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003570
3571/**
Mark Brownda341832010-03-15 19:23:37 +00003572 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003573 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003574 * @pin: pin name
3575 *
3576 * Enables input/output pin regardless of any other state. This is
3577 * intended for use with microphone bias supplies used in microphone
3578 * jack detection.
3579 *
3580 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3581 * do any widget power switching.
3582 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003583int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3584 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003585{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003586 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003587
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003588 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003589 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003590 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003591 }
3592
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003593 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003594 w->connected = 1;
3595 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003596 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003597
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003598 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003599}
3600EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3601
3602/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003603 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003604 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003605 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003606 *
Mark Brown74b8f952009-06-06 11:26:15 +01003607 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003608 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3609 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003610 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003611int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3612 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003613{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003614 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003615}
3616EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3617
3618/**
Mark Brown5817b522008-09-24 11:23:11 +01003619 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003620 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003621 * @pin: pin name
3622 *
3623 * Marks the specified pin as being not connected, disabling it along
3624 * any parent or child widgets. At present this is identical to
3625 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3626 * additional things such as disabling controls which only affect
3627 * paths through the pin.
3628 *
3629 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3630 * do any widget power switching.
3631 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003632int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003633{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003634 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003635}
3636EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3637
3638/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003639 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003640 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003641 * @pin: audio signal pin endpoint (or start point)
3642 *
3643 * Get audio pin status - connected or disconnected.
3644 *
3645 * Returns 1 for connected otherwise 0.
3646 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003647int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3648 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003649{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003650 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003651
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003652 if (w)
3653 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003654
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003655 return 0;
3656}
Liam Girdwooda5302182008-07-07 13:35:17 +01003657EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003658
3659/**
Mark Brown1547aba2010-05-07 21:11:40 +01003660 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003661 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003662 * @pin: audio signal pin endpoint (or start point)
3663 *
3664 * Mark the given endpoint or pin as ignoring suspend. When the
3665 * system is disabled a path between two endpoints flagged as ignoring
3666 * suspend will not be disabled. The path must already be enabled via
3667 * normal means at suspend time, it will not be turned on if it was not
3668 * already enabled.
3669 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003670int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3671 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003672{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003673 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003674
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003675 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003676 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003677 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003678 }
3679
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003680 w->ignore_suspend = 1;
3681
3682 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003683}
3684EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3685
Stephen Warren16332812011-11-23 12:42:04 -07003686static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3687 struct snd_soc_dapm_widget *w)
3688{
3689 struct snd_soc_dapm_path *p;
3690
3691 list_for_each_entry(p, &card->paths, list) {
3692 if ((p->source == w) || (p->sink == w)) {
3693 dev_dbg(card->dev,
3694 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3695 p->source->name, p->source->id, p->source->dapm,
3696 p->sink->name, p->sink->id, p->sink->dapm);
3697
3698 /* Connected to something other than the codec */
3699 if (p->source->dapm != p->sink->dapm)
3700 return true;
3701 /*
3702 * Loopback connection from codec external pin to
3703 * codec external pin
3704 */
3705 if (p->sink->id == snd_soc_dapm_input) {
3706 switch (p->source->id) {
3707 case snd_soc_dapm_output:
3708 case snd_soc_dapm_micbias:
3709 return true;
3710 default:
3711 break;
3712 }
3713 }
3714 }
3715 }
3716
3717 return false;
3718}
3719
3720/**
3721 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3722 * @codec: The codec whose pins should be processed
3723 *
3724 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3725 * which are unused. Pins are used if they are connected externally to the
3726 * codec, whether that be to some other device, or a loop-back connection to
3727 * the codec itself.
3728 */
3729void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3730{
3731 struct snd_soc_card *card = codec->card;
3732 struct snd_soc_dapm_context *dapm = &codec->dapm;
3733 struct snd_soc_dapm_widget *w;
3734
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003735 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003736 &card->dapm, &codec->dapm);
3737
3738 list_for_each_entry(w, &card->widgets, list) {
3739 if (w->dapm != dapm)
3740 continue;
3741 switch (w->id) {
3742 case snd_soc_dapm_input:
3743 case snd_soc_dapm_output:
3744 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003745 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003746 w->name);
3747 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003748 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003749 "... Not in map; disabling\n");
3750 snd_soc_dapm_nc_pin(dapm, w->name);
3751 }
3752 break;
3753 default:
3754 break;
3755 }
3756 }
3757}
3758
Mark Brown1547aba2010-05-07 21:11:40 +01003759/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003760 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003761 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003762 *
3763 * Free all dapm widgets and resources.
3764 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003765void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003766{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003767 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003768 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003769 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003770 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003771}
3772EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3773
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003774static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003775{
Liam Girdwood01005a72012-07-06 16:57:05 +01003776 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003777 struct snd_soc_dapm_widget *w;
3778 LIST_HEAD(down_list);
3779 int powerdown = 0;
3780
Liam Girdwood01005a72012-07-06 16:57:05 +01003781 mutex_lock(&card->dapm_mutex);
3782
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003783 list_for_each_entry(w, &dapm->card->widgets, list) {
3784 if (w->dapm != dapm)
3785 continue;
Mark Brown51737472009-06-22 13:16:51 +01003786 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003787 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003788 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003789 powerdown = 1;
3790 }
3791 }
3792
3793 /* If there were no widgets to power down we're already in
3794 * standby.
3795 */
3796 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003797 if (dapm->bias_level == SND_SOC_BIAS_ON)
3798 snd_soc_dapm_set_bias_level(dapm,
3799 SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003800 dapm_seq_run(dapm, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003801 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3802 snd_soc_dapm_set_bias_level(dapm,
3803 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003804 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003805
3806 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003807}
Mark Brown51737472009-06-22 13:16:51 +01003808
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003809/*
3810 * snd_soc_dapm_shutdown - callback for system shutdown
3811 */
3812void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3813{
3814 struct snd_soc_codec *codec;
3815
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003816 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003817 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003818 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3819 snd_soc_dapm_set_bias_level(&codec->dapm,
3820 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003821 }
Mark Brown51737472009-06-22 13:16:51 +01003822}
3823
Richard Purdie2b97eab2006-10-06 18:32:18 +02003824/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003825MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003826MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3827MODULE_LICENSE("GPL");