blob: 9dd2d1d63981a2fe2b1029ffc1d9c5ea34fede06 [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
Lars-Peter Clausen6b75bf02013-06-14 13:16:51 +0200507static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
508{
509 kfree(kctl->private_data);
510}
511
Stephen Warren85762e72013-03-29 15:40:10 -0600512/*
513 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
514 * create it. Either way, add the widget into the control's widget list
515 */
516static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
517 int kci, struct snd_soc_dapm_path *path)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200518{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200519 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000520 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000521 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600522 size_t prefix_len;
523 int shared;
524 struct snd_kcontrol *kcontrol;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600525 struct snd_soc_dapm_widget_list *wlist;
Stephen Warren85762e72013-03-29 15:40:10 -0600526 int wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600527 size_t wlistsize;
Stephen Warren85762e72013-03-29 15:40:10 -0600528 bool wname_in_long_name, kcname_in_long_name;
529 size_t name_len;
530 char *long_name;
531 const char *name;
532 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000533
534 if (dapm->codec)
535 prefix = dapm->codec->name_prefix;
536 else
537 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200538
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000539 if (prefix)
540 prefix_len = strlen(prefix) + 1;
541 else
542 prefix_len = 0;
543
Stephen Warren85762e72013-03-29 15:40:10 -0600544 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
545 &kcontrol);
546
547 if (kcontrol) {
548 wlist = kcontrol->private_data;
549 wlistentries = wlist->num_widgets + 1;
550 } else {
551 wlist = NULL;
552 wlistentries = 1;
553 }
554
555 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
556 wlistentries * sizeof(struct snd_soc_dapm_widget *);
557 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
558 if (wlist == NULL) {
559 dev_err(dapm->dev, "ASoC: can't allocate widget list for %s\n",
560 w->name);
561 return -ENOMEM;
562 }
563 wlist->num_widgets = wlistentries;
564 wlist->widgets[wlistentries - 1] = w;
565
566 if (!kcontrol) {
567 if (shared) {
568 wname_in_long_name = false;
569 kcname_in_long_name = true;
570 } else {
571 switch (w->id) {
572 case snd_soc_dapm_switch:
573 case snd_soc_dapm_mixer:
574 wname_in_long_name = true;
575 kcname_in_long_name = true;
576 break;
577 case snd_soc_dapm_mixer_named_ctl:
578 wname_in_long_name = false;
579 kcname_in_long_name = true;
580 break;
581 case snd_soc_dapm_mux:
582 case snd_soc_dapm_virt_mux:
583 case snd_soc_dapm_value_mux:
584 wname_in_long_name = true;
585 kcname_in_long_name = false;
586 break;
587 default:
588 kfree(wlist);
589 return -EINVAL;
590 }
591 }
592
593 if (wname_in_long_name && kcname_in_long_name) {
594 name_len = strlen(w->name) - prefix_len + 1 +
595 strlen(w->kcontrol_news[kci].name) + 1;
596
597 long_name = kmalloc(name_len, GFP_KERNEL);
598 if (long_name == NULL) {
599 kfree(wlist);
600 return -ENOMEM;
601 }
602
603 /*
604 * The control will get a prefix from the control
605 * creation process but we're also using the same
606 * prefix for widgets so cut the prefix off the
607 * front of the widget name.
608 */
609 snprintf(long_name, name_len, "%s %s",
610 w->name + prefix_len,
611 w->kcontrol_news[kci].name);
612 long_name[name_len - 1] = '\0';
613
614 name = long_name;
615 } else if (wname_in_long_name) {
616 long_name = NULL;
617 name = w->name + prefix_len;
618 } else {
619 long_name = NULL;
620 name = w->kcontrol_news[kci].name;
621 }
622
623 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], wlist, name,
624 prefix);
Lars-Peter Clausen6b75bf02013-06-14 13:16:51 +0200625 kcontrol->private_free = dapm_kcontrol_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600626 ret = snd_ctl_add(card, kcontrol);
627 if (ret < 0) {
628 dev_err(dapm->dev,
629 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
630 w->name, name, ret);
631 kfree(wlist);
632 kfree(long_name);
633 return ret;
634 }
635
636 path->long_name = long_name;
637 }
638
639 kcontrol->private_data = wlist;
640 w->kcontrols[kci] = kcontrol;
641 path->kcontrol = kcontrol;
642
643 return 0;
644}
645
646/* create new dapm mixer control */
647static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
648{
649 int i, ret;
650 struct snd_soc_dapm_path *path;
651
Richard Purdie2b97eab2006-10-06 18:32:18 +0200652 /* add kcontrol */
653 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200654 /* match name */
655 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200656 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600657 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200658 continue;
659
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200660 if (w->kcontrols[i]) {
661 path->kcontrol = w->kcontrols[i];
662 continue;
663 }
664
Stephen Warren85762e72013-03-29 15:40:10 -0600665 ret = dapm_create_or_share_mixmux_kcontrol(w, i, path);
666 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200667 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200668 }
669 }
Stephen Warren85762e72013-03-29 15:40:10 -0600670
671 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200672}
673
674/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200675static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200676{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200677 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600678 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600679 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200680
Stephen Warrenaf468002011-04-28 17:38:01 -0600681 if (w->num_kcontrols != 1) {
682 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000683 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600684 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200685 return -EINVAL;
686 }
687
Stephen Warren85762e72013-03-29 15:40:10 -0600688 path = list_first_entry(&w->sources, struct snd_soc_dapm_path,
689 list_sink);
690 if (!path) {
691 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
692 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600693 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200694
Stephen Warren85762e72013-03-29 15:40:10 -0600695 ret = dapm_create_or_share_mixmux_kcontrol(w, 0, path);
696 if (ret < 0)
697 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600698
Richard Purdie2b97eab2006-10-06 18:32:18 +0200699 list_for_each_entry(path, &w->sources, list_sink)
Stephen Warren85762e72013-03-29 15:40:10 -0600700 path->kcontrol = w->kcontrols[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200701
Stephen Warrenaf468002011-04-28 17:38:01 -0600702 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200703}
704
705/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200706static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200707{
Mark Browna6c65732010-03-03 17:45:21 +0000708 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200709 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000710 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200711
Mark Browna6c65732010-03-03 17:45:21 +0000712 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200713}
714
715/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100716static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
717 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200718{
719 struct snd_soc_dapm_path *p;
720
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100721 list_for_each_entry(p, sink, list_source) {
722 if (p->walked) {
723 p->walked = 0;
724 dapm_clear_walk_output(dapm, &p->sink->sinks);
725 }
726 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200727}
728
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100729static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
730 struct list_head *source)
731{
732 struct snd_soc_dapm_path *p;
733
734 list_for_each_entry(p, source, list_sink) {
735 if (p->walked) {
736 p->walked = 0;
737 dapm_clear_walk_input(dapm, &p->source->sources);
738 }
739 }
740}
741
742
Mark Brown99497882010-05-07 20:24:05 +0100743/* We implement power down on suspend by checking the power state of
744 * the ALSA card - when we are suspending the ALSA state for the card
745 * is set to D3.
746 */
747static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
748{
Mark Brown12ea2c72011-03-02 18:17:32 +0000749 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100750
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000751 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100752 case SNDRV_CTL_POWER_D3hot:
753 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100754 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000755 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200756 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100757 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100758 default:
759 return 1;
760 }
761}
762
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100763/* add widget to list if it's not already in the list */
764static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
765 struct snd_soc_dapm_widget *w)
766{
767 struct snd_soc_dapm_widget_list *wlist;
768 int wlistsize, wlistentries, i;
769
770 if (*list == NULL)
771 return -EINVAL;
772
773 wlist = *list;
774
775 /* is this widget already in the list */
776 for (i = 0; i < wlist->num_widgets; i++) {
777 if (wlist->widgets[i] == w)
778 return 0;
779 }
780
781 /* allocate some new space */
782 wlistentries = wlist->num_widgets + 1;
783 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
784 wlistentries * sizeof(struct snd_soc_dapm_widget *);
785 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
786 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000787 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100788 w->name);
789 return -ENOMEM;
790 }
791 wlist = *list;
792
793 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000794 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100795 w->name, wlist->num_widgets);
796
797 wlist->widgets[wlist->num_widgets] = w;
798 wlist->num_widgets++;
799 return 1;
800}
801
Richard Purdie2b97eab2006-10-06 18:32:18 +0200802/*
803 * Recursively check for a completed path to an active or physically connected
804 * output widget. Returns number of complete paths.
805 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100806static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
807 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200808{
809 struct snd_soc_dapm_path *path;
810 int con = 0;
811
Mark Brown024dc072011-10-09 11:52:05 +0100812 if (widget->outputs >= 0)
813 return widget->outputs;
814
Mark Brownde02d072011-09-20 21:43:24 +0100815 DAPM_UPDATE_STAT(widget, path_checks);
816
Mark Brown62ea8742012-01-21 21:14:48 +0000817 switch (widget->id) {
818 case snd_soc_dapm_supply:
819 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200820 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100821 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000822 default:
823 break;
824 }
Mark Brown246d0a12009-04-22 18:24:55 +0100825
Mark Brown010ff262009-08-17 17:39:22 +0100826 switch (widget->id) {
827 case snd_soc_dapm_adc:
828 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800829 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100830 if (widget->active) {
831 widget->outputs = snd_soc_dapm_suspend_check(widget);
832 return widget->outputs;
833 }
Mark Brown010ff262009-08-17 17:39:22 +0100834 default:
835 break;
836 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200837
838 if (widget->connected) {
839 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100840 if (widget->id == snd_soc_dapm_output && !widget->ext) {
841 widget->outputs = snd_soc_dapm_suspend_check(widget);
842 return widget->outputs;
843 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200844
845 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100846 if (widget->id == snd_soc_dapm_hp ||
847 widget->id == snd_soc_dapm_spk ||
848 (widget->id == snd_soc_dapm_line &&
849 !list_empty(&widget->sources))) {
850 widget->outputs = snd_soc_dapm_suspend_check(widget);
851 return widget->outputs;
852 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200853 }
854
855 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100856 DAPM_UPDATE_STAT(widget, neighbour_checks);
857
Mark Brownbf3a9e12011-06-13 16:42:29 +0100858 if (path->weak)
859 continue;
860
Mark Brown8af294b2013-02-22 17:48:15 +0000861 if (path->walking)
862 return 1;
863
Richard Purdie2b97eab2006-10-06 18:32:18 +0200864 if (path->walked)
865 continue;
866
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100867 trace_snd_soc_dapm_output_path(widget, path);
868
Richard Purdie2b97eab2006-10-06 18:32:18 +0200869 if (path->sink && path->connect) {
870 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000871 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100872
873 /* do we need to add this widget to the list ? */
874 if (list) {
875 int err;
876 err = dapm_list_add_widget(list, path->sink);
877 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000878 dev_err(widget->dapm->dev,
879 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100880 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000881 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100882 return con;
883 }
884 }
885
886 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000887
888 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200889 }
890 }
891
Mark Brown024dc072011-10-09 11:52:05 +0100892 widget->outputs = con;
893
Richard Purdie2b97eab2006-10-06 18:32:18 +0200894 return con;
895}
896
897/*
898 * Recursively check for a completed path to an active or physically connected
899 * input widget. Returns number of complete paths.
900 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100901static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
902 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200903{
904 struct snd_soc_dapm_path *path;
905 int con = 0;
906
Mark Brown024dc072011-10-09 11:52:05 +0100907 if (widget->inputs >= 0)
908 return widget->inputs;
909
Mark Brownde02d072011-09-20 21:43:24 +0100910 DAPM_UPDATE_STAT(widget, path_checks);
911
Mark Brown62ea8742012-01-21 21:14:48 +0000912 switch (widget->id) {
913 case snd_soc_dapm_supply:
914 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200915 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100916 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000917 default:
918 break;
919 }
Mark Brown246d0a12009-04-22 18:24:55 +0100920
Richard Purdie2b97eab2006-10-06 18:32:18 +0200921 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100922 switch (widget->id) {
923 case snd_soc_dapm_dac:
924 case snd_soc_dapm_aif_in:
Mark Brown888df392012-02-16 19:37:51 -0800925 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100926 if (widget->active) {
927 widget->inputs = snd_soc_dapm_suspend_check(widget);
928 return widget->inputs;
929 }
Mark Brown010ff262009-08-17 17:39:22 +0100930 default:
931 break;
932 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200933
934 if (widget->connected) {
935 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100936 if (widget->id == snd_soc_dapm_input && !widget->ext) {
937 widget->inputs = snd_soc_dapm_suspend_check(widget);
938 return widget->inputs;
939 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200940
941 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100942 if (widget->id == snd_soc_dapm_vmid) {
943 widget->inputs = snd_soc_dapm_suspend_check(widget);
944 return widget->inputs;
945 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200946
947 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300948 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100949 (widget->id == snd_soc_dapm_line &&
950 !list_empty(&widget->sinks))) {
951 widget->inputs = snd_soc_dapm_suspend_check(widget);
952 return widget->inputs;
953 }
954
Mark Brown1ab97c82011-11-27 16:21:51 +0000955 /* signal generator */
956 if (widget->id == snd_soc_dapm_siggen) {
957 widget->inputs = snd_soc_dapm_suspend_check(widget);
958 return widget->inputs;
959 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200960 }
961
962 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +0100963 DAPM_UPDATE_STAT(widget, neighbour_checks);
964
Mark Brownbf3a9e12011-06-13 16:42:29 +0100965 if (path->weak)
966 continue;
967
Mark Brown8af294b2013-02-22 17:48:15 +0000968 if (path->walking)
969 return 1;
970
Richard Purdie2b97eab2006-10-06 18:32:18 +0200971 if (path->walked)
972 continue;
973
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100974 trace_snd_soc_dapm_input_path(widget, path);
975
Richard Purdie2b97eab2006-10-06 18:32:18 +0200976 if (path->source && path->connect) {
977 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000978 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100979
980 /* do we need to add this widget to the list ? */
981 if (list) {
982 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +0100983 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100984 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000985 dev_err(widget->dapm->dev,
986 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100987 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000988 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100989 return con;
990 }
991 }
992
993 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000994
995 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200996 }
997 }
998
Mark Brown024dc072011-10-09 11:52:05 +0100999 widget->inputs = con;
1000
Richard Purdie2b97eab2006-10-06 18:32:18 +02001001 return con;
1002}
1003
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001004/**
1005 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1006 * @dai: the soc DAI.
1007 * @stream: stream direction.
1008 * @list: list of active widgets for this stream.
1009 *
1010 * Queries DAPM graph as to whether an valid audio stream path exists for
1011 * the initial stream specified by name. This takes into account
1012 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1013 *
1014 * Returns the number of valid paths or negative error.
1015 */
1016int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1017 struct snd_soc_dapm_widget_list **list)
1018{
1019 struct snd_soc_card *card = dai->card;
1020 int paths;
1021
1022 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1023 dapm_reset(card);
1024
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001025 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001026 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001027 dapm_clear_walk_output(&card->dapm,
1028 &dai->playback_widget->sinks);
1029 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001030 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001031 dapm_clear_walk_input(&card->dapm,
1032 &dai->capture_widget->sources);
1033 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001034
1035 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001036 mutex_unlock(&card->dapm_mutex);
1037
1038 return paths;
1039}
1040
Richard Purdie2b97eab2006-10-06 18:32:18 +02001041/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001042 * Handler for generic register modifier widget.
1043 */
1044int dapm_reg_event(struct snd_soc_dapm_widget *w,
1045 struct snd_kcontrol *kcontrol, int event)
1046{
1047 unsigned int val;
1048
1049 if (SND_SOC_DAPM_EVENT_ON(event))
1050 val = w->on_val;
1051 else
1052 val = w->off_val;
1053
Liam Girdwood49575fb52012-03-06 18:16:19 +00001054 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001055 w->mask << w->shift, val << w->shift);
1056
1057 return 0;
1058}
Mark Brown11589412008-07-29 11:42:23 +01001059EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001060
Mark Brown62ea8742012-01-21 21:14:48 +00001061/*
1062 * Handler for regulator supply widget.
1063 */
1064int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1065 struct snd_kcontrol *kcontrol, int event)
1066{
Mark Brownc05b84d2012-09-07 12:57:11 +08001067 int ret;
1068
1069 if (SND_SOC_DAPM_EVENT_ON(event)) {
1070 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001071 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001072 if (ret != 0)
1073 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001074 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001075 w->name, ret);
1076 }
1077
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001078 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001079 } else {
1080 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001081 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001082 if (ret != 0)
1083 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001084 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001085 w->name, ret);
1086 }
1087
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001088 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001089 }
Mark Brown62ea8742012-01-21 21:14:48 +00001090}
1091EXPORT_SYMBOL_GPL(dapm_regulator_event);
1092
Ola Liljad7e7eb92012-05-24 15:26:25 +02001093/*
1094 * Handler for clock supply widget.
1095 */
1096int dapm_clock_event(struct snd_soc_dapm_widget *w,
1097 struct snd_kcontrol *kcontrol, int event)
1098{
1099 if (!w->clk)
1100 return -EIO;
1101
Mark Brownec029952012-06-04 08:16:20 +01001102#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001103 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001104 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001105 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001106 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001107 return 0;
1108 }
Mark Brownec029952012-06-04 08:16:20 +01001109#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001110 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001111}
1112EXPORT_SYMBOL_GPL(dapm_clock_event);
1113
Mark Brownd8050022011-09-28 18:28:23 +01001114static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1115{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001116 if (w->power_checked)
1117 return w->new_power;
1118
Mark Brownd8050022011-09-28 18:28:23 +01001119 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001120 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001121 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001122 w->new_power = w->power_check(w);
1123
1124 w->power_checked = true;
1125
1126 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001127}
1128
Mark Browncd0f2d42009-04-20 16:56:59 +01001129/* Generic check to see if a widget should be powered.
1130 */
1131static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1132{
1133 int in, out;
1134
Mark Brownde02d072011-09-20 21:43:24 +01001135 DAPM_UPDATE_STAT(w, power_checks);
1136
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001137 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001138 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001139 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001140 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001141 return out != 0 && in != 0;
1142}
1143
Mark Brown888df392012-02-16 19:37:51 -08001144static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
1145{
1146 DAPM_UPDATE_STAT(w, power_checks);
1147
Mark Brown1eee1b32012-04-10 13:57:46 +01001148 if (w->active)
1149 return w->active;
1150
1151 return dapm_generic_check_power(w);
Mark Brown888df392012-02-16 19:37:51 -08001152}
1153
Mark Brown6ea31b92009-04-20 17:15:41 +01001154/* Check to see if an ADC has power */
1155static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1156{
1157 int in;
1158
Mark Brownde02d072011-09-20 21:43:24 +01001159 DAPM_UPDATE_STAT(w, power_checks);
1160
Mark Brown6ea31b92009-04-20 17:15:41 +01001161 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001162 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001163 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001164 return in != 0;
1165 } else {
1166 return dapm_generic_check_power(w);
1167 }
1168}
1169
1170/* Check to see if a DAC has power */
1171static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1172{
1173 int out;
1174
Mark Brownde02d072011-09-20 21:43:24 +01001175 DAPM_UPDATE_STAT(w, power_checks);
1176
Mark Brown6ea31b92009-04-20 17:15:41 +01001177 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001178 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001179 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001180 return out != 0;
1181 } else {
1182 return dapm_generic_check_power(w);
1183 }
1184}
1185
Mark Brown246d0a12009-04-22 18:24:55 +01001186/* Check to see if a power supply is needed */
1187static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1188{
1189 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001190
Mark Brownde02d072011-09-20 21:43:24 +01001191 DAPM_UPDATE_STAT(w, power_checks);
1192
Mark Brown246d0a12009-04-22 18:24:55 +01001193 /* Check if one of our outputs is connected */
1194 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001195 DAPM_UPDATE_STAT(w, neighbour_checks);
1196
Mark Brownbf3a9e12011-06-13 16:42:29 +01001197 if (path->weak)
1198 continue;
1199
Mark Brown215edda2009-09-08 18:59:05 +01001200 if (path->connected &&
1201 !path->connected(path->source, path->sink))
1202 continue;
1203
Mark Brown30173582011-02-11 11:42:19 +00001204 if (!path->sink)
1205 continue;
1206
Mark Brownf68d7e12011-10-04 22:57:50 +01001207 if (dapm_widget_power_check(path->sink))
1208 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001209 }
1210
Mark Brownf68d7e12011-10-04 22:57:50 +01001211 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001212}
1213
Mark Brown35c64bc2011-09-28 18:23:53 +01001214static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1215{
1216 return 1;
1217}
1218
Mark Brown38357ab2009-06-06 19:03:23 +01001219static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1220 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001221 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001222{
Mark Brown828a8422011-01-15 13:14:30 +00001223 int *sort;
1224
1225 if (power_up)
1226 sort = dapm_up_seq;
1227 else
1228 sort = dapm_down_seq;
1229
Mark Brown38357ab2009-06-06 19:03:23 +01001230 if (sort[a->id] != sort[b->id])
1231 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001232 if (a->subseq != b->subseq) {
1233 if (power_up)
1234 return a->subseq - b->subseq;
1235 else
1236 return b->subseq - a->subseq;
1237 }
Mark Brownb22ead22009-06-07 12:51:26 +01001238 if (a->reg != b->reg)
1239 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001240 if (a->dapm != b->dapm)
1241 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001242
Mark Brown38357ab2009-06-06 19:03:23 +01001243 return 0;
1244}
Mark Brown42aa3412009-03-01 19:21:10 +00001245
Mark Brown38357ab2009-06-06 19:03:23 +01001246/* Insert a widget in order into a DAPM power sequence. */
1247static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1248 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001249 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001250{
1251 struct snd_soc_dapm_widget *w;
1252
1253 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001254 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001255 list_add_tail(&new_widget->power_list, &w->power_list);
1256 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001257 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001258
Mark Brown38357ab2009-06-06 19:03:23 +01001259 list_add_tail(&new_widget->power_list, list);
1260}
Mark Brown42aa3412009-03-01 19:21:10 +00001261
Mark Brown68f89ad2010-11-03 23:51:49 -04001262static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1263 struct snd_soc_dapm_widget *w, int event)
1264{
1265 struct snd_soc_card *card = dapm->card;
1266 const char *ev_name;
1267 int power, ret;
1268
1269 switch (event) {
1270 case SND_SOC_DAPM_PRE_PMU:
1271 ev_name = "PRE_PMU";
1272 power = 1;
1273 break;
1274 case SND_SOC_DAPM_POST_PMU:
1275 ev_name = "POST_PMU";
1276 power = 1;
1277 break;
1278 case SND_SOC_DAPM_PRE_PMD:
1279 ev_name = "PRE_PMD";
1280 power = 0;
1281 break;
1282 case SND_SOC_DAPM_POST_PMD:
1283 ev_name = "POST_PMD";
1284 power = 0;
1285 break;
1286 default:
1287 BUG();
1288 return;
1289 }
1290
1291 if (w->power != power)
1292 return;
1293
1294 if (w->event && (w->event_flags & event)) {
1295 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1296 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001297 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001298 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001299 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001300 if (ret < 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001301 dev_err(dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001302 ev_name, w->name, ret);
1303 }
1304}
1305
Mark Brownb22ead22009-06-07 12:51:26 +01001306/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001307static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001308 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001309{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001310 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001311 struct snd_soc_dapm_widget *w;
1312 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001313 unsigned int value = 0;
1314 unsigned int mask = 0;
1315 unsigned int cur_mask;
1316
1317 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1318 power_list)->reg;
1319
1320 list_for_each_entry(w, pending, power_list) {
1321 cur_mask = 1 << w->shift;
1322 BUG_ON(reg != w->reg);
1323
1324 if (w->invert)
1325 power = !w->power;
1326 else
1327 power = w->power;
1328
1329 mask |= cur_mask;
1330 if (power)
1331 value |= cur_mask;
1332
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001333 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001334 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1335 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001336
Mark Brown68f89ad2010-11-03 23:51:49 -04001337 /* Check for events */
1338 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1339 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001340 }
1341
Mark Brown81628102009-06-07 13:21:24 +01001342 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001343 /* Any widget will do, they should all be updating the
1344 * same register.
1345 */
1346 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1347 power_list);
1348
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001349 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001350 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001351 value, mask, reg, card->pop_time);
1352 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001353 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001354 }
1355
1356 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001357 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1358 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001359 }
Mark Brown42aa3412009-03-01 19:21:10 +00001360}
1361
Mark Brownb22ead22009-06-07 12:51:26 +01001362/* Apply a DAPM power sequence.
1363 *
1364 * We walk over a pre-sorted list of widgets to apply power to. In
1365 * order to minimise the number of writes to the device required
1366 * multiple widgets will be updated in a single write where possible.
1367 * Currently anything that requires more than a single write is not
1368 * handled.
1369 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001370static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001371 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001372{
1373 struct snd_soc_dapm_widget *w, *n;
1374 LIST_HEAD(pending);
1375 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001376 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001377 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001378 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001379 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001380 int *sort;
1381
1382 if (power_up)
1383 sort = dapm_up_seq;
1384 else
1385 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001386
Mark Brownb22ead22009-06-07 12:51:26 +01001387 list_for_each_entry_safe(w, n, list, power_list) {
1388 ret = 0;
1389
1390 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001391 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001392 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001393 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001394 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001395
Mark Brown474b62d2011-01-18 16:14:44 +00001396 if (cur_dapm && cur_dapm->seq_notifier) {
1397 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1398 if (sort[i] == cur_sort)
1399 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001400 i,
1401 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001402 }
1403
Mark Brownb22ead22009-06-07 12:51:26 +01001404 INIT_LIST_HEAD(&pending);
1405 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001406 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001407 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001408 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001409 }
1410
Mark Brown163cac02009-06-07 10:12:52 +01001411 switch (w->id) {
1412 case snd_soc_dapm_pre:
1413 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001414 list_for_each_entry_safe_continue(w, n, list,
1415 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001416
Mark Brownb22ead22009-06-07 12:51:26 +01001417 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001418 ret = w->event(w,
1419 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001420 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001421 ret = w->event(w,
1422 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001423 break;
1424
1425 case snd_soc_dapm_post:
1426 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001427 list_for_each_entry_safe_continue(w, n, list,
1428 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001429
Mark Brownb22ead22009-06-07 12:51:26 +01001430 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001431 ret = w->event(w,
1432 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001433 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001434 ret = w->event(w,
1435 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001436 break;
1437
Mark Brown163cac02009-06-07 10:12:52 +01001438 default:
Mark Brown81628102009-06-07 13:21:24 +01001439 /* Queue it up for application */
1440 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001441 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001442 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001443 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001444 list_move(&w->power_list, &pending);
1445 break;
Mark Brown163cac02009-06-07 10:12:52 +01001446 }
Mark Brownb22ead22009-06-07 12:51:26 +01001447
1448 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001449 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001450 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001451 }
Mark Brownb22ead22009-06-07 12:51:26 +01001452
1453 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001454 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001455
1456 if (cur_dapm && cur_dapm->seq_notifier) {
1457 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1458 if (sort[i] == cur_sort)
1459 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001460 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001461 }
Mark Brown163cac02009-06-07 10:12:52 +01001462}
1463
Mark Brown97404f22010-12-14 16:13:57 +00001464static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1465{
1466 struct snd_soc_dapm_update *update = dapm->update;
1467 struct snd_soc_dapm_widget *w;
1468 int ret;
1469
1470 if (!update)
1471 return;
1472
1473 w = update->widget;
1474
1475 if (w->event &&
1476 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1477 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1478 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001479 dev_err(dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Mark Brown97404f22010-12-14 16:13:57 +00001480 w->name, ret);
1481 }
1482
Liam Girdwood49575fb52012-03-06 18:16:19 +00001483 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001484 update->val);
1485 if (ret < 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001486 dev_err(dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1487 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001488
1489 if (w->event &&
1490 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1491 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1492 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001493 dev_err(dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Mark Brown97404f22010-12-14 16:13:57 +00001494 w->name, ret);
1495 }
1496}
1497
Mark Brown9d0624a2011-02-18 11:49:43 -08001498/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1499 * they're changing state.
1500 */
1501static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1502{
1503 struct snd_soc_dapm_context *d = data;
1504 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001505
Mark Brown56fba412011-06-04 11:25:10 +01001506 /* If we're off and we're not supposed to be go into STANDBY */
1507 if (d->bias_level == SND_SOC_BIAS_OFF &&
1508 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001509 if (d->dev)
1510 pm_runtime_get_sync(d->dev);
1511
Mark Brown9d0624a2011-02-18 11:49:43 -08001512 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1513 if (ret != 0)
1514 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001515 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001516 }
1517
Mark Brown56fba412011-06-04 11:25:10 +01001518 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1519 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001520 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1521 if (ret != 0)
1522 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001523 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001524 }
1525}
1526
1527/* Async callback run prior to DAPM sequences - brings to their final
1528 * state.
1529 */
1530static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1531{
1532 struct snd_soc_dapm_context *d = data;
1533 int ret;
1534
1535 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001536 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1537 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1538 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001539 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1540 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001541 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001542 ret);
1543 }
1544
1545 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001546 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1547 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001548 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1549 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001550 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1551 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001552
1553 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001554 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001555 }
1556
1557 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001558 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1559 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001560 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1561 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001562 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001563 ret);
1564 }
1565}
Mark Brown97404f22010-12-14 16:13:57 +00001566
Mark Brownfe4fda52011-10-03 22:36:57 +01001567static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1568 bool power, bool connect)
1569{
1570 /* If a connection is being made or broken then that update
1571 * will have marked the peer dirty, otherwise the widgets are
1572 * not connected and this update has no impact. */
1573 if (!connect)
1574 return;
1575
1576 /* If the peer is already in the state we're moving to then we
1577 * won't have an impact on it. */
1578 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001579 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001580}
1581
Mark Brown05623c42011-09-28 17:02:31 +01001582static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1583 struct list_head *up_list,
1584 struct list_head *down_list)
1585{
Mark Browndb432b42011-10-03 21:06:40 +01001586 struct snd_soc_dapm_path *path;
1587
Mark Brown05623c42011-09-28 17:02:31 +01001588 if (w->power == power)
1589 return;
1590
1591 trace_snd_soc_dapm_widget_power(w, power);
1592
Mark Browndb432b42011-10-03 21:06:40 +01001593 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001594 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001595 */
1596 list_for_each_entry(path, &w->sources, list_sink) {
1597 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001598 dapm_widget_set_peer_power(path->source, power,
1599 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001600 }
1601 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001602 switch (w->id) {
1603 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001604 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001605 case snd_soc_dapm_clock_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001606 /* Supplies can't affect their outputs, only their inputs */
1607 break;
1608 default:
1609 list_for_each_entry(path, &w->sinks, list_source) {
1610 if (path->sink) {
1611 dapm_widget_set_peer_power(path->sink, power,
1612 path->connect);
1613 }
Mark Browndb432b42011-10-03 21:06:40 +01001614 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001615 break;
Mark Browndb432b42011-10-03 21:06:40 +01001616 }
1617
Mark Brown05623c42011-09-28 17:02:31 +01001618 if (power)
1619 dapm_seq_insert(w, up_list, true);
1620 else
1621 dapm_seq_insert(w, down_list, false);
1622
1623 w->power = power;
1624}
1625
Mark Brown7c81beb2011-09-20 22:22:32 +01001626static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1627 struct list_head *up_list,
1628 struct list_head *down_list)
1629{
Mark Brown7c81beb2011-09-20 22:22:32 +01001630 int power;
1631
1632 switch (w->id) {
1633 case snd_soc_dapm_pre:
1634 dapm_seq_insert(w, down_list, false);
1635 break;
1636 case snd_soc_dapm_post:
1637 dapm_seq_insert(w, up_list, true);
1638 break;
1639
1640 default:
Mark Brownd8050022011-09-28 18:28:23 +01001641 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001642
Mark Brown05623c42011-09-28 17:02:31 +01001643 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001644 break;
1645 }
1646}
1647
Mark Brown42aa3412009-03-01 19:21:10 +00001648/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001649 * Scan each dapm widget for complete audio path.
1650 * A complete path is a route that has valid endpoints i.e.:-
1651 *
1652 * o DAC to output pin.
1653 * o Input Pin to ADC.
1654 * o Input pin to Output pin (bypass, sidetone)
1655 * o DAC to ADC (loopback).
1656 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001657static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001658{
Mark Brown12ea2c72011-03-02 18:17:32 +00001659 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001660 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001661 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001662 LIST_HEAD(up_list);
1663 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001664 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001665 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001666
Mark Brown84e90932010-11-04 00:07:02 -04001667 trace_snd_soc_dapm_start(card);
1668
Mark Brown56fba412011-06-04 11:25:10 +01001669 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001670 if (d->idle_bias_off)
1671 d->target_bias_level = SND_SOC_BIAS_OFF;
1672 else
1673 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001674 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001675
Liam Girdwood6c120e12012-02-15 15:15:34 +00001676 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001677
Mark Brown6d3ddc82009-05-16 17:47:29 +01001678 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001679 * lists indicating if they should be powered up or down. We
1680 * only check widgets that have been flagged as dirty but note
1681 * that new widgets may be added to the dirty list while we
1682 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001683 */
Mark Browndb432b42011-10-03 21:06:40 +01001684 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001685 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001686 }
1687
Mark Brownf9de6d72011-09-28 17:19:47 +01001688 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001689 switch (w->id) {
1690 case snd_soc_dapm_pre:
1691 case snd_soc_dapm_post:
1692 /* These widgets always need to be powered */
1693 break;
1694 default:
1695 list_del_init(&w->dirty);
1696 break;
1697 }
Mark Browndb432b42011-10-03 21:06:40 +01001698
Mark Brownf9de6d72011-09-28 17:19:47 +01001699 if (w->power) {
1700 d = w->dapm;
1701
1702 /* Supplies and micbiases only bring the
1703 * context up to STANDBY as unless something
1704 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001705 * generally don't require full power. Signal
1706 * generators are virtual pins and have no
1707 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001708 */
1709 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001710 case snd_soc_dapm_siggen:
1711 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001712 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001713 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001714 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001715 case snd_soc_dapm_micbias:
1716 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1717 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1718 break;
1719 default:
1720 d->target_bias_level = SND_SOC_BIAS_ON;
1721 break;
1722 }
1723 }
1724
1725 }
1726
Mark Brown85a843c2011-09-21 21:29:47 +01001727 /* Force all contexts in the card to the same bias state if
1728 * they're not ground referenced.
1729 */
Mark Brown56fba412011-06-04 11:25:10 +01001730 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001731 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001732 if (d->target_bias_level > bias)
1733 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001734 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001735 if (!d->idle_bias_off)
1736 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001737
Mark Brownde02d072011-09-20 21:43:24 +01001738 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001739
Mark Brown9d0624a2011-02-18 11:49:43 -08001740 /* Run all the bias changes in parallel */
1741 list_for_each_entry(d, &dapm->card->dapm_list, list)
1742 async_schedule_domain(dapm_pre_sequence_async, d,
1743 &async_domain);
1744 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001745
Mark Brown6d3ddc82009-05-16 17:47:29 +01001746 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001747 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001748
Mark Brown97404f22010-12-14 16:13:57 +00001749 dapm_widget_update(dapm);
1750
Mark Brown6d3ddc82009-05-16 17:47:29 +01001751 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001752 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001753
Mark Brown9d0624a2011-02-18 11:49:43 -08001754 /* Run all the bias changes in parallel */
1755 list_for_each_entry(d, &dapm->card->dapm_list, list)
1756 async_schedule_domain(dapm_post_sequence_async, d,
1757 &async_domain);
1758 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001759
Liam Girdwood8078d872012-02-15 15:15:35 +00001760 /* do we need to notify any clients that DAPM event is complete */
1761 list_for_each_entry(d, &card->dapm_list, list) {
1762 if (d->stream_event)
1763 d->stream_event(d, event);
1764 }
1765
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001766 pop_dbg(dapm->dev, card->pop_time,
1767 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001768 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001769
Mark Brown84e90932010-11-04 00:07:02 -04001770 trace_snd_soc_dapm_done(card);
1771
Mark Brown42aa3412009-03-01 19:21:10 +00001772 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001773}
1774
Mark Brown79fb9382009-08-21 16:38:13 +01001775#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001776static ssize_t dapm_widget_power_read_file(struct file *file,
1777 char __user *user_buf,
1778 size_t count, loff_t *ppos)
1779{
1780 struct snd_soc_dapm_widget *w = file->private_data;
1781 char *buf;
1782 int in, out;
1783 ssize_t ret;
1784 struct snd_soc_dapm_path *p = NULL;
1785
1786 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1787 if (!buf)
1788 return -ENOMEM;
1789
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001790 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001791 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001792 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001793 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001794
Mark Brownf13ebad2012-03-03 18:01:01 +00001795 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1796 w->name, w->power ? "On" : "Off",
1797 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001798
Mark Brownd033c362009-12-04 15:25:56 +00001799 if (w->reg >= 0)
1800 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1801 " - R%d(0x%x) bit %d",
1802 w->reg, w->reg, w->shift);
1803
1804 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1805
Mark Brown3eef08b2009-09-14 16:49:00 +01001806 if (w->sname)
1807 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1808 w->sname,
1809 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001810
1811 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001812 if (p->connected && !p->connected(w, p->sink))
1813 continue;
1814
Mark Brown79fb9382009-08-21 16:38:13 +01001815 if (p->connect)
1816 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001817 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001818 p->name ? p->name : "static",
1819 p->source->name);
1820 }
1821 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001822 if (p->connected && !p->connected(w, p->sink))
1823 continue;
1824
Mark Brown79fb9382009-08-21 16:38:13 +01001825 if (p->connect)
1826 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001827 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001828 p->name ? p->name : "static",
1829 p->sink->name);
1830 }
1831
1832 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1833
1834 kfree(buf);
1835 return ret;
1836}
1837
1838static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001839 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001840 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001841 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001842};
1843
Mark Brownef49e4f2011-04-04 20:48:13 +09001844static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1845 size_t count, loff_t *ppos)
1846{
1847 struct snd_soc_dapm_context *dapm = file->private_data;
1848 char *level;
1849
1850 switch (dapm->bias_level) {
1851 case SND_SOC_BIAS_ON:
1852 level = "On\n";
1853 break;
1854 case SND_SOC_BIAS_PREPARE:
1855 level = "Prepare\n";
1856 break;
1857 case SND_SOC_BIAS_STANDBY:
1858 level = "Standby\n";
1859 break;
1860 case SND_SOC_BIAS_OFF:
1861 level = "Off\n";
1862 break;
1863 default:
1864 BUG();
1865 level = "Unknown\n";
1866 break;
1867 }
1868
1869 return simple_read_from_buffer(user_buf, count, ppos, level,
1870 strlen(level));
1871}
1872
1873static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001874 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001875 .read = dapm_bias_read_file,
1876 .llseek = default_llseek,
1877};
1878
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001879void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1880 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001881{
Mark Brown79fb9382009-08-21 16:38:13 +01001882 struct dentry *d;
1883
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001884 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1885
1886 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001887 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001888 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001889 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001890 }
Mark Brown79fb9382009-08-21 16:38:13 +01001891
Mark Brownef49e4f2011-04-04 20:48:13 +09001892 d = debugfs_create_file("bias_level", 0444,
1893 dapm->debugfs_dapm, dapm,
1894 &dapm_bias_fops);
1895 if (!d)
1896 dev_warn(dapm->dev,
1897 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001898}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001899
1900static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1901{
1902 struct snd_soc_dapm_context *dapm = w->dapm;
1903 struct dentry *d;
1904
1905 if (!dapm->debugfs_dapm || !w->name)
1906 return;
1907
1908 d = debugfs_create_file(w->name, 0444,
1909 dapm->debugfs_dapm, w,
1910 &dapm_widget_power_fops);
1911 if (!d)
1912 dev_warn(w->dapm->dev,
1913 "ASoC: Failed to create %s debugfs file\n",
1914 w->name);
1915}
1916
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001917static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1918{
1919 debugfs_remove_recursive(dapm->debugfs_dapm);
1920}
1921
Mark Brown79fb9382009-08-21 16:38:13 +01001922#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001923void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1924 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001925{
1926}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001927
1928static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1929{
1930}
1931
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001932static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1933{
1934}
1935
Mark Brown79fb9382009-08-21 16:38:13 +01001936#endif
1937
Richard Purdie2b97eab2006-10-06 18:32:18 +02001938/* test and update the power status of a mux widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001939static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001940 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001941{
1942 struct snd_soc_dapm_path *path;
1943 int found = 0;
1944
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001945 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001946 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001947 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001948 return -ENODEV;
1949
Richard Purdie2b97eab2006-10-06 18:32:18 +02001950 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001951 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001952 if (path->kcontrol != kcontrol)
1953 continue;
1954
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001955 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001956 continue;
1957
1958 found = 1;
1959 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001960 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001961 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001962 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001963 } else {
1964 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001965 dapm_mark_dirty(path->source,
1966 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001967 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001968 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001969 }
1970
Mark Browndb432b42011-10-03 21:06:40 +01001971 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001972 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001973 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001974 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001975
Liam Girdwood618dae12012-04-25 12:12:51 +01001976 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001977}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001978
1979int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1980 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1981{
1982 struct snd_soc_card *card = widget->dapm->card;
1983 int ret;
1984
Liam Girdwood3cd04342012-03-09 12:02:08 +00001985 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001986 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1987 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001988 if (ret > 0)
1989 soc_dpcm_runtime_update(widget);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001990 return ret;
1991}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001992EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001993
Milan plzik1b075e32008-01-10 14:39:46 +01001994/* test and update the power status of a mixer or switch widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001995static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001996 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001997{
1998 struct snd_soc_dapm_path *path;
1999 int found = 0;
2000
Milan plzik1b075e32008-01-10 14:39:46 +01002001 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002002 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01002003 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002004 return -ENODEV;
2005
Richard Purdie2b97eab2006-10-06 18:32:18 +02002006 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002007 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002008 if (path->kcontrol != kcontrol)
2009 continue;
2010
2011 /* found, now check type */
2012 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002013 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002014 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002015 }
2016
Mark Browndb432b42011-10-03 21:06:40 +01002017 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01002018 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002019 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01002020 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002021
Liam Girdwood618dae12012-04-25 12:12:51 +01002022 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002023}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002024
2025int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
2026 struct snd_kcontrol *kcontrol, int connect)
2027{
2028 struct snd_soc_card *card = widget->dapm->card;
2029 int ret;
2030
Liam Girdwood3cd04342012-03-09 12:02:08 +00002031 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002032 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
2033 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002034 if (ret > 0)
2035 soc_dpcm_runtime_update(widget);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002036 return ret;
2037}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002038EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002039
2040/* show dapm widget status in sys fs */
2041static ssize_t dapm_widget_show(struct device *dev,
2042 struct device_attribute *attr, char *buf)
2043{
Mark Brown36ae1a92012-01-06 17:12:45 -08002044 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002045 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002046 struct snd_soc_dapm_widget *w;
2047 int count = 0;
2048 char *state = "not set";
2049
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002050 list_for_each_entry(w, &codec->card->widgets, list) {
2051 if (w->dapm != &codec->dapm)
2052 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002053
2054 /* only display widgets that burnm power */
2055 switch (w->id) {
2056 case snd_soc_dapm_hp:
2057 case snd_soc_dapm_mic:
2058 case snd_soc_dapm_spk:
2059 case snd_soc_dapm_line:
2060 case snd_soc_dapm_micbias:
2061 case snd_soc_dapm_dac:
2062 case snd_soc_dapm_adc:
2063 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002064 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002065 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002066 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002067 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002068 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002069 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002070 if (w->name)
2071 count += sprintf(buf + count, "%s: %s\n",
2072 w->name, w->power ? "On":"Off");
2073 break;
2074 default:
2075 break;
2076 }
2077 }
2078
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002079 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002080 case SND_SOC_BIAS_ON:
2081 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002082 break;
Mark Brown0be98982008-05-19 12:31:28 +02002083 case SND_SOC_BIAS_PREPARE:
2084 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002085 break;
Mark Brown0be98982008-05-19 12:31:28 +02002086 case SND_SOC_BIAS_STANDBY:
2087 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002088 break;
Mark Brown0be98982008-05-19 12:31:28 +02002089 case SND_SOC_BIAS_OFF:
2090 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002091 break;
2092 }
2093 count += sprintf(buf + count, "PM State: %s\n", state);
2094
2095 return count;
2096}
2097
2098static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2099
2100int snd_soc_dapm_sys_add(struct device *dev)
2101{
Troy Kisky12ef1932008-10-13 17:42:14 -07002102 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002103}
2104
2105static void snd_soc_dapm_sys_remove(struct device *dev)
2106{
Mark Brownaef90842009-05-16 17:53:16 +01002107 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002108}
2109
2110/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002111static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002112{
2113 struct snd_soc_dapm_widget *w, *next_w;
2114 struct snd_soc_dapm_path *p, *next_p;
2115
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002116 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2117 if (w->dapm != dapm)
2118 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002119 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002120 /*
2121 * remove source and sink paths associated to this widget.
2122 * While removing the path, remove reference to it from both
2123 * source and sink widgets so that path is removed only once.
2124 */
2125 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
2126 list_del(&p->list_sink);
2127 list_del(&p->list_source);
2128 list_del(&p->list);
2129 kfree(p->long_name);
2130 kfree(p);
2131 }
2132 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2133 list_del(&p->list_sink);
2134 list_del(&p->list_source);
2135 list_del(&p->list);
2136 kfree(p->long_name);
2137 kfree(p);
2138 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002139 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002140 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002141 kfree(w);
2142 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002143}
2144
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002145static struct snd_soc_dapm_widget *dapm_find_widget(
2146 struct snd_soc_dapm_context *dapm, const char *pin,
2147 bool search_other_contexts)
2148{
2149 struct snd_soc_dapm_widget *w;
2150 struct snd_soc_dapm_widget *fallback = NULL;
2151
2152 list_for_each_entry(w, &dapm->card->widgets, list) {
2153 if (!strcmp(w->name, pin)) {
2154 if (w->dapm == dapm)
2155 return w;
2156 else
2157 fallback = w;
2158 }
2159 }
2160
2161 if (search_other_contexts)
2162 return fallback;
2163
2164 return NULL;
2165}
2166
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002167static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002168 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002169{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002170 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002171
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002172 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002173 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002174 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002175 }
2176
Mark Brown1a8b2d92012-02-16 11:50:07 -08002177 if (w->connected != status)
2178 dapm_mark_dirty(w, "pin configuration");
2179
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002180 w->connected = status;
2181 if (status == 0)
2182 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002183
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002184 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002185}
2186
Richard Purdie2b97eab2006-10-06 18:32:18 +02002187/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002188 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002189 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002190 *
2191 * Walks all dapm audio paths and powers widgets according to their
2192 * stream or path usage.
2193 *
2194 * Returns 0 for success.
2195 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002196int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002197{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002198 int ret;
2199
Mark Brown4f4c0072011-10-07 14:29:19 +01002200 /*
2201 * Suppress early reports (eg, jacks syncing their state) to avoid
2202 * silly DAPM runs during card startup.
2203 */
2204 if (!dapm->card || !dapm->card->instantiated)
2205 return 0;
2206
Liam Girdwood3cd04342012-03-09 12:02:08 +00002207 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002208 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2209 mutex_unlock(&dapm->card->dapm_mutex);
2210 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002211}
Liam Girdwooda5302182008-07-07 13:35:17 +01002212EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002213
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002214static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002215 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002216{
2217 struct snd_soc_dapm_path *path;
2218 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002219 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002220 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002221 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002222 const char *source;
2223 char prefixed_sink[80];
2224 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002225 int ret = 0;
2226
Mark Brown88e8b9a2011-03-02 18:18:24 +00002227 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002228 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2229 dapm->codec->name_prefix, route->sink);
2230 sink = prefixed_sink;
2231 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2232 dapm->codec->name_prefix, route->source);
2233 source = prefixed_source;
2234 } else {
2235 sink = route->sink;
2236 source = route->source;
2237 }
2238
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002239 /*
2240 * find src and dest widgets over all widgets but favor a widget from
2241 * current DAPM context
2242 */
2243 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002245 wtsink = w;
2246 if (w->dapm == dapm)
2247 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002248 continue;
2249 }
2250 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002251 wtsource = w;
2252 if (w->dapm == dapm)
2253 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002254 }
2255 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002256 /* use widget from another DAPM context if not found from this */
2257 if (!wsink)
2258 wsink = wtsink;
2259 if (!wsource)
2260 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002261
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002262 if (wsource == NULL) {
2263 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2264 route->source);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002265 return -ENODEV;
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002266 }
2267 if (wsink == NULL) {
2268 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2269 route->sink);
2270 return -ENODEV;
2271 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002272
2273 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2274 if (!path)
2275 return -ENOMEM;
2276
2277 path->source = wsource;
2278 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002279 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002280 INIT_LIST_HEAD(&path->list);
2281 INIT_LIST_HEAD(&path->list_source);
2282 INIT_LIST_HEAD(&path->list_sink);
2283
2284 /* check for external widgets */
2285 if (wsink->id == snd_soc_dapm_input) {
2286 if (wsource->id == snd_soc_dapm_micbias ||
2287 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002288 wsource->id == snd_soc_dapm_line ||
2289 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002290 wsink->ext = 1;
2291 }
2292 if (wsource->id == snd_soc_dapm_output) {
2293 if (wsink->id == snd_soc_dapm_spk ||
2294 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002295 wsink->id == snd_soc_dapm_line ||
2296 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002297 wsource->ext = 1;
2298 }
2299
2300 /* connect static paths */
2301 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002302 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002303 list_add(&path->list_sink, &wsink->sources);
2304 list_add(&path->list_source, &wsource->sinks);
2305 path->connect = 1;
2306 return 0;
2307 }
2308
2309 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002310 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002311 case snd_soc_dapm_adc:
2312 case snd_soc_dapm_dac:
2313 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002314 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002315 case snd_soc_dapm_input:
2316 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002317 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002318 case snd_soc_dapm_micbias:
2319 case snd_soc_dapm_vmid:
2320 case snd_soc_dapm_pre:
2321 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002322 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002323 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002324 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002325 case snd_soc_dapm_aif_in:
2326 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -08002327 case snd_soc_dapm_dai:
Mark Brownc74184e2012-04-04 22:12:09 +01002328 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002329 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002330 list_add(&path->list_sink, &wsink->sources);
2331 list_add(&path->list_source, &wsource->sinks);
2332 path->connect = 1;
2333 return 0;
2334 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002335 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002336 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002337 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002338 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002339 if (ret != 0)
2340 goto err;
2341 break;
2342 case snd_soc_dapm_switch:
2343 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002344 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002345 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002346 if (ret != 0)
2347 goto err;
2348 break;
2349 case snd_soc_dapm_hp:
2350 case snd_soc_dapm_mic:
2351 case snd_soc_dapm_line:
2352 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002353 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002354 list_add(&path->list_sink, &wsink->sources);
2355 list_add(&path->list_source, &wsource->sinks);
2356 path->connect = 0;
2357 return 0;
2358 }
Mark Brownfabd0382012-07-05 17:20:06 +01002359
2360 dapm_mark_dirty(wsource, "Route added");
2361 dapm_mark_dirty(wsink, "Route added");
2362
Richard Purdie2b97eab2006-10-06 18:32:18 +02002363 return 0;
2364
2365err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002366 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002367 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002368 kfree(path);
2369 return ret;
2370}
Mark Brown105f1c22008-05-13 14:52:19 +02002371
Mark Brownefcc3c62012-07-05 17:24:19 +01002372static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2373 const struct snd_soc_dapm_route *route)
2374{
2375 struct snd_soc_dapm_path *path, *p;
2376 const char *sink;
2377 const char *source;
2378 char prefixed_sink[80];
2379 char prefixed_source[80];
2380
2381 if (route->control) {
2382 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002383 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002384 return -EINVAL;
2385 }
2386
2387 if (dapm->codec && dapm->codec->name_prefix) {
2388 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2389 dapm->codec->name_prefix, route->sink);
2390 sink = prefixed_sink;
2391 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2392 dapm->codec->name_prefix, route->source);
2393 source = prefixed_source;
2394 } else {
2395 sink = route->sink;
2396 source = route->source;
2397 }
2398
2399 path = NULL;
2400 list_for_each_entry(p, &dapm->card->paths, list) {
2401 if (strcmp(p->source->name, source) != 0)
2402 continue;
2403 if (strcmp(p->sink->name, sink) != 0)
2404 continue;
2405 path = p;
2406 break;
2407 }
2408
2409 if (path) {
2410 dapm_mark_dirty(path->source, "Route removed");
2411 dapm_mark_dirty(path->sink, "Route removed");
2412
2413 list_del(&path->list);
2414 list_del(&path->list_sink);
2415 list_del(&path->list_source);
2416 kfree(path);
2417 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002418 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002419 source, sink);
2420 }
2421
2422 return 0;
2423}
2424
Mark Brown105f1c22008-05-13 14:52:19 +02002425/**
Mark Brown105f1c22008-05-13 14:52:19 +02002426 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002427 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002428 * @route: audio routes
2429 * @num: number of routes
2430 *
2431 * Connects 2 dapm widgets together via a named audio path. The sink is
2432 * the widget receiving the audio signal, whilst the source is the sender
2433 * of the audio signal.
2434 *
2435 * Returns 0 for success else error. On error all resources can be freed
2436 * with a call to snd_soc_card_free().
2437 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002438int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002439 const struct snd_soc_dapm_route *route, int num)
2440{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002441 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002442
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002443 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002444 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002445 r = snd_soc_dapm_add_route(dapm, route);
2446 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002447 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2448 route->source,
2449 route->control ? route->control : "direct",
2450 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002451 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002452 }
2453 route++;
2454 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002455 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002456
Dan Carpenter60884c22012-04-13 22:25:43 +03002457 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002458}
2459EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2460
Mark Brownefcc3c62012-07-05 17:24:19 +01002461/**
2462 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2463 * @dapm: DAPM context
2464 * @route: audio routes
2465 * @num: number of routes
2466 *
2467 * Removes routes from the DAPM context.
2468 */
2469int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2470 const struct snd_soc_dapm_route *route, int num)
2471{
2472 int i, ret = 0;
2473
2474 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2475 for (i = 0; i < num; i++) {
2476 snd_soc_dapm_del_route(dapm, route);
2477 route++;
2478 }
2479 mutex_unlock(&dapm->card->dapm_mutex);
2480
2481 return ret;
2482}
2483EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2484
Mark Brownbf3a9e12011-06-13 16:42:29 +01002485static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2486 const struct snd_soc_dapm_route *route)
2487{
2488 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2489 route->source,
2490 true);
2491 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2492 route->sink,
2493 true);
2494 struct snd_soc_dapm_path *path;
2495 int count = 0;
2496
2497 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002498 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002499 route->source);
2500 return -ENODEV;
2501 }
2502
2503 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002504 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002505 route->sink);
2506 return -ENODEV;
2507 }
2508
2509 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002510 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002511 route->source, route->sink);
2512
2513 list_for_each_entry(path, &source->sinks, list_source) {
2514 if (path->sink == sink) {
2515 path->weak = 1;
2516 count++;
2517 }
2518 }
2519
2520 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002521 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002522 route->source, route->sink);
2523 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002524 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002525 count, route->source, route->sink);
2526
2527 return 0;
2528}
2529
2530/**
2531 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2532 * @dapm: DAPM context
2533 * @route: audio routes
2534 * @num: number of routes
2535 *
2536 * Mark existing routes matching those specified in the passed array
2537 * as being weak, meaning that they are ignored for the purpose of
2538 * power decisions. The main intended use case is for sidetone paths
2539 * which couple audio between other independent paths if they are both
2540 * active in order to make the combination work better at the user
2541 * level but which aren't intended to be "used".
2542 *
2543 * Note that CODEC drivers should not use this as sidetone type paths
2544 * can frequently also be used as bypass paths.
2545 */
2546int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2547 const struct snd_soc_dapm_route *route, int num)
2548{
2549 int i, err;
2550 int ret = 0;
2551
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002552 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002553 for (i = 0; i < num; i++) {
2554 err = snd_soc_dapm_weak_route(dapm, route);
2555 if (err)
2556 ret = err;
2557 route++;
2558 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002559 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002560
2561 return ret;
2562}
2563EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2564
Mark Brown105f1c22008-05-13 14:52:19 +02002565/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002566 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002567 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002568 *
2569 * Checks the codec for any new dapm widgets and creates them if found.
2570 *
2571 * Returns 0 for success.
2572 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002573int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002574{
2575 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002576 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002577
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002578 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2579
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002580 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002581 {
2582 if (w->new)
2583 continue;
2584
Stephen Warrenfad59882011-04-28 17:37:59 -06002585 if (w->num_kcontrols) {
2586 w->kcontrols = kzalloc(w->num_kcontrols *
2587 sizeof(struct snd_kcontrol *),
2588 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002589 if (!w->kcontrols) {
2590 mutex_unlock(&dapm->card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002591 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002592 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002593 }
2594
Richard Purdie2b97eab2006-10-06 18:32:18 +02002595 switch(w->id) {
2596 case snd_soc_dapm_switch:
2597 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002598 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002599 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002600 break;
2601 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002602 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002603 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002604 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002605 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002606 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002607 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002608 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002609 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002610 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002611 break;
2612 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002613
2614 /* Read the initial power state from the device */
2615 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002616 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002617 val &= 1 << w->shift;
2618 if (w->invert)
2619 val = !val;
2620
2621 if (val)
2622 w->power = 1;
2623 }
2624
Richard Purdie2b97eab2006-10-06 18:32:18 +02002625 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002626
Mark Brown7508b122011-10-05 12:09:12 +01002627 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002628 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002629 }
2630
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002631 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002632 mutex_unlock(&dapm->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002633 return 0;
2634}
2635EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2636
2637/**
2638 * snd_soc_dapm_get_volsw - dapm mixer get callback
2639 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002640 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002641 *
2642 * Callback to get the value of a dapm mixer control.
2643 *
2644 * Returns 0 for success.
2645 */
2646int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2647 struct snd_ctl_elem_value *ucontrol)
2648{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002649 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2650 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002651 struct soc_mixer_control *mc =
2652 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002653 unsigned int reg = mc->reg;
2654 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002655 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002656 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002657 unsigned int invert = mc->invert;
2658
2659 if (snd_soc_volsw_is_stereo(mc))
2660 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002661 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002662 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002663
Richard Purdie2b97eab2006-10-06 18:32:18 +02002664 ucontrol->value.integer.value[0] =
2665 (snd_soc_read(widget->codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002666 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002667 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002668 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002669
2670 return 0;
2671}
2672EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2673
2674/**
2675 * snd_soc_dapm_put_volsw - dapm mixer set callback
2676 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002677 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002678 *
2679 * Callback to set the value of a dapm mixer control.
2680 *
2681 * Returns 0 for success.
2682 */
2683int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2684 struct snd_ctl_elem_value *ucontrol)
2685{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002686 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2687 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2688 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002689 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002690 struct soc_mixer_control *mc =
2691 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002692 unsigned int reg = mc->reg;
2693 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002694 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002695 unsigned int mask = (1 << fls(max)) - 1;
2696 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002697 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002698 int connect, change;
2699 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002700 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002701
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002702 if (snd_soc_volsw_is_stereo(mc))
2703 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002704 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002705 kcontrol->id.name);
2706
Richard Purdie2b97eab2006-10-06 18:32:18 +02002707 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002708 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002709
2710 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002711 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002712 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002713 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002714
Liam Girdwood3cd04342012-03-09 12:02:08 +00002715 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002716
Stephen Warrene9cf7042011-01-27 14:54:05 -07002717 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002718 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002719 for (wi = 0; wi < wlist->num_widgets; wi++) {
2720 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002721
Stephen Warrenfafd2172011-04-28 17:38:00 -06002722 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002723
Stephen Warrenfafd2172011-04-28 17:38:00 -06002724 update.kcontrol = kcontrol;
2725 update.widget = widget;
2726 update.reg = reg;
2727 update.mask = mask;
2728 update.val = val;
2729 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002730
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002731 soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002732
2733 widget->dapm->update = NULL;
2734 }
Mark Brown283375c2009-12-07 18:09:03 +00002735 }
2736
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002737 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002738 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002739}
2740EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2741
2742/**
2743 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2744 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002745 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002746 *
2747 * Callback to get the value of a dapm enumerated double mixer control.
2748 *
2749 * Returns 0 for success.
2750 */
2751int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2752 struct snd_ctl_elem_value *ucontrol)
2753{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002754 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2755 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002756 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002757 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002758
Richard Purdie2b97eab2006-10-06 18:32:18 +02002759 val = snd_soc_read(widget->codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002760 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002761 if (e->shift_l != e->shift_r)
2762 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002763 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002764
2765 return 0;
2766}
2767EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2768
2769/**
2770 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2771 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002772 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002773 *
2774 * Callback to set the value of a dapm enumerated double mixer control.
2775 *
2776 * Returns 0 for success.
2777 */
2778int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2779 struct snd_ctl_elem_value *ucontrol)
2780{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002781 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2782 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2783 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002784 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002785 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002786 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002787 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002788 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002789 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002790
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002791 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002792 return -EINVAL;
2793 mux = ucontrol->value.enumerated.item[0];
2794 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002795 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002796 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002797 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002798 return -EINVAL;
2799 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002800 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002801 }
2802
Liam Girdwood3cd04342012-03-09 12:02:08 +00002803 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002804
Mark Brown3a655772009-10-05 17:23:30 +01002805 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002806 if (change) {
2807 for (wi = 0; wi < wlist->num_widgets; wi++) {
2808 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002809
Stephen Warrenfafd2172011-04-28 17:38:00 -06002810 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002811
Stephen Warrenfafd2172011-04-28 17:38:00 -06002812 update.kcontrol = kcontrol;
2813 update.widget = widget;
2814 update.reg = e->reg;
2815 update.mask = mask;
2816 update.val = val;
2817 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002818
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002819 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002820
Stephen Warrenfafd2172011-04-28 17:38:00 -06002821 widget->dapm->update = NULL;
2822 }
2823 }
2824
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002825 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002826 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002827}
2828EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2829
2830/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002831 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2832 * @kcontrol: mixer control
2833 * @ucontrol: control element information
2834 *
2835 * Returns 0 for success.
2836 */
2837int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2838 struct snd_ctl_elem_value *ucontrol)
2839{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002840 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2841 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002842
2843 ucontrol->value.enumerated.item[0] = widget->value;
2844
2845 return 0;
2846}
2847EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2848
2849/**
2850 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2851 * @kcontrol: mixer control
2852 * @ucontrol: control element information
2853 *
2854 * Returns 0 for success.
2855 */
2856int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2857 struct snd_ctl_elem_value *ucontrol)
2858{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002859 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2860 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2861 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002862 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002863 struct soc_enum *e =
2864 (struct soc_enum *)kcontrol->private_value;
2865 int change;
2866 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002867 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002868
2869 if (ucontrol->value.enumerated.item[0] >= e->max)
2870 return -EINVAL;
2871
Liam Girdwood3cd04342012-03-09 12:02:08 +00002872 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002873
2874 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002875 if (change) {
2876 for (wi = 0; wi < wlist->num_widgets; wi++) {
2877 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002878
Stephen Warrenfafd2172011-04-28 17:38:00 -06002879 widget->value = ucontrol->value.enumerated.item[0];
2880
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002881 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002882 }
2883 }
2884
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002885 mutex_unlock(&card->dapm_mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002886 return ret;
2887}
2888EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2889
2890/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002891 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2892 * callback
2893 * @kcontrol: mixer control
2894 * @ucontrol: control element information
2895 *
2896 * Callback to get the value of a dapm semi enumerated double mixer control.
2897 *
2898 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2899 * used for handling bitfield coded enumeration for example.
2900 *
2901 * Returns 0 for success.
2902 */
2903int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2904 struct snd_ctl_elem_value *ucontrol)
2905{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002906 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2907 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002908 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002909 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002910
2911 reg_val = snd_soc_read(widget->codec, e->reg);
2912 val = (reg_val >> e->shift_l) & e->mask;
2913 for (mux = 0; mux < e->max; mux++) {
2914 if (val == e->values[mux])
2915 break;
2916 }
2917 ucontrol->value.enumerated.item[0] = mux;
2918 if (e->shift_l != e->shift_r) {
2919 val = (reg_val >> e->shift_r) & e->mask;
2920 for (mux = 0; mux < e->max; mux++) {
2921 if (val == e->values[mux])
2922 break;
2923 }
2924 ucontrol->value.enumerated.item[1] = mux;
2925 }
2926
2927 return 0;
2928}
2929EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2930
2931/**
2932 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2933 * callback
2934 * @kcontrol: mixer control
2935 * @ucontrol: control element information
2936 *
2937 * Callback to set the value of a dapm semi enumerated double mixer control.
2938 *
2939 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2940 * used for handling bitfield coded enumeration for example.
2941 *
2942 * Returns 0 for success.
2943 */
2944int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2945 struct snd_ctl_elem_value *ucontrol)
2946{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002947 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2948 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2949 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002950 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002951 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002952 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002953 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002954 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002955 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002956
2957 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2958 return -EINVAL;
2959 mux = ucontrol->value.enumerated.item[0];
2960 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2961 mask = e->mask << e->shift_l;
2962 if (e->shift_l != e->shift_r) {
2963 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2964 return -EINVAL;
2965 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2966 mask |= e->mask << e->shift_r;
2967 }
2968
Liam Girdwood3cd04342012-03-09 12:02:08 +00002969 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002970
Mark Brown3a655772009-10-05 17:23:30 +01002971 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002972 if (change) {
2973 for (wi = 0; wi < wlist->num_widgets; wi++) {
2974 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002975
Stephen Warrenfafd2172011-04-28 17:38:00 -06002976 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002977
Stephen Warrenfafd2172011-04-28 17:38:00 -06002978 update.kcontrol = kcontrol;
2979 update.widget = widget;
2980 update.reg = e->reg;
2981 update.mask = mask;
2982 update.val = val;
2983 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002984
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002985 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002986
Stephen Warrenfafd2172011-04-28 17:38:00 -06002987 widget->dapm->update = NULL;
2988 }
2989 }
2990
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002991 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002992 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002993}
2994EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2995
2996/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002997 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2998 *
2999 * @kcontrol: mixer control
3000 * @uinfo: control element information
3001 *
3002 * Callback to provide information about a pin switch control.
3003 */
3004int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3005 struct snd_ctl_elem_info *uinfo)
3006{
3007 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3008 uinfo->count = 1;
3009 uinfo->value.integer.min = 0;
3010 uinfo->value.integer.max = 1;
3011
3012 return 0;
3013}
3014EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3015
3016/**
3017 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3018 *
3019 * @kcontrol: mixer control
3020 * @ucontrol: Value
3021 */
3022int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3023 struct snd_ctl_elem_value *ucontrol)
3024{
Mark Brown48a8c392012-02-14 17:11:15 -08003025 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003026 const char *pin = (const char *)kcontrol->private_value;
3027
Liam Girdwood3cd04342012-03-09 12:02:08 +00003028 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003029
3030 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003031 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003032
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003033 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003034
3035 return 0;
3036}
3037EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3038
3039/**
3040 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3041 *
3042 * @kcontrol: mixer control
3043 * @ucontrol: Value
3044 */
3045int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3046 struct snd_ctl_elem_value *ucontrol)
3047{
Mark Brown48a8c392012-02-14 17:11:15 -08003048 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003049 const char *pin = (const char *)kcontrol->private_value;
3050
Liam Girdwood3cd04342012-03-09 12:02:08 +00003051 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003052
3053 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003054 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003055 else
Mark Brown48a8c392012-02-14 17:11:15 -08003056 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003057
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003058 mutex_unlock(&card->dapm_mutex);
3059
Mark Brown48a8c392012-02-14 17:11:15 -08003060 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003061 return 0;
3062}
3063EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3064
Mark Brown5ba06fc2012-02-16 11:07:13 -08003065static struct snd_soc_dapm_widget *
3066snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3067 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003068{
3069 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003070 size_t name_len;
Mark Brown62ea8742012-01-21 21:14:48 +00003071 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003072
3073 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003074 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003075
Mark Brown62ea8742012-01-21 21:14:48 +00003076 switch (w->id) {
3077 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003078 w->regulator = devm_regulator_get(dapm->dev, w->name);
3079 if (IS_ERR(w->regulator)) {
3080 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003081 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003082 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003083 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003084 }
Mark Brown8784c772013-01-10 19:33:47 +00003085
3086 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
3087 ret = regulator_allow_bypass(w->regulator, true);
3088 if (ret != 0)
3089 dev_warn(w->dapm->dev,
3090 "ASoC: Failed to unbypass %s: %d\n",
3091 w->name, ret);
3092 }
Mark Brown62ea8742012-01-21 21:14:48 +00003093 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003094 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003095#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003096 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003097 if (IS_ERR(w->clk)) {
3098 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003099 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003100 w->name, ret);
3101 return NULL;
3102 }
Mark Brownec029952012-06-04 08:16:20 +01003103#else
3104 return NULL;
3105#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003106 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003107 default:
3108 break;
3109 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003110
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003111 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00003112 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003113 name_len += 1 + strlen(dapm->codec->name_prefix);
3114 w->name = kmalloc(name_len, GFP_KERNEL);
3115 if (w->name == NULL) {
3116 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003117 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003118 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00003119 if (dapm->codec && dapm->codec->name_prefix)
Mark Brown888df392012-02-16 19:37:51 -08003120 snprintf((char *)w->name, name_len, "%s %s",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003121 dapm->codec->name_prefix, widget->name);
3122 else
Mark Brown888df392012-02-16 19:37:51 -08003123 snprintf((char *)w->name, name_len, "%s", widget->name);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003124
Mark Brown7ca3a182011-10-08 14:04:50 +01003125 switch (w->id) {
3126 case snd_soc_dapm_switch:
3127 case snd_soc_dapm_mixer:
3128 case snd_soc_dapm_mixer_named_ctl:
3129 w->power_check = dapm_generic_check_power;
3130 break;
3131 case snd_soc_dapm_mux:
3132 case snd_soc_dapm_virt_mux:
3133 case snd_soc_dapm_value_mux:
3134 w->power_check = dapm_generic_check_power;
3135 break;
3136 case snd_soc_dapm_adc:
3137 case snd_soc_dapm_aif_out:
3138 w->power_check = dapm_adc_check_power;
3139 break;
3140 case snd_soc_dapm_dac:
3141 case snd_soc_dapm_aif_in:
3142 w->power_check = dapm_dac_check_power;
3143 break;
3144 case snd_soc_dapm_pga:
3145 case snd_soc_dapm_out_drv:
3146 case snd_soc_dapm_input:
3147 case snd_soc_dapm_output:
3148 case snd_soc_dapm_micbias:
3149 case snd_soc_dapm_spk:
3150 case snd_soc_dapm_hp:
3151 case snd_soc_dapm_mic:
3152 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003153 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003154 w->power_check = dapm_generic_check_power;
3155 break;
3156 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003157 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003158 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003159 w->power_check = dapm_supply_check_power;
3160 break;
Mark Brown888df392012-02-16 19:37:51 -08003161 case snd_soc_dapm_dai:
3162 w->power_check = dapm_dai_check_power;
3163 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003164 default:
3165 w->power_check = dapm_always_on_check_power;
3166 break;
3167 }
3168
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003169 w->dapm = dapm;
3170 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003171 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003172 INIT_LIST_HEAD(&w->sources);
3173 INIT_LIST_HEAD(&w->sinks);
3174 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003175 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003176 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003177
3178 /* machine layer set ups unconnected pins and insertions */
3179 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003180 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003181}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003182
3183/**
Mark Brown4ba13272008-05-13 14:51:19 +02003184 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003185 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003186 * @widget: widget array
3187 * @num: number of widgets
3188 *
3189 * Creates new DAPM controls based upon the templates.
3190 *
3191 * Returns 0 for success else error.
3192 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003193int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003194 const struct snd_soc_dapm_widget *widget,
3195 int num)
3196{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003197 struct snd_soc_dapm_widget *w;
3198 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003199 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003200
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003201 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003202 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003203 w = snd_soc_dapm_new_control(dapm, widget);
3204 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003205 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003206 "ASoC: Failed to create DAPM control %s\n",
3207 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003208 ret = -ENOMEM;
3209 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003210 }
Mark Brown4ba13272008-05-13 14:51:19 +02003211 widget++;
3212 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003213 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003214 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003215}
3216EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3217
Mark Brownc74184e2012-04-04 22:12:09 +01003218static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3219 struct snd_kcontrol *kcontrol, int event)
3220{
3221 struct snd_soc_dapm_path *source_p, *sink_p;
3222 struct snd_soc_dai *source, *sink;
3223 const struct snd_soc_pcm_stream *config = w->params;
3224 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003225 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003226 u64 fmt;
3227 int ret;
3228
3229 BUG_ON(!config);
3230 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3231
3232 /* We only support a single source and sink, pick the first */
3233 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3234 list_sink);
3235 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3236 list_source);
3237
3238 BUG_ON(!source_p || !sink_p);
3239 BUG_ON(!sink_p->source || !source_p->sink);
3240 BUG_ON(!source_p->source || !sink_p->sink);
3241
3242 source = source_p->source->priv;
3243 sink = sink_p->sink->priv;
3244
3245 /* Be a little careful as we don't want to overflow the mask array */
3246 if (config->formats) {
3247 fmt = ffs(config->formats) - 1;
3248 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003249 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003250 config->formats);
3251 fmt = 0;
3252 }
3253
3254 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003255 params = kzalloc(sizeof(*params), GFP_KERNEL);
3256 if (!params) {
3257 ret = -ENOMEM;
3258 goto out;
3259 }
3260 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003261
Mark Brown9747cec2012-04-26 19:12:21 +01003262 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003263 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003264 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003265 config->rate_max;
3266
Mark Brown9747cec2012-04-26 19:12:21 +01003267 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003268 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003269 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003270 = config->channels_max;
3271
3272 memset(&substream, 0, sizeof(substream));
3273
3274 switch (event) {
3275 case SND_SOC_DAPM_PRE_PMU:
3276 if (source->driver->ops && source->driver->ops->hw_params) {
3277 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3278 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003279 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003280 if (ret != 0) {
3281 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003282 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003283 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003284 }
3285 }
3286
3287 if (sink->driver->ops && sink->driver->ops->hw_params) {
3288 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003289 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003290 sink);
3291 if (ret != 0) {
3292 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003293 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003294 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003295 }
3296 }
3297 break;
3298
3299 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003300 ret = snd_soc_dai_digital_mute(sink, 0,
3301 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003302 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003303 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003304 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003305 break;
3306
3307 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003308 ret = snd_soc_dai_digital_mute(sink, 1,
3309 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003310 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003311 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003312 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003313 break;
3314
3315 default:
3316 BUG();
3317 return -EINVAL;
3318 }
3319
Mark Brown9747cec2012-04-26 19:12:21 +01003320out:
3321 kfree(params);
3322 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003323}
3324
3325int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3326 const struct snd_soc_pcm_stream *params,
3327 struct snd_soc_dapm_widget *source,
3328 struct snd_soc_dapm_widget *sink)
3329{
3330 struct snd_soc_dapm_route routes[2];
3331 struct snd_soc_dapm_widget template;
3332 struct snd_soc_dapm_widget *w;
3333 size_t len;
3334 char *link_name;
3335
3336 len = strlen(source->name) + strlen(sink->name) + 2;
3337 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3338 if (!link_name)
3339 return -ENOMEM;
3340 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3341
3342 memset(&template, 0, sizeof(template));
3343 template.reg = SND_SOC_NOPM;
3344 template.id = snd_soc_dapm_dai_link;
3345 template.name = link_name;
3346 template.event = snd_soc_dai_link_event;
3347 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3348 SND_SOC_DAPM_PRE_PMD;
3349
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003350 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003351
3352 w = snd_soc_dapm_new_control(&card->dapm, &template);
3353 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003354 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003355 link_name);
3356 return -ENOMEM;
3357 }
3358
3359 w->params = params;
3360
3361 memset(&routes, 0, sizeof(routes));
3362
3363 routes[0].source = source->name;
3364 routes[0].sink = link_name;
3365 routes[1].source = link_name;
3366 routes[1].sink = sink->name;
3367
3368 return snd_soc_dapm_add_routes(&card->dapm, routes,
3369 ARRAY_SIZE(routes));
3370}
3371
Mark Brown888df392012-02-16 19:37:51 -08003372int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3373 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003374{
Mark Brown888df392012-02-16 19:37:51 -08003375 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003376 struct snd_soc_dapm_widget *w;
3377
Mark Brown888df392012-02-16 19:37:51 -08003378 WARN_ON(dapm->dev != dai->dev);
3379
3380 memset(&template, 0, sizeof(template));
3381 template.reg = SND_SOC_NOPM;
3382
3383 if (dai->driver->playback.stream_name) {
3384 template.id = snd_soc_dapm_dai;
3385 template.name = dai->driver->playback.stream_name;
3386 template.sname = dai->driver->playback.stream_name;
3387
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003388 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003389 template.name);
3390
3391 w = snd_soc_dapm_new_control(dapm, &template);
3392 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003393 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003394 dai->driver->playback.stream_name);
3395 }
3396
3397 w->priv = dai;
3398 dai->playback_widget = w;
3399 }
3400
3401 if (dai->driver->capture.stream_name) {
3402 template.id = snd_soc_dapm_dai;
3403 template.name = dai->driver->capture.stream_name;
3404 template.sname = dai->driver->capture.stream_name;
3405
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003406 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003407 template.name);
3408
3409 w = snd_soc_dapm_new_control(dapm, &template);
3410 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003411 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003412 dai->driver->capture.stream_name);
3413 }
3414
3415 w->priv = dai;
3416 dai->capture_widget = w;
3417 }
3418
3419 return 0;
3420}
3421
3422int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3423{
3424 struct snd_soc_dapm_widget *dai_w, *w;
3425 struct snd_soc_dai *dai;
3426 struct snd_soc_dapm_route r;
3427
3428 memset(&r, 0, sizeof(r));
3429
3430 /* For each DAI widget... */
3431 list_for_each_entry(dai_w, &card->widgets, list) {
3432 if (dai_w->id != snd_soc_dapm_dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003433 continue;
Mark Brown888df392012-02-16 19:37:51 -08003434
3435 dai = dai_w->priv;
3436
3437 /* ...find all widgets with the same stream and link them */
3438 list_for_each_entry(w, &card->widgets, list) {
3439 if (w->dapm != dai_w->dapm)
3440 continue;
3441
3442 if (w->id == snd_soc_dapm_dai)
3443 continue;
3444
3445 if (!w->sname)
3446 continue;
3447
3448 if (dai->driver->playback.stream_name &&
3449 strstr(w->sname,
3450 dai->driver->playback.stream_name)) {
3451 r.source = dai->playback_widget->name;
3452 r.sink = w->name;
3453 dev_dbg(dai->dev, "%s -> %s\n",
3454 r.source, r.sink);
3455
3456 snd_soc_dapm_add_route(w->dapm, &r);
3457 }
3458
3459 if (dai->driver->capture.stream_name &&
3460 strstr(w->sname,
3461 dai->driver->capture.stream_name)) {
3462 r.source = w->name;
3463 r.sink = dai->capture_widget->name;
3464 dev_dbg(dai->dev, "%s -> %s\n",
3465 r.source, r.sink);
3466
3467 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003468 }
3469 }
3470 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003471
Mark Brown888df392012-02-16 19:37:51 -08003472 return 0;
3473}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003474
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003475static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3476 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003477{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003478
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003479 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3480 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3481 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003482
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003483 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3484 w_cpu = cpu_dai->playback_widget;
3485 w_codec = codec_dai->playback_widget;
3486 } else {
3487 w_cpu = cpu_dai->capture_widget;
3488 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003489 }
3490
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003491 if (w_cpu) {
3492
3493 dapm_mark_dirty(w_cpu, "stream event");
3494
3495 switch (event) {
3496 case SND_SOC_DAPM_STREAM_START:
3497 w_cpu->active = 1;
3498 break;
3499 case SND_SOC_DAPM_STREAM_STOP:
3500 w_cpu->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 if (w_codec) {
3511
3512 dapm_mark_dirty(w_codec, "stream event");
3513
3514 switch (event) {
3515 case SND_SOC_DAPM_STREAM_START:
3516 w_codec->active = 1;
3517 break;
3518 case SND_SOC_DAPM_STREAM_STOP:
3519 w_codec->active = 0;
3520 break;
3521 case SND_SOC_DAPM_STREAM_SUSPEND:
3522 case SND_SOC_DAPM_STREAM_RESUME:
3523 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3524 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3525 break;
3526 }
3527 }
3528
3529 dapm_power_widgets(&rtd->card->dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003530}
3531
3532/**
3533 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3534 * @rtd: PCM runtime data
3535 * @stream: stream name
3536 * @event: stream event
3537 *
3538 * Sends a stream event to the dapm core. The core then makes any
3539 * necessary widget power changes.
3540 *
3541 * Returns 0 for success else error.
3542 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003543void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3544 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003545{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003546 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003547
Liam Girdwood3cd04342012-03-09 12:02:08 +00003548 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003549 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003550 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003551}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003552
3553/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003554 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003555 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003556 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003557 *
Mark Brown74b8f952009-06-06 11:26:15 +01003558 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003559 * a valid audio route and active audio stream.
3560 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3561 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003562 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003563int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003564{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003565 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003566}
Liam Girdwooda5302182008-07-07 13:35:17 +01003567EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003568
3569/**
Mark Brownda341832010-03-15 19:23:37 +00003570 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003571 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003572 * @pin: pin name
3573 *
3574 * Enables input/output pin regardless of any other state. This is
3575 * intended for use with microphone bias supplies used in microphone
3576 * jack detection.
3577 *
3578 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3579 * do any widget power switching.
3580 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003581int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3582 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003583{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003584 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003585
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003586 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003587 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003588 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003589 }
3590
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003591 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003592 w->connected = 1;
3593 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003594 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003595
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003596 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003597}
3598EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3599
3600/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003601 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003602 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003603 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003604 *
Mark Brown74b8f952009-06-06 11:26:15 +01003605 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003606 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3607 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003608 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003609int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3610 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003611{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003612 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003613}
3614EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3615
3616/**
Mark Brown5817b522008-09-24 11:23:11 +01003617 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003618 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003619 * @pin: pin name
3620 *
3621 * Marks the specified pin as being not connected, disabling it along
3622 * any parent or child widgets. At present this is identical to
3623 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3624 * additional things such as disabling controls which only affect
3625 * paths through the pin.
3626 *
3627 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3628 * do any widget power switching.
3629 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003630int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003631{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003632 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003633}
3634EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3635
3636/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003637 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003638 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003639 * @pin: audio signal pin endpoint (or start point)
3640 *
3641 * Get audio pin status - connected or disconnected.
3642 *
3643 * Returns 1 for connected otherwise 0.
3644 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003645int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3646 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003647{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003648 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003649
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003650 if (w)
3651 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003652
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003653 return 0;
3654}
Liam Girdwooda5302182008-07-07 13:35:17 +01003655EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003656
3657/**
Mark Brown1547aba2010-05-07 21:11:40 +01003658 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003659 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003660 * @pin: audio signal pin endpoint (or start point)
3661 *
3662 * Mark the given endpoint or pin as ignoring suspend. When the
3663 * system is disabled a path between two endpoints flagged as ignoring
3664 * suspend will not be disabled. The path must already be enabled via
3665 * normal means at suspend time, it will not be turned on if it was not
3666 * already enabled.
3667 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003668int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3669 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003670{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003671 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003672
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003673 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003674 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003675 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003676 }
3677
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003678 w->ignore_suspend = 1;
3679
3680 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003681}
3682EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3683
Stephen Warren16332812011-11-23 12:42:04 -07003684static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3685 struct snd_soc_dapm_widget *w)
3686{
3687 struct snd_soc_dapm_path *p;
3688
3689 list_for_each_entry(p, &card->paths, list) {
3690 if ((p->source == w) || (p->sink == w)) {
3691 dev_dbg(card->dev,
3692 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3693 p->source->name, p->source->id, p->source->dapm,
3694 p->sink->name, p->sink->id, p->sink->dapm);
3695
3696 /* Connected to something other than the codec */
3697 if (p->source->dapm != p->sink->dapm)
3698 return true;
3699 /*
3700 * Loopback connection from codec external pin to
3701 * codec external pin
3702 */
3703 if (p->sink->id == snd_soc_dapm_input) {
3704 switch (p->source->id) {
3705 case snd_soc_dapm_output:
3706 case snd_soc_dapm_micbias:
3707 return true;
3708 default:
3709 break;
3710 }
3711 }
3712 }
3713 }
3714
3715 return false;
3716}
3717
3718/**
3719 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3720 * @codec: The codec whose pins should be processed
3721 *
3722 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3723 * which are unused. Pins are used if they are connected externally to the
3724 * codec, whether that be to some other device, or a loop-back connection to
3725 * the codec itself.
3726 */
3727void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3728{
3729 struct snd_soc_card *card = codec->card;
3730 struct snd_soc_dapm_context *dapm = &codec->dapm;
3731 struct snd_soc_dapm_widget *w;
3732
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003733 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003734 &card->dapm, &codec->dapm);
3735
3736 list_for_each_entry(w, &card->widgets, list) {
3737 if (w->dapm != dapm)
3738 continue;
3739 switch (w->id) {
3740 case snd_soc_dapm_input:
3741 case snd_soc_dapm_output:
3742 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003743 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003744 w->name);
3745 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003746 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003747 "... Not in map; disabling\n");
3748 snd_soc_dapm_nc_pin(dapm, w->name);
3749 }
3750 break;
3751 default:
3752 break;
3753 }
3754 }
3755}
3756
Mark Brown1547aba2010-05-07 21:11:40 +01003757/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003758 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003759 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003760 *
3761 * Free all dapm widgets and resources.
3762 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003763void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003764{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003765 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003766 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003767 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003768 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003769}
3770EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3771
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003772static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003773{
Liam Girdwood01005a72012-07-06 16:57:05 +01003774 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003775 struct snd_soc_dapm_widget *w;
3776 LIST_HEAD(down_list);
3777 int powerdown = 0;
3778
Liam Girdwood01005a72012-07-06 16:57:05 +01003779 mutex_lock(&card->dapm_mutex);
3780
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003781 list_for_each_entry(w, &dapm->card->widgets, list) {
3782 if (w->dapm != dapm)
3783 continue;
Mark Brown51737472009-06-22 13:16:51 +01003784 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003785 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003786 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003787 powerdown = 1;
3788 }
3789 }
3790
3791 /* If there were no widgets to power down we're already in
3792 * standby.
3793 */
3794 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003795 if (dapm->bias_level == SND_SOC_BIAS_ON)
3796 snd_soc_dapm_set_bias_level(dapm,
3797 SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003798 dapm_seq_run(dapm, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003799 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3800 snd_soc_dapm_set_bias_level(dapm,
3801 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003802 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003803
3804 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003805}
Mark Brown51737472009-06-22 13:16:51 +01003806
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003807/*
3808 * snd_soc_dapm_shutdown - callback for system shutdown
3809 */
3810void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3811{
3812 struct snd_soc_codec *codec;
3813
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003814 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003815 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003816 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3817 snd_soc_dapm_set_bias_level(&codec->dapm,
3818 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003819 }
Mark Brown51737472009-06-22 13:16:51 +01003820}
3821
Richard Purdie2b97eab2006-10-06 18:32:18 +02003822/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003823MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003824MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3825MODULE_LICENSE("GPL");