blob: e4e5420de725afbad7ebc475e1f7d03cf8d28826 [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 Brown15e4c72f2008-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 Brown15e4c72f2008-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 Brown15e4c72f2008-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 Brown15e4c72f2008-07-02 11:51:20 +0100124 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200125
126 kfree(buf);
Mark Brown15e4c72f2008-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
Stephen Warren85762e72013-03-29 15:40:10 -0600507/*
508 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
509 * create it. Either way, add the widget into the control's widget list
510 */
511static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
512 int kci, struct snd_soc_dapm_path *path)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200513{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200514 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000515 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000516 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600517 size_t prefix_len;
518 int shared;
519 struct snd_kcontrol *kcontrol;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600520 struct snd_soc_dapm_widget_list *wlist;
Stephen Warren85762e72013-03-29 15:40:10 -0600521 int wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600522 size_t wlistsize;
Stephen Warren85762e72013-03-29 15:40:10 -0600523 bool wname_in_long_name, kcname_in_long_name;
524 size_t name_len;
525 char *long_name;
526 const char *name;
527 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000528
529 if (dapm->codec)
530 prefix = dapm->codec->name_prefix;
531 else
532 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200533
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000534 if (prefix)
535 prefix_len = strlen(prefix) + 1;
536 else
537 prefix_len = 0;
538
Stephen Warren85762e72013-03-29 15:40:10 -0600539 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
540 &kcontrol);
541
542 if (kcontrol) {
543 wlist = kcontrol->private_data;
544 wlistentries = wlist->num_widgets + 1;
545 } else {
546 wlist = NULL;
547 wlistentries = 1;
548 }
549
550 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
551 wlistentries * sizeof(struct snd_soc_dapm_widget *);
552 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
553 if (wlist == NULL) {
554 dev_err(dapm->dev, "ASoC: can't allocate widget list for %s\n",
555 w->name);
556 return -ENOMEM;
557 }
558 wlist->num_widgets = wlistentries;
559 wlist->widgets[wlistentries - 1] = w;
560
561 if (!kcontrol) {
562 if (shared) {
563 wname_in_long_name = false;
564 kcname_in_long_name = true;
565 } else {
566 switch (w->id) {
567 case snd_soc_dapm_switch:
568 case snd_soc_dapm_mixer:
569 wname_in_long_name = true;
570 kcname_in_long_name = true;
571 break;
572 case snd_soc_dapm_mixer_named_ctl:
573 wname_in_long_name = false;
574 kcname_in_long_name = true;
575 break;
576 case snd_soc_dapm_mux:
577 case snd_soc_dapm_virt_mux:
578 case snd_soc_dapm_value_mux:
579 wname_in_long_name = true;
580 kcname_in_long_name = false;
581 break;
582 default:
583 kfree(wlist);
584 return -EINVAL;
585 }
586 }
587
588 if (wname_in_long_name && kcname_in_long_name) {
589 name_len = strlen(w->name) - prefix_len + 1 +
590 strlen(w->kcontrol_news[kci].name) + 1;
591
592 long_name = kmalloc(name_len, GFP_KERNEL);
593 if (long_name == NULL) {
594 kfree(wlist);
595 return -ENOMEM;
596 }
597
598 /*
599 * The control will get a prefix from the control
600 * creation process but we're also using the same
601 * prefix for widgets so cut the prefix off the
602 * front of the widget name.
603 */
604 snprintf(long_name, name_len, "%s %s",
605 w->name + prefix_len,
606 w->kcontrol_news[kci].name);
607 long_name[name_len - 1] = '\0';
608
609 name = long_name;
610 } else if (wname_in_long_name) {
611 long_name = NULL;
612 name = w->name + prefix_len;
613 } else {
614 long_name = NULL;
615 name = w->kcontrol_news[kci].name;
616 }
617
618 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], wlist, name,
619 prefix);
620 ret = snd_ctl_add(card, kcontrol);
621 if (ret < 0) {
622 dev_err(dapm->dev,
623 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
624 w->name, name, ret);
625 kfree(wlist);
626 kfree(long_name);
627 return ret;
628 }
629
630 path->long_name = long_name;
631 }
632
633 kcontrol->private_data = wlist;
634 w->kcontrols[kci] = kcontrol;
635 path->kcontrol = kcontrol;
636
637 return 0;
638}
639
640/* create new dapm mixer control */
641static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
642{
643 int i, ret;
644 struct snd_soc_dapm_path *path;
645
Richard Purdie2b97eab2006-10-06 18:32:18 +0200646 /* add kcontrol */
647 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200648 /* match name */
649 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200650 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600651 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200652 continue;
653
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200654 if (w->kcontrols[i]) {
655 path->kcontrol = w->kcontrols[i];
656 continue;
657 }
658
Stephen Warren85762e72013-03-29 15:40:10 -0600659 ret = dapm_create_or_share_mixmux_kcontrol(w, i, path);
660 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200661 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200662 }
663 }
Stephen Warren85762e72013-03-29 15:40:10 -0600664
665 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200666}
667
668/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200669static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200670{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200671 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600672 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600673 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200674
Stephen Warrenaf468002011-04-28 17:38:01 -0600675 if (w->num_kcontrols != 1) {
676 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000677 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600678 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200679 return -EINVAL;
680 }
681
Stephen Warren85762e72013-03-29 15:40:10 -0600682 path = list_first_entry(&w->sources, struct snd_soc_dapm_path,
683 list_sink);
684 if (!path) {
685 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
686 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600687 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200688
Stephen Warren85762e72013-03-29 15:40:10 -0600689 ret = dapm_create_or_share_mixmux_kcontrol(w, 0, path);
690 if (ret < 0)
691 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600692
Richard Purdie2b97eab2006-10-06 18:32:18 +0200693 list_for_each_entry(path, &w->sources, list_sink)
Stephen Warren85762e72013-03-29 15:40:10 -0600694 path->kcontrol = w->kcontrols[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200695
Stephen Warrenaf468002011-04-28 17:38:01 -0600696 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200697}
698
699/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200700static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200701{
Mark Browna6c65732010-03-03 17:45:21 +0000702 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200703 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000704 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200705
Mark Browna6c65732010-03-03 17:45:21 +0000706 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200707}
708
709/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100710static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
711 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200712{
713 struct snd_soc_dapm_path *p;
714
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100715 list_for_each_entry(p, sink, list_source) {
716 if (p->walked) {
717 p->walked = 0;
718 dapm_clear_walk_output(dapm, &p->sink->sinks);
719 }
720 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200721}
722
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100723static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
724 struct list_head *source)
725{
726 struct snd_soc_dapm_path *p;
727
728 list_for_each_entry(p, source, list_sink) {
729 if (p->walked) {
730 p->walked = 0;
731 dapm_clear_walk_input(dapm, &p->source->sources);
732 }
733 }
734}
735
736
Mark Brown9949788b2010-05-07 20:24:05 +0100737/* We implement power down on suspend by checking the power state of
738 * the ALSA card - when we are suspending the ALSA state for the card
739 * is set to D3.
740 */
741static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
742{
Mark Brown12ea2c72011-03-02 18:17:32 +0000743 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100744
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000745 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100746 case SNDRV_CTL_POWER_D3hot:
747 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100748 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000749 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200750 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100751 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100752 default:
753 return 1;
754 }
755}
756
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100757/* add widget to list if it's not already in the list */
758static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
759 struct snd_soc_dapm_widget *w)
760{
761 struct snd_soc_dapm_widget_list *wlist;
762 int wlistsize, wlistentries, i;
763
764 if (*list == NULL)
765 return -EINVAL;
766
767 wlist = *list;
768
769 /* is this widget already in the list */
770 for (i = 0; i < wlist->num_widgets; i++) {
771 if (wlist->widgets[i] == w)
772 return 0;
773 }
774
775 /* allocate some new space */
776 wlistentries = wlist->num_widgets + 1;
777 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
778 wlistentries * sizeof(struct snd_soc_dapm_widget *);
779 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
780 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000781 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100782 w->name);
783 return -ENOMEM;
784 }
785 wlist = *list;
786
787 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000788 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100789 w->name, wlist->num_widgets);
790
791 wlist->widgets[wlist->num_widgets] = w;
792 wlist->num_widgets++;
793 return 1;
794}
795
Richard Purdie2b97eab2006-10-06 18:32:18 +0200796/*
797 * Recursively check for a completed path to an active or physically connected
798 * output widget. Returns number of complete paths.
799 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100800static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
801 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200802{
803 struct snd_soc_dapm_path *path;
804 int con = 0;
805
Mark Brown024dc072011-10-09 11:52:05 +0100806 if (widget->outputs >= 0)
807 return widget->outputs;
808
Mark Brownde02d072011-09-20 21:43:24 +0100809 DAPM_UPDATE_STAT(widget, path_checks);
810
Mark Brown62ea8742012-01-21 21:14:48 +0000811 switch (widget->id) {
812 case snd_soc_dapm_supply:
813 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200814 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100815 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000816 default:
817 break;
818 }
Mark Brown246d0a12009-04-22 18:24:55 +0100819
Mark Brown010ff262009-08-17 17:39:22 +0100820 switch (widget->id) {
821 case snd_soc_dapm_adc:
822 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800823 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100824 if (widget->active) {
825 widget->outputs = snd_soc_dapm_suspend_check(widget);
826 return widget->outputs;
827 }
Mark Brown010ff262009-08-17 17:39:22 +0100828 default:
829 break;
830 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200831
832 if (widget->connected) {
833 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100834 if (widget->id == snd_soc_dapm_output && !widget->ext) {
835 widget->outputs = snd_soc_dapm_suspend_check(widget);
836 return widget->outputs;
837 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200838
839 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100840 if (widget->id == snd_soc_dapm_hp ||
841 widget->id == snd_soc_dapm_spk ||
842 (widget->id == snd_soc_dapm_line &&
843 !list_empty(&widget->sources))) {
844 widget->outputs = snd_soc_dapm_suspend_check(widget);
845 return widget->outputs;
846 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200847 }
848
849 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100850 DAPM_UPDATE_STAT(widget, neighbour_checks);
851
Mark Brownbf3a9e12011-06-13 16:42:29 +0100852 if (path->weak)
853 continue;
854
Mark Brown8af294b2013-02-22 17:48:15 +0000855 if (path->walking)
856 return 1;
857
Richard Purdie2b97eab2006-10-06 18:32:18 +0200858 if (path->walked)
859 continue;
860
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100861 trace_snd_soc_dapm_output_path(widget, path);
862
Richard Purdie2b97eab2006-10-06 18:32:18 +0200863 if (path->sink && path->connect) {
864 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000865 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100866
867 /* do we need to add this widget to the list ? */
868 if (list) {
869 int err;
870 err = dapm_list_add_widget(list, path->sink);
871 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000872 dev_err(widget->dapm->dev,
873 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100874 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000875 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100876 return con;
877 }
878 }
879
880 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000881
882 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200883 }
884 }
885
Mark Brown024dc072011-10-09 11:52:05 +0100886 widget->outputs = con;
887
Richard Purdie2b97eab2006-10-06 18:32:18 +0200888 return con;
889}
890
891/*
892 * Recursively check for a completed path to an active or physically connected
893 * input widget. Returns number of complete paths.
894 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100895static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
896 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200897{
898 struct snd_soc_dapm_path *path;
899 int con = 0;
900
Mark Brown024dc072011-10-09 11:52:05 +0100901 if (widget->inputs >= 0)
902 return widget->inputs;
903
Mark Brownde02d072011-09-20 21:43:24 +0100904 DAPM_UPDATE_STAT(widget, path_checks);
905
Mark Brown62ea8742012-01-21 21:14:48 +0000906 switch (widget->id) {
907 case snd_soc_dapm_supply:
908 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200909 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100910 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000911 default:
912 break;
913 }
Mark Brown246d0a12009-04-22 18:24:55 +0100914
Richard Purdie2b97eab2006-10-06 18:32:18 +0200915 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100916 switch (widget->id) {
917 case snd_soc_dapm_dac:
918 case snd_soc_dapm_aif_in:
Mark Brown888df392012-02-16 19:37:51 -0800919 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100920 if (widget->active) {
921 widget->inputs = snd_soc_dapm_suspend_check(widget);
922 return widget->inputs;
923 }
Mark Brown010ff262009-08-17 17:39:22 +0100924 default:
925 break;
926 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200927
928 if (widget->connected) {
929 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100930 if (widget->id == snd_soc_dapm_input && !widget->ext) {
931 widget->inputs = snd_soc_dapm_suspend_check(widget);
932 return widget->inputs;
933 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200934
935 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100936 if (widget->id == snd_soc_dapm_vmid) {
937 widget->inputs = snd_soc_dapm_suspend_check(widget);
938 return widget->inputs;
939 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200940
941 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300942 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100943 (widget->id == snd_soc_dapm_line &&
944 !list_empty(&widget->sinks))) {
945 widget->inputs = snd_soc_dapm_suspend_check(widget);
946 return widget->inputs;
947 }
948
Mark Brown1ab97c82011-11-27 16:21:51 +0000949 /* signal generator */
950 if (widget->id == snd_soc_dapm_siggen) {
951 widget->inputs = snd_soc_dapm_suspend_check(widget);
952 return widget->inputs;
953 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200954 }
955
956 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +0100957 DAPM_UPDATE_STAT(widget, neighbour_checks);
958
Mark Brownbf3a9e12011-06-13 16:42:29 +0100959 if (path->weak)
960 continue;
961
Mark Brown8af294b2013-02-22 17:48:15 +0000962 if (path->walking)
963 return 1;
964
Richard Purdie2b97eab2006-10-06 18:32:18 +0200965 if (path->walked)
966 continue;
967
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100968 trace_snd_soc_dapm_input_path(widget, path);
969
Richard Purdie2b97eab2006-10-06 18:32:18 +0200970 if (path->source && path->connect) {
971 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000972 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100973
974 /* do we need to add this widget to the list ? */
975 if (list) {
976 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +0100977 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100978 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000979 dev_err(widget->dapm->dev,
980 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100981 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000982 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100983 return con;
984 }
985 }
986
987 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000988
989 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200990 }
991 }
992
Mark Brown024dc072011-10-09 11:52:05 +0100993 widget->inputs = con;
994
Richard Purdie2b97eab2006-10-06 18:32:18 +0200995 return con;
996}
997
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100998/**
999 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1000 * @dai: the soc DAI.
1001 * @stream: stream direction.
1002 * @list: list of active widgets for this stream.
1003 *
1004 * Queries DAPM graph as to whether an valid audio stream path exists for
1005 * the initial stream specified by name. This takes into account
1006 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1007 *
1008 * Returns the number of valid paths or negative error.
1009 */
1010int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1011 struct snd_soc_dapm_widget_list **list)
1012{
1013 struct snd_soc_card *card = dai->card;
1014 int paths;
1015
1016 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1017 dapm_reset(card);
1018
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001019 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001020 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001021 dapm_clear_walk_output(&card->dapm,
1022 &dai->playback_widget->sinks);
1023 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001024 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001025 dapm_clear_walk_input(&card->dapm,
1026 &dai->capture_widget->sources);
1027 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001028
1029 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001030 mutex_unlock(&card->dapm_mutex);
1031
1032 return paths;
1033}
1034
Richard Purdie2b97eab2006-10-06 18:32:18 +02001035/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001036 * Handler for generic register modifier widget.
1037 */
1038int dapm_reg_event(struct snd_soc_dapm_widget *w,
1039 struct snd_kcontrol *kcontrol, int event)
1040{
1041 unsigned int val;
1042
1043 if (SND_SOC_DAPM_EVENT_ON(event))
1044 val = w->on_val;
1045 else
1046 val = w->off_val;
1047
Liam Girdwood49575fb52012-03-06 18:16:19 +00001048 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001049 w->mask << w->shift, val << w->shift);
1050
1051 return 0;
1052}
Mark Brown11589412008-07-29 11:42:23 +01001053EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001054
Mark Brown62ea8742012-01-21 21:14:48 +00001055/*
1056 * Handler for regulator supply widget.
1057 */
1058int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1059 struct snd_kcontrol *kcontrol, int event)
1060{
Mark Brownc05b84d2012-09-07 12:57:11 +08001061 int ret;
1062
1063 if (SND_SOC_DAPM_EVENT_ON(event)) {
1064 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001065 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001066 if (ret != 0)
1067 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001068 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001069 w->name, ret);
1070 }
1071
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001072 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001073 } else {
1074 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001075 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001076 if (ret != 0)
1077 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001078 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001079 w->name, ret);
1080 }
1081
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001082 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001083 }
Mark Brown62ea8742012-01-21 21:14:48 +00001084}
1085EXPORT_SYMBOL_GPL(dapm_regulator_event);
1086
Ola Liljad7e7eb92012-05-24 15:26:25 +02001087/*
1088 * Handler for clock supply widget.
1089 */
1090int dapm_clock_event(struct snd_soc_dapm_widget *w,
1091 struct snd_kcontrol *kcontrol, int event)
1092{
1093 if (!w->clk)
1094 return -EIO;
1095
Mark Brownec029952012-06-04 08:16:20 +01001096#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001097 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001098 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001099 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001100 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001101 return 0;
1102 }
Mark Brownec029952012-06-04 08:16:20 +01001103#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001104 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001105}
1106EXPORT_SYMBOL_GPL(dapm_clock_event);
1107
Mark Brownd805002b2011-09-28 18:28:23 +01001108static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1109{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001110 if (w->power_checked)
1111 return w->new_power;
1112
Mark Brownd805002b2011-09-28 18:28:23 +01001113 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001114 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +01001115 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001116 w->new_power = w->power_check(w);
1117
1118 w->power_checked = true;
1119
1120 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +01001121}
1122
Mark Browncd0f2d42009-04-20 16:56:59 +01001123/* Generic check to see if a widget should be powered.
1124 */
1125static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1126{
1127 int in, out;
1128
Mark Brownde02d072011-09-20 21:43:24 +01001129 DAPM_UPDATE_STAT(w, power_checks);
1130
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001131 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001132 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001133 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001134 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001135 return out != 0 && in != 0;
1136}
1137
Mark Brown888df392012-02-16 19:37:51 -08001138static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
1139{
1140 DAPM_UPDATE_STAT(w, power_checks);
1141
Mark Brown1eee1b32012-04-10 13:57:46 +01001142 if (w->active)
1143 return w->active;
1144
1145 return dapm_generic_check_power(w);
Mark Brown888df392012-02-16 19:37:51 -08001146}
1147
Mark Brown6ea31b92009-04-20 17:15:41 +01001148/* Check to see if an ADC has power */
1149static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1150{
1151 int in;
1152
Mark Brownde02d072011-09-20 21:43:24 +01001153 DAPM_UPDATE_STAT(w, power_checks);
1154
Mark Brown6ea31b92009-04-20 17:15:41 +01001155 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001156 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001157 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001158 return in != 0;
1159 } else {
1160 return dapm_generic_check_power(w);
1161 }
1162}
1163
1164/* Check to see if a DAC has power */
1165static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1166{
1167 int out;
1168
Mark Brownde02d072011-09-20 21:43:24 +01001169 DAPM_UPDATE_STAT(w, power_checks);
1170
Mark Brown6ea31b92009-04-20 17:15:41 +01001171 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001172 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001173 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001174 return out != 0;
1175 } else {
1176 return dapm_generic_check_power(w);
1177 }
1178}
1179
Mark Brown246d0a12009-04-22 18:24:55 +01001180/* Check to see if a power supply is needed */
1181static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1182{
1183 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001184
Mark Brownde02d072011-09-20 21:43:24 +01001185 DAPM_UPDATE_STAT(w, power_checks);
1186
Mark Brown246d0a12009-04-22 18:24:55 +01001187 /* Check if one of our outputs is connected */
1188 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001189 DAPM_UPDATE_STAT(w, neighbour_checks);
1190
Mark Brownbf3a9e12011-06-13 16:42:29 +01001191 if (path->weak)
1192 continue;
1193
Mark Brown215edda2009-09-08 18:59:05 +01001194 if (path->connected &&
1195 !path->connected(path->source, path->sink))
1196 continue;
1197
Mark Brown30173582011-02-11 11:42:19 +00001198 if (!path->sink)
1199 continue;
1200
Mark Brownf68d7e12011-10-04 22:57:50 +01001201 if (dapm_widget_power_check(path->sink))
1202 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001203 }
1204
Mark Brownf68d7e12011-10-04 22:57:50 +01001205 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001206}
1207
Mark Brown35c64bc2011-09-28 18:23:53 +01001208static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1209{
1210 return 1;
1211}
1212
Mark Brown38357ab2009-06-06 19:03:23 +01001213static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1214 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001215 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001216{
Mark Brown828a8422011-01-15 13:14:30 +00001217 int *sort;
1218
1219 if (power_up)
1220 sort = dapm_up_seq;
1221 else
1222 sort = dapm_down_seq;
1223
Mark Brown38357ab2009-06-06 19:03:23 +01001224 if (sort[a->id] != sort[b->id])
1225 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001226 if (a->subseq != b->subseq) {
1227 if (power_up)
1228 return a->subseq - b->subseq;
1229 else
1230 return b->subseq - a->subseq;
1231 }
Mark Brownb22ead22009-06-07 12:51:26 +01001232 if (a->reg != b->reg)
1233 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001234 if (a->dapm != b->dapm)
1235 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001236
Mark Brown38357ab2009-06-06 19:03:23 +01001237 return 0;
1238}
Mark Brown42aa3412009-03-01 19:21:10 +00001239
Mark Brown38357ab2009-06-06 19:03:23 +01001240/* Insert a widget in order into a DAPM power sequence. */
1241static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1242 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001243 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001244{
1245 struct snd_soc_dapm_widget *w;
1246
1247 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001248 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001249 list_add_tail(&new_widget->power_list, &w->power_list);
1250 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001251 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001252
Mark Brown38357ab2009-06-06 19:03:23 +01001253 list_add_tail(&new_widget->power_list, list);
1254}
Mark Brown42aa3412009-03-01 19:21:10 +00001255
Mark Brown68f89ad2010-11-03 23:51:49 -04001256static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1257 struct snd_soc_dapm_widget *w, int event)
1258{
1259 struct snd_soc_card *card = dapm->card;
1260 const char *ev_name;
1261 int power, ret;
1262
1263 switch (event) {
1264 case SND_SOC_DAPM_PRE_PMU:
1265 ev_name = "PRE_PMU";
1266 power = 1;
1267 break;
1268 case SND_SOC_DAPM_POST_PMU:
1269 ev_name = "POST_PMU";
1270 power = 1;
1271 break;
1272 case SND_SOC_DAPM_PRE_PMD:
1273 ev_name = "PRE_PMD";
1274 power = 0;
1275 break;
1276 case SND_SOC_DAPM_POST_PMD:
1277 ev_name = "POST_PMD";
1278 power = 0;
1279 break;
Mark Brown80114122013-02-25 15:14:19 +00001280 case SND_SOC_DAPM_WILL_PMU:
1281 ev_name = "WILL_PMU";
1282 power = 1;
1283 break;
1284 case SND_SOC_DAPM_WILL_PMD:
1285 ev_name = "WILL_PMD";
1286 power = 0;
1287 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001288 default:
1289 BUG();
1290 return;
1291 }
1292
1293 if (w->power != power)
1294 return;
1295
1296 if (w->event && (w->event_flags & event)) {
1297 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1298 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001299 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001300 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001301 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001302 if (ret < 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001303 dev_err(dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001304 ev_name, w->name, ret);
1305 }
1306}
1307
Mark Brownb22ead22009-06-07 12:51:26 +01001308/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001309static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001310 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001311{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001312 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001313 struct snd_soc_dapm_widget *w;
1314 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001315 unsigned int value = 0;
1316 unsigned int mask = 0;
1317 unsigned int cur_mask;
1318
1319 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1320 power_list)->reg;
1321
1322 list_for_each_entry(w, pending, power_list) {
1323 cur_mask = 1 << w->shift;
1324 BUG_ON(reg != w->reg);
1325
1326 if (w->invert)
1327 power = !w->power;
1328 else
1329 power = w->power;
1330
1331 mask |= cur_mask;
1332 if (power)
1333 value |= cur_mask;
1334
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001335 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001336 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1337 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001338
Mark Brown68f89ad2010-11-03 23:51:49 -04001339 /* Check for events */
1340 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1341 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001342 }
1343
Mark Brown81628102009-06-07 13:21:24 +01001344 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001345 /* Any widget will do, they should all be updating the
1346 * same register.
1347 */
1348 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1349 power_list);
1350
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001351 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001352 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001353 value, mask, reg, card->pop_time);
1354 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001355 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001356 }
1357
1358 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001359 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1360 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001361 }
Mark Brown42aa3412009-03-01 19:21:10 +00001362}
1363
Mark Brownb22ead22009-06-07 12:51:26 +01001364/* Apply a DAPM power sequence.
1365 *
1366 * We walk over a pre-sorted list of widgets to apply power to. In
1367 * order to minimise the number of writes to the device required
1368 * multiple widgets will be updated in a single write where possible.
1369 * Currently anything that requires more than a single write is not
1370 * handled.
1371 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001372static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001373 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001374{
1375 struct snd_soc_dapm_widget *w, *n;
1376 LIST_HEAD(pending);
1377 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001378 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001379 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001380 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001381 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001382 int *sort;
1383
1384 if (power_up)
1385 sort = dapm_up_seq;
1386 else
1387 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001388
Mark Brownb22ead22009-06-07 12:51:26 +01001389 list_for_each_entry_safe(w, n, list, power_list) {
1390 ret = 0;
1391
1392 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001393 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001394 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001395 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001396 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001397
Mark Brown474b62d2011-01-18 16:14:44 +00001398 if (cur_dapm && cur_dapm->seq_notifier) {
1399 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1400 if (sort[i] == cur_sort)
1401 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001402 i,
1403 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001404 }
1405
Mark Brownb22ead22009-06-07 12:51:26 +01001406 INIT_LIST_HEAD(&pending);
1407 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001408 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001409 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001410 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001411 }
1412
Mark Brown163cac02009-06-07 10:12:52 +01001413 switch (w->id) {
1414 case snd_soc_dapm_pre:
1415 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001416 list_for_each_entry_safe_continue(w, n, list,
1417 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001418
Mark Brownb22ead22009-06-07 12:51:26 +01001419 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001420 ret = w->event(w,
1421 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001422 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001423 ret = w->event(w,
1424 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001425 break;
1426
1427 case snd_soc_dapm_post:
1428 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001429 list_for_each_entry_safe_continue(w, n, list,
1430 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001431
Mark Brownb22ead22009-06-07 12:51:26 +01001432 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001433 ret = w->event(w,
1434 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001435 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001436 ret = w->event(w,
1437 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001438 break;
1439
Mark Brown163cac02009-06-07 10:12:52 +01001440 default:
Mark Brown81628102009-06-07 13:21:24 +01001441 /* Queue it up for application */
1442 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001443 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001444 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001445 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001446 list_move(&w->power_list, &pending);
1447 break;
Mark Brown163cac02009-06-07 10:12:52 +01001448 }
Mark Brownb22ead22009-06-07 12:51:26 +01001449
1450 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001451 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001452 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001453 }
Mark Brownb22ead22009-06-07 12:51:26 +01001454
1455 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001456 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001457
1458 if (cur_dapm && cur_dapm->seq_notifier) {
1459 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1460 if (sort[i] == cur_sort)
1461 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001462 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001463 }
Mark Brown163cac02009-06-07 10:12:52 +01001464}
1465
Mark Brown97404f22010-12-14 16:13:57 +00001466static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1467{
1468 struct snd_soc_dapm_update *update = dapm->update;
1469 struct snd_soc_dapm_widget *w;
1470 int ret;
1471
1472 if (!update)
1473 return;
1474
1475 w = update->widget;
1476
1477 if (w->event &&
1478 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1479 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1480 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001481 dev_err(dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Mark Brown97404f22010-12-14 16:13:57 +00001482 w->name, ret);
1483 }
1484
Liam Girdwood49575fb52012-03-06 18:16:19 +00001485 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001486 update->val);
1487 if (ret < 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001488 dev_err(dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1489 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001490
1491 if (w->event &&
1492 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1493 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1494 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001495 dev_err(dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Mark Brown97404f22010-12-14 16:13:57 +00001496 w->name, ret);
1497 }
1498}
1499
Mark Brown9d0624a2011-02-18 11:49:43 -08001500/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1501 * they're changing state.
1502 */
1503static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1504{
1505 struct snd_soc_dapm_context *d = data;
1506 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001507
Mark Brown56fba412011-06-04 11:25:10 +01001508 /* If we're off and we're not supposed to be go into STANDBY */
1509 if (d->bias_level == SND_SOC_BIAS_OFF &&
1510 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001511 if (d->dev)
1512 pm_runtime_get_sync(d->dev);
1513
Mark Brown9d0624a2011-02-18 11:49:43 -08001514 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1515 if (ret != 0)
1516 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001517 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001518 }
1519
Mark Brown56fba412011-06-04 11:25:10 +01001520 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1521 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001522 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1523 if (ret != 0)
1524 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001525 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001526 }
1527}
1528
1529/* Async callback run prior to DAPM sequences - brings to their final
1530 * state.
1531 */
1532static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1533{
1534 struct snd_soc_dapm_context *d = data;
1535 int ret;
1536
1537 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001538 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1539 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1540 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001541 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1542 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001543 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001544 ret);
1545 }
1546
1547 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001548 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1549 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001550 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1551 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001552 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1553 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001554
1555 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001556 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001557 }
1558
1559 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001560 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1561 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001562 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1563 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001564 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001565 ret);
1566 }
1567}
Mark Brown97404f22010-12-14 16:13:57 +00001568
Mark Brownfe4fda52011-10-03 22:36:57 +01001569static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1570 bool power, bool connect)
1571{
1572 /* If a connection is being made or broken then that update
1573 * will have marked the peer dirty, otherwise the widgets are
1574 * not connected and this update has no impact. */
1575 if (!connect)
1576 return;
1577
1578 /* If the peer is already in the state we're moving to then we
1579 * won't have an impact on it. */
1580 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001581 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001582}
1583
Mark Brown05623c42011-09-28 17:02:31 +01001584static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1585 struct list_head *up_list,
1586 struct list_head *down_list)
1587{
Mark Browndb432b42011-10-03 21:06:40 +01001588 struct snd_soc_dapm_path *path;
1589
Mark Brown05623c42011-09-28 17:02:31 +01001590 if (w->power == power)
1591 return;
1592
1593 trace_snd_soc_dapm_widget_power(w, power);
1594
Mark Browndb432b42011-10-03 21:06:40 +01001595 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001596 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001597 */
1598 list_for_each_entry(path, &w->sources, list_sink) {
1599 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001600 dapm_widget_set_peer_power(path->source, power,
1601 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001602 }
1603 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001604 switch (w->id) {
1605 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001606 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001607 case snd_soc_dapm_clock_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001608 /* Supplies can't affect their outputs, only their inputs */
1609 break;
1610 default:
1611 list_for_each_entry(path, &w->sinks, list_source) {
1612 if (path->sink) {
1613 dapm_widget_set_peer_power(path->sink, power,
1614 path->connect);
1615 }
Mark Browndb432b42011-10-03 21:06:40 +01001616 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001617 break;
Mark Browndb432b42011-10-03 21:06:40 +01001618 }
1619
Mark Brown05623c42011-09-28 17:02:31 +01001620 if (power)
1621 dapm_seq_insert(w, up_list, true);
1622 else
1623 dapm_seq_insert(w, down_list, false);
1624
1625 w->power = power;
1626}
1627
Mark Brown7c81beb2011-09-20 22:22:32 +01001628static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1629 struct list_head *up_list,
1630 struct list_head *down_list)
1631{
Mark Brown7c81beb2011-09-20 22:22:32 +01001632 int power;
1633
1634 switch (w->id) {
1635 case snd_soc_dapm_pre:
1636 dapm_seq_insert(w, down_list, false);
1637 break;
1638 case snd_soc_dapm_post:
1639 dapm_seq_insert(w, up_list, true);
1640 break;
1641
1642 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001643 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001644
Mark Brown05623c42011-09-28 17:02:31 +01001645 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001646 break;
1647 }
1648}
1649
Mark Brown42aa3412009-03-01 19:21:10 +00001650/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001651 * Scan each dapm widget for complete audio path.
1652 * A complete path is a route that has valid endpoints i.e.:-
1653 *
1654 * o DAC to output pin.
1655 * o Input Pin to ADC.
1656 * o Input pin to Output pin (bypass, sidetone)
1657 * o DAC to ADC (loopback).
1658 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001659static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001660{
Mark Brown12ea2c72011-03-02 18:17:32 +00001661 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001662 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001663 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001664 LIST_HEAD(up_list);
1665 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001666 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001667 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001668
Mark Brown84e90932010-11-04 00:07:02 -04001669 trace_snd_soc_dapm_start(card);
1670
Mark Brown56fba412011-06-04 11:25:10 +01001671 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001672 if (d->idle_bias_off)
1673 d->target_bias_level = SND_SOC_BIAS_OFF;
1674 else
1675 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001676 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001677
Liam Girdwood6c120e12012-02-15 15:15:34 +00001678 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001679
Mark Brown6d3ddc82009-05-16 17:47:29 +01001680 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001681 * lists indicating if they should be powered up or down. We
1682 * only check widgets that have been flagged as dirty but note
1683 * that new widgets may be added to the dirty list while we
1684 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001685 */
Mark Browndb432b42011-10-03 21:06:40 +01001686 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001687 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001688 }
1689
Mark Brownf9de6d72011-09-28 17:19:47 +01001690 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001691 switch (w->id) {
1692 case snd_soc_dapm_pre:
1693 case snd_soc_dapm_post:
1694 /* These widgets always need to be powered */
1695 break;
1696 default:
1697 list_del_init(&w->dirty);
1698 break;
1699 }
Mark Browndb432b42011-10-03 21:06:40 +01001700
Mark Brownf9de6d72011-09-28 17:19:47 +01001701 if (w->power) {
1702 d = w->dapm;
1703
1704 /* Supplies and micbiases only bring the
1705 * context up to STANDBY as unless something
1706 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001707 * generally don't require full power. Signal
1708 * generators are virtual pins and have no
1709 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001710 */
1711 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001712 case snd_soc_dapm_siggen:
1713 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001714 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001715 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001716 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001717 case snd_soc_dapm_micbias:
1718 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1719 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1720 break;
1721 default:
1722 d->target_bias_level = SND_SOC_BIAS_ON;
1723 break;
1724 }
1725 }
1726
1727 }
1728
Mark Brown85a843c2011-09-21 21:29:47 +01001729 /* Force all contexts in the card to the same bias state if
1730 * they're not ground referenced.
1731 */
Mark Brown56fba412011-06-04 11:25:10 +01001732 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001733 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001734 if (d->target_bias_level > bias)
1735 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001736 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001737 if (!d->idle_bias_off)
1738 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001739
Mark Brownde02d072011-09-20 21:43:24 +01001740 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001741
Mark Brown9d0624a2011-02-18 11:49:43 -08001742 /* Run all the bias changes in parallel */
1743 list_for_each_entry(d, &dapm->card->dapm_list, list)
1744 async_schedule_domain(dapm_pre_sequence_async, d,
1745 &async_domain);
1746 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001747
Mark Brown80114122013-02-25 15:14:19 +00001748 list_for_each_entry(w, &down_list, list) {
1749 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_WILL_PMD);
1750 }
1751
1752 list_for_each_entry(w, &up_list, list) {
1753 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_WILL_PMU);
1754 }
1755
Mark Brown6d3ddc82009-05-16 17:47:29 +01001756 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001757 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001758
Mark Brown97404f22010-12-14 16:13:57 +00001759 dapm_widget_update(dapm);
1760
Mark Brown6d3ddc82009-05-16 17:47:29 +01001761 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001762 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001763
Mark Brown9d0624a2011-02-18 11:49:43 -08001764 /* Run all the bias changes in parallel */
1765 list_for_each_entry(d, &dapm->card->dapm_list, list)
1766 async_schedule_domain(dapm_post_sequence_async, d,
1767 &async_domain);
1768 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001769
Liam Girdwood8078d872012-02-15 15:15:35 +00001770 /* do we need to notify any clients that DAPM event is complete */
1771 list_for_each_entry(d, &card->dapm_list, list) {
1772 if (d->stream_event)
1773 d->stream_event(d, event);
1774 }
1775
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001776 pop_dbg(dapm->dev, card->pop_time,
1777 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001778 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001779
Mark Brown84e90932010-11-04 00:07:02 -04001780 trace_snd_soc_dapm_done(card);
1781
Mark Brown42aa3412009-03-01 19:21:10 +00001782 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001783}
1784
Mark Brown79fb9382009-08-21 16:38:13 +01001785#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001786static ssize_t dapm_widget_power_read_file(struct file *file,
1787 char __user *user_buf,
1788 size_t count, loff_t *ppos)
1789{
1790 struct snd_soc_dapm_widget *w = file->private_data;
1791 char *buf;
1792 int in, out;
1793 ssize_t ret;
1794 struct snd_soc_dapm_path *p = NULL;
1795
1796 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1797 if (!buf)
1798 return -ENOMEM;
1799
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001800 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001801 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001802 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001803 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001804
Mark Brownf13ebad2012-03-03 18:01:01 +00001805 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1806 w->name, w->power ? "On" : "Off",
1807 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001808
Mark Brownd033c362009-12-04 15:25:56 +00001809 if (w->reg >= 0)
1810 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1811 " - R%d(0x%x) bit %d",
1812 w->reg, w->reg, w->shift);
1813
1814 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1815
Mark Brown3eef08b2009-09-14 16:49:00 +01001816 if (w->sname)
1817 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1818 w->sname,
1819 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001820
1821 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001822 if (p->connected && !p->connected(w, p->sink))
1823 continue;
1824
Mark Brown79fb9382009-08-21 16:38:13 +01001825 if (p->connect)
1826 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001827 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001828 p->name ? p->name : "static",
1829 p->source->name);
1830 }
1831 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001832 if (p->connected && !p->connected(w, p->sink))
1833 continue;
1834
Mark Brown79fb9382009-08-21 16:38:13 +01001835 if (p->connect)
1836 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001837 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001838 p->name ? p->name : "static",
1839 p->sink->name);
1840 }
1841
1842 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1843
1844 kfree(buf);
1845 return ret;
1846}
1847
1848static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001849 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001850 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001851 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001852};
1853
Mark Brownef49e4f2011-04-04 20:48:13 +09001854static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1855 size_t count, loff_t *ppos)
1856{
1857 struct snd_soc_dapm_context *dapm = file->private_data;
1858 char *level;
1859
1860 switch (dapm->bias_level) {
1861 case SND_SOC_BIAS_ON:
1862 level = "On\n";
1863 break;
1864 case SND_SOC_BIAS_PREPARE:
1865 level = "Prepare\n";
1866 break;
1867 case SND_SOC_BIAS_STANDBY:
1868 level = "Standby\n";
1869 break;
1870 case SND_SOC_BIAS_OFF:
1871 level = "Off\n";
1872 break;
1873 default:
1874 BUG();
1875 level = "Unknown\n";
1876 break;
1877 }
1878
1879 return simple_read_from_buffer(user_buf, count, ppos, level,
1880 strlen(level));
1881}
1882
1883static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001884 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001885 .read = dapm_bias_read_file,
1886 .llseek = default_llseek,
1887};
1888
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001889void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1890 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001891{
Mark Brown79fb9382009-08-21 16:38:13 +01001892 struct dentry *d;
1893
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001894 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1895
1896 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001897 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001898 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001899 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001900 }
Mark Brown79fb9382009-08-21 16:38:13 +01001901
Mark Brownef49e4f2011-04-04 20:48:13 +09001902 d = debugfs_create_file("bias_level", 0444,
1903 dapm->debugfs_dapm, dapm,
1904 &dapm_bias_fops);
1905 if (!d)
1906 dev_warn(dapm->dev,
1907 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001908}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001909
1910static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1911{
1912 struct snd_soc_dapm_context *dapm = w->dapm;
1913 struct dentry *d;
1914
1915 if (!dapm->debugfs_dapm || !w->name)
1916 return;
1917
1918 d = debugfs_create_file(w->name, 0444,
1919 dapm->debugfs_dapm, w,
1920 &dapm_widget_power_fops);
1921 if (!d)
1922 dev_warn(w->dapm->dev,
1923 "ASoC: Failed to create %s debugfs file\n",
1924 w->name);
1925}
1926
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001927static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1928{
1929 debugfs_remove_recursive(dapm->debugfs_dapm);
1930}
1931
Mark Brown79fb9382009-08-21 16:38:13 +01001932#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001933void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1934 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001935{
1936}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001937
1938static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1939{
1940}
1941
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001942static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1943{
1944}
1945
Mark Brown79fb9382009-08-21 16:38:13 +01001946#endif
1947
Richard Purdie2b97eab2006-10-06 18:32:18 +02001948/* test and update the power status of a mux widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001949static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001950 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001951{
1952 struct snd_soc_dapm_path *path;
1953 int found = 0;
1954
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001955 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001956 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001957 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001958 return -ENODEV;
1959
Richard Purdie2b97eab2006-10-06 18:32:18 +02001960 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001961 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001962 if (path->kcontrol != kcontrol)
1963 continue;
1964
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001965 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001966 continue;
1967
1968 found = 1;
1969 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001970 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001971 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001972 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001973 } else {
1974 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001975 dapm_mark_dirty(path->source,
1976 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001977 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001978 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001979 }
1980
Mark Browndb432b42011-10-03 21:06:40 +01001981 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001982 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001983 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001984 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001985
Liam Girdwood618dae12012-04-25 12:12:51 +01001986 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001987}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001988
1989int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1990 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1991{
1992 struct snd_soc_card *card = widget->dapm->card;
1993 int ret;
1994
Liam Girdwood3cd04342012-03-09 12:02:08 +00001995 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001996 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1997 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001998 if (ret > 0)
1999 soc_dpcm_runtime_update(widget);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002000 return ret;
2001}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002002EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002003
Milan plzik1b075e32008-01-10 14:39:46 +01002004/* test and update the power status of a mixer or switch widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002005static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00002006 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002007{
2008 struct snd_soc_dapm_path *path;
2009 int found = 0;
2010
Milan plzik1b075e32008-01-10 14:39:46 +01002011 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002012 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01002013 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002014 return -ENODEV;
2015
Richard Purdie2b97eab2006-10-06 18:32:18 +02002016 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002017 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002018 if (path->kcontrol != kcontrol)
2019 continue;
2020
2021 /* found, now check type */
2022 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002023 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002024 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002025 }
2026
Mark Browndb432b42011-10-03 21:06:40 +01002027 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01002028 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002029 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01002030 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002031
Liam Girdwood618dae12012-04-25 12:12:51 +01002032 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002033}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002034
2035int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
2036 struct snd_kcontrol *kcontrol, int connect)
2037{
2038 struct snd_soc_card *card = widget->dapm->card;
2039 int ret;
2040
Liam Girdwood3cd04342012-03-09 12:02:08 +00002041 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002042 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
2043 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002044 if (ret > 0)
2045 soc_dpcm_runtime_update(widget);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002046 return ret;
2047}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002048EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002049
2050/* show dapm widget status in sys fs */
2051static ssize_t dapm_widget_show(struct device *dev,
2052 struct device_attribute *attr, char *buf)
2053{
Mark Brown36ae1a92012-01-06 17:12:45 -08002054 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002055 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002056 struct snd_soc_dapm_widget *w;
2057 int count = 0;
2058 char *state = "not set";
2059
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002060 list_for_each_entry(w, &codec->card->widgets, list) {
2061 if (w->dapm != &codec->dapm)
2062 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002063
2064 /* only display widgets that burnm power */
2065 switch (w->id) {
2066 case snd_soc_dapm_hp:
2067 case snd_soc_dapm_mic:
2068 case snd_soc_dapm_spk:
2069 case snd_soc_dapm_line:
2070 case snd_soc_dapm_micbias:
2071 case snd_soc_dapm_dac:
2072 case snd_soc_dapm_adc:
2073 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002074 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002075 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002076 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002077 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002078 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002079 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002080 if (w->name)
2081 count += sprintf(buf + count, "%s: %s\n",
2082 w->name, w->power ? "On":"Off");
2083 break;
2084 default:
2085 break;
2086 }
2087 }
2088
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002089 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002090 case SND_SOC_BIAS_ON:
2091 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002092 break;
Mark Brown0be98982008-05-19 12:31:28 +02002093 case SND_SOC_BIAS_PREPARE:
2094 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002095 break;
Mark Brown0be98982008-05-19 12:31:28 +02002096 case SND_SOC_BIAS_STANDBY:
2097 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002098 break;
Mark Brown0be98982008-05-19 12:31:28 +02002099 case SND_SOC_BIAS_OFF:
2100 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002101 break;
2102 }
2103 count += sprintf(buf + count, "PM State: %s\n", state);
2104
2105 return count;
2106}
2107
2108static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2109
2110int snd_soc_dapm_sys_add(struct device *dev)
2111{
Troy Kisky12ef1932008-10-13 17:42:14 -07002112 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002113}
2114
2115static void snd_soc_dapm_sys_remove(struct device *dev)
2116{
Mark Brownaef90842009-05-16 17:53:16 +01002117 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002118}
2119
2120/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002121static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002122{
2123 struct snd_soc_dapm_widget *w, *next_w;
2124 struct snd_soc_dapm_path *p, *next_p;
2125
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002126 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2127 if (w->dapm != dapm)
2128 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002129 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002130 /*
2131 * remove source and sink paths associated to this widget.
2132 * While removing the path, remove reference to it from both
2133 * source and sink widgets so that path is removed only once.
2134 */
2135 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
2136 list_del(&p->list_sink);
2137 list_del(&p->list_source);
2138 list_del(&p->list);
2139 kfree(p->long_name);
2140 kfree(p);
2141 }
2142 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2143 list_del(&p->list_sink);
2144 list_del(&p->list_source);
2145 list_del(&p->list);
2146 kfree(p->long_name);
2147 kfree(p);
2148 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002149 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002150 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002151 kfree(w);
2152 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002153}
2154
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002155static struct snd_soc_dapm_widget *dapm_find_widget(
2156 struct snd_soc_dapm_context *dapm, const char *pin,
2157 bool search_other_contexts)
2158{
2159 struct snd_soc_dapm_widget *w;
2160 struct snd_soc_dapm_widget *fallback = NULL;
2161
2162 list_for_each_entry(w, &dapm->card->widgets, list) {
2163 if (!strcmp(w->name, pin)) {
2164 if (w->dapm == dapm)
2165 return w;
2166 else
2167 fallback = w;
2168 }
2169 }
2170
2171 if (search_other_contexts)
2172 return fallback;
2173
2174 return NULL;
2175}
2176
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002177static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002178 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002179{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002180 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002181
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002182 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002183 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002184 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002185 }
2186
Mark Brown1a8b2d92012-02-16 11:50:07 -08002187 if (w->connected != status)
2188 dapm_mark_dirty(w, "pin configuration");
2189
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002190 w->connected = status;
2191 if (status == 0)
2192 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002193
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002194 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002195}
2196
Richard Purdie2b97eab2006-10-06 18:32:18 +02002197/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002198 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002199 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002200 *
2201 * Walks all dapm audio paths and powers widgets according to their
2202 * stream or path usage.
2203 *
2204 * Returns 0 for success.
2205 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002206int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002207{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002208 int ret;
2209
Mark Brown4f4c0072011-10-07 14:29:19 +01002210 /*
2211 * Suppress early reports (eg, jacks syncing their state) to avoid
2212 * silly DAPM runs during card startup.
2213 */
2214 if (!dapm->card || !dapm->card->instantiated)
2215 return 0;
2216
Liam Girdwood3cd04342012-03-09 12:02:08 +00002217 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002218 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2219 mutex_unlock(&dapm->card->dapm_mutex);
2220 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002221}
Liam Girdwooda5302182008-07-07 13:35:17 +01002222EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002223
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002224static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002225 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002226{
2227 struct snd_soc_dapm_path *path;
2228 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002229 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002230 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002231 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002232 const char *source;
2233 char prefixed_sink[80];
2234 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002235 int ret = 0;
2236
Mark Brown88e8b9a2011-03-02 18:18:24 +00002237 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002238 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2239 dapm->codec->name_prefix, route->sink);
2240 sink = prefixed_sink;
2241 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2242 dapm->codec->name_prefix, route->source);
2243 source = prefixed_source;
2244 } else {
2245 sink = route->sink;
2246 source = route->source;
2247 }
2248
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002249 /*
2250 * find src and dest widgets over all widgets but favor a widget from
2251 * current DAPM context
2252 */
2253 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002254 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002255 wtsink = w;
2256 if (w->dapm == dapm)
2257 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002258 continue;
2259 }
2260 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002261 wtsource = w;
2262 if (w->dapm == dapm)
2263 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002264 }
2265 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002266 /* use widget from another DAPM context if not found from this */
2267 if (!wsink)
2268 wsink = wtsink;
2269 if (!wsource)
2270 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002271
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002272 if (wsource == NULL) {
2273 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2274 route->source);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002275 return -ENODEV;
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002276 }
2277 if (wsink == NULL) {
2278 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2279 route->sink);
2280 return -ENODEV;
2281 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002282
2283 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2284 if (!path)
2285 return -ENOMEM;
2286
2287 path->source = wsource;
2288 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002289 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002290 INIT_LIST_HEAD(&path->list);
2291 INIT_LIST_HEAD(&path->list_source);
2292 INIT_LIST_HEAD(&path->list_sink);
2293
2294 /* check for external widgets */
2295 if (wsink->id == snd_soc_dapm_input) {
2296 if (wsource->id == snd_soc_dapm_micbias ||
2297 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002298 wsource->id == snd_soc_dapm_line ||
2299 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002300 wsink->ext = 1;
2301 }
2302 if (wsource->id == snd_soc_dapm_output) {
2303 if (wsink->id == snd_soc_dapm_spk ||
2304 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002305 wsink->id == snd_soc_dapm_line ||
2306 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002307 wsource->ext = 1;
2308 }
2309
2310 /* connect static paths */
2311 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002312 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002313 list_add(&path->list_sink, &wsink->sources);
2314 list_add(&path->list_source, &wsource->sinks);
2315 path->connect = 1;
2316 return 0;
2317 }
2318
2319 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002320 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002321 case snd_soc_dapm_adc:
2322 case snd_soc_dapm_dac:
2323 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002324 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002325 case snd_soc_dapm_input:
2326 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002327 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002328 case snd_soc_dapm_micbias:
2329 case snd_soc_dapm_vmid:
2330 case snd_soc_dapm_pre:
2331 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002332 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002333 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002334 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002335 case snd_soc_dapm_aif_in:
2336 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -08002337 case snd_soc_dapm_dai:
Mark Brownc74184e2012-04-04 22:12:09 +01002338 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002339 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002340 list_add(&path->list_sink, &wsink->sources);
2341 list_add(&path->list_source, &wsource->sinks);
2342 path->connect = 1;
2343 return 0;
2344 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002345 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002346 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002347 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002348 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002349 if (ret != 0)
2350 goto err;
2351 break;
2352 case snd_soc_dapm_switch:
2353 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002354 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002355 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002356 if (ret != 0)
2357 goto err;
2358 break;
2359 case snd_soc_dapm_hp:
2360 case snd_soc_dapm_mic:
2361 case snd_soc_dapm_line:
2362 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002363 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002364 list_add(&path->list_sink, &wsink->sources);
2365 list_add(&path->list_source, &wsource->sinks);
2366 path->connect = 0;
2367 return 0;
2368 }
Mark Brownfabd0382012-07-05 17:20:06 +01002369
2370 dapm_mark_dirty(wsource, "Route added");
2371 dapm_mark_dirty(wsink, "Route added");
2372
Richard Purdie2b97eab2006-10-06 18:32:18 +02002373 return 0;
2374
2375err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002376 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002377 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002378 kfree(path);
2379 return ret;
2380}
Mark Brown105f1c22008-05-13 14:52:19 +02002381
Mark Brownefcc3c62012-07-05 17:24:19 +01002382static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2383 const struct snd_soc_dapm_route *route)
2384{
2385 struct snd_soc_dapm_path *path, *p;
2386 const char *sink;
2387 const char *source;
2388 char prefixed_sink[80];
2389 char prefixed_source[80];
2390
2391 if (route->control) {
2392 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002393 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002394 return -EINVAL;
2395 }
2396
2397 if (dapm->codec && dapm->codec->name_prefix) {
2398 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2399 dapm->codec->name_prefix, route->sink);
2400 sink = prefixed_sink;
2401 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2402 dapm->codec->name_prefix, route->source);
2403 source = prefixed_source;
2404 } else {
2405 sink = route->sink;
2406 source = route->source;
2407 }
2408
2409 path = NULL;
2410 list_for_each_entry(p, &dapm->card->paths, list) {
2411 if (strcmp(p->source->name, source) != 0)
2412 continue;
2413 if (strcmp(p->sink->name, sink) != 0)
2414 continue;
2415 path = p;
2416 break;
2417 }
2418
2419 if (path) {
2420 dapm_mark_dirty(path->source, "Route removed");
2421 dapm_mark_dirty(path->sink, "Route removed");
2422
2423 list_del(&path->list);
2424 list_del(&path->list_sink);
2425 list_del(&path->list_source);
2426 kfree(path);
2427 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002428 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002429 source, sink);
2430 }
2431
2432 return 0;
2433}
2434
Mark Brown105f1c22008-05-13 14:52:19 +02002435/**
Mark Brown105f1c22008-05-13 14:52:19 +02002436 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002437 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002438 * @route: audio routes
2439 * @num: number of routes
2440 *
2441 * Connects 2 dapm widgets together via a named audio path. The sink is
2442 * the widget receiving the audio signal, whilst the source is the sender
2443 * of the audio signal.
2444 *
2445 * Returns 0 for success else error. On error all resources can be freed
2446 * with a call to snd_soc_card_free().
2447 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002448int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002449 const struct snd_soc_dapm_route *route, int num)
2450{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002451 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002452
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002453 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002454 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002455 r = snd_soc_dapm_add_route(dapm, route);
2456 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002457 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2458 route->source,
2459 route->control ? route->control : "direct",
2460 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002461 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002462 }
2463 route++;
2464 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002465 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002466
Dan Carpenter60884c22012-04-13 22:25:43 +03002467 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002468}
2469EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2470
Mark Brownefcc3c62012-07-05 17:24:19 +01002471/**
2472 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2473 * @dapm: DAPM context
2474 * @route: audio routes
2475 * @num: number of routes
2476 *
2477 * Removes routes from the DAPM context.
2478 */
2479int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2480 const struct snd_soc_dapm_route *route, int num)
2481{
2482 int i, ret = 0;
2483
2484 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2485 for (i = 0; i < num; i++) {
2486 snd_soc_dapm_del_route(dapm, route);
2487 route++;
2488 }
2489 mutex_unlock(&dapm->card->dapm_mutex);
2490
2491 return ret;
2492}
2493EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2494
Mark Brownbf3a9e12011-06-13 16:42:29 +01002495static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2496 const struct snd_soc_dapm_route *route)
2497{
2498 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2499 route->source,
2500 true);
2501 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2502 route->sink,
2503 true);
2504 struct snd_soc_dapm_path *path;
2505 int count = 0;
2506
2507 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002508 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002509 route->source);
2510 return -ENODEV;
2511 }
2512
2513 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002514 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002515 route->sink);
2516 return -ENODEV;
2517 }
2518
2519 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002520 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002521 route->source, route->sink);
2522
2523 list_for_each_entry(path, &source->sinks, list_source) {
2524 if (path->sink == sink) {
2525 path->weak = 1;
2526 count++;
2527 }
2528 }
2529
2530 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002531 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002532 route->source, route->sink);
2533 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002534 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002535 count, route->source, route->sink);
2536
2537 return 0;
2538}
2539
2540/**
2541 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2542 * @dapm: DAPM context
2543 * @route: audio routes
2544 * @num: number of routes
2545 *
2546 * Mark existing routes matching those specified in the passed array
2547 * as being weak, meaning that they are ignored for the purpose of
2548 * power decisions. The main intended use case is for sidetone paths
2549 * which couple audio between other independent paths if they are both
2550 * active in order to make the combination work better at the user
2551 * level but which aren't intended to be "used".
2552 *
2553 * Note that CODEC drivers should not use this as sidetone type paths
2554 * can frequently also be used as bypass paths.
2555 */
2556int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2557 const struct snd_soc_dapm_route *route, int num)
2558{
2559 int i, err;
2560 int ret = 0;
2561
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002562 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002563 for (i = 0; i < num; i++) {
2564 err = snd_soc_dapm_weak_route(dapm, route);
2565 if (err)
2566 ret = err;
2567 route++;
2568 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002569 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002570
2571 return ret;
2572}
2573EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2574
Mark Brown105f1c22008-05-13 14:52:19 +02002575/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002576 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002577 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002578 *
2579 * Checks the codec for any new dapm widgets and creates them if found.
2580 *
2581 * Returns 0 for success.
2582 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002583int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002584{
2585 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002586 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002587
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002588 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2589
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002590 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002591 {
2592 if (w->new)
2593 continue;
2594
Stephen Warrenfad59882011-04-28 17:37:59 -06002595 if (w->num_kcontrols) {
2596 w->kcontrols = kzalloc(w->num_kcontrols *
2597 sizeof(struct snd_kcontrol *),
2598 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002599 if (!w->kcontrols) {
2600 mutex_unlock(&dapm->card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002601 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002602 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002603 }
2604
Richard Purdie2b97eab2006-10-06 18:32:18 +02002605 switch(w->id) {
2606 case snd_soc_dapm_switch:
2607 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002608 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002609 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002610 break;
2611 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002612 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002613 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002614 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002615 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002616 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002617 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002618 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002619 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002620 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002621 break;
2622 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002623
2624 /* Read the initial power state from the device */
2625 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002626 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002627 val &= 1 << w->shift;
2628 if (w->invert)
2629 val = !val;
2630
2631 if (val)
2632 w->power = 1;
2633 }
2634
Richard Purdie2b97eab2006-10-06 18:32:18 +02002635 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002636
Mark Brown7508b122011-10-05 12:09:12 +01002637 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002638 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002639 }
2640
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002641 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002642 mutex_unlock(&dapm->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002643 return 0;
2644}
2645EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2646
2647/**
2648 * snd_soc_dapm_get_volsw - dapm mixer get callback
2649 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002650 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002651 *
2652 * Callback to get the value of a dapm mixer control.
2653 *
2654 * Returns 0 for success.
2655 */
2656int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2657 struct snd_ctl_elem_value *ucontrol)
2658{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002659 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2660 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002661 struct soc_mixer_control *mc =
2662 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002663 unsigned int reg = mc->reg;
2664 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002665 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002666 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002667 unsigned int invert = mc->invert;
2668
2669 if (snd_soc_volsw_is_stereo(mc))
2670 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002671 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002672 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002673
Richard Purdie2b97eab2006-10-06 18:32:18 +02002674 ucontrol->value.integer.value[0] =
2675 (snd_soc_read(widget->codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002676 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002677 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002678 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002679
2680 return 0;
2681}
2682EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2683
2684/**
2685 * snd_soc_dapm_put_volsw - dapm mixer set callback
2686 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002687 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002688 *
2689 * Callback to set the value of a dapm mixer control.
2690 *
2691 * Returns 0 for success.
2692 */
2693int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2694 struct snd_ctl_elem_value *ucontrol)
2695{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002696 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2697 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2698 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002699 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002700 struct soc_mixer_control *mc =
2701 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002702 unsigned int reg = mc->reg;
2703 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002704 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002705 unsigned int mask = (1 << fls(max)) - 1;
2706 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002707 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002708 int connect, change;
2709 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002710 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002711
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002712 if (snd_soc_volsw_is_stereo(mc))
2713 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002714 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002715 kcontrol->id.name);
2716
Richard Purdie2b97eab2006-10-06 18:32:18 +02002717 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002718 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002719
2720 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002721 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002722 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002723 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002724
Liam Girdwood3cd04342012-03-09 12:02:08 +00002725 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002726
Stephen Warrene9cf7042011-01-27 14:54:05 -07002727 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002728 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002729 for (wi = 0; wi < wlist->num_widgets; wi++) {
2730 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002731
Stephen Warrenfafd2172011-04-28 17:38:00 -06002732 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002733
Stephen Warrenfafd2172011-04-28 17:38:00 -06002734 update.kcontrol = kcontrol;
2735 update.widget = widget;
2736 update.reg = reg;
2737 update.mask = mask;
2738 update.val = val;
2739 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002740
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002741 soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002742
2743 widget->dapm->update = NULL;
2744 }
Mark Brown283375c2009-12-07 18:09:03 +00002745 }
2746
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002747 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002748 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002749}
2750EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2751
2752/**
2753 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2754 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002755 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002756 *
2757 * Callback to get the value of a dapm enumerated double mixer control.
2758 *
2759 * Returns 0 for success.
2760 */
2761int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2762 struct snd_ctl_elem_value *ucontrol)
2763{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002764 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2765 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002766 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002767 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002768
Richard Purdie2b97eab2006-10-06 18:32:18 +02002769 val = snd_soc_read(widget->codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002770 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002771 if (e->shift_l != e->shift_r)
2772 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002773 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002774
2775 return 0;
2776}
2777EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2778
2779/**
2780 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2781 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002782 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002783 *
2784 * Callback to set the value of a dapm enumerated double mixer control.
2785 *
2786 * Returns 0 for success.
2787 */
2788int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2789 struct snd_ctl_elem_value *ucontrol)
2790{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002791 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2792 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2793 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002794 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002795 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002796 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002797 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002798 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002799 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002800
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002801 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002802 return -EINVAL;
2803 mux = ucontrol->value.enumerated.item[0];
2804 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002805 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002806 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002807 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002808 return -EINVAL;
2809 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002810 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002811 }
2812
Liam Girdwood3cd04342012-03-09 12:02:08 +00002813 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002814
Mark Brown3a655772009-10-05 17:23:30 +01002815 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002816 if (change) {
2817 for (wi = 0; wi < wlist->num_widgets; wi++) {
2818 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002819
Stephen Warrenfafd2172011-04-28 17:38:00 -06002820 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002821
Stephen Warrenfafd2172011-04-28 17:38:00 -06002822 update.kcontrol = kcontrol;
2823 update.widget = widget;
2824 update.reg = e->reg;
2825 update.mask = mask;
2826 update.val = val;
2827 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002828
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002829 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002830
Stephen Warrenfafd2172011-04-28 17:38:00 -06002831 widget->dapm->update = NULL;
2832 }
2833 }
2834
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002835 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002836 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002837}
2838EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2839
2840/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002841 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2842 * @kcontrol: mixer control
2843 * @ucontrol: control element information
2844 *
2845 * Returns 0 for success.
2846 */
2847int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2848 struct snd_ctl_elem_value *ucontrol)
2849{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002850 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2851 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002852
2853 ucontrol->value.enumerated.item[0] = widget->value;
2854
2855 return 0;
2856}
2857EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2858
2859/**
2860 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2861 * @kcontrol: mixer control
2862 * @ucontrol: control element information
2863 *
2864 * Returns 0 for success.
2865 */
2866int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2867 struct snd_ctl_elem_value *ucontrol)
2868{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002869 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2870 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2871 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002872 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002873 struct soc_enum *e =
2874 (struct soc_enum *)kcontrol->private_value;
2875 int change;
2876 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002877 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002878
2879 if (ucontrol->value.enumerated.item[0] >= e->max)
2880 return -EINVAL;
2881
Liam Girdwood3cd04342012-03-09 12:02:08 +00002882 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002883
2884 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002885 if (change) {
2886 for (wi = 0; wi < wlist->num_widgets; wi++) {
2887 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002888
Stephen Warrenfafd2172011-04-28 17:38:00 -06002889 widget->value = ucontrol->value.enumerated.item[0];
2890
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002891 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002892 }
2893 }
2894
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002895 mutex_unlock(&card->dapm_mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002896 return ret;
2897}
2898EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2899
2900/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002901 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2902 * callback
2903 * @kcontrol: mixer control
2904 * @ucontrol: control element information
2905 *
2906 * Callback to get the value of a dapm semi enumerated double mixer control.
2907 *
2908 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2909 * used for handling bitfield coded enumeration for example.
2910 *
2911 * Returns 0 for success.
2912 */
2913int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2914 struct snd_ctl_elem_value *ucontrol)
2915{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002916 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2917 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002918 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002919 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002920
2921 reg_val = snd_soc_read(widget->codec, e->reg);
2922 val = (reg_val >> e->shift_l) & e->mask;
2923 for (mux = 0; mux < e->max; mux++) {
2924 if (val == e->values[mux])
2925 break;
2926 }
2927 ucontrol->value.enumerated.item[0] = mux;
2928 if (e->shift_l != e->shift_r) {
2929 val = (reg_val >> e->shift_r) & e->mask;
2930 for (mux = 0; mux < e->max; mux++) {
2931 if (val == e->values[mux])
2932 break;
2933 }
2934 ucontrol->value.enumerated.item[1] = mux;
2935 }
2936
2937 return 0;
2938}
2939EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2940
2941/**
2942 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2943 * callback
2944 * @kcontrol: mixer control
2945 * @ucontrol: control element information
2946 *
2947 * Callback to set the value of a dapm semi enumerated double mixer control.
2948 *
2949 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2950 * used for handling bitfield coded enumeration for example.
2951 *
2952 * Returns 0 for success.
2953 */
2954int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2955 struct snd_ctl_elem_value *ucontrol)
2956{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002957 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2958 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2959 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002960 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002961 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002962 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002963 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002964 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002965 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002966
2967 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2968 return -EINVAL;
2969 mux = ucontrol->value.enumerated.item[0];
2970 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2971 mask = e->mask << e->shift_l;
2972 if (e->shift_l != e->shift_r) {
2973 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2974 return -EINVAL;
2975 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2976 mask |= e->mask << e->shift_r;
2977 }
2978
Liam Girdwood3cd04342012-03-09 12:02:08 +00002979 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002980
Mark Brown3a655772009-10-05 17:23:30 +01002981 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002982 if (change) {
2983 for (wi = 0; wi < wlist->num_widgets; wi++) {
2984 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002985
Stephen Warrenfafd2172011-04-28 17:38:00 -06002986 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002987
Stephen Warrenfafd2172011-04-28 17:38:00 -06002988 update.kcontrol = kcontrol;
2989 update.widget = widget;
2990 update.reg = e->reg;
2991 update.mask = mask;
2992 update.val = val;
2993 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002994
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002995 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002996
Stephen Warrenfafd2172011-04-28 17:38:00 -06002997 widget->dapm->update = NULL;
2998 }
2999 }
3000
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003001 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00003002 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003003}
3004EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3005
3006/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003007 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3008 *
3009 * @kcontrol: mixer control
3010 * @uinfo: control element information
3011 *
3012 * Callback to provide information about a pin switch control.
3013 */
3014int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3015 struct snd_ctl_elem_info *uinfo)
3016{
3017 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3018 uinfo->count = 1;
3019 uinfo->value.integer.min = 0;
3020 uinfo->value.integer.max = 1;
3021
3022 return 0;
3023}
3024EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3025
3026/**
3027 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3028 *
3029 * @kcontrol: mixer control
3030 * @ucontrol: Value
3031 */
3032int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3033 struct snd_ctl_elem_value *ucontrol)
3034{
Mark Brown48a8c392012-02-14 17:11:15 -08003035 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003036 const char *pin = (const char *)kcontrol->private_value;
3037
Liam Girdwood3cd04342012-03-09 12:02:08 +00003038 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003039
3040 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003041 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003042
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003043 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003044
3045 return 0;
3046}
3047EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3048
3049/**
3050 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3051 *
3052 * @kcontrol: mixer control
3053 * @ucontrol: Value
3054 */
3055int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3056 struct snd_ctl_elem_value *ucontrol)
3057{
Mark Brown48a8c392012-02-14 17:11:15 -08003058 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003059 const char *pin = (const char *)kcontrol->private_value;
3060
Liam Girdwood3cd04342012-03-09 12:02:08 +00003061 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003062
3063 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003064 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003065 else
Mark Brown48a8c392012-02-14 17:11:15 -08003066 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003067
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003068 mutex_unlock(&card->dapm_mutex);
3069
Mark Brown48a8c392012-02-14 17:11:15 -08003070 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003071 return 0;
3072}
3073EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3074
Mark Brown5ba06fc2012-02-16 11:07:13 -08003075static struct snd_soc_dapm_widget *
3076snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3077 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003078{
3079 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003080 size_t name_len;
Mark Brown62ea8742012-01-21 21:14:48 +00003081 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003082
3083 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003084 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003085
Mark Brown62ea8742012-01-21 21:14:48 +00003086 switch (w->id) {
3087 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003088 w->regulator = devm_regulator_get(dapm->dev, w->name);
3089 if (IS_ERR(w->regulator)) {
3090 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003091 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003092 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003093 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003094 }
Mark Brown8784c772013-01-10 19:33:47 +00003095
3096 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
3097 ret = regulator_allow_bypass(w->regulator, true);
3098 if (ret != 0)
3099 dev_warn(w->dapm->dev,
3100 "ASoC: Failed to unbypass %s: %d\n",
3101 w->name, ret);
3102 }
Mark Brown62ea8742012-01-21 21:14:48 +00003103 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003104 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003105#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003106 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003107 if (IS_ERR(w->clk)) {
3108 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003109 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003110 w->name, ret);
3111 return NULL;
3112 }
Mark Brownec029952012-06-04 08:16:20 +01003113#else
3114 return NULL;
3115#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003116 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003117 default:
3118 break;
3119 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003120
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003121 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00003122 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003123 name_len += 1 + strlen(dapm->codec->name_prefix);
3124 w->name = kmalloc(name_len, GFP_KERNEL);
3125 if (w->name == NULL) {
3126 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003127 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003128 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00003129 if (dapm->codec && dapm->codec->name_prefix)
Mark Brown888df392012-02-16 19:37:51 -08003130 snprintf((char *)w->name, name_len, "%s %s",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003131 dapm->codec->name_prefix, widget->name);
3132 else
Mark Brown888df392012-02-16 19:37:51 -08003133 snprintf((char *)w->name, name_len, "%s", widget->name);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003134
Mark Brown7ca3a182011-10-08 14:04:50 +01003135 switch (w->id) {
3136 case snd_soc_dapm_switch:
3137 case snd_soc_dapm_mixer:
3138 case snd_soc_dapm_mixer_named_ctl:
3139 w->power_check = dapm_generic_check_power;
3140 break;
3141 case snd_soc_dapm_mux:
3142 case snd_soc_dapm_virt_mux:
3143 case snd_soc_dapm_value_mux:
3144 w->power_check = dapm_generic_check_power;
3145 break;
3146 case snd_soc_dapm_adc:
3147 case snd_soc_dapm_aif_out:
3148 w->power_check = dapm_adc_check_power;
3149 break;
3150 case snd_soc_dapm_dac:
3151 case snd_soc_dapm_aif_in:
3152 w->power_check = dapm_dac_check_power;
3153 break;
3154 case snd_soc_dapm_pga:
3155 case snd_soc_dapm_out_drv:
3156 case snd_soc_dapm_input:
3157 case snd_soc_dapm_output:
3158 case snd_soc_dapm_micbias:
3159 case snd_soc_dapm_spk:
3160 case snd_soc_dapm_hp:
3161 case snd_soc_dapm_mic:
3162 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003163 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003164 w->power_check = dapm_generic_check_power;
3165 break;
3166 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003167 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003168 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003169 w->power_check = dapm_supply_check_power;
3170 break;
Mark Brown888df392012-02-16 19:37:51 -08003171 case snd_soc_dapm_dai:
3172 w->power_check = dapm_dai_check_power;
3173 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003174 default:
3175 w->power_check = dapm_always_on_check_power;
3176 break;
3177 }
3178
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003179 w->dapm = dapm;
3180 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003181 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003182 INIT_LIST_HEAD(&w->sources);
3183 INIT_LIST_HEAD(&w->sinks);
3184 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003185 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003186 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003187
3188 /* machine layer set ups unconnected pins and insertions */
3189 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003190 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003191}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003192
3193/**
Mark Brown4ba13272008-05-13 14:51:19 +02003194 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003195 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003196 * @widget: widget array
3197 * @num: number of widgets
3198 *
3199 * Creates new DAPM controls based upon the templates.
3200 *
3201 * Returns 0 for success else error.
3202 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003203int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003204 const struct snd_soc_dapm_widget *widget,
3205 int num)
3206{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003207 struct snd_soc_dapm_widget *w;
3208 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003209 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003210
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003211 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003212 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003213 w = snd_soc_dapm_new_control(dapm, widget);
3214 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003215 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003216 "ASoC: Failed to create DAPM control %s\n",
3217 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003218 ret = -ENOMEM;
3219 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003220 }
Mark Brown4ba13272008-05-13 14:51:19 +02003221 widget++;
3222 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003223 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003224 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003225}
3226EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3227
Mark Brownc74184e2012-04-04 22:12:09 +01003228static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3229 struct snd_kcontrol *kcontrol, int event)
3230{
3231 struct snd_soc_dapm_path *source_p, *sink_p;
3232 struct snd_soc_dai *source, *sink;
3233 const struct snd_soc_pcm_stream *config = w->params;
3234 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003235 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003236 u64 fmt;
3237 int ret;
3238
3239 BUG_ON(!config);
3240 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3241
3242 /* We only support a single source and sink, pick the first */
3243 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3244 list_sink);
3245 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3246 list_source);
3247
3248 BUG_ON(!source_p || !sink_p);
3249 BUG_ON(!sink_p->source || !source_p->sink);
3250 BUG_ON(!source_p->source || !sink_p->sink);
3251
3252 source = source_p->source->priv;
3253 sink = sink_p->sink->priv;
3254
3255 /* Be a little careful as we don't want to overflow the mask array */
3256 if (config->formats) {
3257 fmt = ffs(config->formats) - 1;
3258 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003259 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003260 config->formats);
3261 fmt = 0;
3262 }
3263
3264 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003265 params = kzalloc(sizeof(*params), GFP_KERNEL);
3266 if (!params) {
3267 ret = -ENOMEM;
3268 goto out;
3269 }
3270 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003271
Mark Brown9747cec2012-04-26 19:12:21 +01003272 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003273 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003274 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003275 config->rate_max;
3276
Mark Brown9747cec2012-04-26 19:12:21 +01003277 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003278 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003279 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003280 = config->channels_max;
3281
3282 memset(&substream, 0, sizeof(substream));
3283
3284 switch (event) {
3285 case SND_SOC_DAPM_PRE_PMU:
3286 if (source->driver->ops && source->driver->ops->hw_params) {
3287 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3288 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003289 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003290 if (ret != 0) {
3291 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003292 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003293 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003294 }
3295 }
3296
3297 if (sink->driver->ops && sink->driver->ops->hw_params) {
3298 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003299 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003300 sink);
3301 if (ret != 0) {
3302 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003303 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003304 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003305 }
3306 }
3307 break;
3308
3309 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003310 ret = snd_soc_dai_digital_mute(sink, 0,
3311 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003312 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003313 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003314 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003315 break;
3316
3317 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003318 ret = snd_soc_dai_digital_mute(sink, 1,
3319 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003320 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003321 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003322 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003323 break;
3324
3325 default:
3326 BUG();
3327 return -EINVAL;
3328 }
3329
Mark Brown9747cec2012-04-26 19:12:21 +01003330out:
3331 kfree(params);
3332 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003333}
3334
3335int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3336 const struct snd_soc_pcm_stream *params,
3337 struct snd_soc_dapm_widget *source,
3338 struct snd_soc_dapm_widget *sink)
3339{
3340 struct snd_soc_dapm_route routes[2];
3341 struct snd_soc_dapm_widget template;
3342 struct snd_soc_dapm_widget *w;
3343 size_t len;
3344 char *link_name;
3345
3346 len = strlen(source->name) + strlen(sink->name) + 2;
3347 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3348 if (!link_name)
3349 return -ENOMEM;
3350 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3351
3352 memset(&template, 0, sizeof(template));
3353 template.reg = SND_SOC_NOPM;
3354 template.id = snd_soc_dapm_dai_link;
3355 template.name = link_name;
3356 template.event = snd_soc_dai_link_event;
3357 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3358 SND_SOC_DAPM_PRE_PMD;
3359
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003360 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003361
3362 w = snd_soc_dapm_new_control(&card->dapm, &template);
3363 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003364 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003365 link_name);
3366 return -ENOMEM;
3367 }
3368
3369 w->params = params;
3370
3371 memset(&routes, 0, sizeof(routes));
3372
3373 routes[0].source = source->name;
3374 routes[0].sink = link_name;
3375 routes[1].source = link_name;
3376 routes[1].sink = sink->name;
3377
3378 return snd_soc_dapm_add_routes(&card->dapm, routes,
3379 ARRAY_SIZE(routes));
3380}
3381
Mark Brown888df392012-02-16 19:37:51 -08003382int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3383 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003384{
Mark Brown888df392012-02-16 19:37:51 -08003385 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003386 struct snd_soc_dapm_widget *w;
3387
Mark Brown888df392012-02-16 19:37:51 -08003388 WARN_ON(dapm->dev != dai->dev);
3389
3390 memset(&template, 0, sizeof(template));
3391 template.reg = SND_SOC_NOPM;
3392
3393 if (dai->driver->playback.stream_name) {
3394 template.id = snd_soc_dapm_dai;
3395 template.name = dai->driver->playback.stream_name;
3396 template.sname = dai->driver->playback.stream_name;
3397
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003398 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003399 template.name);
3400
3401 w = snd_soc_dapm_new_control(dapm, &template);
3402 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003403 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003404 dai->driver->playback.stream_name);
3405 }
3406
3407 w->priv = dai;
3408 dai->playback_widget = w;
3409 }
3410
3411 if (dai->driver->capture.stream_name) {
3412 template.id = snd_soc_dapm_dai;
3413 template.name = dai->driver->capture.stream_name;
3414 template.sname = dai->driver->capture.stream_name;
3415
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003416 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003417 template.name);
3418
3419 w = snd_soc_dapm_new_control(dapm, &template);
3420 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003421 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003422 dai->driver->capture.stream_name);
3423 }
3424
3425 w->priv = dai;
3426 dai->capture_widget = w;
3427 }
3428
3429 return 0;
3430}
3431
3432int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3433{
3434 struct snd_soc_dapm_widget *dai_w, *w;
3435 struct snd_soc_dai *dai;
3436 struct snd_soc_dapm_route r;
3437
3438 memset(&r, 0, sizeof(r));
3439
3440 /* For each DAI widget... */
3441 list_for_each_entry(dai_w, &card->widgets, list) {
3442 if (dai_w->id != snd_soc_dapm_dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003443 continue;
Mark Brown888df392012-02-16 19:37:51 -08003444
3445 dai = dai_w->priv;
3446
3447 /* ...find all widgets with the same stream and link them */
3448 list_for_each_entry(w, &card->widgets, list) {
3449 if (w->dapm != dai_w->dapm)
3450 continue;
3451
3452 if (w->id == snd_soc_dapm_dai)
3453 continue;
3454
3455 if (!w->sname)
3456 continue;
3457
3458 if (dai->driver->playback.stream_name &&
3459 strstr(w->sname,
3460 dai->driver->playback.stream_name)) {
3461 r.source = dai->playback_widget->name;
3462 r.sink = w->name;
3463 dev_dbg(dai->dev, "%s -> %s\n",
3464 r.source, r.sink);
3465
3466 snd_soc_dapm_add_route(w->dapm, &r);
3467 }
3468
3469 if (dai->driver->capture.stream_name &&
3470 strstr(w->sname,
3471 dai->driver->capture.stream_name)) {
3472 r.source = w->name;
3473 r.sink = dai->capture_widget->name;
3474 dev_dbg(dai->dev, "%s -> %s\n",
3475 r.source, r.sink);
3476
3477 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003478 }
3479 }
3480 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003481
Mark Brown888df392012-02-16 19:37:51 -08003482 return 0;
3483}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003484
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003485static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3486 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003487{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003488
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003489 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3490 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3491 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003492
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003493 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3494 w_cpu = cpu_dai->playback_widget;
3495 w_codec = codec_dai->playback_widget;
3496 } else {
3497 w_cpu = cpu_dai->capture_widget;
3498 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003499 }
3500
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003501 if (w_cpu) {
3502
3503 dapm_mark_dirty(w_cpu, "stream event");
3504
3505 switch (event) {
3506 case SND_SOC_DAPM_STREAM_START:
3507 w_cpu->active = 1;
3508 break;
3509 case SND_SOC_DAPM_STREAM_STOP:
3510 w_cpu->active = 0;
3511 break;
3512 case SND_SOC_DAPM_STREAM_SUSPEND:
3513 case SND_SOC_DAPM_STREAM_RESUME:
3514 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3515 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3516 break;
3517 }
3518 }
3519
3520 if (w_codec) {
3521
3522 dapm_mark_dirty(w_codec, "stream event");
3523
3524 switch (event) {
3525 case SND_SOC_DAPM_STREAM_START:
3526 w_codec->active = 1;
3527 break;
3528 case SND_SOC_DAPM_STREAM_STOP:
3529 w_codec->active = 0;
3530 break;
3531 case SND_SOC_DAPM_STREAM_SUSPEND:
3532 case SND_SOC_DAPM_STREAM_RESUME:
3533 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3534 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3535 break;
3536 }
3537 }
3538
3539 dapm_power_widgets(&rtd->card->dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003540}
3541
3542/**
3543 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3544 * @rtd: PCM runtime data
3545 * @stream: stream name
3546 * @event: stream event
3547 *
3548 * Sends a stream event to the dapm core. The core then makes any
3549 * necessary widget power changes.
3550 *
3551 * Returns 0 for success else error.
3552 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003553void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3554 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003555{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003556 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003557
Liam Girdwood3cd04342012-03-09 12:02:08 +00003558 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003559 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003560 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003561}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003562
3563/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003564 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003565 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003566 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003567 *
Mark Brown74b8f952009-06-06 11:26:15 +01003568 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003569 * a valid audio route and active audio stream.
3570 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3571 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003572 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003573int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003574{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003575 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003576}
Liam Girdwooda5302182008-07-07 13:35:17 +01003577EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003578
3579/**
Mark Brownda341832010-03-15 19:23:37 +00003580 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003581 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003582 * @pin: pin name
3583 *
3584 * Enables input/output pin regardless of any other state. This is
3585 * intended for use with microphone bias supplies used in microphone
3586 * jack detection.
3587 *
3588 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3589 * do any widget power switching.
3590 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003591int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3592 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003593{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003594 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003595
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003596 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003597 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003598 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003599 }
3600
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003601 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003602 w->connected = 1;
3603 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003604 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003605
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003606 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003607}
3608EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3609
3610/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003611 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003612 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003613 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003614 *
Mark Brown74b8f952009-06-06 11:26:15 +01003615 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003616 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3617 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003618 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003619int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3620 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003621{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003622 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003623}
3624EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3625
3626/**
Mark Brown5817b522008-09-24 11:23:11 +01003627 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003628 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003629 * @pin: pin name
3630 *
3631 * Marks the specified pin as being not connected, disabling it along
3632 * any parent or child widgets. At present this is identical to
3633 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3634 * additional things such as disabling controls which only affect
3635 * paths through the pin.
3636 *
3637 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3638 * do any widget power switching.
3639 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003640int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003641{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003642 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003643}
3644EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3645
3646/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003647 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003648 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003649 * @pin: audio signal pin endpoint (or start point)
3650 *
3651 * Get audio pin status - connected or disconnected.
3652 *
3653 * Returns 1 for connected otherwise 0.
3654 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003655int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3656 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003657{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003658 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003659
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003660 if (w)
3661 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003662
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003663 return 0;
3664}
Liam Girdwooda5302182008-07-07 13:35:17 +01003665EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003666
3667/**
Mark Brown1547aba2010-05-07 21:11:40 +01003668 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003669 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003670 * @pin: audio signal pin endpoint (or start point)
3671 *
3672 * Mark the given endpoint or pin as ignoring suspend. When the
3673 * system is disabled a path between two endpoints flagged as ignoring
3674 * suspend will not be disabled. The path must already be enabled via
3675 * normal means at suspend time, it will not be turned on if it was not
3676 * already enabled.
3677 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003678int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3679 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003680{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003681 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003682
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003683 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003684 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003685 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003686 }
3687
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003688 w->ignore_suspend = 1;
3689
3690 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003691}
3692EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3693
Stephen Warren16332812011-11-23 12:42:04 -07003694static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3695 struct snd_soc_dapm_widget *w)
3696{
3697 struct snd_soc_dapm_path *p;
3698
3699 list_for_each_entry(p, &card->paths, list) {
3700 if ((p->source == w) || (p->sink == w)) {
3701 dev_dbg(card->dev,
3702 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3703 p->source->name, p->source->id, p->source->dapm,
3704 p->sink->name, p->sink->id, p->sink->dapm);
3705
3706 /* Connected to something other than the codec */
3707 if (p->source->dapm != p->sink->dapm)
3708 return true;
3709 /*
3710 * Loopback connection from codec external pin to
3711 * codec external pin
3712 */
3713 if (p->sink->id == snd_soc_dapm_input) {
3714 switch (p->source->id) {
3715 case snd_soc_dapm_output:
3716 case snd_soc_dapm_micbias:
3717 return true;
3718 default:
3719 break;
3720 }
3721 }
3722 }
3723 }
3724
3725 return false;
3726}
3727
3728/**
3729 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3730 * @codec: The codec whose pins should be processed
3731 *
3732 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3733 * which are unused. Pins are used if they are connected externally to the
3734 * codec, whether that be to some other device, or a loop-back connection to
3735 * the codec itself.
3736 */
3737void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3738{
3739 struct snd_soc_card *card = codec->card;
3740 struct snd_soc_dapm_context *dapm = &codec->dapm;
3741 struct snd_soc_dapm_widget *w;
3742
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003743 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003744 &card->dapm, &codec->dapm);
3745
3746 list_for_each_entry(w, &card->widgets, list) {
3747 if (w->dapm != dapm)
3748 continue;
3749 switch (w->id) {
3750 case snd_soc_dapm_input:
3751 case snd_soc_dapm_output:
3752 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003753 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003754 w->name);
3755 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003756 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003757 "... Not in map; disabling\n");
3758 snd_soc_dapm_nc_pin(dapm, w->name);
3759 }
3760 break;
3761 default:
3762 break;
3763 }
3764 }
3765}
3766
Mark Brown1547aba2010-05-07 21:11:40 +01003767/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003768 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003769 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003770 *
3771 * Free all dapm widgets and resources.
3772 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003773void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003774{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003775 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003776 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003777 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003778 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003779}
3780EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3781
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003782static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003783{
Liam Girdwood01005a72012-07-06 16:57:05 +01003784 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003785 struct snd_soc_dapm_widget *w;
3786 LIST_HEAD(down_list);
3787 int powerdown = 0;
3788
Liam Girdwood01005a72012-07-06 16:57:05 +01003789 mutex_lock(&card->dapm_mutex);
3790
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003791 list_for_each_entry(w, &dapm->card->widgets, list) {
3792 if (w->dapm != dapm)
3793 continue;
Mark Brown51737472009-06-22 13:16:51 +01003794 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003795 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003796 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003797 powerdown = 1;
3798 }
3799 }
3800
3801 /* If there were no widgets to power down we're already in
3802 * standby.
3803 */
3804 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003805 if (dapm->bias_level == SND_SOC_BIAS_ON)
3806 snd_soc_dapm_set_bias_level(dapm,
3807 SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003808 dapm_seq_run(dapm, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003809 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3810 snd_soc_dapm_set_bias_level(dapm,
3811 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003812 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003813
3814 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003815}
Mark Brown51737472009-06-22 13:16:51 +01003816
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003817/*
3818 * snd_soc_dapm_shutdown - callback for system shutdown
3819 */
3820void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3821{
3822 struct snd_soc_codec *codec;
3823
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003824 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003825 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003826 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3827 snd_soc_dapm_set_bias_level(&codec->dapm,
3828 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003829 }
Mark Brown51737472009-06-22 13:16:51 +01003830}
3831
Richard Purdie2b97eab2006-10-06 18:32:18 +02003832/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003833MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003834MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3835MODULE_LICENSE("GPL");