blob: 68acec6672428f386f0052f91bcfad4ec5224938 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
Liam Girdwood612a3fe2012-02-06 16:05:29 +000017 * mic/headphone insertion events.
Richard Purdie2b97eab2006-10-06 18:32:18 +020018 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Liam Girdwood612a3fe2012-02-06 16:05:29 +000021 * o Delayed power down of audio subsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020024 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080029#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020030#include <linux/delay.h>
31#include <linux/pm.h>
32#include <linux/bitops.h>
33#include <linux/platform_device.h>
34#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020035#include <linux/debugfs.h>
Mark Brownf1aac482011-12-05 15:17:06 +000036#include <linux/pm_runtime.h>
Mark Brown62ea8742012-01-21 21:14:48 +000037#include <linux/regulator/consumer.h>
Ola Liljad7e7eb92012-05-24 15:26:25 +020038#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020040#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020043#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020044#include <sound/initval.h>
45
Mark Brown84e90932010-11-04 00:07:02 -040046#include <trace/events/asoc.h>
47
Mark Brownde02d072011-09-20 21:43:24 +010048#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49
Richard Purdie2b97eab2006-10-06 18:32:18 +020050/* dapm power sequences - make this per codec in the future */
51static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010052 [snd_soc_dapm_pre] = 0,
53 [snd_soc_dapm_supply] = 1,
Mark Brown62ea8742012-01-21 21:14:48 +000054 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020055 [snd_soc_dapm_clock_supply] = 1,
Mark Brown38357ab2009-06-06 19:03:23 +010056 [snd_soc_dapm_micbias] = 2,
Mark Brownc74184e2012-04-04 22:12:09 +010057 [snd_soc_dapm_dai_link] = 2,
Mark Brown888df392012-02-16 19:37:51 -080058 [snd_soc_dapm_dai] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010059 [snd_soc_dapm_aif_in] = 3,
60 [snd_soc_dapm_aif_out] = 3,
61 [snd_soc_dapm_mic] = 4,
62 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000063 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010064 [snd_soc_dapm_value_mux] = 5,
65 [snd_soc_dapm_dac] = 6,
66 [snd_soc_dapm_mixer] = 7,
67 [snd_soc_dapm_mixer_named_ctl] = 7,
68 [snd_soc_dapm_pga] = 8,
69 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060070 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010071 [snd_soc_dapm_hp] = 10,
72 [snd_soc_dapm_spk] = 10,
Mark Brown7e1f7c82012-04-12 17:29:36 +010073 [snd_soc_dapm_line] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010074 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020075};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000076
Richard Purdie2b97eab2006-10-06 18:32:18 +020077static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010078 [snd_soc_dapm_pre] = 0,
79 [snd_soc_dapm_adc] = 1,
80 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010081 [snd_soc_dapm_spk] = 2,
Mark Brown7e1f7c82012-04-12 17:29:36 +010082 [snd_soc_dapm_line] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060083 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010084 [snd_soc_dapm_pga] = 4,
85 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010086 [snd_soc_dapm_mixer] = 5,
87 [snd_soc_dapm_dac] = 6,
88 [snd_soc_dapm_mic] = 7,
89 [snd_soc_dapm_micbias] = 8,
90 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000091 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010092 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010093 [snd_soc_dapm_aif_in] = 10,
94 [snd_soc_dapm_aif_out] = 10,
Mark Brown888df392012-02-16 19:37:51 -080095 [snd_soc_dapm_dai] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +010096 [snd_soc_dapm_dai_link] = 11,
Ola Liljad7e7eb92012-05-24 15:26:25 +020097 [snd_soc_dapm_clock_supply] = 12,
Mark Brownc74184e2012-04-04 22:12:09 +010098 [snd_soc_dapm_regulator_supply] = 12,
99 [snd_soc_dapm_supply] = 12,
100 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200101};
102
Troy Kisky12ef1932008-10-13 17:42:14 -0700103static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100104{
105 if (pop_time)
106 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
107}
108
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200109static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100110{
111 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200112 char *buf;
113
114 if (!pop_time)
115 return;
116
117 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
118 if (buf == NULL)
119 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100120
121 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200122 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100123 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100124 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200125
126 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100127}
128
Mark Browndb432b42011-10-03 21:06:40 +0100129static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
130{
131 return !list_empty(&w->dirty);
132}
133
Mark Brown25c77c52011-10-08 13:36:03 +0100134void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100135{
Mark Brown75c1f892011-10-04 22:28:08 +0100136 if (!dapm_dirty_widget(w)) {
137 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
138 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100139 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100140 }
Mark Browndb432b42011-10-03 21:06:40 +0100141}
Mark Brown25c77c52011-10-08 13:36:03 +0100142EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100143
Mark Browne2d32ff2012-08-31 17:38:32 -0700144void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
145{
146 struct snd_soc_card *card = dapm->card;
147 struct snd_soc_dapm_widget *w;
148
149 mutex_lock(&card->dapm_mutex);
150
151 list_for_each_entry(w, &card->widgets, list) {
152 switch (w->id) {
153 case snd_soc_dapm_input:
154 case snd_soc_dapm_output:
155 dapm_mark_dirty(w, "Rechecking inputs and outputs");
156 break;
157 default:
158 break;
159 }
160 }
161
162 mutex_unlock(&card->dapm_mutex);
163}
164EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
165
Richard Purdie2b97eab2006-10-06 18:32:18 +0200166/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100167static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200168 const struct snd_soc_dapm_widget *_widget)
169{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100170 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200171}
172
Liam Girdwood48056082011-07-20 12:23:33 +0100173/* get snd_card from DAPM context */
174static inline struct snd_card *dapm_get_snd_card(
175 struct snd_soc_dapm_context *dapm)
176{
177 if (dapm->codec)
178 return dapm->codec->card->snd_card;
179 else if (dapm->platform)
180 return dapm->platform->card->snd_card;
181 else
182 BUG();
183
184 /* unreachable */
185 return NULL;
186}
187
188/* get soc_card from DAPM context */
189static inline struct snd_soc_card *dapm_get_soc_card(
190 struct snd_soc_dapm_context *dapm)
191{
192 if (dapm->codec)
193 return dapm->codec->card;
194 else if (dapm->platform)
195 return dapm->platform->card;
196 else
197 BUG();
198
199 /* unreachable */
200 return NULL;
201}
202
Liam Girdwood6c120e12012-02-15 15:15:34 +0000203static void dapm_reset(struct snd_soc_card *card)
204{
205 struct snd_soc_dapm_widget *w;
206
207 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
208
209 list_for_each_entry(w, &card->widgets, list) {
210 w->power_checked = false;
211 w->inputs = -1;
212 w->outputs = -1;
213 }
214}
215
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100216static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
217{
218 if (w->codec)
219 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100220 else if (w->platform)
221 return snd_soc_platform_read(w->platform, reg);
222
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000223 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100224 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100225}
226
227static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
228{
229 if (w->codec)
230 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100231 else if (w->platform)
232 return snd_soc_platform_write(w->platform, reg, val);
233
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000234 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100235 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100236}
237
Liam Girdwood49575fb52012-03-06 18:16:19 +0000238static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
239{
Mark Browne06ab3b2012-03-06 23:58:22 +0000240 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000241 mutex_lock(&w->codec->mutex);
242 else if (w->platform)
243 mutex_lock(&w->platform->mutex);
244}
245
246static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
247{
Mark Browne06ab3b2012-03-06 23:58:22 +0000248 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000249 mutex_unlock(&w->codec->mutex);
250 else if (w->platform)
251 mutex_unlock(&w->platform->mutex);
252}
253
254static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100255 unsigned short reg, unsigned int mask, unsigned int value)
256{
Mark Brown8a713da2011-12-03 12:33:55 +0000257 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100258 unsigned int old, new;
259 int ret;
260
Mark Brown8a713da2011-12-03 12:33:55 +0000261 if (w->codec && w->codec->using_regmap) {
262 ret = regmap_update_bits_check(w->codec->control_data,
263 reg, mask, value, &change);
264 if (ret != 0)
265 return ret;
266 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000267 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000268 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000269 if (ret < 0) {
270 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100271 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000272 }
Mark Brown8a713da2011-12-03 12:33:55 +0000273
274 old = ret;
275 new = (old & ~mask) | (value & mask);
276 change = old != new;
277 if (change) {
278 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000279 if (ret < 0) {
280 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000281 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000282 }
Mark Brown8a713da2011-12-03 12:33:55 +0000283 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000284 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100285 }
286
287 return change;
288}
289
Mark Brown452c5ea2009-05-17 21:41:23 +0100290/**
291 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800292 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100293 * @level: level to configure
294 *
295 * Configure the bias (power) levels for the SoC audio device.
296 *
297 * Returns 0 for success else error.
298 */
Mark Browned5a4c42011-02-18 11:12:42 -0800299static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200300 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100301{
Mark Browned5a4c42011-02-18 11:12:42 -0800302 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100303 int ret = 0;
304
Mark Brown84e90932010-11-04 00:07:02 -0400305 trace_snd_soc_bias_level_start(card, level);
306
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000307 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100308 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100309 if (ret != 0)
310 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100311
Mark Browncc4c6702011-06-06 19:03:34 +0100312 if (dapm->codec) {
313 if (dapm->codec->driver->set_bias_level)
314 ret = dapm->codec->driver->set_bias_level(dapm->codec,
315 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100316 else
317 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100318 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100319 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100320 }
Liam Girdwood41231282012-07-06 16:56:16 +0100321
Mark Brown171ec6b2011-06-06 18:15:19 +0100322 if (ret != 0)
323 goto out;
324
325 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100326 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100327out:
Mark Brown84e90932010-11-04 00:07:02 -0400328 trace_snd_soc_bias_level_done(card, level);
329
Mark Brown452c5ea2009-05-17 21:41:23 +0100330 return ret;
331}
332
Richard Purdie2b97eab2006-10-06 18:32:18 +0200333/* set up initial codec paths */
334static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
335 struct snd_soc_dapm_path *p, int i)
336{
337 switch (w->id) {
338 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000339 case snd_soc_dapm_mixer:
340 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200341 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100342 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600343 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400344 unsigned int reg = mc->reg;
345 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100346 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400347 unsigned int mask = (1 << fls(max)) - 1;
348 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200349
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100350 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200351 val = (val >> shift) & mask;
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200352 if (invert)
353 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200354
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200355 p->connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200356 }
357 break;
358 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600359 struct soc_enum *e = (struct soc_enum *)
360 w->kcontrol_news[i].private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200361 int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200362
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100363 val = soc_widget_read(w, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200364 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200365
366 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100367 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200368 if (!(strcmp(p->name, e->texts[i])) && item == i)
369 p->connect = 1;
370 }
371 }
372 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000373 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600374 struct soc_enum *e = (struct soc_enum *)
375 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000376
377 p->connect = 0;
378 /* since a virtual mux has no backing registers to
379 * decide which path to connect, it will try to match
380 * with the first enumeration. This is to ensure
381 * that the default mux choice (the first) will be
382 * correctly powered up during initialization.
383 */
384 if (!strcmp(p->name, e->texts[0]))
385 p->connect = 1;
386 }
387 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200388 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200389 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600390 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200391 int val, item;
392
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100393 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200394 val = (val >> e->shift_l) & e->mask;
395 for (item = 0; item < e->max; item++) {
396 if (val == e->values[item])
397 break;
398 }
399
400 p->connect = 0;
401 for (i = 0; i < e->max; i++) {
402 if (!(strcmp(p->name, e->texts[i])) && item == i)
403 p->connect = 1;
404 }
405 }
406 break;
Mark Brown56563102011-10-03 22:41:09 +0100407 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200408 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600409 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200410 case snd_soc_dapm_output:
411 case snd_soc_dapm_adc:
412 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000413 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200414 case snd_soc_dapm_dac:
415 case snd_soc_dapm_micbias:
416 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100417 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000418 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200419 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100420 case snd_soc_dapm_aif_in:
421 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800422 case snd_soc_dapm_dai:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200423 case snd_soc_dapm_hp:
424 case snd_soc_dapm_mic:
425 case snd_soc_dapm_spk:
426 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100427 case snd_soc_dapm_dai_link:
Mark Brown56563102011-10-03 22:41:09 +0100428 p->connect = 1;
429 break;
430 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200431 case snd_soc_dapm_pre:
432 case snd_soc_dapm_post:
433 p->connect = 0;
434 break;
435 }
436}
437
Mark Brown74b8f952009-06-06 11:26:15 +0100438/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200439static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200440 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
441 struct snd_soc_dapm_path *path, const char *control_name,
442 const struct snd_kcontrol_new *kcontrol)
443{
444 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
445 int i;
446
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100447 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200448 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200449 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200450 list_add(&path->list_sink, &dest->sources);
451 list_add(&path->list_source, &src->sinks);
452 path->name = (char*)e->texts[i];
453 dapm_set_path_status(dest, path, 0);
454 return 0;
455 }
456 }
457
458 return -ENODEV;
459}
460
Mark Brown74b8f952009-06-06 11:26:15 +0100461/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200462static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200463 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
464 struct snd_soc_dapm_path *path, const char *control_name)
465{
466 int i;
467
468 /* search for mixer kcontrol */
469 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600470 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200471 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200472 list_add(&path->list_sink, &dest->sources);
473 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600474 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200475 dapm_set_path_status(dest, path, i);
476 return 0;
477 }
478 }
479 return -ENODEV;
480}
481
Stephen Warrenaf468002011-04-28 17:38:01 -0600482static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600483 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600484 const struct snd_kcontrol_new *kcontrol_new,
485 struct snd_kcontrol **kcontrol)
486{
487 struct snd_soc_dapm_widget *w;
488 int i;
489
490 *kcontrol = NULL;
491
492 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600493 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
494 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600495 for (i = 0; i < w->num_kcontrols; i++) {
496 if (&w->kcontrol_news[i] == kcontrol_new) {
497 if (w->kcontrols)
498 *kcontrol = w->kcontrols[i];
499 return 1;
500 }
501 }
502 }
503
504 return 0;
505}
506
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 Brown99497882010-05-07 20:24:05 +0100737/* We implement power down on suspend by checking the power state of
738 * the ALSA card - when we are suspending the ALSA state for the card
739 * is set to D3.
740 */
741static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
742{
Mark Brown12ea2c72011-03-02 18:17:32 +0000743 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100744
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000745 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100746 case SNDRV_CTL_POWER_D3hot:
747 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100748 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000749 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200750 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100751 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100752 default:
753 return 1;
754 }
755}
756
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100757/* add widget to list if it's not already in the list */
758static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
759 struct snd_soc_dapm_widget *w)
760{
761 struct snd_soc_dapm_widget_list *wlist;
762 int wlistsize, wlistentries, i;
763
764 if (*list == NULL)
765 return -EINVAL;
766
767 wlist = *list;
768
769 /* is this widget already in the list */
770 for (i = 0; i < wlist->num_widgets; i++) {
771 if (wlist->widgets[i] == w)
772 return 0;
773 }
774
775 /* allocate some new space */
776 wlistentries = wlist->num_widgets + 1;
777 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
778 wlistentries * sizeof(struct snd_soc_dapm_widget *);
779 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
780 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000781 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100782 w->name);
783 return -ENOMEM;
784 }
785 wlist = *list;
786
787 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000788 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100789 w->name, wlist->num_widgets);
790
791 wlist->widgets[wlist->num_widgets] = w;
792 wlist->num_widgets++;
793 return 1;
794}
795
Richard Purdie2b97eab2006-10-06 18:32:18 +0200796/*
797 * Recursively check for a completed path to an active or physically connected
798 * output widget. Returns number of complete paths.
799 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100800static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
801 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200802{
803 struct snd_soc_dapm_path *path;
804 int con = 0;
805
Mark Brown024dc072011-10-09 11:52:05 +0100806 if (widget->outputs >= 0)
807 return widget->outputs;
808
Mark Brownde02d072011-09-20 21:43:24 +0100809 DAPM_UPDATE_STAT(widget, path_checks);
810
Mark Brown62ea8742012-01-21 21:14:48 +0000811 switch (widget->id) {
812 case snd_soc_dapm_supply:
813 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200814 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100815 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000816 default:
817 break;
818 }
Mark Brown246d0a12009-04-22 18:24:55 +0100819
Mark Brown010ff262009-08-17 17:39:22 +0100820 switch (widget->id) {
821 case snd_soc_dapm_adc:
822 case snd_soc_dapm_aif_out:
Mark 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
Richard Purdie2b97eab2006-10-06 18:32:18 +0200855 if (path->walked)
856 continue;
857
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100858 trace_snd_soc_dapm_output_path(widget, path);
859
Richard Purdie2b97eab2006-10-06 18:32:18 +0200860 if (path->sink && path->connect) {
861 path->walked = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100862
863 /* do we need to add this widget to the list ? */
864 if (list) {
865 int err;
866 err = dapm_list_add_widget(list, path->sink);
867 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000868 dev_err(widget->dapm->dev,
869 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100870 widget->name);
871 return con;
872 }
873 }
874
875 con += is_connected_output_ep(path->sink, list);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200876 }
877 }
878
Mark Brown024dc072011-10-09 11:52:05 +0100879 widget->outputs = con;
880
Richard Purdie2b97eab2006-10-06 18:32:18 +0200881 return con;
882}
883
884/*
885 * Recursively check for a completed path to an active or physically connected
886 * input widget. Returns number of complete paths.
887 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100888static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
889 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200890{
891 struct snd_soc_dapm_path *path;
892 int con = 0;
893
Mark Brown024dc072011-10-09 11:52:05 +0100894 if (widget->inputs >= 0)
895 return widget->inputs;
896
Mark Brownde02d072011-09-20 21:43:24 +0100897 DAPM_UPDATE_STAT(widget, path_checks);
898
Mark Brown62ea8742012-01-21 21:14:48 +0000899 switch (widget->id) {
900 case snd_soc_dapm_supply:
901 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200902 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100903 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000904 default:
905 break;
906 }
Mark Brown246d0a12009-04-22 18:24:55 +0100907
Richard Purdie2b97eab2006-10-06 18:32:18 +0200908 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100909 switch (widget->id) {
910 case snd_soc_dapm_dac:
911 case snd_soc_dapm_aif_in:
Mark Brown888df392012-02-16 19:37:51 -0800912 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100913 if (widget->active) {
914 widget->inputs = snd_soc_dapm_suspend_check(widget);
915 return widget->inputs;
916 }
Mark Brown010ff262009-08-17 17:39:22 +0100917 default:
918 break;
919 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200920
921 if (widget->connected) {
922 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100923 if (widget->id == snd_soc_dapm_input && !widget->ext) {
924 widget->inputs = snd_soc_dapm_suspend_check(widget);
925 return widget->inputs;
926 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200927
928 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100929 if (widget->id == snd_soc_dapm_vmid) {
930 widget->inputs = snd_soc_dapm_suspend_check(widget);
931 return widget->inputs;
932 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200933
934 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300935 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100936 (widget->id == snd_soc_dapm_line &&
937 !list_empty(&widget->sinks))) {
938 widget->inputs = snd_soc_dapm_suspend_check(widget);
939 return widget->inputs;
940 }
941
Mark Brown1ab97c82011-11-27 16:21:51 +0000942 /* signal generator */
943 if (widget->id == snd_soc_dapm_siggen) {
944 widget->inputs = snd_soc_dapm_suspend_check(widget);
945 return widget->inputs;
946 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200947 }
948
949 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +0100950 DAPM_UPDATE_STAT(widget, neighbour_checks);
951
Mark Brownbf3a9e12011-06-13 16:42:29 +0100952 if (path->weak)
953 continue;
954
Richard Purdie2b97eab2006-10-06 18:32:18 +0200955 if (path->walked)
956 continue;
957
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100958 trace_snd_soc_dapm_input_path(widget, path);
959
Richard Purdie2b97eab2006-10-06 18:32:18 +0200960 if (path->source && path->connect) {
961 path->walked = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100962
963 /* do we need to add this widget to the list ? */
964 if (list) {
965 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +0100966 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100967 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000968 dev_err(widget->dapm->dev,
969 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100970 widget->name);
971 return con;
972 }
973 }
974
975 con += is_connected_input_ep(path->source, list);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200976 }
977 }
978
Mark Brown024dc072011-10-09 11:52:05 +0100979 widget->inputs = con;
980
Richard Purdie2b97eab2006-10-06 18:32:18 +0200981 return con;
982}
983
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100984/**
985 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
986 * @dai: the soc DAI.
987 * @stream: stream direction.
988 * @list: list of active widgets for this stream.
989 *
990 * Queries DAPM graph as to whether an valid audio stream path exists for
991 * the initial stream specified by name. This takes into account
992 * current mixer and mux kcontrol settings. Creates list of valid widgets.
993 *
994 * Returns the number of valid paths or negative error.
995 */
996int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
997 struct snd_soc_dapm_widget_list **list)
998{
999 struct snd_soc_card *card = dai->card;
1000 int paths;
1001
1002 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1003 dapm_reset(card);
1004
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001005 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001006 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001007 dapm_clear_walk_output(&card->dapm,
1008 &dai->playback_widget->sinks);
1009 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001010 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001011 dapm_clear_walk_input(&card->dapm,
1012 &dai->capture_widget->sources);
1013 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001014
1015 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001016 mutex_unlock(&card->dapm_mutex);
1017
1018 return paths;
1019}
1020
Richard Purdie2b97eab2006-10-06 18:32:18 +02001021/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001022 * Handler for generic register modifier widget.
1023 */
1024int dapm_reg_event(struct snd_soc_dapm_widget *w,
1025 struct snd_kcontrol *kcontrol, int event)
1026{
1027 unsigned int val;
1028
1029 if (SND_SOC_DAPM_EVENT_ON(event))
1030 val = w->on_val;
1031 else
1032 val = w->off_val;
1033
Liam Girdwood49575fb52012-03-06 18:16:19 +00001034 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001035 w->mask << w->shift, val << w->shift);
1036
1037 return 0;
1038}
Mark Brown11589412008-07-29 11:42:23 +01001039EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001040
Mark Brown62ea8742012-01-21 21:14:48 +00001041/*
1042 * Handler for regulator supply widget.
1043 */
1044int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1045 struct snd_kcontrol *kcontrol, int event)
1046{
Mark Brownc05b84d2012-09-07 12:57:11 +08001047 int ret;
1048
1049 if (SND_SOC_DAPM_EVENT_ON(event)) {
1050 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001051 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001052 if (ret != 0)
1053 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001054 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001055 w->name, ret);
1056 }
1057
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001058 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001059 } else {
1060 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001061 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001062 if (ret != 0)
1063 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001064 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001065 w->name, ret);
1066 }
1067
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001068 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001069 }
Mark Brown62ea8742012-01-21 21:14:48 +00001070}
1071EXPORT_SYMBOL_GPL(dapm_regulator_event);
1072
Ola Liljad7e7eb92012-05-24 15:26:25 +02001073/*
1074 * Handler for clock supply widget.
1075 */
1076int dapm_clock_event(struct snd_soc_dapm_widget *w,
1077 struct snd_kcontrol *kcontrol, int event)
1078{
1079 if (!w->clk)
1080 return -EIO;
1081
Mark Brownec029952012-06-04 08:16:20 +01001082#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001083 if (SND_SOC_DAPM_EVENT_ON(event)) {
1084 return clk_enable(w->clk);
1085 } else {
1086 clk_disable(w->clk);
1087 return 0;
1088 }
Mark Brownec029952012-06-04 08:16:20 +01001089#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001090 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001091}
1092EXPORT_SYMBOL_GPL(dapm_clock_event);
1093
Mark Brownd8050022011-09-28 18:28:23 +01001094static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1095{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001096 if (w->power_checked)
1097 return w->new_power;
1098
Mark Brownd8050022011-09-28 18:28:23 +01001099 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001100 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001101 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001102 w->new_power = w->power_check(w);
1103
1104 w->power_checked = true;
1105
1106 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001107}
1108
Mark Browncd0f2d42009-04-20 16:56:59 +01001109/* Generic check to see if a widget should be powered.
1110 */
1111static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1112{
1113 int in, out;
1114
Mark Brownde02d072011-09-20 21:43:24 +01001115 DAPM_UPDATE_STAT(w, power_checks);
1116
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001117 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001118 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001119 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001120 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001121 return out != 0 && in != 0;
1122}
1123
Mark Brown888df392012-02-16 19:37:51 -08001124static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
1125{
1126 DAPM_UPDATE_STAT(w, power_checks);
1127
Mark Brown1eee1b32012-04-10 13:57:46 +01001128 if (w->active)
1129 return w->active;
1130
1131 return dapm_generic_check_power(w);
Mark Brown888df392012-02-16 19:37:51 -08001132}
1133
Mark Brown6ea31b92009-04-20 17:15:41 +01001134/* Check to see if an ADC has power */
1135static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1136{
1137 int in;
1138
Mark Brownde02d072011-09-20 21:43:24 +01001139 DAPM_UPDATE_STAT(w, power_checks);
1140
Mark Brown6ea31b92009-04-20 17:15:41 +01001141 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001142 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001143 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001144 return in != 0;
1145 } else {
1146 return dapm_generic_check_power(w);
1147 }
1148}
1149
1150/* Check to see if a DAC has power */
1151static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1152{
1153 int out;
1154
Mark Brownde02d072011-09-20 21:43:24 +01001155 DAPM_UPDATE_STAT(w, power_checks);
1156
Mark Brown6ea31b92009-04-20 17:15:41 +01001157 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001158 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001159 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001160 return out != 0;
1161 } else {
1162 return dapm_generic_check_power(w);
1163 }
1164}
1165
Mark Brown246d0a12009-04-22 18:24:55 +01001166/* Check to see if a power supply is needed */
1167static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1168{
1169 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001170
Mark Brownde02d072011-09-20 21:43:24 +01001171 DAPM_UPDATE_STAT(w, power_checks);
1172
Mark Brown246d0a12009-04-22 18:24:55 +01001173 /* Check if one of our outputs is connected */
1174 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001175 DAPM_UPDATE_STAT(w, neighbour_checks);
1176
Mark Brownbf3a9e12011-06-13 16:42:29 +01001177 if (path->weak)
1178 continue;
1179
Mark Brown215edda2009-09-08 18:59:05 +01001180 if (path->connected &&
1181 !path->connected(path->source, path->sink))
1182 continue;
1183
Mark Brown30173582011-02-11 11:42:19 +00001184 if (!path->sink)
1185 continue;
1186
Mark Brownf68d7e12011-10-04 22:57:50 +01001187 if (dapm_widget_power_check(path->sink))
1188 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001189 }
1190
Mark Brownf68d7e12011-10-04 22:57:50 +01001191 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001192}
1193
Mark Brown35c64bc2011-09-28 18:23:53 +01001194static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1195{
1196 return 1;
1197}
1198
Mark Brown38357ab2009-06-06 19:03:23 +01001199static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1200 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001201 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001202{
Mark Brown828a8422011-01-15 13:14:30 +00001203 int *sort;
1204
1205 if (power_up)
1206 sort = dapm_up_seq;
1207 else
1208 sort = dapm_down_seq;
1209
Mark Brown38357ab2009-06-06 19:03:23 +01001210 if (sort[a->id] != sort[b->id])
1211 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001212 if (a->subseq != b->subseq) {
1213 if (power_up)
1214 return a->subseq - b->subseq;
1215 else
1216 return b->subseq - a->subseq;
1217 }
Mark Brownb22ead22009-06-07 12:51:26 +01001218 if (a->reg != b->reg)
1219 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001220 if (a->dapm != b->dapm)
1221 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001222
Mark Brown38357ab2009-06-06 19:03:23 +01001223 return 0;
1224}
Mark Brown42aa3412009-03-01 19:21:10 +00001225
Mark Brown38357ab2009-06-06 19:03:23 +01001226/* Insert a widget in order into a DAPM power sequence. */
1227static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1228 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001229 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001230{
1231 struct snd_soc_dapm_widget *w;
1232
1233 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001234 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001235 list_add_tail(&new_widget->power_list, &w->power_list);
1236 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001237 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001238
Mark Brown38357ab2009-06-06 19:03:23 +01001239 list_add_tail(&new_widget->power_list, list);
1240}
Mark Brown42aa3412009-03-01 19:21:10 +00001241
Mark Brown68f89ad2010-11-03 23:51:49 -04001242static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1243 struct snd_soc_dapm_widget *w, int event)
1244{
1245 struct snd_soc_card *card = dapm->card;
1246 const char *ev_name;
1247 int power, ret;
1248
1249 switch (event) {
1250 case SND_SOC_DAPM_PRE_PMU:
1251 ev_name = "PRE_PMU";
1252 power = 1;
1253 break;
1254 case SND_SOC_DAPM_POST_PMU:
1255 ev_name = "POST_PMU";
1256 power = 1;
1257 break;
1258 case SND_SOC_DAPM_PRE_PMD:
1259 ev_name = "PRE_PMD";
1260 power = 0;
1261 break;
1262 case SND_SOC_DAPM_POST_PMD:
1263 ev_name = "POST_PMD";
1264 power = 0;
1265 break;
1266 default:
1267 BUG();
1268 return;
1269 }
1270
1271 if (w->power != power)
1272 return;
1273
1274 if (w->event && (w->event_flags & event)) {
1275 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1276 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001277 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001278 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001279 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001280 if (ret < 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001281 dev_err(dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001282 ev_name, w->name, ret);
1283 }
1284}
1285
Mark Brownb22ead22009-06-07 12:51:26 +01001286/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001287static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001288 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001289{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001290 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001291 struct snd_soc_dapm_widget *w;
1292 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001293 unsigned int value = 0;
1294 unsigned int mask = 0;
1295 unsigned int cur_mask;
1296
1297 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1298 power_list)->reg;
1299
1300 list_for_each_entry(w, pending, power_list) {
1301 cur_mask = 1 << w->shift;
1302 BUG_ON(reg != w->reg);
1303
1304 if (w->invert)
1305 power = !w->power;
1306 else
1307 power = w->power;
1308
1309 mask |= cur_mask;
1310 if (power)
1311 value |= cur_mask;
1312
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001313 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001314 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1315 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001316
Mark Brown68f89ad2010-11-03 23:51:49 -04001317 /* Check for events */
1318 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1319 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001320 }
1321
Mark Brown81628102009-06-07 13:21:24 +01001322 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001323 /* Any widget will do, they should all be updating the
1324 * same register.
1325 */
1326 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1327 power_list);
1328
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001329 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001330 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001331 value, mask, reg, card->pop_time);
1332 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001333 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001334 }
1335
1336 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001337 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1338 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001339 }
Mark Brown42aa3412009-03-01 19:21:10 +00001340}
1341
Mark Brownb22ead22009-06-07 12:51:26 +01001342/* Apply a DAPM power sequence.
1343 *
1344 * We walk over a pre-sorted list of widgets to apply power to. In
1345 * order to minimise the number of writes to the device required
1346 * multiple widgets will be updated in a single write where possible.
1347 * Currently anything that requires more than a single write is not
1348 * handled.
1349 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001350static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001351 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001352{
1353 struct snd_soc_dapm_widget *w, *n;
1354 LIST_HEAD(pending);
1355 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001356 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001357 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001358 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001359 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001360 int *sort;
1361
1362 if (power_up)
1363 sort = dapm_up_seq;
1364 else
1365 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001366
Mark Brownb22ead22009-06-07 12:51:26 +01001367 list_for_each_entry_safe(w, n, list, power_list) {
1368 ret = 0;
1369
1370 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001371 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001372 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001373 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001374 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001375
Mark Brown474b62d2011-01-18 16:14:44 +00001376 if (cur_dapm && cur_dapm->seq_notifier) {
1377 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1378 if (sort[i] == cur_sort)
1379 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001380 i,
1381 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001382 }
1383
Mark Brownb22ead22009-06-07 12:51:26 +01001384 INIT_LIST_HEAD(&pending);
1385 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001386 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001387 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001388 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001389 }
1390
Mark Brown163cac02009-06-07 10:12:52 +01001391 switch (w->id) {
1392 case snd_soc_dapm_pre:
1393 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001394 list_for_each_entry_safe_continue(w, n, list,
1395 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001396
Mark Brownb22ead22009-06-07 12:51:26 +01001397 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001398 ret = w->event(w,
1399 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001400 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001401 ret = w->event(w,
1402 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001403 break;
1404
1405 case snd_soc_dapm_post:
1406 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001407 list_for_each_entry_safe_continue(w, n, list,
1408 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001409
Mark Brownb22ead22009-06-07 12:51:26 +01001410 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001411 ret = w->event(w,
1412 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001413 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001414 ret = w->event(w,
1415 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001416 break;
1417
Mark Brown163cac02009-06-07 10:12:52 +01001418 default:
Mark Brown81628102009-06-07 13:21:24 +01001419 /* Queue it up for application */
1420 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001421 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001422 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001423 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001424 list_move(&w->power_list, &pending);
1425 break;
Mark Brown163cac02009-06-07 10:12:52 +01001426 }
Mark Brownb22ead22009-06-07 12:51:26 +01001427
1428 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001429 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001430 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001431 }
Mark Brownb22ead22009-06-07 12:51:26 +01001432
1433 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001434 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001435
1436 if (cur_dapm && cur_dapm->seq_notifier) {
1437 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1438 if (sort[i] == cur_sort)
1439 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001440 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001441 }
Mark Brown163cac02009-06-07 10:12:52 +01001442}
1443
Mark Brown97404f22010-12-14 16:13:57 +00001444static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1445{
1446 struct snd_soc_dapm_update *update = dapm->update;
1447 struct snd_soc_dapm_widget *w;
1448 int ret;
1449
1450 if (!update)
1451 return;
1452
1453 w = update->widget;
1454
1455 if (w->event &&
1456 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1457 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1458 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001459 dev_err(dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Mark Brown97404f22010-12-14 16:13:57 +00001460 w->name, ret);
1461 }
1462
Liam Girdwood49575fb52012-03-06 18:16:19 +00001463 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001464 update->val);
1465 if (ret < 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001466 dev_err(dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1467 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001468
1469 if (w->event &&
1470 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1471 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1472 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001473 dev_err(dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Mark Brown97404f22010-12-14 16:13:57 +00001474 w->name, ret);
1475 }
1476}
1477
Mark Brown9d0624a2011-02-18 11:49:43 -08001478/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1479 * they're changing state.
1480 */
1481static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1482{
1483 struct snd_soc_dapm_context *d = data;
1484 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001485
Mark Brown56fba412011-06-04 11:25:10 +01001486 /* If we're off and we're not supposed to be go into STANDBY */
1487 if (d->bias_level == SND_SOC_BIAS_OFF &&
1488 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001489 if (d->dev)
1490 pm_runtime_get_sync(d->dev);
1491
Mark Brown9d0624a2011-02-18 11:49:43 -08001492 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1493 if (ret != 0)
1494 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001495 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001496 }
1497
Mark Brown56fba412011-06-04 11:25:10 +01001498 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1499 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001500 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1501 if (ret != 0)
1502 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001503 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001504 }
1505}
1506
1507/* Async callback run prior to DAPM sequences - brings to their final
1508 * state.
1509 */
1510static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1511{
1512 struct snd_soc_dapm_context *d = data;
1513 int ret;
1514
1515 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001516 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1517 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1518 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001519 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1520 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001521 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001522 ret);
1523 }
1524
1525 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001526 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1527 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001528 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1529 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001530 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1531 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001532
1533 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001534 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001535 }
1536
1537 /* If we just powered up then move to active 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_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001540 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1541 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001542 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001543 ret);
1544 }
1545}
Mark Brown97404f22010-12-14 16:13:57 +00001546
Mark Brownfe4fda52011-10-03 22:36:57 +01001547static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1548 bool power, bool connect)
1549{
1550 /* If a connection is being made or broken then that update
1551 * will have marked the peer dirty, otherwise the widgets are
1552 * not connected and this update has no impact. */
1553 if (!connect)
1554 return;
1555
1556 /* If the peer is already in the state we're moving to then we
1557 * won't have an impact on it. */
1558 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001559 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001560}
1561
Mark Brown05623c42011-09-28 17:02:31 +01001562static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1563 struct list_head *up_list,
1564 struct list_head *down_list)
1565{
Mark Browndb432b42011-10-03 21:06:40 +01001566 struct snd_soc_dapm_path *path;
1567
Mark Brown05623c42011-09-28 17:02:31 +01001568 if (w->power == power)
1569 return;
1570
1571 trace_snd_soc_dapm_widget_power(w, power);
1572
Mark Browndb432b42011-10-03 21:06:40 +01001573 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001574 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001575 */
1576 list_for_each_entry(path, &w->sources, list_sink) {
1577 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001578 dapm_widget_set_peer_power(path->source, power,
1579 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001580 }
1581 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001582 switch (w->id) {
1583 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001584 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001585 case snd_soc_dapm_clock_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001586 /* Supplies can't affect their outputs, only their inputs */
1587 break;
1588 default:
1589 list_for_each_entry(path, &w->sinks, list_source) {
1590 if (path->sink) {
1591 dapm_widget_set_peer_power(path->sink, power,
1592 path->connect);
1593 }
Mark Browndb432b42011-10-03 21:06:40 +01001594 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001595 break;
Mark Browndb432b42011-10-03 21:06:40 +01001596 }
1597
Mark Brown05623c42011-09-28 17:02:31 +01001598 if (power)
1599 dapm_seq_insert(w, up_list, true);
1600 else
1601 dapm_seq_insert(w, down_list, false);
1602
1603 w->power = power;
1604}
1605
Mark Brown7c81beb2011-09-20 22:22:32 +01001606static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1607 struct list_head *up_list,
1608 struct list_head *down_list)
1609{
Mark Brown7c81beb2011-09-20 22:22:32 +01001610 int power;
1611
1612 switch (w->id) {
1613 case snd_soc_dapm_pre:
1614 dapm_seq_insert(w, down_list, false);
1615 break;
1616 case snd_soc_dapm_post:
1617 dapm_seq_insert(w, up_list, true);
1618 break;
1619
1620 default:
Mark Brownd8050022011-09-28 18:28:23 +01001621 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001622
Mark Brown05623c42011-09-28 17:02:31 +01001623 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001624 break;
1625 }
1626}
1627
Mark Brown42aa3412009-03-01 19:21:10 +00001628/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001629 * Scan each dapm widget for complete audio path.
1630 * A complete path is a route that has valid endpoints i.e.:-
1631 *
1632 * o DAC to output pin.
1633 * o Input Pin to ADC.
1634 * o Input pin to Output pin (bypass, sidetone)
1635 * o DAC to ADC (loopback).
1636 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001637static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001638{
Mark Brown12ea2c72011-03-02 18:17:32 +00001639 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001640 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001641 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001642 LIST_HEAD(up_list);
1643 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001644 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001645 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001646
Mark Brown84e90932010-11-04 00:07:02 -04001647 trace_snd_soc_dapm_start(card);
1648
Mark Brown56fba412011-06-04 11:25:10 +01001649 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001650 if (d->idle_bias_off)
1651 d->target_bias_level = SND_SOC_BIAS_OFF;
1652 else
1653 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001654 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001655
Liam Girdwood6c120e12012-02-15 15:15:34 +00001656 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001657
Mark Brown6d3ddc82009-05-16 17:47:29 +01001658 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001659 * lists indicating if they should be powered up or down. We
1660 * only check widgets that have been flagged as dirty but note
1661 * that new widgets may be added to the dirty list while we
1662 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001663 */
Mark Browndb432b42011-10-03 21:06:40 +01001664 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001665 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001666 }
1667
Mark Brownf9de6d72011-09-28 17:19:47 +01001668 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001669 switch (w->id) {
1670 case snd_soc_dapm_pre:
1671 case snd_soc_dapm_post:
1672 /* These widgets always need to be powered */
1673 break;
1674 default:
1675 list_del_init(&w->dirty);
1676 break;
1677 }
Mark Browndb432b42011-10-03 21:06:40 +01001678
Mark Brownf9de6d72011-09-28 17:19:47 +01001679 if (w->power) {
1680 d = w->dapm;
1681
1682 /* Supplies and micbiases only bring the
1683 * context up to STANDBY as unless something
1684 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001685 * generally don't require full power. Signal
1686 * generators are virtual pins and have no
1687 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001688 */
1689 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001690 case snd_soc_dapm_siggen:
1691 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001692 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001693 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001694 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001695 case snd_soc_dapm_micbias:
1696 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1697 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1698 break;
1699 default:
1700 d->target_bias_level = SND_SOC_BIAS_ON;
1701 break;
1702 }
1703 }
1704
1705 }
1706
Mark Brown85a843c2011-09-21 21:29:47 +01001707 /* Force all contexts in the card to the same bias state if
1708 * they're not ground referenced.
1709 */
Mark Brown56fba412011-06-04 11:25:10 +01001710 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001711 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001712 if (d->target_bias_level > bias)
1713 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001714 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001715 if (!d->idle_bias_off)
1716 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001717
Mark Brownde02d072011-09-20 21:43:24 +01001718 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001719
Mark Brown9d0624a2011-02-18 11:49:43 -08001720 /* Run all the bias changes in parallel */
1721 list_for_each_entry(d, &dapm->card->dapm_list, list)
1722 async_schedule_domain(dapm_pre_sequence_async, d,
1723 &async_domain);
1724 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001725
Mark Brown6d3ddc82009-05-16 17:47:29 +01001726 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001727 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001728
Mark Brown97404f22010-12-14 16:13:57 +00001729 dapm_widget_update(dapm);
1730
Mark Brown6d3ddc82009-05-16 17:47:29 +01001731 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001732 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001733
Mark Brown9d0624a2011-02-18 11:49:43 -08001734 /* Run all the bias changes in parallel */
1735 list_for_each_entry(d, &dapm->card->dapm_list, list)
1736 async_schedule_domain(dapm_post_sequence_async, d,
1737 &async_domain);
1738 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001739
Liam Girdwood8078d872012-02-15 15:15:35 +00001740 /* do we need to notify any clients that DAPM event is complete */
1741 list_for_each_entry(d, &card->dapm_list, list) {
1742 if (d->stream_event)
1743 d->stream_event(d, event);
1744 }
1745
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001746 pop_dbg(dapm->dev, card->pop_time,
1747 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001748 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001749
Mark Brown84e90932010-11-04 00:07:02 -04001750 trace_snd_soc_dapm_done(card);
1751
Mark Brown42aa3412009-03-01 19:21:10 +00001752 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001753}
1754
Mark Brown79fb9382009-08-21 16:38:13 +01001755#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001756static ssize_t dapm_widget_power_read_file(struct file *file,
1757 char __user *user_buf,
1758 size_t count, loff_t *ppos)
1759{
1760 struct snd_soc_dapm_widget *w = file->private_data;
1761 char *buf;
1762 int in, out;
1763 ssize_t ret;
1764 struct snd_soc_dapm_path *p = NULL;
1765
1766 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1767 if (!buf)
1768 return -ENOMEM;
1769
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001770 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001771 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001772 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001773 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001774
Mark Brownf13ebad2012-03-03 18:01:01 +00001775 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1776 w->name, w->power ? "On" : "Off",
1777 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001778
Mark Brownd033c362009-12-04 15:25:56 +00001779 if (w->reg >= 0)
1780 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1781 " - R%d(0x%x) bit %d",
1782 w->reg, w->reg, w->shift);
1783
1784 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1785
Mark Brown3eef08b2009-09-14 16:49:00 +01001786 if (w->sname)
1787 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1788 w->sname,
1789 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001790
1791 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001792 if (p->connected && !p->connected(w, p->sink))
1793 continue;
1794
Mark Brown79fb9382009-08-21 16:38:13 +01001795 if (p->connect)
1796 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001797 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001798 p->name ? p->name : "static",
1799 p->source->name);
1800 }
1801 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001802 if (p->connected && !p->connected(w, p->sink))
1803 continue;
1804
Mark Brown79fb9382009-08-21 16:38:13 +01001805 if (p->connect)
1806 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001807 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001808 p->name ? p->name : "static",
1809 p->sink->name);
1810 }
1811
1812 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1813
1814 kfree(buf);
1815 return ret;
1816}
1817
1818static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001819 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001820 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001821 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001822};
1823
Mark Brownef49e4f2011-04-04 20:48:13 +09001824static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1825 size_t count, loff_t *ppos)
1826{
1827 struct snd_soc_dapm_context *dapm = file->private_data;
1828 char *level;
1829
1830 switch (dapm->bias_level) {
1831 case SND_SOC_BIAS_ON:
1832 level = "On\n";
1833 break;
1834 case SND_SOC_BIAS_PREPARE:
1835 level = "Prepare\n";
1836 break;
1837 case SND_SOC_BIAS_STANDBY:
1838 level = "Standby\n";
1839 break;
1840 case SND_SOC_BIAS_OFF:
1841 level = "Off\n";
1842 break;
1843 default:
1844 BUG();
1845 level = "Unknown\n";
1846 break;
1847 }
1848
1849 return simple_read_from_buffer(user_buf, count, ppos, level,
1850 strlen(level));
1851}
1852
1853static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001854 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001855 .read = dapm_bias_read_file,
1856 .llseek = default_llseek,
1857};
1858
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001859void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1860 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001861{
Mark Brown79fb9382009-08-21 16:38:13 +01001862 struct dentry *d;
1863
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001864 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1865
1866 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001867 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001868 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001869 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001870 }
Mark Brown79fb9382009-08-21 16:38:13 +01001871
Mark Brownef49e4f2011-04-04 20:48:13 +09001872 d = debugfs_create_file("bias_level", 0444,
1873 dapm->debugfs_dapm, dapm,
1874 &dapm_bias_fops);
1875 if (!d)
1876 dev_warn(dapm->dev,
1877 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001878}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001879
1880static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1881{
1882 struct snd_soc_dapm_context *dapm = w->dapm;
1883 struct dentry *d;
1884
1885 if (!dapm->debugfs_dapm || !w->name)
1886 return;
1887
1888 d = debugfs_create_file(w->name, 0444,
1889 dapm->debugfs_dapm, w,
1890 &dapm_widget_power_fops);
1891 if (!d)
1892 dev_warn(w->dapm->dev,
1893 "ASoC: Failed to create %s debugfs file\n",
1894 w->name);
1895}
1896
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001897static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1898{
1899 debugfs_remove_recursive(dapm->debugfs_dapm);
1900}
1901
Mark Brown79fb9382009-08-21 16:38:13 +01001902#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001903void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1904 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001905{
1906}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001907
1908static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1909{
1910}
1911
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001912static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1913{
1914}
1915
Mark Brown79fb9382009-08-21 16:38:13 +01001916#endif
1917
Richard Purdie2b97eab2006-10-06 18:32:18 +02001918/* test and update the power status of a mux widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001919static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001920 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001921{
1922 struct snd_soc_dapm_path *path;
1923 int found = 0;
1924
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001925 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001926 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001927 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001928 return -ENODEV;
1929
Richard Purdie2b97eab2006-10-06 18:32:18 +02001930 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001931 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001932 if (path->kcontrol != kcontrol)
1933 continue;
1934
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001935 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001936 continue;
1937
1938 found = 1;
1939 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001940 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001941 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001942 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001943 } else {
1944 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001945 dapm_mark_dirty(path->source,
1946 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001947 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001948 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001949 }
1950
Mark Browndb432b42011-10-03 21:06:40 +01001951 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001952 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001953 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001954 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001955
Liam Girdwood618dae12012-04-25 12:12:51 +01001956 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001957}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001958
1959int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1960 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1961{
1962 struct snd_soc_card *card = widget->dapm->card;
1963 int ret;
1964
Liam Girdwood3cd04342012-03-09 12:02:08 +00001965 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001966 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1967 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001968 if (ret > 0)
1969 soc_dpcm_runtime_update(widget);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001970 return ret;
1971}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001972EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001973
Milan plzik1b075e32008-01-10 14:39:46 +01001974/* test and update the power status of a mixer or switch widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001975static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001976 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001977{
1978 struct snd_soc_dapm_path *path;
1979 int found = 0;
1980
Milan plzik1b075e32008-01-10 14:39:46 +01001981 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001982 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001983 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001984 return -ENODEV;
1985
Richard Purdie2b97eab2006-10-06 18:32:18 +02001986 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001987 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001988 if (path->kcontrol != kcontrol)
1989 continue;
1990
1991 /* found, now check type */
1992 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001993 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001994 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001995 }
1996
Mark Browndb432b42011-10-03 21:06:40 +01001997 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001998 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001999 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01002000 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002001
Liam Girdwood618dae12012-04-25 12:12:51 +01002002 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002003}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002004
2005int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
2006 struct snd_kcontrol *kcontrol, int connect)
2007{
2008 struct snd_soc_card *card = widget->dapm->card;
2009 int ret;
2010
Liam Girdwood3cd04342012-03-09 12:02:08 +00002011 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002012 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
2013 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002014 if (ret > 0)
2015 soc_dpcm_runtime_update(widget);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002016 return ret;
2017}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002018EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002019
2020/* show dapm widget status in sys fs */
2021static ssize_t dapm_widget_show(struct device *dev,
2022 struct device_attribute *attr, char *buf)
2023{
Mark Brown36ae1a92012-01-06 17:12:45 -08002024 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002025 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002026 struct snd_soc_dapm_widget *w;
2027 int count = 0;
2028 char *state = "not set";
2029
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002030 list_for_each_entry(w, &codec->card->widgets, list) {
2031 if (w->dapm != &codec->dapm)
2032 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002033
2034 /* only display widgets that burnm power */
2035 switch (w->id) {
2036 case snd_soc_dapm_hp:
2037 case snd_soc_dapm_mic:
2038 case snd_soc_dapm_spk:
2039 case snd_soc_dapm_line:
2040 case snd_soc_dapm_micbias:
2041 case snd_soc_dapm_dac:
2042 case snd_soc_dapm_adc:
2043 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002044 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002045 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002046 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002047 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002048 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002049 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002050 if (w->name)
2051 count += sprintf(buf + count, "%s: %s\n",
2052 w->name, w->power ? "On":"Off");
2053 break;
2054 default:
2055 break;
2056 }
2057 }
2058
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002059 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002060 case SND_SOC_BIAS_ON:
2061 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002062 break;
Mark Brown0be98982008-05-19 12:31:28 +02002063 case SND_SOC_BIAS_PREPARE:
2064 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002065 break;
Mark Brown0be98982008-05-19 12:31:28 +02002066 case SND_SOC_BIAS_STANDBY:
2067 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002068 break;
Mark Brown0be98982008-05-19 12:31:28 +02002069 case SND_SOC_BIAS_OFF:
2070 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002071 break;
2072 }
2073 count += sprintf(buf + count, "PM State: %s\n", state);
2074
2075 return count;
2076}
2077
2078static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2079
2080int snd_soc_dapm_sys_add(struct device *dev)
2081{
Troy Kisky12ef1932008-10-13 17:42:14 -07002082 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002083}
2084
2085static void snd_soc_dapm_sys_remove(struct device *dev)
2086{
Mark Brownaef90842009-05-16 17:53:16 +01002087 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002088}
2089
2090/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002091static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002092{
2093 struct snd_soc_dapm_widget *w, *next_w;
2094 struct snd_soc_dapm_path *p, *next_p;
2095
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002096 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2097 if (w->dapm != dapm)
2098 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002099 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002100 /*
2101 * remove source and sink paths associated to this widget.
2102 * While removing the path, remove reference to it from both
2103 * source and sink widgets so that path is removed only once.
2104 */
2105 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
2106 list_del(&p->list_sink);
2107 list_del(&p->list_source);
2108 list_del(&p->list);
2109 kfree(p->long_name);
2110 kfree(p);
2111 }
2112 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2113 list_del(&p->list_sink);
2114 list_del(&p->list_source);
2115 list_del(&p->list);
2116 kfree(p->long_name);
2117 kfree(p);
2118 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002119 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002120 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002121 kfree(w);
2122 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002123}
2124
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002125static struct snd_soc_dapm_widget *dapm_find_widget(
2126 struct snd_soc_dapm_context *dapm, const char *pin,
2127 bool search_other_contexts)
2128{
2129 struct snd_soc_dapm_widget *w;
2130 struct snd_soc_dapm_widget *fallback = NULL;
2131
2132 list_for_each_entry(w, &dapm->card->widgets, list) {
2133 if (!strcmp(w->name, pin)) {
2134 if (w->dapm == dapm)
2135 return w;
2136 else
2137 fallback = w;
2138 }
2139 }
2140
2141 if (search_other_contexts)
2142 return fallback;
2143
2144 return NULL;
2145}
2146
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002147static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002148 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002149{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002150 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002151
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002152 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002153 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002154 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002155 }
2156
Mark Brown1a8b2d92012-02-16 11:50:07 -08002157 if (w->connected != status)
2158 dapm_mark_dirty(w, "pin configuration");
2159
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002160 w->connected = status;
2161 if (status == 0)
2162 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002163
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002164 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002165}
2166
Richard Purdie2b97eab2006-10-06 18:32:18 +02002167/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002168 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002169 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002170 *
2171 * Walks all dapm audio paths and powers widgets according to their
2172 * stream or path usage.
2173 *
2174 * Returns 0 for success.
2175 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002176int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002177{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002178 int ret;
2179
Mark Brown4f4c0072011-10-07 14:29:19 +01002180 /*
2181 * Suppress early reports (eg, jacks syncing their state) to avoid
2182 * silly DAPM runs during card startup.
2183 */
2184 if (!dapm->card || !dapm->card->instantiated)
2185 return 0;
2186
Liam Girdwood3cd04342012-03-09 12:02:08 +00002187 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002188 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2189 mutex_unlock(&dapm->card->dapm_mutex);
2190 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002191}
Liam Girdwooda5302182008-07-07 13:35:17 +01002192EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002193
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002194static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002195 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002196{
2197 struct snd_soc_dapm_path *path;
2198 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002199 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002200 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002201 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002202 const char *source;
2203 char prefixed_sink[80];
2204 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002205 int ret = 0;
2206
Mark Brown88e8b9a2011-03-02 18:18:24 +00002207 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002208 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2209 dapm->codec->name_prefix, route->sink);
2210 sink = prefixed_sink;
2211 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2212 dapm->codec->name_prefix, route->source);
2213 source = prefixed_source;
2214 } else {
2215 sink = route->sink;
2216 source = route->source;
2217 }
2218
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002219 /*
2220 * find src and dest widgets over all widgets but favor a widget from
2221 * current DAPM context
2222 */
2223 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002224 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002225 wtsink = w;
2226 if (w->dapm == dapm)
2227 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002228 continue;
2229 }
2230 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002231 wtsource = w;
2232 if (w->dapm == dapm)
2233 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002234 }
2235 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002236 /* use widget from another DAPM context if not found from this */
2237 if (!wsink)
2238 wsink = wtsink;
2239 if (!wsource)
2240 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002241
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002242 if (wsource == NULL) {
2243 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2244 route->source);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002245 return -ENODEV;
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002246 }
2247 if (wsink == NULL) {
2248 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2249 route->sink);
2250 return -ENODEV;
2251 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002252
2253 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2254 if (!path)
2255 return -ENOMEM;
2256
2257 path->source = wsource;
2258 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002259 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002260 INIT_LIST_HEAD(&path->list);
2261 INIT_LIST_HEAD(&path->list_source);
2262 INIT_LIST_HEAD(&path->list_sink);
2263
2264 /* check for external widgets */
2265 if (wsink->id == snd_soc_dapm_input) {
2266 if (wsource->id == snd_soc_dapm_micbias ||
2267 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002268 wsource->id == snd_soc_dapm_line ||
2269 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002270 wsink->ext = 1;
2271 }
2272 if (wsource->id == snd_soc_dapm_output) {
2273 if (wsink->id == snd_soc_dapm_spk ||
2274 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002275 wsink->id == snd_soc_dapm_line ||
2276 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002277 wsource->ext = 1;
2278 }
2279
2280 /* connect static paths */
2281 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002282 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002283 list_add(&path->list_sink, &wsink->sources);
2284 list_add(&path->list_source, &wsource->sinks);
2285 path->connect = 1;
2286 return 0;
2287 }
2288
2289 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002290 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002291 case snd_soc_dapm_adc:
2292 case snd_soc_dapm_dac:
2293 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002294 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002295 case snd_soc_dapm_input:
2296 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002297 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002298 case snd_soc_dapm_micbias:
2299 case snd_soc_dapm_vmid:
2300 case snd_soc_dapm_pre:
2301 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002302 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002303 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002304 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002305 case snd_soc_dapm_aif_in:
2306 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -08002307 case snd_soc_dapm_dai:
Mark Brownc74184e2012-04-04 22:12:09 +01002308 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002309 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002310 list_add(&path->list_sink, &wsink->sources);
2311 list_add(&path->list_source, &wsource->sinks);
2312 path->connect = 1;
2313 return 0;
2314 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002315 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002316 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002317 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002318 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002319 if (ret != 0)
2320 goto err;
2321 break;
2322 case snd_soc_dapm_switch:
2323 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002324 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002325 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002326 if (ret != 0)
2327 goto err;
2328 break;
2329 case snd_soc_dapm_hp:
2330 case snd_soc_dapm_mic:
2331 case snd_soc_dapm_line:
2332 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002333 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002334 list_add(&path->list_sink, &wsink->sources);
2335 list_add(&path->list_source, &wsource->sinks);
2336 path->connect = 0;
2337 return 0;
2338 }
Mark Brownfabd0382012-07-05 17:20:06 +01002339
2340 dapm_mark_dirty(wsource, "Route added");
2341 dapm_mark_dirty(wsink, "Route added");
2342
Richard Purdie2b97eab2006-10-06 18:32:18 +02002343 return 0;
2344
2345err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002346 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002347 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002348 kfree(path);
2349 return ret;
2350}
Mark Brown105f1c22008-05-13 14:52:19 +02002351
Mark Brownefcc3c62012-07-05 17:24:19 +01002352static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2353 const struct snd_soc_dapm_route *route)
2354{
2355 struct snd_soc_dapm_path *path, *p;
2356 const char *sink;
2357 const char *source;
2358 char prefixed_sink[80];
2359 char prefixed_source[80];
2360
2361 if (route->control) {
2362 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002363 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002364 return -EINVAL;
2365 }
2366
2367 if (dapm->codec && dapm->codec->name_prefix) {
2368 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2369 dapm->codec->name_prefix, route->sink);
2370 sink = prefixed_sink;
2371 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2372 dapm->codec->name_prefix, route->source);
2373 source = prefixed_source;
2374 } else {
2375 sink = route->sink;
2376 source = route->source;
2377 }
2378
2379 path = NULL;
2380 list_for_each_entry(p, &dapm->card->paths, list) {
2381 if (strcmp(p->source->name, source) != 0)
2382 continue;
2383 if (strcmp(p->sink->name, sink) != 0)
2384 continue;
2385 path = p;
2386 break;
2387 }
2388
2389 if (path) {
2390 dapm_mark_dirty(path->source, "Route removed");
2391 dapm_mark_dirty(path->sink, "Route removed");
2392
2393 list_del(&path->list);
2394 list_del(&path->list_sink);
2395 list_del(&path->list_source);
2396 kfree(path);
2397 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002398 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002399 source, sink);
2400 }
2401
2402 return 0;
2403}
2404
Mark Brown105f1c22008-05-13 14:52:19 +02002405/**
Mark Brown105f1c22008-05-13 14:52:19 +02002406 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002407 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002408 * @route: audio routes
2409 * @num: number of routes
2410 *
2411 * Connects 2 dapm widgets together via a named audio path. The sink is
2412 * the widget receiving the audio signal, whilst the source is the sender
2413 * of the audio signal.
2414 *
2415 * Returns 0 for success else error. On error all resources can be freed
2416 * with a call to snd_soc_card_free().
2417 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002418int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002419 const struct snd_soc_dapm_route *route, int num)
2420{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002421 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002422
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002423 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002424 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002425 r = snd_soc_dapm_add_route(dapm, route);
2426 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002427 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2428 route->source,
2429 route->control ? route->control : "direct",
2430 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002431 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002432 }
2433 route++;
2434 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002435 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002436
Dan Carpenter60884c22012-04-13 22:25:43 +03002437 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002438}
2439EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2440
Mark Brownefcc3c62012-07-05 17:24:19 +01002441/**
2442 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2443 * @dapm: DAPM context
2444 * @route: audio routes
2445 * @num: number of routes
2446 *
2447 * Removes routes from the DAPM context.
2448 */
2449int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2450 const struct snd_soc_dapm_route *route, int num)
2451{
2452 int i, ret = 0;
2453
2454 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2455 for (i = 0; i < num; i++) {
2456 snd_soc_dapm_del_route(dapm, route);
2457 route++;
2458 }
2459 mutex_unlock(&dapm->card->dapm_mutex);
2460
2461 return ret;
2462}
2463EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2464
Mark Brownbf3a9e12011-06-13 16:42:29 +01002465static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2466 const struct snd_soc_dapm_route *route)
2467{
2468 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2469 route->source,
2470 true);
2471 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2472 route->sink,
2473 true);
2474 struct snd_soc_dapm_path *path;
2475 int count = 0;
2476
2477 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002478 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002479 route->source);
2480 return -ENODEV;
2481 }
2482
2483 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002484 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002485 route->sink);
2486 return -ENODEV;
2487 }
2488
2489 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002490 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002491 route->source, route->sink);
2492
2493 list_for_each_entry(path, &source->sinks, list_source) {
2494 if (path->sink == sink) {
2495 path->weak = 1;
2496 count++;
2497 }
2498 }
2499
2500 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002501 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002502 route->source, route->sink);
2503 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002504 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002505 count, route->source, route->sink);
2506
2507 return 0;
2508}
2509
2510/**
2511 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2512 * @dapm: DAPM context
2513 * @route: audio routes
2514 * @num: number of routes
2515 *
2516 * Mark existing routes matching those specified in the passed array
2517 * as being weak, meaning that they are ignored for the purpose of
2518 * power decisions. The main intended use case is for sidetone paths
2519 * which couple audio between other independent paths if they are both
2520 * active in order to make the combination work better at the user
2521 * level but which aren't intended to be "used".
2522 *
2523 * Note that CODEC drivers should not use this as sidetone type paths
2524 * can frequently also be used as bypass paths.
2525 */
2526int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2527 const struct snd_soc_dapm_route *route, int num)
2528{
2529 int i, err;
2530 int ret = 0;
2531
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002532 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002533 for (i = 0; i < num; i++) {
2534 err = snd_soc_dapm_weak_route(dapm, route);
2535 if (err)
2536 ret = err;
2537 route++;
2538 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002539 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002540
2541 return ret;
2542}
2543EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2544
Mark Brown105f1c22008-05-13 14:52:19 +02002545/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002546 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002547 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002548 *
2549 * Checks the codec for any new dapm widgets and creates them if found.
2550 *
2551 * Returns 0 for success.
2552 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002553int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002554{
2555 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002556 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002557
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002558 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2559
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002560 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002561 {
2562 if (w->new)
2563 continue;
2564
Stephen Warrenfad59882011-04-28 17:37:59 -06002565 if (w->num_kcontrols) {
2566 w->kcontrols = kzalloc(w->num_kcontrols *
2567 sizeof(struct snd_kcontrol *),
2568 GFP_KERNEL);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002569 if (!w->kcontrols) {
2570 mutex_unlock(&dapm->card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002571 return -ENOMEM;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002572 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002573 }
2574
Richard Purdie2b97eab2006-10-06 18:32:18 +02002575 switch(w->id) {
2576 case snd_soc_dapm_switch:
2577 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002578 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002579 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002580 break;
2581 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002582 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002583 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002584 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002585 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002586 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002587 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002588 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002589 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002590 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002591 break;
2592 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002593
2594 /* Read the initial power state from the device */
2595 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002596 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002597 val &= 1 << w->shift;
2598 if (w->invert)
2599 val = !val;
2600
2601 if (val)
2602 w->power = 1;
2603 }
2604
Richard Purdie2b97eab2006-10-06 18:32:18 +02002605 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002606
Mark Brown7508b122011-10-05 12:09:12 +01002607 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002608 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002609 }
2610
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002611 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002612 mutex_unlock(&dapm->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002613 return 0;
2614}
2615EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2616
2617/**
2618 * snd_soc_dapm_get_volsw - dapm mixer get callback
2619 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002620 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002621 *
2622 * Callback to get the value of a dapm mixer control.
2623 *
2624 * Returns 0 for success.
2625 */
2626int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2627 struct snd_ctl_elem_value *ucontrol)
2628{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002629 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2630 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002631 struct soc_mixer_control *mc =
2632 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002633 unsigned int reg = mc->reg;
2634 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002635 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002636 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002637 unsigned int invert = mc->invert;
2638
2639 if (snd_soc_volsw_is_stereo(mc))
2640 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002641 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002642 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002643
Richard Purdie2b97eab2006-10-06 18:32:18 +02002644 ucontrol->value.integer.value[0] =
2645 (snd_soc_read(widget->codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002646 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002647 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002648 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002649
2650 return 0;
2651}
2652EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2653
2654/**
2655 * snd_soc_dapm_put_volsw - dapm mixer set callback
2656 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002657 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002658 *
2659 * Callback to set the value of a dapm mixer control.
2660 *
2661 * Returns 0 for success.
2662 */
2663int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2664 struct snd_ctl_elem_value *ucontrol)
2665{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002666 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2667 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2668 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002669 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002670 struct soc_mixer_control *mc =
2671 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002672 unsigned int reg = mc->reg;
2673 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002674 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002675 unsigned int mask = (1 << fls(max)) - 1;
2676 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002677 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002678 int connect, change;
2679 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002680 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002681
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002682 if (snd_soc_volsw_is_stereo(mc))
2683 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002684 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002685 kcontrol->id.name);
2686
Richard Purdie2b97eab2006-10-06 18:32:18 +02002687 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002688 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002689
2690 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002691 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002692 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002693 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002694
Liam Girdwood3cd04342012-03-09 12:02:08 +00002695 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002696
Stephen Warrene9cf7042011-01-27 14:54:05 -07002697 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002698 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002699 for (wi = 0; wi < wlist->num_widgets; wi++) {
2700 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002701
Stephen Warrenfafd2172011-04-28 17:38:00 -06002702 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002703
Stephen Warrenfafd2172011-04-28 17:38:00 -06002704 update.kcontrol = kcontrol;
2705 update.widget = widget;
2706 update.reg = reg;
2707 update.mask = mask;
2708 update.val = val;
2709 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002710
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002711 soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002712
2713 widget->dapm->update = NULL;
2714 }
Mark Brown283375c2009-12-07 18:09:03 +00002715 }
2716
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002717 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002718 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002719}
2720EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2721
2722/**
2723 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2724 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002725 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002726 *
2727 * Callback to get the value of a dapm enumerated double mixer control.
2728 *
2729 * Returns 0 for success.
2730 */
2731int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2732 struct snd_ctl_elem_value *ucontrol)
2733{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002734 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2735 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002736 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002737 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002738
Richard Purdie2b97eab2006-10-06 18:32:18 +02002739 val = snd_soc_read(widget->codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002740 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002741 if (e->shift_l != e->shift_r)
2742 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002743 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002744
2745 return 0;
2746}
2747EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2748
2749/**
2750 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2751 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002752 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002753 *
2754 * Callback to set the value of a dapm enumerated double mixer control.
2755 *
2756 * Returns 0 for success.
2757 */
2758int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2759 struct snd_ctl_elem_value *ucontrol)
2760{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002761 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2762 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2763 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002764 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002765 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002766 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002767 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002768 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002769 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002770
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002771 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002772 return -EINVAL;
2773 mux = ucontrol->value.enumerated.item[0];
2774 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002775 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002776 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002777 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002778 return -EINVAL;
2779 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002780 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002781 }
2782
Liam Girdwood3cd04342012-03-09 12:02:08 +00002783 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002784
Mark Brown3a655772009-10-05 17:23:30 +01002785 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002786 if (change) {
2787 for (wi = 0; wi < wlist->num_widgets; wi++) {
2788 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002789
Stephen Warrenfafd2172011-04-28 17:38:00 -06002790 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002791
Stephen Warrenfafd2172011-04-28 17:38:00 -06002792 update.kcontrol = kcontrol;
2793 update.widget = widget;
2794 update.reg = e->reg;
2795 update.mask = mask;
2796 update.val = val;
2797 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002798
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002799 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002800
Stephen Warrenfafd2172011-04-28 17:38:00 -06002801 widget->dapm->update = NULL;
2802 }
2803 }
2804
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002805 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002806 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002807}
2808EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2809
2810/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002811 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2812 * @kcontrol: mixer control
2813 * @ucontrol: control element information
2814 *
2815 * Returns 0 for success.
2816 */
2817int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2818 struct snd_ctl_elem_value *ucontrol)
2819{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002820 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2821 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002822
2823 ucontrol->value.enumerated.item[0] = widget->value;
2824
2825 return 0;
2826}
2827EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2828
2829/**
2830 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2831 * @kcontrol: mixer control
2832 * @ucontrol: control element information
2833 *
2834 * Returns 0 for success.
2835 */
2836int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2837 struct snd_ctl_elem_value *ucontrol)
2838{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002839 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2840 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2841 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002842 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002843 struct soc_enum *e =
2844 (struct soc_enum *)kcontrol->private_value;
2845 int change;
2846 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002847 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002848
2849 if (ucontrol->value.enumerated.item[0] >= e->max)
2850 return -EINVAL;
2851
Liam Girdwood3cd04342012-03-09 12:02:08 +00002852 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002853
2854 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002855 if (change) {
2856 for (wi = 0; wi < wlist->num_widgets; wi++) {
2857 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002858
Stephen Warrenfafd2172011-04-28 17:38:00 -06002859 widget->value = ucontrol->value.enumerated.item[0];
2860
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002861 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002862 }
2863 }
2864
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002865 mutex_unlock(&card->dapm_mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002866 return ret;
2867}
2868EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2869
2870/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002871 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2872 * callback
2873 * @kcontrol: mixer control
2874 * @ucontrol: control element information
2875 *
2876 * Callback to get the value of a dapm semi enumerated double mixer control.
2877 *
2878 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2879 * used for handling bitfield coded enumeration for example.
2880 *
2881 * Returns 0 for success.
2882 */
2883int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2884 struct snd_ctl_elem_value *ucontrol)
2885{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002886 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2887 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002888 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002889 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002890
2891 reg_val = snd_soc_read(widget->codec, e->reg);
2892 val = (reg_val >> e->shift_l) & e->mask;
2893 for (mux = 0; mux < e->max; mux++) {
2894 if (val == e->values[mux])
2895 break;
2896 }
2897 ucontrol->value.enumerated.item[0] = mux;
2898 if (e->shift_l != e->shift_r) {
2899 val = (reg_val >> e->shift_r) & e->mask;
2900 for (mux = 0; mux < e->max; mux++) {
2901 if (val == e->values[mux])
2902 break;
2903 }
2904 ucontrol->value.enumerated.item[1] = mux;
2905 }
2906
2907 return 0;
2908}
2909EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2910
2911/**
2912 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2913 * callback
2914 * @kcontrol: mixer control
2915 * @ucontrol: control element information
2916 *
2917 * Callback to set the value of a dapm semi enumerated double mixer control.
2918 *
2919 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2920 * used for handling bitfield coded enumeration for example.
2921 *
2922 * Returns 0 for success.
2923 */
2924int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2925 struct snd_ctl_elem_value *ucontrol)
2926{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002927 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2928 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2929 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002930 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002931 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002932 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002933 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002934 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002935 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002936
2937 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2938 return -EINVAL;
2939 mux = ucontrol->value.enumerated.item[0];
2940 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2941 mask = e->mask << e->shift_l;
2942 if (e->shift_l != e->shift_r) {
2943 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2944 return -EINVAL;
2945 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2946 mask |= e->mask << e->shift_r;
2947 }
2948
Liam Girdwood3cd04342012-03-09 12:02:08 +00002949 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002950
Mark Brown3a655772009-10-05 17:23:30 +01002951 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002952 if (change) {
2953 for (wi = 0; wi < wlist->num_widgets; wi++) {
2954 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002955
Stephen Warrenfafd2172011-04-28 17:38:00 -06002956 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002957
Stephen Warrenfafd2172011-04-28 17:38:00 -06002958 update.kcontrol = kcontrol;
2959 update.widget = widget;
2960 update.reg = e->reg;
2961 update.mask = mask;
2962 update.val = val;
2963 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002964
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002965 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002966
Stephen Warrenfafd2172011-04-28 17:38:00 -06002967 widget->dapm->update = NULL;
2968 }
2969 }
2970
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002971 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002972 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002973}
2974EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2975
2976/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002977 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2978 *
2979 * @kcontrol: mixer control
2980 * @uinfo: control element information
2981 *
2982 * Callback to provide information about a pin switch control.
2983 */
2984int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2985 struct snd_ctl_elem_info *uinfo)
2986{
2987 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2988 uinfo->count = 1;
2989 uinfo->value.integer.min = 0;
2990 uinfo->value.integer.max = 1;
2991
2992 return 0;
2993}
2994EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2995
2996/**
2997 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2998 *
2999 * @kcontrol: mixer control
3000 * @ucontrol: Value
3001 */
3002int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3003 struct snd_ctl_elem_value *ucontrol)
3004{
Mark Brown48a8c392012-02-14 17:11:15 -08003005 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003006 const char *pin = (const char *)kcontrol->private_value;
3007
Liam Girdwood3cd04342012-03-09 12:02:08 +00003008 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003009
3010 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003011 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003012
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003013 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003014
3015 return 0;
3016}
3017EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3018
3019/**
3020 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3021 *
3022 * @kcontrol: mixer control
3023 * @ucontrol: Value
3024 */
3025int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3026 struct snd_ctl_elem_value *ucontrol)
3027{
Mark Brown48a8c392012-02-14 17:11:15 -08003028 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003029 const char *pin = (const char *)kcontrol->private_value;
3030
Liam Girdwood3cd04342012-03-09 12:02:08 +00003031 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003032
3033 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003034 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003035 else
Mark Brown48a8c392012-02-14 17:11:15 -08003036 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003037
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003038 mutex_unlock(&card->dapm_mutex);
3039
Mark Brown48a8c392012-02-14 17:11:15 -08003040 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003041 return 0;
3042}
3043EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3044
Mark Brown5ba06fc2012-02-16 11:07:13 -08003045static struct snd_soc_dapm_widget *
3046snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3047 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003048{
3049 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003050 size_t name_len;
Mark Brown62ea8742012-01-21 21:14:48 +00003051 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003052
3053 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003054 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003055
Mark Brown62ea8742012-01-21 21:14:48 +00003056 switch (w->id) {
3057 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003058 w->regulator = devm_regulator_get(dapm->dev, w->name);
3059 if (IS_ERR(w->regulator)) {
3060 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003061 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003062 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003063 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003064 }
Mark Brown8784c772013-01-10 19:33:47 +00003065
3066 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
3067 ret = regulator_allow_bypass(w->regulator, true);
3068 if (ret != 0)
3069 dev_warn(w->dapm->dev,
3070 "ASoC: Failed to unbypass %s: %d\n",
3071 w->name, ret);
3072 }
Mark Brown62ea8742012-01-21 21:14:48 +00003073 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003074 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003075#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003076 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003077 if (IS_ERR(w->clk)) {
3078 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003079 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003080 w->name, ret);
3081 return NULL;
3082 }
Mark Brownec029952012-06-04 08:16:20 +01003083#else
3084 return NULL;
3085#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003086 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003087 default:
3088 break;
3089 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003090
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003091 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00003092 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003093 name_len += 1 + strlen(dapm->codec->name_prefix);
3094 w->name = kmalloc(name_len, GFP_KERNEL);
3095 if (w->name == NULL) {
3096 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003097 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003098 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00003099 if (dapm->codec && dapm->codec->name_prefix)
Mark Brown888df392012-02-16 19:37:51 -08003100 snprintf((char *)w->name, name_len, "%s %s",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003101 dapm->codec->name_prefix, widget->name);
3102 else
Mark Brown888df392012-02-16 19:37:51 -08003103 snprintf((char *)w->name, name_len, "%s", widget->name);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003104
Mark Brown7ca3a182011-10-08 14:04:50 +01003105 switch (w->id) {
3106 case snd_soc_dapm_switch:
3107 case snd_soc_dapm_mixer:
3108 case snd_soc_dapm_mixer_named_ctl:
3109 w->power_check = dapm_generic_check_power;
3110 break;
3111 case snd_soc_dapm_mux:
3112 case snd_soc_dapm_virt_mux:
3113 case snd_soc_dapm_value_mux:
3114 w->power_check = dapm_generic_check_power;
3115 break;
3116 case snd_soc_dapm_adc:
3117 case snd_soc_dapm_aif_out:
3118 w->power_check = dapm_adc_check_power;
3119 break;
3120 case snd_soc_dapm_dac:
3121 case snd_soc_dapm_aif_in:
3122 w->power_check = dapm_dac_check_power;
3123 break;
3124 case snd_soc_dapm_pga:
3125 case snd_soc_dapm_out_drv:
3126 case snd_soc_dapm_input:
3127 case snd_soc_dapm_output:
3128 case snd_soc_dapm_micbias:
3129 case snd_soc_dapm_spk:
3130 case snd_soc_dapm_hp:
3131 case snd_soc_dapm_mic:
3132 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003133 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003134 w->power_check = dapm_generic_check_power;
3135 break;
3136 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003137 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003138 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003139 w->power_check = dapm_supply_check_power;
3140 break;
Mark Brown888df392012-02-16 19:37:51 -08003141 case snd_soc_dapm_dai:
3142 w->power_check = dapm_dai_check_power;
3143 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003144 default:
3145 w->power_check = dapm_always_on_check_power;
3146 break;
3147 }
3148
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003149 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003150 w->dapm = dapm;
3151 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003152 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003153 INIT_LIST_HEAD(&w->sources);
3154 INIT_LIST_HEAD(&w->sinks);
3155 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003156 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003157 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003158
3159 /* machine layer set ups unconnected pins and insertions */
3160 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003161 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003162}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003163
3164/**
Mark Brown4ba13272008-05-13 14:51:19 +02003165 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003166 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003167 * @widget: widget array
3168 * @num: number of widgets
3169 *
3170 * Creates new DAPM controls based upon the templates.
3171 *
3172 * Returns 0 for success else error.
3173 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003174int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003175 const struct snd_soc_dapm_widget *widget,
3176 int num)
3177{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003178 struct snd_soc_dapm_widget *w;
3179 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003180 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003181
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003182 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003183 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003184 w = snd_soc_dapm_new_control(dapm, widget);
3185 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003186 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003187 "ASoC: Failed to create DAPM control %s\n",
3188 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003189 ret = -ENOMEM;
3190 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003191 }
Mark Brown4ba13272008-05-13 14:51:19 +02003192 widget++;
3193 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003194 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003195 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003196}
3197EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3198
Mark Brownc74184e2012-04-04 22:12:09 +01003199static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3200 struct snd_kcontrol *kcontrol, int event)
3201{
3202 struct snd_soc_dapm_path *source_p, *sink_p;
3203 struct snd_soc_dai *source, *sink;
3204 const struct snd_soc_pcm_stream *config = w->params;
3205 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003206 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003207 u64 fmt;
3208 int ret;
3209
3210 BUG_ON(!config);
3211 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3212
3213 /* We only support a single source and sink, pick the first */
3214 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3215 list_sink);
3216 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3217 list_source);
3218
3219 BUG_ON(!source_p || !sink_p);
3220 BUG_ON(!sink_p->source || !source_p->sink);
3221 BUG_ON(!source_p->source || !sink_p->sink);
3222
3223 source = source_p->source->priv;
3224 sink = sink_p->sink->priv;
3225
3226 /* Be a little careful as we don't want to overflow the mask array */
3227 if (config->formats) {
3228 fmt = ffs(config->formats) - 1;
3229 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003230 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003231 config->formats);
3232 fmt = 0;
3233 }
3234
3235 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003236 params = kzalloc(sizeof(*params), GFP_KERNEL);
3237 if (!params) {
3238 ret = -ENOMEM;
3239 goto out;
3240 }
3241 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003242
Mark Brown9747cec2012-04-26 19:12:21 +01003243 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003244 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003245 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003246 config->rate_max;
3247
Mark Brown9747cec2012-04-26 19:12:21 +01003248 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003249 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003250 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003251 = config->channels_max;
3252
3253 memset(&substream, 0, sizeof(substream));
3254
3255 switch (event) {
3256 case SND_SOC_DAPM_PRE_PMU:
3257 if (source->driver->ops && source->driver->ops->hw_params) {
3258 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3259 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003260 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003261 if (ret != 0) {
3262 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003263 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003264 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003265 }
3266 }
3267
3268 if (sink->driver->ops && sink->driver->ops->hw_params) {
3269 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003270 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003271 sink);
3272 if (ret != 0) {
3273 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003274 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003275 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003276 }
3277 }
3278 break;
3279
3280 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003281 ret = snd_soc_dai_digital_mute(sink, 0,
3282 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003283 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003284 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003285 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003286 break;
3287
3288 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003289 ret = snd_soc_dai_digital_mute(sink, 1,
3290 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003291 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003292 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003293 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003294 break;
3295
3296 default:
3297 BUG();
3298 return -EINVAL;
3299 }
3300
Mark Brown9747cec2012-04-26 19:12:21 +01003301out:
3302 kfree(params);
3303 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003304}
3305
3306int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3307 const struct snd_soc_pcm_stream *params,
3308 struct snd_soc_dapm_widget *source,
3309 struct snd_soc_dapm_widget *sink)
3310{
3311 struct snd_soc_dapm_route routes[2];
3312 struct snd_soc_dapm_widget template;
3313 struct snd_soc_dapm_widget *w;
3314 size_t len;
3315 char *link_name;
3316
3317 len = strlen(source->name) + strlen(sink->name) + 2;
3318 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3319 if (!link_name)
3320 return -ENOMEM;
3321 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3322
3323 memset(&template, 0, sizeof(template));
3324 template.reg = SND_SOC_NOPM;
3325 template.id = snd_soc_dapm_dai_link;
3326 template.name = link_name;
3327 template.event = snd_soc_dai_link_event;
3328 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3329 SND_SOC_DAPM_PRE_PMD;
3330
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003331 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003332
3333 w = snd_soc_dapm_new_control(&card->dapm, &template);
3334 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003335 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003336 link_name);
3337 return -ENOMEM;
3338 }
3339
3340 w->params = params;
3341
3342 memset(&routes, 0, sizeof(routes));
3343
3344 routes[0].source = source->name;
3345 routes[0].sink = link_name;
3346 routes[1].source = link_name;
3347 routes[1].sink = sink->name;
3348
3349 return snd_soc_dapm_add_routes(&card->dapm, routes,
3350 ARRAY_SIZE(routes));
3351}
3352
Mark Brown888df392012-02-16 19:37:51 -08003353int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3354 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003355{
Mark Brown888df392012-02-16 19:37:51 -08003356 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003357 struct snd_soc_dapm_widget *w;
3358
Mark Brown888df392012-02-16 19:37:51 -08003359 WARN_ON(dapm->dev != dai->dev);
3360
3361 memset(&template, 0, sizeof(template));
3362 template.reg = SND_SOC_NOPM;
3363
3364 if (dai->driver->playback.stream_name) {
3365 template.id = snd_soc_dapm_dai;
3366 template.name = dai->driver->playback.stream_name;
3367 template.sname = dai->driver->playback.stream_name;
3368
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003369 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003370 template.name);
3371
3372 w = snd_soc_dapm_new_control(dapm, &template);
3373 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003374 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003375 dai->driver->playback.stream_name);
3376 }
3377
3378 w->priv = dai;
3379 dai->playback_widget = w;
3380 }
3381
3382 if (dai->driver->capture.stream_name) {
3383 template.id = snd_soc_dapm_dai;
3384 template.name = dai->driver->capture.stream_name;
3385 template.sname = dai->driver->capture.stream_name;
3386
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003387 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003388 template.name);
3389
3390 w = snd_soc_dapm_new_control(dapm, &template);
3391 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003392 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003393 dai->driver->capture.stream_name);
3394 }
3395
3396 w->priv = dai;
3397 dai->capture_widget = w;
3398 }
3399
3400 return 0;
3401}
3402
3403int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3404{
3405 struct snd_soc_dapm_widget *dai_w, *w;
3406 struct snd_soc_dai *dai;
3407 struct snd_soc_dapm_route r;
3408
3409 memset(&r, 0, sizeof(r));
3410
3411 /* For each DAI widget... */
3412 list_for_each_entry(dai_w, &card->widgets, list) {
3413 if (dai_w->id != snd_soc_dapm_dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003414 continue;
Mark Brown888df392012-02-16 19:37:51 -08003415
3416 dai = dai_w->priv;
3417
3418 /* ...find all widgets with the same stream and link them */
3419 list_for_each_entry(w, &card->widgets, list) {
3420 if (w->dapm != dai_w->dapm)
3421 continue;
3422
3423 if (w->id == snd_soc_dapm_dai)
3424 continue;
3425
3426 if (!w->sname)
3427 continue;
3428
3429 if (dai->driver->playback.stream_name &&
3430 strstr(w->sname,
3431 dai->driver->playback.stream_name)) {
3432 r.source = dai->playback_widget->name;
3433 r.sink = w->name;
3434 dev_dbg(dai->dev, "%s -> %s\n",
3435 r.source, r.sink);
3436
3437 snd_soc_dapm_add_route(w->dapm, &r);
3438 }
3439
3440 if (dai->driver->capture.stream_name &&
3441 strstr(w->sname,
3442 dai->driver->capture.stream_name)) {
3443 r.source = w->name;
3444 r.sink = dai->capture_widget->name;
3445 dev_dbg(dai->dev, "%s -> %s\n",
3446 r.source, r.sink);
3447
3448 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003449 }
3450 }
3451 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003452
Mark Brown888df392012-02-16 19:37:51 -08003453 return 0;
3454}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003455
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003456static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3457 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003458{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003459
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003460 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3461 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3462 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003463
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003464 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3465 w_cpu = cpu_dai->playback_widget;
3466 w_codec = codec_dai->playback_widget;
3467 } else {
3468 w_cpu = cpu_dai->capture_widget;
3469 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003470 }
3471
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003472 if (w_cpu) {
3473
3474 dapm_mark_dirty(w_cpu, "stream event");
3475
3476 switch (event) {
3477 case SND_SOC_DAPM_STREAM_START:
3478 w_cpu->active = 1;
3479 break;
3480 case SND_SOC_DAPM_STREAM_STOP:
3481 w_cpu->active = 0;
3482 break;
3483 case SND_SOC_DAPM_STREAM_SUSPEND:
3484 case SND_SOC_DAPM_STREAM_RESUME:
3485 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3486 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3487 break;
3488 }
3489 }
3490
3491 if (w_codec) {
3492
3493 dapm_mark_dirty(w_codec, "stream event");
3494
3495 switch (event) {
3496 case SND_SOC_DAPM_STREAM_START:
3497 w_codec->active = 1;
3498 break;
3499 case SND_SOC_DAPM_STREAM_STOP:
3500 w_codec->active = 0;
3501 break;
3502 case SND_SOC_DAPM_STREAM_SUSPEND:
3503 case SND_SOC_DAPM_STREAM_RESUME:
3504 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3505 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3506 break;
3507 }
3508 }
3509
3510 dapm_power_widgets(&rtd->card->dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003511}
3512
3513/**
3514 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3515 * @rtd: PCM runtime data
3516 * @stream: stream name
3517 * @event: stream event
3518 *
3519 * Sends a stream event to the dapm core. The core then makes any
3520 * necessary widget power changes.
3521 *
3522 * Returns 0 for success else error.
3523 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003524void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3525 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003526{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003527 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003528
Liam Girdwood3cd04342012-03-09 12:02:08 +00003529 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003530 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003531 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003532}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003533
3534/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003535 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003536 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003537 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003538 *
Mark Brown74b8f952009-06-06 11:26:15 +01003539 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003540 * a valid audio route and active audio stream.
3541 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3542 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003543 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003544int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003545{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003546 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003547}
Liam Girdwooda5302182008-07-07 13:35:17 +01003548EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003549
3550/**
Mark Brownda341832010-03-15 19:23:37 +00003551 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003552 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003553 * @pin: pin name
3554 *
3555 * Enables input/output pin regardless of any other state. This is
3556 * intended for use with microphone bias supplies used in microphone
3557 * jack detection.
3558 *
3559 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3560 * do any widget power switching.
3561 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003562int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3563 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003564{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003565 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003566
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003567 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003568 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003569 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003570 }
3571
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003572 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003573 w->connected = 1;
3574 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003575 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003576
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003577 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003578}
3579EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3580
3581/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003582 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003583 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003584 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003585 *
Mark Brown74b8f952009-06-06 11:26:15 +01003586 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003587 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3588 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003589 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003590int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3591 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003592{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003593 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003594}
3595EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3596
3597/**
Mark Brown5817b522008-09-24 11:23:11 +01003598 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003599 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003600 * @pin: pin name
3601 *
3602 * Marks the specified pin as being not connected, disabling it along
3603 * any parent or child widgets. At present this is identical to
3604 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3605 * additional things such as disabling controls which only affect
3606 * paths through the pin.
3607 *
3608 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3609 * do any widget power switching.
3610 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003611int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003612{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003613 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003614}
3615EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3616
3617/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003618 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003619 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003620 * @pin: audio signal pin endpoint (or start point)
3621 *
3622 * Get audio pin status - connected or disconnected.
3623 *
3624 * Returns 1 for connected otherwise 0.
3625 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003626int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3627 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003628{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003629 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003630
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003631 if (w)
3632 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003633
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003634 return 0;
3635}
Liam Girdwooda5302182008-07-07 13:35:17 +01003636EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003637
3638/**
Mark Brown1547aba2010-05-07 21:11:40 +01003639 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003640 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003641 * @pin: audio signal pin endpoint (or start point)
3642 *
3643 * Mark the given endpoint or pin as ignoring suspend. When the
3644 * system is disabled a path between two endpoints flagged as ignoring
3645 * suspend will not be disabled. The path must already be enabled via
3646 * normal means at suspend time, it will not be turned on if it was not
3647 * already enabled.
3648 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003649int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3650 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003651{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003652 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003653
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003654 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003655 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003656 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003657 }
3658
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003659 w->ignore_suspend = 1;
3660
3661 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003662}
3663EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3664
Stephen Warren16332812011-11-23 12:42:04 -07003665static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3666 struct snd_soc_dapm_widget *w)
3667{
3668 struct snd_soc_dapm_path *p;
3669
3670 list_for_each_entry(p, &card->paths, list) {
3671 if ((p->source == w) || (p->sink == w)) {
3672 dev_dbg(card->dev,
3673 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3674 p->source->name, p->source->id, p->source->dapm,
3675 p->sink->name, p->sink->id, p->sink->dapm);
3676
3677 /* Connected to something other than the codec */
3678 if (p->source->dapm != p->sink->dapm)
3679 return true;
3680 /*
3681 * Loopback connection from codec external pin to
3682 * codec external pin
3683 */
3684 if (p->sink->id == snd_soc_dapm_input) {
3685 switch (p->source->id) {
3686 case snd_soc_dapm_output:
3687 case snd_soc_dapm_micbias:
3688 return true;
3689 default:
3690 break;
3691 }
3692 }
3693 }
3694 }
3695
3696 return false;
3697}
3698
3699/**
3700 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3701 * @codec: The codec whose pins should be processed
3702 *
3703 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3704 * which are unused. Pins are used if they are connected externally to the
3705 * codec, whether that be to some other device, or a loop-back connection to
3706 * the codec itself.
3707 */
3708void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3709{
3710 struct snd_soc_card *card = codec->card;
3711 struct snd_soc_dapm_context *dapm = &codec->dapm;
3712 struct snd_soc_dapm_widget *w;
3713
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003714 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003715 &card->dapm, &codec->dapm);
3716
3717 list_for_each_entry(w, &card->widgets, list) {
3718 if (w->dapm != dapm)
3719 continue;
3720 switch (w->id) {
3721 case snd_soc_dapm_input:
3722 case snd_soc_dapm_output:
3723 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003724 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003725 w->name);
3726 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003727 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003728 "... Not in map; disabling\n");
3729 snd_soc_dapm_nc_pin(dapm, w->name);
3730 }
3731 break;
3732 default:
3733 break;
3734 }
3735 }
3736}
3737
Mark Brown1547aba2010-05-07 21:11:40 +01003738/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003739 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003740 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003741 *
3742 * Free all dapm widgets and resources.
3743 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003744void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003745{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003746 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003747 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003748 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003749 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003750}
3751EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3752
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003753static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003754{
Liam Girdwood01005a72012-07-06 16:57:05 +01003755 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003756 struct snd_soc_dapm_widget *w;
3757 LIST_HEAD(down_list);
3758 int powerdown = 0;
3759
Liam Girdwood01005a72012-07-06 16:57:05 +01003760 mutex_lock(&card->dapm_mutex);
3761
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003762 list_for_each_entry(w, &dapm->card->widgets, list) {
3763 if (w->dapm != dapm)
3764 continue;
Mark Brown51737472009-06-22 13:16:51 +01003765 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003766 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003767 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003768 powerdown = 1;
3769 }
3770 }
3771
3772 /* If there were no widgets to power down we're already in
3773 * standby.
3774 */
3775 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003776 if (dapm->bias_level == SND_SOC_BIAS_ON)
3777 snd_soc_dapm_set_bias_level(dapm,
3778 SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003779 dapm_seq_run(dapm, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003780 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3781 snd_soc_dapm_set_bias_level(dapm,
3782 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003783 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003784
3785 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003786}
Mark Brown51737472009-06-22 13:16:51 +01003787
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003788/*
3789 * snd_soc_dapm_shutdown - callback for system shutdown
3790 */
3791void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3792{
3793 struct snd_soc_codec *codec;
3794
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003795 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003796 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003797 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3798 snd_soc_dapm_set_bias_level(&codec->dapm,
3799 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003800 }
Mark Brown51737472009-06-22 13:16:51 +01003801}
3802
Richard Purdie2b97eab2006-10-06 18:32:18 +02003803/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003804MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003805MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3806MODULE_LICENSE("GPL");