blob: 1e36bc81e5af1fb015ffef62f628781910984bbb [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 Brown888df392012-02-16 19:37:51 -080058 [snd_soc_dapm_dai] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010059 [snd_soc_dapm_aif_in] = 3,
60 [snd_soc_dapm_aif_out] = 3,
61 [snd_soc_dapm_mic] = 4,
62 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000063 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010064 [snd_soc_dapm_value_mux] = 5,
65 [snd_soc_dapm_dac] = 6,
66 [snd_soc_dapm_mixer] = 7,
67 [snd_soc_dapm_mixer_named_ctl] = 7,
68 [snd_soc_dapm_pga] = 8,
69 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060070 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010071 [snd_soc_dapm_hp] = 10,
72 [snd_soc_dapm_spk] = 10,
Mark Brown7e1f7c82012-04-12 17:29:36 +010073 [snd_soc_dapm_line] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010074 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020075};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000076
Richard Purdie2b97eab2006-10-06 18:32:18 +020077static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010078 [snd_soc_dapm_pre] = 0,
79 [snd_soc_dapm_adc] = 1,
80 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010081 [snd_soc_dapm_spk] = 2,
Mark Brown7e1f7c82012-04-12 17:29:36 +010082 [snd_soc_dapm_line] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060083 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010084 [snd_soc_dapm_pga] = 4,
85 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010086 [snd_soc_dapm_mixer] = 5,
87 [snd_soc_dapm_dac] = 6,
88 [snd_soc_dapm_mic] = 7,
89 [snd_soc_dapm_micbias] = 8,
90 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000091 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010092 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010093 [snd_soc_dapm_aif_in] = 10,
94 [snd_soc_dapm_aif_out] = 10,
Mark Brown888df392012-02-16 19:37:51 -080095 [snd_soc_dapm_dai] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +010096 [snd_soc_dapm_dai_link] = 11,
Ola Liljad7e7eb92012-05-24 15:26:25 +020097 [snd_soc_dapm_clock_supply] = 12,
Mark Brownc74184e2012-04-04 22:12:09 +010098 [snd_soc_dapm_regulator_supply] = 12,
99 [snd_soc_dapm_supply] = 12,
100 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200101};
102
Troy Kisky12ef1932008-10-13 17:42:14 -0700103static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100104{
105 if (pop_time)
106 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
107}
108
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200109static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100110{
111 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200112 char *buf;
113
114 if (!pop_time)
115 return;
116
117 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
118 if (buf == NULL)
119 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100120
121 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200122 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100123 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100124 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200125
126 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100127}
128
Mark Browndb432b42011-10-03 21:06:40 +0100129static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
130{
131 return !list_empty(&w->dirty);
132}
133
Mark Brown25c77c52011-10-08 13:36:03 +0100134void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100135{
Mark Brown75c1f892011-10-04 22:28:08 +0100136 if (!dapm_dirty_widget(w)) {
137 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
138 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100139 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100140 }
Mark Browndb432b42011-10-03 21:06:40 +0100141}
Mark Brown25c77c52011-10-08 13:36:03 +0100142EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100143
Mark Browne2d32ff2012-08-31 17:38:32 -0700144void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
145{
146 struct snd_soc_card *card = dapm->card;
147 struct snd_soc_dapm_widget *w;
148
149 mutex_lock(&card->dapm_mutex);
150
151 list_for_each_entry(w, &card->widgets, list) {
152 switch (w->id) {
153 case snd_soc_dapm_input:
154 case snd_soc_dapm_output:
155 dapm_mark_dirty(w, "Rechecking inputs and outputs");
156 break;
157 default:
158 break;
159 }
160 }
161
162 mutex_unlock(&card->dapm_mutex);
163}
164EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
165
Richard Purdie2b97eab2006-10-06 18:32:18 +0200166/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100167static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200168 const struct snd_soc_dapm_widget *_widget)
169{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100170 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200171}
172
Liam Girdwood48056082011-07-20 12:23:33 +0100173/* get snd_card from DAPM context */
174static inline struct snd_card *dapm_get_snd_card(
175 struct snd_soc_dapm_context *dapm)
176{
177 if (dapm->codec)
178 return dapm->codec->card->snd_card;
179 else if (dapm->platform)
180 return dapm->platform->card->snd_card;
181 else
182 BUG();
183
184 /* unreachable */
185 return NULL;
186}
187
188/* get soc_card from DAPM context */
189static inline struct snd_soc_card *dapm_get_soc_card(
190 struct snd_soc_dapm_context *dapm)
191{
192 if (dapm->codec)
193 return dapm->codec->card;
194 else if (dapm->platform)
195 return dapm->platform->card;
196 else
197 BUG();
198
199 /* unreachable */
200 return NULL;
201}
202
Liam Girdwood6c120e12012-02-15 15:15:34 +0000203static void dapm_reset(struct snd_soc_card *card)
204{
205 struct snd_soc_dapm_widget *w;
206
207 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
208
209 list_for_each_entry(w, &card->widgets, list) {
210 w->power_checked = false;
211 w->inputs = -1;
212 w->outputs = -1;
213 }
214}
215
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100216static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
217{
218 if (w->codec)
219 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100220 else if (w->platform)
221 return snd_soc_platform_read(w->platform, reg);
222
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000223 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100224 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100225}
226
227static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
228{
229 if (w->codec)
230 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100231 else if (w->platform)
232 return snd_soc_platform_write(w->platform, reg, val);
233
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000234 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100235 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100236}
237
Liam Girdwood49575fb52012-03-06 18:16:19 +0000238static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
239{
Mark Browne06ab3b2012-03-06 23:58:22 +0000240 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000241 mutex_lock(&w->codec->mutex);
242 else if (w->platform)
243 mutex_lock(&w->platform->mutex);
244}
245
246static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
247{
Mark Browne06ab3b2012-03-06 23:58:22 +0000248 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000249 mutex_unlock(&w->codec->mutex);
250 else if (w->platform)
251 mutex_unlock(&w->platform->mutex);
252}
253
254static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100255 unsigned short reg, unsigned int mask, unsigned int value)
256{
Mark Brown8a713da2011-12-03 12:33:55 +0000257 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100258 unsigned int old, new;
259 int ret;
260
Mark Brown8a713da2011-12-03 12:33:55 +0000261 if (w->codec && w->codec->using_regmap) {
262 ret = regmap_update_bits_check(w->codec->control_data,
263 reg, mask, value, &change);
264 if (ret != 0)
265 return ret;
266 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000267 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000268 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000269 if (ret < 0) {
270 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100271 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000272 }
Mark Brown8a713da2011-12-03 12:33:55 +0000273
274 old = ret;
275 new = (old & ~mask) | (value & mask);
276 change = old != new;
277 if (change) {
278 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000279 if (ret < 0) {
280 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000281 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000282 }
Mark Brown8a713da2011-12-03 12:33:55 +0000283 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000284 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100285 }
286
287 return change;
288}
289
Mark Brown452c5ea2009-05-17 21:41:23 +0100290/**
291 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800292 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100293 * @level: level to configure
294 *
295 * Configure the bias (power) levels for the SoC audio device.
296 *
297 * Returns 0 for success else error.
298 */
Mark Browned5a4c42011-02-18 11:12:42 -0800299static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200300 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100301{
Mark Browned5a4c42011-02-18 11:12:42 -0800302 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100303 int ret = 0;
304
Mark Brown84e90932010-11-04 00:07:02 -0400305 trace_snd_soc_bias_level_start(card, level);
306
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000307 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100308 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100309 if (ret != 0)
310 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100311
Mark Browncc4c6702011-06-06 19:03:34 +0100312 if (dapm->codec) {
313 if (dapm->codec->driver->set_bias_level)
314 ret = dapm->codec->driver->set_bias_level(dapm->codec,
315 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100316 else
317 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100318 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100319 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100320 }
Liam Girdwood41231282012-07-06 16:56:16 +0100321
Mark Brown171ec6b2011-06-06 18:15:19 +0100322 if (ret != 0)
323 goto out;
324
325 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100326 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100327out:
Mark Brown84e90932010-11-04 00:07:02 -0400328 trace_snd_soc_bias_level_done(card, level);
329
Mark Brown452c5ea2009-05-17 21:41:23 +0100330 return ret;
331}
332
Richard Purdie2b97eab2006-10-06 18:32:18 +0200333/* set up initial codec paths */
334static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
335 struct snd_soc_dapm_path *p, int i)
336{
337 switch (w->id) {
338 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000339 case snd_soc_dapm_mixer:
340 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200341 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100342 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600343 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400344 unsigned int reg = mc->reg;
345 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100346 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400347 unsigned int mask = (1 << fls(max)) - 1;
348 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200349
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100350 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200351 val = (val >> shift) & mask;
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200352 if (invert)
353 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200354
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200355 p->connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200356 }
357 break;
358 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600359 struct soc_enum *e = (struct soc_enum *)
360 w->kcontrol_news[i].private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200361 int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200362
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100363 val = soc_widget_read(w, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200364 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200365
366 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100367 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200368 if (!(strcmp(p->name, e->texts[i])) && item == i)
369 p->connect = 1;
370 }
371 }
372 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000373 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600374 struct soc_enum *e = (struct soc_enum *)
375 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000376
377 p->connect = 0;
378 /* since a virtual mux has no backing registers to
379 * decide which path to connect, it will try to match
380 * with the first enumeration. This is to ensure
381 * that the default mux choice (the first) will be
382 * correctly powered up during initialization.
383 */
384 if (!strcmp(p->name, e->texts[0]))
385 p->connect = 1;
386 }
387 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200388 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200389 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600390 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200391 int val, item;
392
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100393 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200394 val = (val >> e->shift_l) & e->mask;
395 for (item = 0; item < e->max; item++) {
396 if (val == e->values[item])
397 break;
398 }
399
400 p->connect = 0;
401 for (i = 0; i < e->max; i++) {
402 if (!(strcmp(p->name, e->texts[i])) && item == i)
403 p->connect = 1;
404 }
405 }
406 break;
Mark Brown56563102011-10-03 22:41:09 +0100407 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200408 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600409 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200410 case snd_soc_dapm_output:
411 case snd_soc_dapm_adc:
412 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000413 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200414 case snd_soc_dapm_dac:
415 case snd_soc_dapm_micbias:
416 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100417 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000418 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200419 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100420 case snd_soc_dapm_aif_in:
421 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800422 case snd_soc_dapm_dai:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200423 case snd_soc_dapm_hp:
424 case snd_soc_dapm_mic:
425 case snd_soc_dapm_spk:
426 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100427 case snd_soc_dapm_dai_link:
Mark Brown56563102011-10-03 22:41:09 +0100428 p->connect = 1;
429 break;
430 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200431 case snd_soc_dapm_pre:
432 case snd_soc_dapm_post:
433 p->connect = 0;
434 break;
435 }
436}
437
Mark Brown74b8f952009-06-06 11:26:15 +0100438/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200439static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200440 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
441 struct snd_soc_dapm_path *path, const char *control_name,
442 const struct snd_kcontrol_new *kcontrol)
443{
444 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
445 int i;
446
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100447 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200448 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200449 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200450 list_add(&path->list_sink, &dest->sources);
451 list_add(&path->list_source, &src->sinks);
452 path->name = (char*)e->texts[i];
453 dapm_set_path_status(dest, path, 0);
454 return 0;
455 }
456 }
457
458 return -ENODEV;
459}
460
Mark Brown74b8f952009-06-06 11:26:15 +0100461/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200462static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200463 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
464 struct snd_soc_dapm_path *path, const char *control_name)
465{
466 int i;
467
468 /* search for mixer kcontrol */
469 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600470 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200471 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200472 list_add(&path->list_sink, &dest->sources);
473 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600474 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200475 dapm_set_path_status(dest, path, i);
476 return 0;
477 }
478 }
479 return -ENODEV;
480}
481
Stephen Warrenaf468002011-04-28 17:38:01 -0600482static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600483 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600484 const struct snd_kcontrol_new *kcontrol_new,
485 struct snd_kcontrol **kcontrol)
486{
487 struct snd_soc_dapm_widget *w;
488 int i;
489
490 *kcontrol = NULL;
491
492 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600493 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
494 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600495 for (i = 0; i < w->num_kcontrols; i++) {
496 if (&w->kcontrol_news[i] == kcontrol_new) {
497 if (w->kcontrols)
498 *kcontrol = w->kcontrols[i];
499 return 1;
500 }
501 }
502 }
503
504 return 0;
505}
506
Richard Purdie2b97eab2006-10-06 18:32:18 +0200507/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200508static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200509{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200510 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200511 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000512 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200513 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000514 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000515 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600516 struct snd_soc_dapm_widget_list *wlist;
517 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000518
519 if (dapm->codec)
520 prefix = dapm->codec->name_prefix;
521 else
522 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200523
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000524 if (prefix)
525 prefix_len = strlen(prefix) + 1;
526 else
527 prefix_len = 0;
528
Richard Purdie2b97eab2006-10-06 18:32:18 +0200529 /* add kcontrol */
530 for (i = 0; i < w->num_kcontrols; i++) {
531
532 /* match name */
533 list_for_each_entry(path, &w->sources, list_sink) {
534
535 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600536 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200537 continue;
538
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200539 if (w->kcontrols[i]) {
540 path->kcontrol = w->kcontrols[i];
541 continue;
542 }
543
Stephen Warrenfafd2172011-04-28 17:38:00 -0600544 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
545 sizeof(struct snd_soc_dapm_widget *),
546 wlist = kzalloc(wlistsize, GFP_KERNEL);
547 if (wlist == NULL) {
548 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000549 "ASoC: can't allocate widget list for %s\n",
Stephen Warrenfafd2172011-04-28 17:38:00 -0600550 w->name);
551 return -ENOMEM;
552 }
553 wlist->num_widgets = 1;
554 wlist->widgets[0] = w;
555
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000556 /* add dapm control with long name.
557 * for dapm_mixer this is the concatenation of the
558 * mixer and kcontrol name.
559 * for dapm_mixer_named_ctl this is simply the
560 * kcontrol name.
561 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600562 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000563 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000564 name_len += 1 + strlen(w->name);
565
Mark Brown219b93f2008-10-28 13:02:31 +0000566 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000567
Stephen Warrenfafd2172011-04-28 17:38:00 -0600568 if (path->long_name == NULL) {
569 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200570 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600571 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200572
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000573 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000574 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000575 /* The control will get a prefix from
576 * the control creation process but
577 * we're also using the same prefix
578 * for widgets so cut the prefix off
579 * the front of the widget name.
580 */
Mark Brown888df392012-02-16 19:37:51 -0800581 snprintf((char *)path->long_name, name_len,
582 "%s %s", w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600583 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000584 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000585 case snd_soc_dapm_mixer_named_ctl:
Mark Brown888df392012-02-16 19:37:51 -0800586 snprintf((char *)path->long_name, name_len,
587 "%s", w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000588 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000589 }
590
Mark Brown888df392012-02-16 19:37:51 -0800591 ((char *)path->long_name)[name_len - 1] = '\0';
Mark Brown219b93f2008-10-28 13:02:31 +0000592
Stephen Warrenfafd2172011-04-28 17:38:00 -0600593 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
594 wlist, path->long_name,
595 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200596 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200597 if (ret < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000598 dev_err(dapm->dev, "ASoC: failed to add widget"
599 " %s dapm kcontrol %s: %d\n",
600 w->name, path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600601 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200602 kfree(path->long_name);
603 path->long_name = NULL;
604 return ret;
605 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600606 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200607 }
608 }
609 return ret;
610}
611
612/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200613static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200614{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200615 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200616 struct snd_soc_dapm_path *path = NULL;
617 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000618 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000619 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000620 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600621 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600622 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600623 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600624 size_t wlistsize;
Mark Brown888df392012-02-16 19:37:51 -0800625 const char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200626
Stephen Warrenaf468002011-04-28 17:38:01 -0600627 if (w->num_kcontrols != 1) {
628 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000629 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600630 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200631 return -EINVAL;
632 }
633
Stephen Warren1007da02011-05-26 09:57:33 -0600634 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600635 &kcontrol);
636 if (kcontrol) {
637 wlist = kcontrol->private_data;
638 wlistentries = wlist->num_widgets + 1;
639 } else {
640 wlist = NULL;
641 wlistentries = 1;
642 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600643 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600644 wlistentries * sizeof(struct snd_soc_dapm_widget *),
645 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600646 if (wlist == NULL) {
647 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000648 "ASoC: can't allocate widget list for %s\n", w->name);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600649 return -ENOMEM;
650 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600651 wlist->num_widgets = wlistentries;
652 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600653
Stephen Warrenaf468002011-04-28 17:38:01 -0600654 if (!kcontrol) {
655 if (dapm->codec)
656 prefix = dapm->codec->name_prefix;
657 else
658 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000659
Stephen Warrenaf468002011-04-28 17:38:01 -0600660 if (shared) {
661 name = w->kcontrol_news[0].name;
662 prefix_len = 0;
663 } else {
664 name = w->name;
665 if (prefix)
666 prefix_len = strlen(prefix) + 1;
667 else
668 prefix_len = 0;
669 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000670
Stephen Warrenaf468002011-04-28 17:38:01 -0600671 /*
672 * The control will get a prefix from the control creation
673 * process but we're also using the same prefix for widgets so
674 * cut the prefix off the front of the widget name.
675 */
676 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
677 name + prefix_len, prefix);
678 ret = snd_ctl_add(card, kcontrol);
679 if (ret < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000680 dev_err(dapm->dev, "ASoC: failed to add kcontrol %s: %d\n",
Mark Brown53daf202011-09-05 10:51:05 -0700681 w->name, ret);
Stephen Warrenaf468002011-04-28 17:38:01 -0600682 kfree(wlist);
683 return ret;
684 }
685 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200686
Stephen Warrenaf468002011-04-28 17:38:01 -0600687 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200688
Stephen Warrenfad59882011-04-28 17:37:59 -0600689 w->kcontrols[0] = kcontrol;
690
Richard Purdie2b97eab2006-10-06 18:32:18 +0200691 list_for_each_entry(path, &w->sources, list_sink)
692 path->kcontrol = kcontrol;
693
Stephen Warrenaf468002011-04-28 17:38:01 -0600694 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200695}
696
697/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200698static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200699{
Mark Browna6c65732010-03-03 17:45:21 +0000700 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200701 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000702 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200703
Mark Browna6c65732010-03-03 17:45:21 +0000704 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200705}
706
707/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200708static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200709{
710 struct snd_soc_dapm_path *p;
711
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200712 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200713 p->walked = 0;
714}
715
Mark Brown99497882010-05-07 20:24:05 +0100716/* We implement power down on suspend by checking the power state of
717 * the ALSA card - when we are suspending the ALSA state for the card
718 * is set to D3.
719 */
720static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
721{
Mark Brown12ea2c72011-03-02 18:17:32 +0000722 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100723
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100725 case SNDRV_CTL_POWER_D3hot:
726 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100727 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000728 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200729 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100730 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100731 default:
732 return 1;
733 }
734}
735
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100736/* add widget to list if it's not already in the list */
737static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
738 struct snd_soc_dapm_widget *w)
739{
740 struct snd_soc_dapm_widget_list *wlist;
741 int wlistsize, wlistentries, i;
742
743 if (*list == NULL)
744 return -EINVAL;
745
746 wlist = *list;
747
748 /* is this widget already in the list */
749 for (i = 0; i < wlist->num_widgets; i++) {
750 if (wlist->widgets[i] == w)
751 return 0;
752 }
753
754 /* allocate some new space */
755 wlistentries = wlist->num_widgets + 1;
756 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
757 wlistentries * sizeof(struct snd_soc_dapm_widget *);
758 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
759 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000760 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100761 w->name);
762 return -ENOMEM;
763 }
764 wlist = *list;
765
766 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000767 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100768 w->name, wlist->num_widgets);
769
770 wlist->widgets[wlist->num_widgets] = w;
771 wlist->num_widgets++;
772 return 1;
773}
774
Richard Purdie2b97eab2006-10-06 18:32:18 +0200775/*
776 * Recursively check for a completed path to an active or physically connected
777 * output widget. Returns number of complete paths.
778 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100779static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
780 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200781{
782 struct snd_soc_dapm_path *path;
783 int con = 0;
784
Mark Brown024dc072011-10-09 11:52:05 +0100785 if (widget->outputs >= 0)
786 return widget->outputs;
787
Mark Brownde02d072011-09-20 21:43:24 +0100788 DAPM_UPDATE_STAT(widget, path_checks);
789
Mark Brown62ea8742012-01-21 21:14:48 +0000790 switch (widget->id) {
791 case snd_soc_dapm_supply:
792 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200793 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100794 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000795 default:
796 break;
797 }
Mark Brown246d0a12009-04-22 18:24:55 +0100798
Mark Brown010ff262009-08-17 17:39:22 +0100799 switch (widget->id) {
800 case snd_soc_dapm_adc:
801 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800802 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100803 if (widget->active) {
804 widget->outputs = snd_soc_dapm_suspend_check(widget);
805 return widget->outputs;
806 }
Mark Brown010ff262009-08-17 17:39:22 +0100807 default:
808 break;
809 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200810
811 if (widget->connected) {
812 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100813 if (widget->id == snd_soc_dapm_output && !widget->ext) {
814 widget->outputs = snd_soc_dapm_suspend_check(widget);
815 return widget->outputs;
816 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200817
818 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100819 if (widget->id == snd_soc_dapm_hp ||
820 widget->id == snd_soc_dapm_spk ||
821 (widget->id == snd_soc_dapm_line &&
822 !list_empty(&widget->sources))) {
823 widget->outputs = snd_soc_dapm_suspend_check(widget);
824 return widget->outputs;
825 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200826 }
827
828 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100829 DAPM_UPDATE_STAT(widget, neighbour_checks);
830
Mark Brownbf3a9e12011-06-13 16:42:29 +0100831 if (path->weak)
832 continue;
833
Richard Purdie2b97eab2006-10-06 18:32:18 +0200834 if (path->walked)
835 continue;
836
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100837 trace_snd_soc_dapm_output_path(widget, path);
838
Richard Purdie2b97eab2006-10-06 18:32:18 +0200839 if (path->sink && path->connect) {
840 path->walked = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100841
842 /* do we need to add this widget to the list ? */
843 if (list) {
844 int err;
845 err = dapm_list_add_widget(list, path->sink);
846 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000847 dev_err(widget->dapm->dev,
848 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100849 widget->name);
850 return con;
851 }
852 }
853
854 con += is_connected_output_ep(path->sink, list);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200855 }
856 }
857
Mark Brown024dc072011-10-09 11:52:05 +0100858 widget->outputs = con;
859
Richard Purdie2b97eab2006-10-06 18:32:18 +0200860 return con;
861}
862
863/*
864 * Recursively check for a completed path to an active or physically connected
865 * input widget. Returns number of complete paths.
866 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100867static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
868 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200869{
870 struct snd_soc_dapm_path *path;
871 int con = 0;
872
Mark Brown024dc072011-10-09 11:52:05 +0100873 if (widget->inputs >= 0)
874 return widget->inputs;
875
Mark Brownde02d072011-09-20 21:43:24 +0100876 DAPM_UPDATE_STAT(widget, path_checks);
877
Mark Brown62ea8742012-01-21 21:14:48 +0000878 switch (widget->id) {
879 case snd_soc_dapm_supply:
880 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200881 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100882 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000883 default:
884 break;
885 }
Mark Brown246d0a12009-04-22 18:24:55 +0100886
Richard Purdie2b97eab2006-10-06 18:32:18 +0200887 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100888 switch (widget->id) {
889 case snd_soc_dapm_dac:
890 case snd_soc_dapm_aif_in:
Mark Brown888df392012-02-16 19:37:51 -0800891 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100892 if (widget->active) {
893 widget->inputs = snd_soc_dapm_suspend_check(widget);
894 return widget->inputs;
895 }
Mark Brown010ff262009-08-17 17:39:22 +0100896 default:
897 break;
898 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200899
900 if (widget->connected) {
901 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100902 if (widget->id == snd_soc_dapm_input && !widget->ext) {
903 widget->inputs = snd_soc_dapm_suspend_check(widget);
904 return widget->inputs;
905 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200906
907 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100908 if (widget->id == snd_soc_dapm_vmid) {
909 widget->inputs = snd_soc_dapm_suspend_check(widget);
910 return widget->inputs;
911 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200912
913 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300914 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100915 (widget->id == snd_soc_dapm_line &&
916 !list_empty(&widget->sinks))) {
917 widget->inputs = snd_soc_dapm_suspend_check(widget);
918 return widget->inputs;
919 }
920
Mark Brown1ab97c82011-11-27 16:21:51 +0000921 /* signal generator */
922 if (widget->id == snd_soc_dapm_siggen) {
923 widget->inputs = snd_soc_dapm_suspend_check(widget);
924 return widget->inputs;
925 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200926 }
927
928 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +0100929 DAPM_UPDATE_STAT(widget, neighbour_checks);
930
Mark Brownbf3a9e12011-06-13 16:42:29 +0100931 if (path->weak)
932 continue;
933
Richard Purdie2b97eab2006-10-06 18:32:18 +0200934 if (path->walked)
935 continue;
936
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100937 trace_snd_soc_dapm_input_path(widget, path);
938
Richard Purdie2b97eab2006-10-06 18:32:18 +0200939 if (path->source && path->connect) {
940 path->walked = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100941
942 /* do we need to add this widget to the list ? */
943 if (list) {
944 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +0100945 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100946 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000947 dev_err(widget->dapm->dev,
948 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100949 widget->name);
950 return con;
951 }
952 }
953
954 con += is_connected_input_ep(path->source, list);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200955 }
956 }
957
Mark Brown024dc072011-10-09 11:52:05 +0100958 widget->inputs = con;
959
Richard Purdie2b97eab2006-10-06 18:32:18 +0200960 return con;
961}
962
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100963/**
964 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
965 * @dai: the soc DAI.
966 * @stream: stream direction.
967 * @list: list of active widgets for this stream.
968 *
969 * Queries DAPM graph as to whether an valid audio stream path exists for
970 * the initial stream specified by name. This takes into account
971 * current mixer and mux kcontrol settings. Creates list of valid widgets.
972 *
973 * Returns the number of valid paths or negative error.
974 */
975int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
976 struct snd_soc_dapm_widget_list **list)
977{
978 struct snd_soc_card *card = dai->card;
979 int paths;
980
981 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
982 dapm_reset(card);
983
984 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
985 paths = is_connected_output_ep(dai->playback_widget, list);
986 else
Liam Girdwoodd298caa2012-06-01 18:03:00 +0100987 paths = is_connected_input_ep(dai->capture_widget, list);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100988
989 trace_snd_soc_dapm_connected(paths, stream);
990 dapm_clear_walk(&card->dapm);
991 mutex_unlock(&card->dapm_mutex);
992
993 return paths;
994}
995
Richard Purdie2b97eab2006-10-06 18:32:18 +0200996/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300997 * Handler for generic register modifier widget.
998 */
999int dapm_reg_event(struct snd_soc_dapm_widget *w,
1000 struct snd_kcontrol *kcontrol, int event)
1001{
1002 unsigned int val;
1003
1004 if (SND_SOC_DAPM_EVENT_ON(event))
1005 val = w->on_val;
1006 else
1007 val = w->off_val;
1008
Liam Girdwood49575fb52012-03-06 18:16:19 +00001009 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001010 w->mask << w->shift, val << w->shift);
1011
1012 return 0;
1013}
Mark Brown11589412008-07-29 11:42:23 +01001014EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001015
Mark Brown62ea8742012-01-21 21:14:48 +00001016/*
1017 * Handler for regulator supply widget.
1018 */
1019int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1020 struct snd_kcontrol *kcontrol, int event)
1021{
Mark Brownc05b84d2012-09-07 12:57:11 +08001022 int ret;
1023
1024 if (SND_SOC_DAPM_EVENT_ON(event)) {
1025 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
1026 ret = regulator_allow_bypass(w->regulator, true);
1027 if (ret != 0)
1028 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001029 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001030 w->name, ret);
1031 }
1032
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001033 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001034 } else {
1035 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
1036 ret = regulator_allow_bypass(w->regulator, false);
1037 if (ret != 0)
1038 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001039 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001040 w->name, ret);
1041 }
1042
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001043 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001044 }
Mark Brown62ea8742012-01-21 21:14:48 +00001045}
1046EXPORT_SYMBOL_GPL(dapm_regulator_event);
1047
Ola Liljad7e7eb92012-05-24 15:26:25 +02001048/*
1049 * Handler for clock supply widget.
1050 */
1051int dapm_clock_event(struct snd_soc_dapm_widget *w,
1052 struct snd_kcontrol *kcontrol, int event)
1053{
1054 if (!w->clk)
1055 return -EIO;
1056
Mark Brownec029952012-06-04 08:16:20 +01001057#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001058 if (SND_SOC_DAPM_EVENT_ON(event)) {
1059 return clk_enable(w->clk);
1060 } else {
1061 clk_disable(w->clk);
1062 return 0;
1063 }
Mark Brownec029952012-06-04 08:16:20 +01001064#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001065 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001066}
1067EXPORT_SYMBOL_GPL(dapm_clock_event);
1068
Mark Brownd8050022011-09-28 18:28:23 +01001069static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1070{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001071 if (w->power_checked)
1072 return w->new_power;
1073
Mark Brownd8050022011-09-28 18:28:23 +01001074 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001075 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001076 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001077 w->new_power = w->power_check(w);
1078
1079 w->power_checked = true;
1080
1081 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001082}
1083
Mark Browncd0f2d42009-04-20 16:56:59 +01001084/* Generic check to see if a widget should be powered.
1085 */
1086static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1087{
1088 int in, out;
1089
Mark Brownde02d072011-09-20 21:43:24 +01001090 DAPM_UPDATE_STAT(w, power_checks);
1091
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001092 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001093 dapm_clear_walk(w->dapm);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001094 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001095 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +01001096 return out != 0 && in != 0;
1097}
1098
Mark Brown888df392012-02-16 19:37:51 -08001099static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
1100{
1101 DAPM_UPDATE_STAT(w, power_checks);
1102
Mark Brown1eee1b32012-04-10 13:57:46 +01001103 if (w->active)
1104 return w->active;
1105
1106 return dapm_generic_check_power(w);
Mark Brown888df392012-02-16 19:37:51 -08001107}
1108
Mark Brown6ea31b92009-04-20 17:15:41 +01001109/* Check to see if an ADC has power */
1110static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1111{
1112 int in;
1113
Mark Brownde02d072011-09-20 21:43:24 +01001114 DAPM_UPDATE_STAT(w, power_checks);
1115
Mark Brown6ea31b92009-04-20 17:15:41 +01001116 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001117 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001118 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +01001119 return in != 0;
1120 } else {
1121 return dapm_generic_check_power(w);
1122 }
1123}
1124
1125/* Check to see if a DAC has power */
1126static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1127{
1128 int out;
1129
Mark Brownde02d072011-09-20 21:43:24 +01001130 DAPM_UPDATE_STAT(w, power_checks);
1131
Mark Brown6ea31b92009-04-20 17:15:41 +01001132 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001133 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001134 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +01001135 return out != 0;
1136 } else {
1137 return dapm_generic_check_power(w);
1138 }
1139}
1140
Mark Brown246d0a12009-04-22 18:24:55 +01001141/* Check to see if a power supply is needed */
1142static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1143{
1144 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001145
Mark Brownde02d072011-09-20 21:43:24 +01001146 DAPM_UPDATE_STAT(w, power_checks);
1147
Mark Brown246d0a12009-04-22 18:24:55 +01001148 /* Check if one of our outputs is connected */
1149 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001150 DAPM_UPDATE_STAT(w, neighbour_checks);
1151
Mark Brownbf3a9e12011-06-13 16:42:29 +01001152 if (path->weak)
1153 continue;
1154
Mark Brown215edda2009-09-08 18:59:05 +01001155 if (path->connected &&
1156 !path->connected(path->source, path->sink))
1157 continue;
1158
Mark Brown30173582011-02-11 11:42:19 +00001159 if (!path->sink)
1160 continue;
1161
Mark Brownf68d7e12011-10-04 22:57:50 +01001162 if (dapm_widget_power_check(path->sink))
1163 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001164 }
1165
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001166 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +01001167
Mark Brownf68d7e12011-10-04 22:57:50 +01001168 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001169}
1170
Mark Brown35c64bc2011-09-28 18:23:53 +01001171static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1172{
1173 return 1;
1174}
1175
Mark Brown38357ab2009-06-06 19:03:23 +01001176static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1177 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001178 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001179{
Mark Brown828a8422011-01-15 13:14:30 +00001180 int *sort;
1181
1182 if (power_up)
1183 sort = dapm_up_seq;
1184 else
1185 sort = dapm_down_seq;
1186
Mark Brown38357ab2009-06-06 19:03:23 +01001187 if (sort[a->id] != sort[b->id])
1188 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001189 if (a->subseq != b->subseq) {
1190 if (power_up)
1191 return a->subseq - b->subseq;
1192 else
1193 return b->subseq - a->subseq;
1194 }
Mark Brownb22ead22009-06-07 12:51:26 +01001195 if (a->reg != b->reg)
1196 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001197 if (a->dapm != b->dapm)
1198 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001199
Mark Brown38357ab2009-06-06 19:03:23 +01001200 return 0;
1201}
Mark Brown42aa3412009-03-01 19:21:10 +00001202
Mark Brown38357ab2009-06-06 19:03:23 +01001203/* Insert a widget in order into a DAPM power sequence. */
1204static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1205 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001206 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001207{
1208 struct snd_soc_dapm_widget *w;
1209
1210 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001211 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001212 list_add_tail(&new_widget->power_list, &w->power_list);
1213 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001214 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001215
Mark Brown38357ab2009-06-06 19:03:23 +01001216 list_add_tail(&new_widget->power_list, list);
1217}
Mark Brown42aa3412009-03-01 19:21:10 +00001218
Mark Brown68f89ad2010-11-03 23:51:49 -04001219static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1220 struct snd_soc_dapm_widget *w, int event)
1221{
1222 struct snd_soc_card *card = dapm->card;
1223 const char *ev_name;
1224 int power, ret;
1225
1226 switch (event) {
1227 case SND_SOC_DAPM_PRE_PMU:
1228 ev_name = "PRE_PMU";
1229 power = 1;
1230 break;
1231 case SND_SOC_DAPM_POST_PMU:
1232 ev_name = "POST_PMU";
1233 power = 1;
1234 break;
1235 case SND_SOC_DAPM_PRE_PMD:
1236 ev_name = "PRE_PMD";
1237 power = 0;
1238 break;
1239 case SND_SOC_DAPM_POST_PMD:
1240 ev_name = "POST_PMD";
1241 power = 0;
1242 break;
1243 default:
1244 BUG();
1245 return;
1246 }
1247
1248 if (w->power != power)
1249 return;
1250
1251 if (w->event && (w->event_flags & event)) {
1252 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1253 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001254 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001255 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001256 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001257 if (ret < 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001258 dev_err(dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001259 ev_name, w->name, ret);
1260 }
1261}
1262
Mark Brownb22ead22009-06-07 12:51:26 +01001263/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001264static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001265 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001266{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001267 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001268 struct snd_soc_dapm_widget *w;
1269 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001270 unsigned int value = 0;
1271 unsigned int mask = 0;
1272 unsigned int cur_mask;
1273
1274 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1275 power_list)->reg;
1276
1277 list_for_each_entry(w, pending, power_list) {
1278 cur_mask = 1 << w->shift;
1279 BUG_ON(reg != w->reg);
1280
1281 if (w->invert)
1282 power = !w->power;
1283 else
1284 power = w->power;
1285
1286 mask |= cur_mask;
1287 if (power)
1288 value |= cur_mask;
1289
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001290 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001291 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1292 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001293
Mark Brown68f89ad2010-11-03 23:51:49 -04001294 /* Check for events */
1295 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1296 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001297 }
1298
Mark Brown81628102009-06-07 13:21:24 +01001299 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001300 /* Any widget will do, they should all be updating the
1301 * same register.
1302 */
1303 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1304 power_list);
1305
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001306 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001307 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001308 value, mask, reg, card->pop_time);
1309 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001310 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001311 }
1312
1313 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001314 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1315 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001316 }
Mark Brown42aa3412009-03-01 19:21:10 +00001317}
1318
Mark Brownb22ead22009-06-07 12:51:26 +01001319/* Apply a DAPM power sequence.
1320 *
1321 * We walk over a pre-sorted list of widgets to apply power to. In
1322 * order to minimise the number of writes to the device required
1323 * multiple widgets will be updated in a single write where possible.
1324 * Currently anything that requires more than a single write is not
1325 * handled.
1326 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001327static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001328 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001329{
1330 struct snd_soc_dapm_widget *w, *n;
1331 LIST_HEAD(pending);
1332 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001333 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001334 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001335 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001336 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001337 int *sort;
1338
1339 if (power_up)
1340 sort = dapm_up_seq;
1341 else
1342 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001343
Mark Brownb22ead22009-06-07 12:51:26 +01001344 list_for_each_entry_safe(w, n, list, power_list) {
1345 ret = 0;
1346
1347 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001348 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001349 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001350 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001351 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001352
Mark Brown474b62d2011-01-18 16:14:44 +00001353 if (cur_dapm && cur_dapm->seq_notifier) {
1354 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1355 if (sort[i] == cur_sort)
1356 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001357 i,
1358 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001359 }
1360
Mark Brownb22ead22009-06-07 12:51:26 +01001361 INIT_LIST_HEAD(&pending);
1362 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001363 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001364 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001365 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001366 }
1367
Mark Brown163cac02009-06-07 10:12:52 +01001368 switch (w->id) {
1369 case snd_soc_dapm_pre:
1370 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001371 list_for_each_entry_safe_continue(w, n, list,
1372 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001373
Mark Brownb22ead22009-06-07 12:51:26 +01001374 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001375 ret = w->event(w,
1376 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001377 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001378 ret = w->event(w,
1379 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001380 break;
1381
1382 case snd_soc_dapm_post:
1383 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001384 list_for_each_entry_safe_continue(w, n, list,
1385 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001386
Mark Brownb22ead22009-06-07 12:51:26 +01001387 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001388 ret = w->event(w,
1389 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001390 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001391 ret = w->event(w,
1392 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001393 break;
1394
Mark Brown163cac02009-06-07 10:12:52 +01001395 default:
Mark Brown81628102009-06-07 13:21:24 +01001396 /* Queue it up for application */
1397 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001398 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001399 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001400 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001401 list_move(&w->power_list, &pending);
1402 break;
Mark Brown163cac02009-06-07 10:12:52 +01001403 }
Mark Brownb22ead22009-06-07 12:51:26 +01001404
1405 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001406 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001407 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001408 }
Mark Brownb22ead22009-06-07 12:51:26 +01001409
1410 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001411 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001412
1413 if (cur_dapm && cur_dapm->seq_notifier) {
1414 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1415 if (sort[i] == cur_sort)
1416 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001417 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001418 }
Mark Brown163cac02009-06-07 10:12:52 +01001419}
1420
Mark Brown97404f22010-12-14 16:13:57 +00001421static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1422{
1423 struct snd_soc_dapm_update *update = dapm->update;
1424 struct snd_soc_dapm_widget *w;
1425 int ret;
1426
1427 if (!update)
1428 return;
1429
1430 w = update->widget;
1431
1432 if (w->event &&
1433 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1434 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1435 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001436 dev_err(dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Mark Brown97404f22010-12-14 16:13:57 +00001437 w->name, ret);
1438 }
1439
Liam Girdwood49575fb52012-03-06 18:16:19 +00001440 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001441 update->val);
1442 if (ret < 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001443 dev_err(dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1444 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001445
1446 if (w->event &&
1447 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1448 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1449 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001450 dev_err(dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Mark Brown97404f22010-12-14 16:13:57 +00001451 w->name, ret);
1452 }
1453}
1454
Mark Brown9d0624a2011-02-18 11:49:43 -08001455/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1456 * they're changing state.
1457 */
1458static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1459{
1460 struct snd_soc_dapm_context *d = data;
1461 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001462
Mark Brown56fba412011-06-04 11:25:10 +01001463 /* If we're off and we're not supposed to be go into STANDBY */
1464 if (d->bias_level == SND_SOC_BIAS_OFF &&
1465 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001466 if (d->dev)
1467 pm_runtime_get_sync(d->dev);
1468
Mark Brown9d0624a2011-02-18 11:49:43 -08001469 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1470 if (ret != 0)
1471 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001472 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001473 }
1474
Mark Brown56fba412011-06-04 11:25:10 +01001475 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1476 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001477 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1478 if (ret != 0)
1479 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001480 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001481 }
1482}
1483
1484/* Async callback run prior to DAPM sequences - brings to their final
1485 * state.
1486 */
1487static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1488{
1489 struct snd_soc_dapm_context *d = data;
1490 int ret;
1491
1492 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001493 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1494 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1495 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001496 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1497 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001498 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001499 ret);
1500 }
1501
1502 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001503 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1504 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001505 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1506 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001507 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1508 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001509
1510 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001511 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001512 }
1513
1514 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001515 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1516 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001517 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1518 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001519 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001520 ret);
1521 }
1522}
Mark Brown97404f22010-12-14 16:13:57 +00001523
Mark Brownfe4fda52011-10-03 22:36:57 +01001524static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1525 bool power, bool connect)
1526{
1527 /* If a connection is being made or broken then that update
1528 * will have marked the peer dirty, otherwise the widgets are
1529 * not connected and this update has no impact. */
1530 if (!connect)
1531 return;
1532
1533 /* If the peer is already in the state we're moving to then we
1534 * won't have an impact on it. */
1535 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001536 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001537}
1538
Mark Brown05623c42011-09-28 17:02:31 +01001539static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1540 struct list_head *up_list,
1541 struct list_head *down_list)
1542{
Mark Browndb432b42011-10-03 21:06:40 +01001543 struct snd_soc_dapm_path *path;
1544
Mark Brown05623c42011-09-28 17:02:31 +01001545 if (w->power == power)
1546 return;
1547
1548 trace_snd_soc_dapm_widget_power(w, power);
1549
Mark Browndb432b42011-10-03 21:06:40 +01001550 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001551 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001552 */
1553 list_for_each_entry(path, &w->sources, list_sink) {
1554 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001555 dapm_widget_set_peer_power(path->source, power,
1556 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001557 }
1558 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001559 switch (w->id) {
1560 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001561 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001562 case snd_soc_dapm_clock_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001563 /* Supplies can't affect their outputs, only their inputs */
1564 break;
1565 default:
1566 list_for_each_entry(path, &w->sinks, list_source) {
1567 if (path->sink) {
1568 dapm_widget_set_peer_power(path->sink, power,
1569 path->connect);
1570 }
Mark Browndb432b42011-10-03 21:06:40 +01001571 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001572 break;
Mark Browndb432b42011-10-03 21:06:40 +01001573 }
1574
Mark Brown05623c42011-09-28 17:02:31 +01001575 if (power)
1576 dapm_seq_insert(w, up_list, true);
1577 else
1578 dapm_seq_insert(w, down_list, false);
1579
1580 w->power = power;
1581}
1582
Mark Brown7c81beb2011-09-20 22:22:32 +01001583static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1584 struct list_head *up_list,
1585 struct list_head *down_list)
1586{
Mark Brown7c81beb2011-09-20 22:22:32 +01001587 int power;
1588
1589 switch (w->id) {
1590 case snd_soc_dapm_pre:
1591 dapm_seq_insert(w, down_list, false);
1592 break;
1593 case snd_soc_dapm_post:
1594 dapm_seq_insert(w, up_list, true);
1595 break;
1596
1597 default:
Mark Brownd8050022011-09-28 18:28:23 +01001598 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001599
Mark Brown05623c42011-09-28 17:02:31 +01001600 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001601 break;
1602 }
1603}
1604
Mark Brown42aa3412009-03-01 19:21:10 +00001605/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001606 * Scan each dapm widget for complete audio path.
1607 * A complete path is a route that has valid endpoints i.e.:-
1608 *
1609 * o DAC to output pin.
1610 * o Input Pin to ADC.
1611 * o Input pin to Output pin (bypass, sidetone)
1612 * o DAC to ADC (loopback).
1613 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001614static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001615{
Mark Brown12ea2c72011-03-02 18:17:32 +00001616 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001617 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001618 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001619 LIST_HEAD(up_list);
1620 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001621 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001622 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001623
Mark Brown84e90932010-11-04 00:07:02 -04001624 trace_snd_soc_dapm_start(card);
1625
Mark Brown56fba412011-06-04 11:25:10 +01001626 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001627 if (d->idle_bias_off)
1628 d->target_bias_level = SND_SOC_BIAS_OFF;
1629 else
1630 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001631 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001632
Liam Girdwood6c120e12012-02-15 15:15:34 +00001633 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001634
Mark Brown6d3ddc82009-05-16 17:47:29 +01001635 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001636 * lists indicating if they should be powered up or down. We
1637 * only check widgets that have been flagged as dirty but note
1638 * that new widgets may be added to the dirty list while we
1639 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001640 */
Mark Browndb432b42011-10-03 21:06:40 +01001641 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001642 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001643 }
1644
Mark Brownf9de6d72011-09-28 17:19:47 +01001645 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001646 switch (w->id) {
1647 case snd_soc_dapm_pre:
1648 case snd_soc_dapm_post:
1649 /* These widgets always need to be powered */
1650 break;
1651 default:
1652 list_del_init(&w->dirty);
1653 break;
1654 }
Mark Browndb432b42011-10-03 21:06:40 +01001655
Mark Brownf9de6d72011-09-28 17:19:47 +01001656 if (w->power) {
1657 d = w->dapm;
1658
1659 /* Supplies and micbiases only bring the
1660 * context up to STANDBY as unless something
1661 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001662 * generally don't require full power. Signal
1663 * generators are virtual pins and have no
1664 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001665 */
1666 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001667 case snd_soc_dapm_siggen:
1668 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001669 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001670 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001671 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001672 case snd_soc_dapm_micbias:
1673 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1674 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1675 break;
1676 default:
1677 d->target_bias_level = SND_SOC_BIAS_ON;
1678 break;
1679 }
1680 }
1681
1682 }
1683
Mark Brown85a843c2011-09-21 21:29:47 +01001684 /* Force all contexts in the card to the same bias state if
1685 * they're not ground referenced.
1686 */
Mark Brown56fba412011-06-04 11:25:10 +01001687 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001688 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001689 if (d->target_bias_level > bias)
1690 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001691 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001692 if (!d->idle_bias_off)
1693 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001694
Mark Brownde02d072011-09-20 21:43:24 +01001695 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001696
Mark Brown9d0624a2011-02-18 11:49:43 -08001697 /* Run all the bias changes in parallel */
1698 list_for_each_entry(d, &dapm->card->dapm_list, list)
1699 async_schedule_domain(dapm_pre_sequence_async, d,
1700 &async_domain);
1701 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001702
Mark Brown6d3ddc82009-05-16 17:47:29 +01001703 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001704 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001705
Mark Brown97404f22010-12-14 16:13:57 +00001706 dapm_widget_update(dapm);
1707
Mark Brown6d3ddc82009-05-16 17:47:29 +01001708 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001709 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001710
Mark Brown9d0624a2011-02-18 11:49:43 -08001711 /* Run all the bias changes in parallel */
1712 list_for_each_entry(d, &dapm->card->dapm_list, list)
1713 async_schedule_domain(dapm_post_sequence_async, d,
1714 &async_domain);
1715 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001716
Liam Girdwood8078d872012-02-15 15:15:35 +00001717 /* do we need to notify any clients that DAPM event is complete */
1718 list_for_each_entry(d, &card->dapm_list, list) {
1719 if (d->stream_event)
1720 d->stream_event(d, event);
1721 }
1722
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001723 pop_dbg(dapm->dev, card->pop_time,
1724 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001725 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001726
Mark Brown84e90932010-11-04 00:07:02 -04001727 trace_snd_soc_dapm_done(card);
1728
Mark Brown42aa3412009-03-01 19:21:10 +00001729 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001730}
1731
Mark Brown79fb9382009-08-21 16:38:13 +01001732#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001733static ssize_t dapm_widget_power_read_file(struct file *file,
1734 char __user *user_buf,
1735 size_t count, loff_t *ppos)
1736{
1737 struct snd_soc_dapm_widget *w = file->private_data;
1738 char *buf;
1739 int in, out;
1740 ssize_t ret;
1741 struct snd_soc_dapm_path *p = NULL;
1742
1743 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1744 if (!buf)
1745 return -ENOMEM;
1746
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001747 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001748 dapm_clear_walk(w->dapm);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001749 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001750 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001751
Mark Brownf13ebad2012-03-03 18:01:01 +00001752 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1753 w->name, w->power ? "On" : "Off",
1754 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001755
Mark Brownd033c362009-12-04 15:25:56 +00001756 if (w->reg >= 0)
1757 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1758 " - R%d(0x%x) bit %d",
1759 w->reg, w->reg, w->shift);
1760
1761 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1762
Mark Brown3eef08b2009-09-14 16:49:00 +01001763 if (w->sname)
1764 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1765 w->sname,
1766 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001767
1768 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001769 if (p->connected && !p->connected(w, p->sink))
1770 continue;
1771
Mark Brown79fb9382009-08-21 16:38:13 +01001772 if (p->connect)
1773 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001774 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001775 p->name ? p->name : "static",
1776 p->source->name);
1777 }
1778 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001779 if (p->connected && !p->connected(w, p->sink))
1780 continue;
1781
Mark Brown79fb9382009-08-21 16:38:13 +01001782 if (p->connect)
1783 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001784 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001785 p->name ? p->name : "static",
1786 p->sink->name);
1787 }
1788
1789 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1790
1791 kfree(buf);
1792 return ret;
1793}
1794
1795static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001796 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001797 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001798 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001799};
1800
Mark Brownef49e4f2011-04-04 20:48:13 +09001801static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1802 size_t count, loff_t *ppos)
1803{
1804 struct snd_soc_dapm_context *dapm = file->private_data;
1805 char *level;
1806
1807 switch (dapm->bias_level) {
1808 case SND_SOC_BIAS_ON:
1809 level = "On\n";
1810 break;
1811 case SND_SOC_BIAS_PREPARE:
1812 level = "Prepare\n";
1813 break;
1814 case SND_SOC_BIAS_STANDBY:
1815 level = "Standby\n";
1816 break;
1817 case SND_SOC_BIAS_OFF:
1818 level = "Off\n";
1819 break;
1820 default:
1821 BUG();
1822 level = "Unknown\n";
1823 break;
1824 }
1825
1826 return simple_read_from_buffer(user_buf, count, ppos, level,
1827 strlen(level));
1828}
1829
1830static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001831 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001832 .read = dapm_bias_read_file,
1833 .llseek = default_llseek,
1834};
1835
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001836void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1837 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001838{
Mark Brown79fb9382009-08-21 16:38:13 +01001839 struct dentry *d;
1840
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001841 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1842
1843 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001844 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001845 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001846 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001847 }
Mark Brown79fb9382009-08-21 16:38:13 +01001848
Mark Brownef49e4f2011-04-04 20:48:13 +09001849 d = debugfs_create_file("bias_level", 0444,
1850 dapm->debugfs_dapm, dapm,
1851 &dapm_bias_fops);
1852 if (!d)
1853 dev_warn(dapm->dev,
1854 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001855}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001856
1857static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1858{
1859 struct snd_soc_dapm_context *dapm = w->dapm;
1860 struct dentry *d;
1861
1862 if (!dapm->debugfs_dapm || !w->name)
1863 return;
1864
1865 d = debugfs_create_file(w->name, 0444,
1866 dapm->debugfs_dapm, w,
1867 &dapm_widget_power_fops);
1868 if (!d)
1869 dev_warn(w->dapm->dev,
1870 "ASoC: Failed to create %s debugfs file\n",
1871 w->name);
1872}
1873
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001874static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1875{
1876 debugfs_remove_recursive(dapm->debugfs_dapm);
1877}
1878
Mark Brown79fb9382009-08-21 16:38:13 +01001879#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001880void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1881 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001882{
1883}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001884
1885static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1886{
1887}
1888
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001889static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1890{
1891}
1892
Mark Brown79fb9382009-08-21 16:38:13 +01001893#endif
1894
Richard Purdie2b97eab2006-10-06 18:32:18 +02001895/* test and update the power status of a mux widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001896static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001897 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001898{
1899 struct snd_soc_dapm_path *path;
1900 int found = 0;
1901
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001902 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001903 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001904 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001905 return -ENODEV;
1906
Richard Purdie2b97eab2006-10-06 18:32:18 +02001907 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001908 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001909 if (path->kcontrol != kcontrol)
1910 continue;
1911
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001912 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001913 continue;
1914
1915 found = 1;
1916 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001917 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001918 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001919 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001920 } else {
1921 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001922 dapm_mark_dirty(path->source,
1923 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001924 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001925 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001926 }
1927
Mark Browndb432b42011-10-03 21:06:40 +01001928 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001929 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001930 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001931 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001932
Liam Girdwood618dae12012-04-25 12:12:51 +01001933 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001934}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001935
1936int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1937 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1938{
1939 struct snd_soc_card *card = widget->dapm->card;
1940 int ret;
1941
Liam Girdwood3cd04342012-03-09 12:02:08 +00001942 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001943 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1944 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001945 if (ret > 0)
1946 soc_dpcm_runtime_update(widget);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001947 return ret;
1948}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001949EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001950
Milan plzik1b075e32008-01-10 14:39:46 +01001951/* test and update the power status of a mixer or switch widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001952static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001953 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001954{
1955 struct snd_soc_dapm_path *path;
1956 int found = 0;
1957
Milan plzik1b075e32008-01-10 14:39:46 +01001958 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001959 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001960 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001961 return -ENODEV;
1962
Richard Purdie2b97eab2006-10-06 18:32:18 +02001963 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001964 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001965 if (path->kcontrol != kcontrol)
1966 continue;
1967
1968 /* found, now check type */
1969 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001970 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001971 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001972 }
1973
Mark Browndb432b42011-10-03 21:06:40 +01001974 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001975 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001976 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001977 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001978
Liam Girdwood618dae12012-04-25 12:12:51 +01001979 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001980}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001981
1982int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1983 struct snd_kcontrol *kcontrol, int connect)
1984{
1985 struct snd_soc_card *card = widget->dapm->card;
1986 int ret;
1987
Liam Girdwood3cd04342012-03-09 12:02:08 +00001988 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001989 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
1990 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001991 if (ret > 0)
1992 soc_dpcm_runtime_update(widget);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001993 return ret;
1994}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001995EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001996
1997/* show dapm widget status in sys fs */
1998static ssize_t dapm_widget_show(struct device *dev,
1999 struct device_attribute *attr, char *buf)
2000{
Mark Brown36ae1a92012-01-06 17:12:45 -08002001 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002002 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002003 struct snd_soc_dapm_widget *w;
2004 int count = 0;
2005 char *state = "not set";
2006
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002007 list_for_each_entry(w, &codec->card->widgets, list) {
2008 if (w->dapm != &codec->dapm)
2009 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002010
2011 /* only display widgets that burnm power */
2012 switch (w->id) {
2013 case snd_soc_dapm_hp:
2014 case snd_soc_dapm_mic:
2015 case snd_soc_dapm_spk:
2016 case snd_soc_dapm_line:
2017 case snd_soc_dapm_micbias:
2018 case snd_soc_dapm_dac:
2019 case snd_soc_dapm_adc:
2020 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002021 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002022 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002023 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002024 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002025 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002026 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002027 if (w->name)
2028 count += sprintf(buf + count, "%s: %s\n",
2029 w->name, w->power ? "On":"Off");
2030 break;
2031 default:
2032 break;
2033 }
2034 }
2035
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002036 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002037 case SND_SOC_BIAS_ON:
2038 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002039 break;
Mark Brown0be98982008-05-19 12:31:28 +02002040 case SND_SOC_BIAS_PREPARE:
2041 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002042 break;
Mark Brown0be98982008-05-19 12:31:28 +02002043 case SND_SOC_BIAS_STANDBY:
2044 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002045 break;
Mark Brown0be98982008-05-19 12:31:28 +02002046 case SND_SOC_BIAS_OFF:
2047 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002048 break;
2049 }
2050 count += sprintf(buf + count, "PM State: %s\n", state);
2051
2052 return count;
2053}
2054
2055static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2056
2057int snd_soc_dapm_sys_add(struct device *dev)
2058{
Troy Kisky12ef1932008-10-13 17:42:14 -07002059 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002060}
2061
2062static void snd_soc_dapm_sys_remove(struct device *dev)
2063{
Mark Brownaef90842009-05-16 17:53:16 +01002064 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002065}
2066
2067/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002068static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002069{
2070 struct snd_soc_dapm_widget *w, *next_w;
2071 struct snd_soc_dapm_path *p, *next_p;
2072
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002073 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2074 if (w->dapm != dapm)
2075 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002076 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002077 /*
2078 * remove source and sink paths associated to this widget.
2079 * While removing the path, remove reference to it from both
2080 * source and sink widgets so that path is removed only once.
2081 */
2082 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
2083 list_del(&p->list_sink);
2084 list_del(&p->list_source);
2085 list_del(&p->list);
2086 kfree(p->long_name);
2087 kfree(p);
2088 }
2089 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2090 list_del(&p->list_sink);
2091 list_del(&p->list_source);
2092 list_del(&p->list);
2093 kfree(p->long_name);
2094 kfree(p);
2095 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002096 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002097 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002098 kfree(w);
2099 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002100}
2101
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002102static struct snd_soc_dapm_widget *dapm_find_widget(
2103 struct snd_soc_dapm_context *dapm, const char *pin,
2104 bool search_other_contexts)
2105{
2106 struct snd_soc_dapm_widget *w;
2107 struct snd_soc_dapm_widget *fallback = NULL;
2108
2109 list_for_each_entry(w, &dapm->card->widgets, list) {
2110 if (!strcmp(w->name, pin)) {
2111 if (w->dapm == dapm)
2112 return w;
2113 else
2114 fallback = w;
2115 }
2116 }
2117
2118 if (search_other_contexts)
2119 return fallback;
2120
2121 return NULL;
2122}
2123
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002124static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002125 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002126{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002127 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002128
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002129 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002130 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002131 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002132 }
2133
Mark Brown1a8b2d92012-02-16 11:50:07 -08002134 if (w->connected != status)
2135 dapm_mark_dirty(w, "pin configuration");
2136
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002137 w->connected = status;
2138 if (status == 0)
2139 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002140
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002141 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002142}
2143
Richard Purdie2b97eab2006-10-06 18:32:18 +02002144/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002145 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002146 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002147 *
2148 * Walks all dapm audio paths and powers widgets according to their
2149 * stream or path usage.
2150 *
2151 * Returns 0 for success.
2152 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002153int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002154{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002155 int ret;
2156
Mark Brown4f4c0072011-10-07 14:29:19 +01002157 /*
2158 * Suppress early reports (eg, jacks syncing their state) to avoid
2159 * silly DAPM runs during card startup.
2160 */
2161 if (!dapm->card || !dapm->card->instantiated)
2162 return 0;
2163
Liam Girdwood3cd04342012-03-09 12:02:08 +00002164 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002165 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2166 mutex_unlock(&dapm->card->dapm_mutex);
2167 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002168}
Liam Girdwooda5302182008-07-07 13:35:17 +01002169EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002170
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002171static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002172 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002173{
2174 struct snd_soc_dapm_path *path;
2175 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002176 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002177 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002178 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002179 const char *source;
2180 char prefixed_sink[80];
2181 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002182 int ret = 0;
2183
Mark Brown88e8b9a2011-03-02 18:18:24 +00002184 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002185 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2186 dapm->codec->name_prefix, route->sink);
2187 sink = prefixed_sink;
2188 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2189 dapm->codec->name_prefix, route->source);
2190 source = prefixed_source;
2191 } else {
2192 sink = route->sink;
2193 source = route->source;
2194 }
2195
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002196 /*
2197 * find src and dest widgets over all widgets but favor a widget from
2198 * current DAPM context
2199 */
2200 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002201 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002202 wtsink = w;
2203 if (w->dapm == dapm)
2204 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002205 continue;
2206 }
2207 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002208 wtsource = w;
2209 if (w->dapm == dapm)
2210 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002211 }
2212 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002213 /* use widget from another DAPM context if not found from this */
2214 if (!wsink)
2215 wsink = wtsink;
2216 if (!wsource)
2217 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002218
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002219 if (wsource == NULL) {
2220 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2221 route->source);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002222 return -ENODEV;
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002223 }
2224 if (wsink == NULL) {
2225 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2226 route->sink);
2227 return -ENODEV;
2228 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002229
2230 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2231 if (!path)
2232 return -ENOMEM;
2233
2234 path->source = wsource;
2235 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002236 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002237 INIT_LIST_HEAD(&path->list);
2238 INIT_LIST_HEAD(&path->list_source);
2239 INIT_LIST_HEAD(&path->list_sink);
2240
2241 /* check for external widgets */
2242 if (wsink->id == snd_soc_dapm_input) {
2243 if (wsource->id == snd_soc_dapm_micbias ||
2244 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002245 wsource->id == snd_soc_dapm_line ||
2246 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002247 wsink->ext = 1;
2248 }
2249 if (wsource->id == snd_soc_dapm_output) {
2250 if (wsink->id == snd_soc_dapm_spk ||
2251 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002252 wsink->id == snd_soc_dapm_line ||
2253 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002254 wsource->ext = 1;
2255 }
2256
2257 /* connect static paths */
2258 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002259 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002260 list_add(&path->list_sink, &wsink->sources);
2261 list_add(&path->list_source, &wsource->sinks);
2262 path->connect = 1;
2263 return 0;
2264 }
2265
2266 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002267 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002268 case snd_soc_dapm_adc:
2269 case snd_soc_dapm_dac:
2270 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002271 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002272 case snd_soc_dapm_input:
2273 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002274 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002275 case snd_soc_dapm_micbias:
2276 case snd_soc_dapm_vmid:
2277 case snd_soc_dapm_pre:
2278 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002279 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002280 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002281 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002282 case snd_soc_dapm_aif_in:
2283 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -08002284 case snd_soc_dapm_dai:
Mark Brownc74184e2012-04-04 22:12:09 +01002285 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002286 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002287 list_add(&path->list_sink, &wsink->sources);
2288 list_add(&path->list_source, &wsource->sinks);
2289 path->connect = 1;
2290 return 0;
2291 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002292 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002293 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002294 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002295 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002296 if (ret != 0)
2297 goto err;
2298 break;
2299 case snd_soc_dapm_switch:
2300 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002301 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002302 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002303 if (ret != 0)
2304 goto err;
2305 break;
2306 case snd_soc_dapm_hp:
2307 case snd_soc_dapm_mic:
2308 case snd_soc_dapm_line:
2309 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002310 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002311 list_add(&path->list_sink, &wsink->sources);
2312 list_add(&path->list_source, &wsource->sinks);
2313 path->connect = 0;
2314 return 0;
2315 }
Mark Brownfabd0382012-07-05 17:20:06 +01002316
2317 dapm_mark_dirty(wsource, "Route added");
2318 dapm_mark_dirty(wsink, "Route added");
2319
Richard Purdie2b97eab2006-10-06 18:32:18 +02002320 return 0;
2321
2322err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002323 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002324 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002325 kfree(path);
2326 return ret;
2327}
Mark Brown105f1c22008-05-13 14:52:19 +02002328
Mark Brownefcc3c62012-07-05 17:24:19 +01002329static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2330 const struct snd_soc_dapm_route *route)
2331{
2332 struct snd_soc_dapm_path *path, *p;
2333 const char *sink;
2334 const char *source;
2335 char prefixed_sink[80];
2336 char prefixed_source[80];
2337
2338 if (route->control) {
2339 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002340 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002341 return -EINVAL;
2342 }
2343
2344 if (dapm->codec && dapm->codec->name_prefix) {
2345 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2346 dapm->codec->name_prefix, route->sink);
2347 sink = prefixed_sink;
2348 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2349 dapm->codec->name_prefix, route->source);
2350 source = prefixed_source;
2351 } else {
2352 sink = route->sink;
2353 source = route->source;
2354 }
2355
2356 path = NULL;
2357 list_for_each_entry(p, &dapm->card->paths, list) {
2358 if (strcmp(p->source->name, source) != 0)
2359 continue;
2360 if (strcmp(p->sink->name, sink) != 0)
2361 continue;
2362 path = p;
2363 break;
2364 }
2365
2366 if (path) {
2367 dapm_mark_dirty(path->source, "Route removed");
2368 dapm_mark_dirty(path->sink, "Route removed");
2369
2370 list_del(&path->list);
2371 list_del(&path->list_sink);
2372 list_del(&path->list_source);
2373 kfree(path);
2374 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002375 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002376 source, sink);
2377 }
2378
2379 return 0;
2380}
2381
Mark Brown105f1c22008-05-13 14:52:19 +02002382/**
Mark Brown105f1c22008-05-13 14:52:19 +02002383 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002384 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002385 * @route: audio routes
2386 * @num: number of routes
2387 *
2388 * Connects 2 dapm widgets together via a named audio path. The sink is
2389 * the widget receiving the audio signal, whilst the source is the sender
2390 * of the audio signal.
2391 *
2392 * Returns 0 for success else error. On error all resources can be freed
2393 * with a call to snd_soc_card_free().
2394 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002395int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002396 const struct snd_soc_dapm_route *route, int num)
2397{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002398 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002399
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002400 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002401 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002402 r = snd_soc_dapm_add_route(dapm, route);
2403 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002404 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2405 route->source,
2406 route->control ? route->control : "direct",
2407 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002408 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002409 }
2410 route++;
2411 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002412 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002413
Dan Carpenter60884c22012-04-13 22:25:43 +03002414 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002415}
2416EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2417
Mark Brownefcc3c62012-07-05 17:24:19 +01002418/**
2419 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2420 * @dapm: DAPM context
2421 * @route: audio routes
2422 * @num: number of routes
2423 *
2424 * Removes routes from the DAPM context.
2425 */
2426int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2427 const struct snd_soc_dapm_route *route, int num)
2428{
2429 int i, ret = 0;
2430
2431 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2432 for (i = 0; i < num; i++) {
2433 snd_soc_dapm_del_route(dapm, route);
2434 route++;
2435 }
2436 mutex_unlock(&dapm->card->dapm_mutex);
2437
2438 return ret;
2439}
2440EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2441
Mark Brownbf3a9e12011-06-13 16:42:29 +01002442static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2443 const struct snd_soc_dapm_route *route)
2444{
2445 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2446 route->source,
2447 true);
2448 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2449 route->sink,
2450 true);
2451 struct snd_soc_dapm_path *path;
2452 int count = 0;
2453
2454 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002455 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002456 route->source);
2457 return -ENODEV;
2458 }
2459
2460 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002461 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002462 route->sink);
2463 return -ENODEV;
2464 }
2465
2466 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002467 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002468 route->source, route->sink);
2469
2470 list_for_each_entry(path, &source->sinks, list_source) {
2471 if (path->sink == sink) {
2472 path->weak = 1;
2473 count++;
2474 }
2475 }
2476
2477 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002478 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002479 route->source, route->sink);
2480 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002481 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002482 count, route->source, route->sink);
2483
2484 return 0;
2485}
2486
2487/**
2488 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2489 * @dapm: DAPM context
2490 * @route: audio routes
2491 * @num: number of routes
2492 *
2493 * Mark existing routes matching those specified in the passed array
2494 * as being weak, meaning that they are ignored for the purpose of
2495 * power decisions. The main intended use case is for sidetone paths
2496 * which couple audio between other independent paths if they are both
2497 * active in order to make the combination work better at the user
2498 * level but which aren't intended to be "used".
2499 *
2500 * Note that CODEC drivers should not use this as sidetone type paths
2501 * can frequently also be used as bypass paths.
2502 */
2503int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2504 const struct snd_soc_dapm_route *route, int num)
2505{
2506 int i, err;
2507 int ret = 0;
2508
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002509 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002510 for (i = 0; i < num; i++) {
2511 err = snd_soc_dapm_weak_route(dapm, route);
2512 if (err)
2513 ret = err;
2514 route++;
2515 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002516 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002517
2518 return ret;
2519}
2520EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2521
Mark Brown105f1c22008-05-13 14:52:19 +02002522/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002523 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002524 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002525 *
2526 * Checks the codec for any new dapm widgets and creates them if found.
2527 *
2528 * Returns 0 for success.
2529 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002530int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002531{
2532 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002533 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002534
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002535 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2536
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002537 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002538 {
2539 if (w->new)
2540 continue;
2541
Stephen Warrenfad59882011-04-28 17:37:59 -06002542 if (w->num_kcontrols) {
2543 w->kcontrols = kzalloc(w->num_kcontrols *
2544 sizeof(struct snd_kcontrol *),
2545 GFP_KERNEL);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002546 if (!w->kcontrols) {
2547 mutex_unlock(&dapm->card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002548 return -ENOMEM;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002549 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002550 }
2551
Richard Purdie2b97eab2006-10-06 18:32:18 +02002552 switch(w->id) {
2553 case snd_soc_dapm_switch:
2554 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002555 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002556 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002557 break;
2558 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002559 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002560 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002561 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002562 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002563 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002564 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002565 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002566 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002567 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002568 break;
2569 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002570
2571 /* Read the initial power state from the device */
2572 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002573 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002574 val &= 1 << w->shift;
2575 if (w->invert)
2576 val = !val;
2577
2578 if (val)
2579 w->power = 1;
2580 }
2581
Richard Purdie2b97eab2006-10-06 18:32:18 +02002582 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002583
Mark Brown7508b122011-10-05 12:09:12 +01002584 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002585 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002586 }
2587
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002588 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002589 mutex_unlock(&dapm->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002590 return 0;
2591}
2592EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2593
2594/**
2595 * snd_soc_dapm_get_volsw - dapm mixer get callback
2596 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002597 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002598 *
2599 * Callback to get the value of a dapm mixer control.
2600 *
2601 * Returns 0 for success.
2602 */
2603int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2604 struct snd_ctl_elem_value *ucontrol)
2605{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002606 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2607 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002608 struct soc_mixer_control *mc =
2609 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002610 unsigned int reg = mc->reg;
2611 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002612 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002613 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002614 unsigned int invert = mc->invert;
2615
2616 if (snd_soc_volsw_is_stereo(mc))
2617 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002618 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002619 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002620
Richard Purdie2b97eab2006-10-06 18:32:18 +02002621 ucontrol->value.integer.value[0] =
2622 (snd_soc_read(widget->codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002623 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002624 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002625 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002626
2627 return 0;
2628}
2629EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2630
2631/**
2632 * snd_soc_dapm_put_volsw - dapm mixer set callback
2633 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002634 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002635 *
2636 * Callback to set the value of a dapm mixer control.
2637 *
2638 * Returns 0 for success.
2639 */
2640int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2641 struct snd_ctl_elem_value *ucontrol)
2642{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002643 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2644 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2645 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002646 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002647 struct soc_mixer_control *mc =
2648 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002649 unsigned int reg = mc->reg;
2650 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002651 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002652 unsigned int mask = (1 << fls(max)) - 1;
2653 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002654 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002655 int connect, change;
2656 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002657 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002658
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002659 if (snd_soc_volsw_is_stereo(mc))
2660 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002661 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002662 kcontrol->id.name);
2663
Richard Purdie2b97eab2006-10-06 18:32:18 +02002664 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002665 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002666
2667 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002668 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002669 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002670 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002671
Liam Girdwood3cd04342012-03-09 12:02:08 +00002672 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002673
Stephen Warrene9cf7042011-01-27 14:54:05 -07002674 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002675 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002676 for (wi = 0; wi < wlist->num_widgets; wi++) {
2677 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002678
Stephen Warrenfafd2172011-04-28 17:38:00 -06002679 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002680
Stephen Warrenfafd2172011-04-28 17:38:00 -06002681 update.kcontrol = kcontrol;
2682 update.widget = widget;
2683 update.reg = reg;
2684 update.mask = mask;
2685 update.val = val;
2686 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002687
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002688 soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002689
2690 widget->dapm->update = NULL;
2691 }
Mark Brown283375c2009-12-07 18:09:03 +00002692 }
2693
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002694 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002695 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002696}
2697EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2698
2699/**
2700 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2701 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002702 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002703 *
2704 * Callback to get the value of a dapm enumerated double mixer control.
2705 *
2706 * Returns 0 for success.
2707 */
2708int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2709 struct snd_ctl_elem_value *ucontrol)
2710{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002711 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2712 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002713 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002714 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002715
Richard Purdie2b97eab2006-10-06 18:32:18 +02002716 val = snd_soc_read(widget->codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002717 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002718 if (e->shift_l != e->shift_r)
2719 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002720 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002721
2722 return 0;
2723}
2724EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2725
2726/**
2727 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2728 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002729 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002730 *
2731 * Callback to set the value of a dapm enumerated double mixer control.
2732 *
2733 * Returns 0 for success.
2734 */
2735int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2736 struct snd_ctl_elem_value *ucontrol)
2737{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002738 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2739 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2740 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002741 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002742 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002743 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002744 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002745 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002746 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002747
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002748 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002749 return -EINVAL;
2750 mux = ucontrol->value.enumerated.item[0];
2751 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002752 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002753 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002754 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002755 return -EINVAL;
2756 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002757 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002758 }
2759
Liam Girdwood3cd04342012-03-09 12:02:08 +00002760 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002761
Mark Brown3a655772009-10-05 17:23:30 +01002762 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002763 if (change) {
2764 for (wi = 0; wi < wlist->num_widgets; wi++) {
2765 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002766
Stephen Warrenfafd2172011-04-28 17:38:00 -06002767 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002768
Stephen Warrenfafd2172011-04-28 17:38:00 -06002769 update.kcontrol = kcontrol;
2770 update.widget = widget;
2771 update.reg = e->reg;
2772 update.mask = mask;
2773 update.val = val;
2774 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002775
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002776 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002777
Stephen Warrenfafd2172011-04-28 17:38:00 -06002778 widget->dapm->update = NULL;
2779 }
2780 }
2781
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002782 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002783 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002784}
2785EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2786
2787/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002788 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2789 * @kcontrol: mixer control
2790 * @ucontrol: control element information
2791 *
2792 * Returns 0 for success.
2793 */
2794int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2795 struct snd_ctl_elem_value *ucontrol)
2796{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002797 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2798 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002799
2800 ucontrol->value.enumerated.item[0] = widget->value;
2801
2802 return 0;
2803}
2804EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2805
2806/**
2807 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2808 * @kcontrol: mixer control
2809 * @ucontrol: control element information
2810 *
2811 * Returns 0 for success.
2812 */
2813int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2814 struct snd_ctl_elem_value *ucontrol)
2815{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002816 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2817 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2818 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002819 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002820 struct soc_enum *e =
2821 (struct soc_enum *)kcontrol->private_value;
2822 int change;
2823 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002824 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002825
2826 if (ucontrol->value.enumerated.item[0] >= e->max)
2827 return -EINVAL;
2828
Liam Girdwood3cd04342012-03-09 12:02:08 +00002829 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002830
2831 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002832 if (change) {
2833 for (wi = 0; wi < wlist->num_widgets; wi++) {
2834 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002835
Stephen Warrenfafd2172011-04-28 17:38:00 -06002836 widget->value = ucontrol->value.enumerated.item[0];
2837
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002838 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002839 }
2840 }
2841
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002842 mutex_unlock(&card->dapm_mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002843 return ret;
2844}
2845EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2846
2847/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002848 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2849 * callback
2850 * @kcontrol: mixer control
2851 * @ucontrol: control element information
2852 *
2853 * Callback to get the value of a dapm semi enumerated double mixer control.
2854 *
2855 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2856 * used for handling bitfield coded enumeration for example.
2857 *
2858 * Returns 0 for success.
2859 */
2860int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2861 struct snd_ctl_elem_value *ucontrol)
2862{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002863 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2864 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002865 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002866 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002867
2868 reg_val = snd_soc_read(widget->codec, e->reg);
2869 val = (reg_val >> e->shift_l) & e->mask;
2870 for (mux = 0; mux < e->max; mux++) {
2871 if (val == e->values[mux])
2872 break;
2873 }
2874 ucontrol->value.enumerated.item[0] = mux;
2875 if (e->shift_l != e->shift_r) {
2876 val = (reg_val >> e->shift_r) & e->mask;
2877 for (mux = 0; mux < e->max; mux++) {
2878 if (val == e->values[mux])
2879 break;
2880 }
2881 ucontrol->value.enumerated.item[1] = mux;
2882 }
2883
2884 return 0;
2885}
2886EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2887
2888/**
2889 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2890 * callback
2891 * @kcontrol: mixer control
2892 * @ucontrol: control element information
2893 *
2894 * Callback to set the value of a dapm semi enumerated double mixer control.
2895 *
2896 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2897 * used for handling bitfield coded enumeration for example.
2898 *
2899 * Returns 0 for success.
2900 */
2901int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2902 struct snd_ctl_elem_value *ucontrol)
2903{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002904 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2905 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2906 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002907 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002908 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002909 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002910 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002911 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002912 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002913
2914 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2915 return -EINVAL;
2916 mux = ucontrol->value.enumerated.item[0];
2917 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2918 mask = e->mask << e->shift_l;
2919 if (e->shift_l != e->shift_r) {
2920 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2921 return -EINVAL;
2922 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2923 mask |= e->mask << e->shift_r;
2924 }
2925
Liam Girdwood3cd04342012-03-09 12:02:08 +00002926 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002927
Mark Brown3a655772009-10-05 17:23:30 +01002928 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002929 if (change) {
2930 for (wi = 0; wi < wlist->num_widgets; wi++) {
2931 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002932
Stephen Warrenfafd2172011-04-28 17:38:00 -06002933 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002934
Stephen Warrenfafd2172011-04-28 17:38:00 -06002935 update.kcontrol = kcontrol;
2936 update.widget = widget;
2937 update.reg = e->reg;
2938 update.mask = mask;
2939 update.val = val;
2940 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002941
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002942 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002943
Stephen Warrenfafd2172011-04-28 17:38:00 -06002944 widget->dapm->update = NULL;
2945 }
2946 }
2947
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002948 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002949 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002950}
2951EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2952
2953/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002954 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2955 *
2956 * @kcontrol: mixer control
2957 * @uinfo: control element information
2958 *
2959 * Callback to provide information about a pin switch control.
2960 */
2961int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2962 struct snd_ctl_elem_info *uinfo)
2963{
2964 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2965 uinfo->count = 1;
2966 uinfo->value.integer.min = 0;
2967 uinfo->value.integer.max = 1;
2968
2969 return 0;
2970}
2971EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2972
2973/**
2974 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2975 *
2976 * @kcontrol: mixer control
2977 * @ucontrol: Value
2978 */
2979int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2980 struct snd_ctl_elem_value *ucontrol)
2981{
Mark Brown48a8c392012-02-14 17:11:15 -08002982 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002983 const char *pin = (const char *)kcontrol->private_value;
2984
Liam Girdwood3cd04342012-03-09 12:02:08 +00002985 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002986
2987 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002988 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002989
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002990 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002991
2992 return 0;
2993}
2994EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2995
2996/**
2997 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2998 *
2999 * @kcontrol: mixer control
3000 * @ucontrol: Value
3001 */
3002int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3003 struct snd_ctl_elem_value *ucontrol)
3004{
Mark Brown48a8c392012-02-14 17:11:15 -08003005 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003006 const char *pin = (const char *)kcontrol->private_value;
3007
Liam Girdwood3cd04342012-03-09 12:02:08 +00003008 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003009
3010 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003011 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003012 else
Mark Brown48a8c392012-02-14 17:11:15 -08003013 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003014
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003015 mutex_unlock(&card->dapm_mutex);
3016
Mark Brown48a8c392012-02-14 17:11:15 -08003017 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003018 return 0;
3019}
3020EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3021
Mark Brown5ba06fc2012-02-16 11:07:13 -08003022static struct snd_soc_dapm_widget *
3023snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3024 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003025{
3026 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003027 size_t name_len;
Mark Brown62ea8742012-01-21 21:14:48 +00003028 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003029
3030 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003031 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003032
Mark Brown62ea8742012-01-21 21:14:48 +00003033 switch (w->id) {
3034 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003035 w->regulator = devm_regulator_get(dapm->dev, w->name);
3036 if (IS_ERR(w->regulator)) {
3037 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003038 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003039 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003040 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003041 }
3042 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003043 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003044#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003045 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003046 if (IS_ERR(w->clk)) {
3047 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003048 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003049 w->name, ret);
3050 return NULL;
3051 }
Mark Brownec029952012-06-04 08:16:20 +01003052#else
3053 return NULL;
3054#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003055 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003056 default:
3057 break;
3058 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003059
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003060 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00003061 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003062 name_len += 1 + strlen(dapm->codec->name_prefix);
3063 w->name = kmalloc(name_len, GFP_KERNEL);
3064 if (w->name == NULL) {
3065 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003066 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003067 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00003068 if (dapm->codec && dapm->codec->name_prefix)
Mark Brown888df392012-02-16 19:37:51 -08003069 snprintf((char *)w->name, name_len, "%s %s",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003070 dapm->codec->name_prefix, widget->name);
3071 else
Mark Brown888df392012-02-16 19:37:51 -08003072 snprintf((char *)w->name, name_len, "%s", widget->name);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003073
Mark Brown7ca3a182011-10-08 14:04:50 +01003074 switch (w->id) {
3075 case snd_soc_dapm_switch:
3076 case snd_soc_dapm_mixer:
3077 case snd_soc_dapm_mixer_named_ctl:
3078 w->power_check = dapm_generic_check_power;
3079 break;
3080 case snd_soc_dapm_mux:
3081 case snd_soc_dapm_virt_mux:
3082 case snd_soc_dapm_value_mux:
3083 w->power_check = dapm_generic_check_power;
3084 break;
3085 case snd_soc_dapm_adc:
3086 case snd_soc_dapm_aif_out:
3087 w->power_check = dapm_adc_check_power;
3088 break;
3089 case snd_soc_dapm_dac:
3090 case snd_soc_dapm_aif_in:
3091 w->power_check = dapm_dac_check_power;
3092 break;
3093 case snd_soc_dapm_pga:
3094 case snd_soc_dapm_out_drv:
3095 case snd_soc_dapm_input:
3096 case snd_soc_dapm_output:
3097 case snd_soc_dapm_micbias:
3098 case snd_soc_dapm_spk:
3099 case snd_soc_dapm_hp:
3100 case snd_soc_dapm_mic:
3101 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003102 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003103 w->power_check = dapm_generic_check_power;
3104 break;
3105 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003106 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003107 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003108 w->power_check = dapm_supply_check_power;
3109 break;
Mark Brown888df392012-02-16 19:37:51 -08003110 case snd_soc_dapm_dai:
3111 w->power_check = dapm_dai_check_power;
3112 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003113 default:
3114 w->power_check = dapm_always_on_check_power;
3115 break;
3116 }
3117
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003118 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003119 w->dapm = dapm;
3120 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003121 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003122 INIT_LIST_HEAD(&w->sources);
3123 INIT_LIST_HEAD(&w->sinks);
3124 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003125 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003126 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003127
3128 /* machine layer set ups unconnected pins and insertions */
3129 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003130 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003131}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003132
3133/**
Mark Brown4ba13272008-05-13 14:51:19 +02003134 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003135 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003136 * @widget: widget array
3137 * @num: number of widgets
3138 *
3139 * Creates new DAPM controls based upon the templates.
3140 *
3141 * Returns 0 for success else error.
3142 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003143int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003144 const struct snd_soc_dapm_widget *widget,
3145 int num)
3146{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003147 struct snd_soc_dapm_widget *w;
3148 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003149 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003150
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003151 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003152 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003153 w = snd_soc_dapm_new_control(dapm, widget);
3154 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003155 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003156 "ASoC: Failed to create DAPM control %s\n",
3157 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003158 ret = -ENOMEM;
3159 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003160 }
Mark Brown4ba13272008-05-13 14:51:19 +02003161 widget++;
3162 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003163 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003164 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003165}
3166EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3167
Mark Brownc74184e2012-04-04 22:12:09 +01003168static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3169 struct snd_kcontrol *kcontrol, int event)
3170{
3171 struct snd_soc_dapm_path *source_p, *sink_p;
3172 struct snd_soc_dai *source, *sink;
3173 const struct snd_soc_pcm_stream *config = w->params;
3174 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003175 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003176 u64 fmt;
3177 int ret;
3178
3179 BUG_ON(!config);
3180 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3181
3182 /* We only support a single source and sink, pick the first */
3183 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3184 list_sink);
3185 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3186 list_source);
3187
3188 BUG_ON(!source_p || !sink_p);
3189 BUG_ON(!sink_p->source || !source_p->sink);
3190 BUG_ON(!source_p->source || !sink_p->sink);
3191
3192 source = source_p->source->priv;
3193 sink = sink_p->sink->priv;
3194
3195 /* Be a little careful as we don't want to overflow the mask array */
3196 if (config->formats) {
3197 fmt = ffs(config->formats) - 1;
3198 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003199 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003200 config->formats);
3201 fmt = 0;
3202 }
3203
3204 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003205 params = kzalloc(sizeof(*params), GFP_KERNEL);
3206 if (!params) {
3207 ret = -ENOMEM;
3208 goto out;
3209 }
3210 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003211
Mark Brown9747cec2012-04-26 19:12:21 +01003212 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003213 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003214 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003215 config->rate_max;
3216
Mark Brown9747cec2012-04-26 19:12:21 +01003217 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003218 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003219 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003220 = config->channels_max;
3221
3222 memset(&substream, 0, sizeof(substream));
3223
3224 switch (event) {
3225 case SND_SOC_DAPM_PRE_PMU:
3226 if (source->driver->ops && source->driver->ops->hw_params) {
3227 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3228 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003229 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003230 if (ret != 0) {
3231 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003232 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003233 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003234 }
3235 }
3236
3237 if (sink->driver->ops && sink->driver->ops->hw_params) {
3238 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003239 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003240 sink);
3241 if (ret != 0) {
3242 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003243 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003244 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003245 }
3246 }
3247 break;
3248
3249 case SND_SOC_DAPM_POST_PMU:
3250 ret = snd_soc_dai_digital_mute(sink, 0);
3251 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003252 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003253 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003254 break;
3255
3256 case SND_SOC_DAPM_PRE_PMD:
3257 ret = snd_soc_dai_digital_mute(sink, 1);
3258 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003259 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003260 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003261 break;
3262
3263 default:
3264 BUG();
3265 return -EINVAL;
3266 }
3267
Mark Brown9747cec2012-04-26 19:12:21 +01003268out:
3269 kfree(params);
3270 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003271}
3272
3273int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3274 const struct snd_soc_pcm_stream *params,
3275 struct snd_soc_dapm_widget *source,
3276 struct snd_soc_dapm_widget *sink)
3277{
3278 struct snd_soc_dapm_route routes[2];
3279 struct snd_soc_dapm_widget template;
3280 struct snd_soc_dapm_widget *w;
3281 size_t len;
3282 char *link_name;
3283
3284 len = strlen(source->name) + strlen(sink->name) + 2;
3285 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3286 if (!link_name)
3287 return -ENOMEM;
3288 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3289
3290 memset(&template, 0, sizeof(template));
3291 template.reg = SND_SOC_NOPM;
3292 template.id = snd_soc_dapm_dai_link;
3293 template.name = link_name;
3294 template.event = snd_soc_dai_link_event;
3295 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3296 SND_SOC_DAPM_PRE_PMD;
3297
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003298 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003299
3300 w = snd_soc_dapm_new_control(&card->dapm, &template);
3301 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003302 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003303 link_name);
3304 return -ENOMEM;
3305 }
3306
3307 w->params = params;
3308
3309 memset(&routes, 0, sizeof(routes));
3310
3311 routes[0].source = source->name;
3312 routes[0].sink = link_name;
3313 routes[1].source = link_name;
3314 routes[1].sink = sink->name;
3315
3316 return snd_soc_dapm_add_routes(&card->dapm, routes,
3317 ARRAY_SIZE(routes));
3318}
3319
Mark Brown888df392012-02-16 19:37:51 -08003320int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3321 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003322{
Mark Brown888df392012-02-16 19:37:51 -08003323 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003324 struct snd_soc_dapm_widget *w;
3325
Mark Brown888df392012-02-16 19:37:51 -08003326 WARN_ON(dapm->dev != dai->dev);
3327
3328 memset(&template, 0, sizeof(template));
3329 template.reg = SND_SOC_NOPM;
3330
3331 if (dai->driver->playback.stream_name) {
3332 template.id = snd_soc_dapm_dai;
3333 template.name = dai->driver->playback.stream_name;
3334 template.sname = dai->driver->playback.stream_name;
3335
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003336 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003337 template.name);
3338
3339 w = snd_soc_dapm_new_control(dapm, &template);
3340 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003341 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003342 dai->driver->playback.stream_name);
3343 }
3344
3345 w->priv = dai;
3346 dai->playback_widget = w;
3347 }
3348
3349 if (dai->driver->capture.stream_name) {
3350 template.id = snd_soc_dapm_dai;
3351 template.name = dai->driver->capture.stream_name;
3352 template.sname = dai->driver->capture.stream_name;
3353
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003354 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003355 template.name);
3356
3357 w = snd_soc_dapm_new_control(dapm, &template);
3358 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003359 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003360 dai->driver->capture.stream_name);
3361 }
3362
3363 w->priv = dai;
3364 dai->capture_widget = w;
3365 }
3366
3367 return 0;
3368}
3369
3370int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3371{
3372 struct snd_soc_dapm_widget *dai_w, *w;
3373 struct snd_soc_dai *dai;
3374 struct snd_soc_dapm_route r;
3375
3376 memset(&r, 0, sizeof(r));
3377
3378 /* For each DAI widget... */
3379 list_for_each_entry(dai_w, &card->widgets, list) {
3380 if (dai_w->id != snd_soc_dapm_dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003381 continue;
Mark Brown888df392012-02-16 19:37:51 -08003382
3383 dai = dai_w->priv;
3384
3385 /* ...find all widgets with the same stream and link them */
3386 list_for_each_entry(w, &card->widgets, list) {
3387 if (w->dapm != dai_w->dapm)
3388 continue;
3389
3390 if (w->id == snd_soc_dapm_dai)
3391 continue;
3392
3393 if (!w->sname)
3394 continue;
3395
3396 if (dai->driver->playback.stream_name &&
3397 strstr(w->sname,
3398 dai->driver->playback.stream_name)) {
3399 r.source = dai->playback_widget->name;
3400 r.sink = w->name;
3401 dev_dbg(dai->dev, "%s -> %s\n",
3402 r.source, r.sink);
3403
3404 snd_soc_dapm_add_route(w->dapm, &r);
3405 }
3406
3407 if (dai->driver->capture.stream_name &&
3408 strstr(w->sname,
3409 dai->driver->capture.stream_name)) {
3410 r.source = w->name;
3411 r.sink = dai->capture_widget->name;
3412 dev_dbg(dai->dev, "%s -> %s\n",
3413 r.source, r.sink);
3414
3415 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003416 }
3417 }
3418 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003419
Mark Brown888df392012-02-16 19:37:51 -08003420 return 0;
3421}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003422
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003423static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3424 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003425{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003426
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003427 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3428 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3429 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003430
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003431 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3432 w_cpu = cpu_dai->playback_widget;
3433 w_codec = codec_dai->playback_widget;
3434 } else {
3435 w_cpu = cpu_dai->capture_widget;
3436 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003437 }
3438
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003439 if (w_cpu) {
3440
3441 dapm_mark_dirty(w_cpu, "stream event");
3442
3443 switch (event) {
3444 case SND_SOC_DAPM_STREAM_START:
3445 w_cpu->active = 1;
3446 break;
3447 case SND_SOC_DAPM_STREAM_STOP:
3448 w_cpu->active = 0;
3449 break;
3450 case SND_SOC_DAPM_STREAM_SUSPEND:
3451 case SND_SOC_DAPM_STREAM_RESUME:
3452 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3453 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3454 break;
3455 }
3456 }
3457
3458 if (w_codec) {
3459
3460 dapm_mark_dirty(w_codec, "stream event");
3461
3462 switch (event) {
3463 case SND_SOC_DAPM_STREAM_START:
3464 w_codec->active = 1;
3465 break;
3466 case SND_SOC_DAPM_STREAM_STOP:
3467 w_codec->active = 0;
3468 break;
3469 case SND_SOC_DAPM_STREAM_SUSPEND:
3470 case SND_SOC_DAPM_STREAM_RESUME:
3471 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3472 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3473 break;
3474 }
3475 }
3476
3477 dapm_power_widgets(&rtd->card->dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003478}
3479
3480/**
3481 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3482 * @rtd: PCM runtime data
3483 * @stream: stream name
3484 * @event: stream event
3485 *
3486 * Sends a stream event to the dapm core. The core then makes any
3487 * necessary widget power changes.
3488 *
3489 * Returns 0 for success else error.
3490 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003491void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3492 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003493{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003494 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003495
Liam Girdwood3cd04342012-03-09 12:02:08 +00003496 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003497 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003498 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003499}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003500
3501/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003502 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003503 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003504 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003505 *
Mark Brown74b8f952009-06-06 11:26:15 +01003506 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003507 * a valid audio route and active audio stream.
3508 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3509 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003510 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003511int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003512{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003513 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003514}
Liam Girdwooda5302182008-07-07 13:35:17 +01003515EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003516
3517/**
Mark Brownda341832010-03-15 19:23:37 +00003518 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003519 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003520 * @pin: pin name
3521 *
3522 * Enables input/output pin regardless of any other state. This is
3523 * intended for use with microphone bias supplies used in microphone
3524 * jack detection.
3525 *
3526 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3527 * do any widget power switching.
3528 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003529int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3530 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003531{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003532 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003533
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003534 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003535 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003536 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003537 }
3538
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003539 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003540 w->connected = 1;
3541 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003542 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003543
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003544 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003545}
3546EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3547
3548/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003549 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003550 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003551 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003552 *
Mark Brown74b8f952009-06-06 11:26:15 +01003553 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003554 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3555 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003556 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003557int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3558 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003559{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003560 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003561}
3562EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3563
3564/**
Mark Brown5817b522008-09-24 11:23:11 +01003565 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003566 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003567 * @pin: pin name
3568 *
3569 * Marks the specified pin as being not connected, disabling it along
3570 * any parent or child widgets. At present this is identical to
3571 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3572 * additional things such as disabling controls which only affect
3573 * paths through the pin.
3574 *
3575 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3576 * do any widget power switching.
3577 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003578int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003579{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003580 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003581}
3582EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3583
3584/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003585 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003586 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003587 * @pin: audio signal pin endpoint (or start point)
3588 *
3589 * Get audio pin status - connected or disconnected.
3590 *
3591 * Returns 1 for connected otherwise 0.
3592 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003593int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3594 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003595{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003596 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003597
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003598 if (w)
3599 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003600
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003601 return 0;
3602}
Liam Girdwooda5302182008-07-07 13:35:17 +01003603EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003604
3605/**
Mark Brown1547aba2010-05-07 21:11:40 +01003606 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003607 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003608 * @pin: audio signal pin endpoint (or start point)
3609 *
3610 * Mark the given endpoint or pin as ignoring suspend. When the
3611 * system is disabled a path between two endpoints flagged as ignoring
3612 * suspend will not be disabled. The path must already be enabled via
3613 * normal means at suspend time, it will not be turned on if it was not
3614 * already enabled.
3615 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003616int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3617 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003618{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003619 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003620
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003621 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003622 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003623 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003624 }
3625
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003626 w->ignore_suspend = 1;
3627
3628 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003629}
3630EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3631
Stephen Warren16332812011-11-23 12:42:04 -07003632static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3633 struct snd_soc_dapm_widget *w)
3634{
3635 struct snd_soc_dapm_path *p;
3636
3637 list_for_each_entry(p, &card->paths, list) {
3638 if ((p->source == w) || (p->sink == w)) {
3639 dev_dbg(card->dev,
3640 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3641 p->source->name, p->source->id, p->source->dapm,
3642 p->sink->name, p->sink->id, p->sink->dapm);
3643
3644 /* Connected to something other than the codec */
3645 if (p->source->dapm != p->sink->dapm)
3646 return true;
3647 /*
3648 * Loopback connection from codec external pin to
3649 * codec external pin
3650 */
3651 if (p->sink->id == snd_soc_dapm_input) {
3652 switch (p->source->id) {
3653 case snd_soc_dapm_output:
3654 case snd_soc_dapm_micbias:
3655 return true;
3656 default:
3657 break;
3658 }
3659 }
3660 }
3661 }
3662
3663 return false;
3664}
3665
3666/**
3667 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3668 * @codec: The codec whose pins should be processed
3669 *
3670 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3671 * which are unused. Pins are used if they are connected externally to the
3672 * codec, whether that be to some other device, or a loop-back connection to
3673 * the codec itself.
3674 */
3675void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3676{
3677 struct snd_soc_card *card = codec->card;
3678 struct snd_soc_dapm_context *dapm = &codec->dapm;
3679 struct snd_soc_dapm_widget *w;
3680
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003681 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003682 &card->dapm, &codec->dapm);
3683
3684 list_for_each_entry(w, &card->widgets, list) {
3685 if (w->dapm != dapm)
3686 continue;
3687 switch (w->id) {
3688 case snd_soc_dapm_input:
3689 case snd_soc_dapm_output:
3690 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003691 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003692 w->name);
3693 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003694 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003695 "... Not in map; disabling\n");
3696 snd_soc_dapm_nc_pin(dapm, w->name);
3697 }
3698 break;
3699 default:
3700 break;
3701 }
3702 }
3703}
3704
Mark Brown1547aba2010-05-07 21:11:40 +01003705/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003706 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003707 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003708 *
3709 * Free all dapm widgets and resources.
3710 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003711void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003712{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003713 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003714 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003715 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003716 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003717}
3718EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3719
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003720static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003721{
Liam Girdwood01005a72012-07-06 16:57:05 +01003722 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003723 struct snd_soc_dapm_widget *w;
3724 LIST_HEAD(down_list);
3725 int powerdown = 0;
3726
Liam Girdwood01005a72012-07-06 16:57:05 +01003727 mutex_lock(&card->dapm_mutex);
3728
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003729 list_for_each_entry(w, &dapm->card->widgets, list) {
3730 if (w->dapm != dapm)
3731 continue;
Mark Brown51737472009-06-22 13:16:51 +01003732 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003733 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003734 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003735 powerdown = 1;
3736 }
3737 }
3738
3739 /* If there were no widgets to power down we're already in
3740 * standby.
3741 */
3742 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003743 if (dapm->bias_level == SND_SOC_BIAS_ON)
3744 snd_soc_dapm_set_bias_level(dapm,
3745 SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003746 dapm_seq_run(dapm, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003747 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3748 snd_soc_dapm_set_bias_level(dapm,
3749 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003750 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003751
3752 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003753}
Mark Brown51737472009-06-22 13:16:51 +01003754
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003755/*
3756 * snd_soc_dapm_shutdown - callback for system shutdown
3757 */
3758void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3759{
3760 struct snd_soc_codec *codec;
3761
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003762 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003763 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003764 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3765 snd_soc_dapm_set_bias_level(&codec->dapm,
3766 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003767 }
Mark Brown51737472009-06-22 13:16:51 +01003768}
3769
Richard Purdie2b97eab2006-10-06 18:32:18 +02003770/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003771MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003772MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3773MODULE_LICENSE("GPL");