blob: d6d9ba2e6916fe8f0ab263c03e3a0de2cd208232 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
Liam Girdwood612a3fe2012-02-06 16:05:29 +000017 * mic/headphone insertion events.
Richard Purdie2b97eab2006-10-06 18:32:18 +020018 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Liam Girdwood612a3fe2012-02-06 16:05:29 +000021 * o Delayed power down of audio subsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020024 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080029#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020030#include <linux/delay.h>
31#include <linux/pm.h>
32#include <linux/bitops.h>
33#include <linux/platform_device.h>
34#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020035#include <linux/debugfs.h>
Mark Brownf1aac482011-12-05 15:17:06 +000036#include <linux/pm_runtime.h>
Mark Brown62ea8742012-01-21 21:14:48 +000037#include <linux/regulator/consumer.h>
Ola Liljad7e7eb92012-05-24 15:26:25 +020038#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020040#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020043#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020044#include <sound/initval.h>
45
Mark Brown84e90932010-11-04 00:07:02 -040046#include <trace/events/asoc.h>
47
Mark Brownde02d072011-09-20 21:43:24 +010048#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49
Richard Purdie2b97eab2006-10-06 18:32:18 +020050/* dapm power sequences - make this per codec in the future */
51static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010052 [snd_soc_dapm_pre] = 0,
53 [snd_soc_dapm_supply] = 1,
Mark Brown62ea8742012-01-21 21:14:48 +000054 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020055 [snd_soc_dapm_clock_supply] = 1,
Mark Brown38357ab2009-06-06 19:03:23 +010056 [snd_soc_dapm_micbias] = 2,
Mark Brownc74184e2012-04-04 22:12:09 +010057 [snd_soc_dapm_dai_link] = 2,
Mark Brown888df392012-02-16 19:37:51 -080058 [snd_soc_dapm_dai] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010059 [snd_soc_dapm_aif_in] = 3,
60 [snd_soc_dapm_aif_out] = 3,
61 [snd_soc_dapm_mic] = 4,
62 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000063 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010064 [snd_soc_dapm_value_mux] = 5,
65 [snd_soc_dapm_dac] = 6,
66 [snd_soc_dapm_mixer] = 7,
67 [snd_soc_dapm_mixer_named_ctl] = 7,
68 [snd_soc_dapm_pga] = 8,
69 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060070 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010071 [snd_soc_dapm_hp] = 10,
72 [snd_soc_dapm_spk] = 10,
Mark Brown7e1f7c82012-04-12 17:29:36 +010073 [snd_soc_dapm_line] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010074 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020075};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000076
Richard Purdie2b97eab2006-10-06 18:32:18 +020077static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010078 [snd_soc_dapm_pre] = 0,
79 [snd_soc_dapm_adc] = 1,
80 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010081 [snd_soc_dapm_spk] = 2,
Mark Brown7e1f7c82012-04-12 17:29:36 +010082 [snd_soc_dapm_line] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060083 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010084 [snd_soc_dapm_pga] = 4,
85 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010086 [snd_soc_dapm_mixer] = 5,
87 [snd_soc_dapm_dac] = 6,
88 [snd_soc_dapm_mic] = 7,
89 [snd_soc_dapm_micbias] = 8,
90 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000091 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010092 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010093 [snd_soc_dapm_aif_in] = 10,
94 [snd_soc_dapm_aif_out] = 10,
Mark Brown888df392012-02-16 19:37:51 -080095 [snd_soc_dapm_dai] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +010096 [snd_soc_dapm_dai_link] = 11,
Ola Liljad7e7eb92012-05-24 15:26:25 +020097 [snd_soc_dapm_clock_supply] = 12,
Mark Brownc74184e2012-04-04 22:12:09 +010098 [snd_soc_dapm_regulator_supply] = 12,
99 [snd_soc_dapm_supply] = 12,
100 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200101};
102
Troy Kisky12ef1932008-10-13 17:42:14 -0700103static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100104{
105 if (pop_time)
106 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
107}
108
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200109static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100110{
111 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200112 char *buf;
113
114 if (!pop_time)
115 return;
116
117 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
118 if (buf == NULL)
119 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100120
121 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200122 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100123 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100124 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200125
126 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100127}
128
Mark Browndb432b42011-10-03 21:06:40 +0100129static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
130{
131 return !list_empty(&w->dirty);
132}
133
Mark Brown25c77c52011-10-08 13:36:03 +0100134void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100135{
Mark Brown75c1f892011-10-04 22:28:08 +0100136 if (!dapm_dirty_widget(w)) {
137 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
138 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100139 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100140 }
Mark Browndb432b42011-10-03 21:06:40 +0100141}
Mark Brown25c77c52011-10-08 13:36:03 +0100142EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100143
Mark Browne2d32ff2012-08-31 17:38:32 -0700144void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
145{
146 struct snd_soc_card *card = dapm->card;
147 struct snd_soc_dapm_widget *w;
148
149 mutex_lock(&card->dapm_mutex);
150
151 list_for_each_entry(w, &card->widgets, list) {
152 switch (w->id) {
153 case snd_soc_dapm_input:
154 case snd_soc_dapm_output:
155 dapm_mark_dirty(w, "Rechecking inputs and outputs");
156 break;
157 default:
158 break;
159 }
160 }
161
162 mutex_unlock(&card->dapm_mutex);
163}
164EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
165
Richard Purdie2b97eab2006-10-06 18:32:18 +0200166/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100167static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200168 const struct snd_soc_dapm_widget *_widget)
169{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100170 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200171}
172
Liam Girdwood48056082011-07-20 12:23:33 +0100173/* get snd_card from DAPM context */
174static inline struct snd_card *dapm_get_snd_card(
175 struct snd_soc_dapm_context *dapm)
176{
177 if (dapm->codec)
178 return dapm->codec->card->snd_card;
179 else if (dapm->platform)
180 return dapm->platform->card->snd_card;
181 else
182 BUG();
183
184 /* unreachable */
185 return NULL;
186}
187
188/* get soc_card from DAPM context */
189static inline struct snd_soc_card *dapm_get_soc_card(
190 struct snd_soc_dapm_context *dapm)
191{
192 if (dapm->codec)
193 return dapm->codec->card;
194 else if (dapm->platform)
195 return dapm->platform->card;
196 else
197 BUG();
198
199 /* unreachable */
200 return NULL;
201}
202
Liam Girdwood6c120e12012-02-15 15:15:34 +0000203static void dapm_reset(struct snd_soc_card *card)
204{
205 struct snd_soc_dapm_widget *w;
206
207 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
208
209 list_for_each_entry(w, &card->widgets, list) {
210 w->power_checked = false;
211 w->inputs = -1;
212 w->outputs = -1;
213 }
214}
215
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100216static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
217{
218 if (w->codec)
219 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100220 else if (w->platform)
221 return snd_soc_platform_read(w->platform, reg);
222
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000223 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100224 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100225}
226
227static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
228{
229 if (w->codec)
230 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100231 else if (w->platform)
232 return snd_soc_platform_write(w->platform, reg, val);
233
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000234 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100235 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100236}
237
Liam Girdwood49575fb52012-03-06 18:16:19 +0000238static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
239{
Mark Browne06ab3b2012-03-06 23:58:22 +0000240 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000241 mutex_lock(&w->codec->mutex);
242 else if (w->platform)
243 mutex_lock(&w->platform->mutex);
244}
245
246static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
247{
Mark Browne06ab3b2012-03-06 23:58:22 +0000248 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000249 mutex_unlock(&w->codec->mutex);
250 else if (w->platform)
251 mutex_unlock(&w->platform->mutex);
252}
253
254static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100255 unsigned short reg, unsigned int mask, unsigned int value)
256{
Mark Brown8a713da2011-12-03 12:33:55 +0000257 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100258 unsigned int old, new;
259 int ret;
260
Mark Brown8a713da2011-12-03 12:33:55 +0000261 if (w->codec && w->codec->using_regmap) {
262 ret = regmap_update_bits_check(w->codec->control_data,
263 reg, mask, value, &change);
264 if (ret != 0)
265 return ret;
266 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000267 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000268 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000269 if (ret < 0) {
270 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100271 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000272 }
Mark Brown8a713da2011-12-03 12:33:55 +0000273
274 old = ret;
275 new = (old & ~mask) | (value & mask);
276 change = old != new;
277 if (change) {
278 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000279 if (ret < 0) {
280 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000281 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000282 }
Mark Brown8a713da2011-12-03 12:33:55 +0000283 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000284 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100285 }
286
287 return change;
288}
289
Mark Brown452c5ea2009-05-17 21:41:23 +0100290/**
291 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800292 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100293 * @level: level to configure
294 *
295 * Configure the bias (power) levels for the SoC audio device.
296 *
297 * Returns 0 for success else error.
298 */
Mark Browned5a4c42011-02-18 11:12:42 -0800299static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200300 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100301{
Mark Browned5a4c42011-02-18 11:12:42 -0800302 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100303 int ret = 0;
304
Mark Brown84e90932010-11-04 00:07:02 -0400305 trace_snd_soc_bias_level_start(card, level);
306
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000307 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100308 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100309 if (ret != 0)
310 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100311
Mark Browncc4c6702011-06-06 19:03:34 +0100312 if (dapm->codec) {
313 if (dapm->codec->driver->set_bias_level)
314 ret = dapm->codec->driver->set_bias_level(dapm->codec,
315 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100316 else
317 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100318 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100319 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100320 }
Liam Girdwood41231282012-07-06 16:56:16 +0100321
Mark Brown171ec6b2011-06-06 18:15:19 +0100322 if (ret != 0)
323 goto out;
324
325 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100326 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100327out:
Mark Brown84e90932010-11-04 00:07:02 -0400328 trace_snd_soc_bias_level_done(card, level);
329
Mark Brown452c5ea2009-05-17 21:41:23 +0100330 return ret;
331}
332
Richard Purdie2b97eab2006-10-06 18:32:18 +0200333/* set up initial codec paths */
334static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
335 struct snd_soc_dapm_path *p, int i)
336{
337 switch (w->id) {
338 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000339 case snd_soc_dapm_mixer:
340 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200341 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100342 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600343 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400344 unsigned int reg = mc->reg;
345 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100346 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400347 unsigned int mask = (1 << fls(max)) - 1;
348 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200349
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100350 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200351 val = (val >> shift) & mask;
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200352 if (invert)
353 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200354
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200355 p->connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200356 }
357 break;
358 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600359 struct soc_enum *e = (struct soc_enum *)
360 w->kcontrol_news[i].private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200361 int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200362
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100363 val = soc_widget_read(w, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200364 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200365
366 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100367 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200368 if (!(strcmp(p->name, e->texts[i])) && item == i)
369 p->connect = 1;
370 }
371 }
372 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000373 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600374 struct soc_enum *e = (struct soc_enum *)
375 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000376
377 p->connect = 0;
378 /* since a virtual mux has no backing registers to
379 * decide which path to connect, it will try to match
380 * with the first enumeration. This is to ensure
381 * that the default mux choice (the first) will be
382 * correctly powered up during initialization.
383 */
384 if (!strcmp(p->name, e->texts[0]))
385 p->connect = 1;
386 }
387 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200388 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200389 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600390 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200391 int val, item;
392
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100393 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200394 val = (val >> e->shift_l) & e->mask;
395 for (item = 0; item < e->max; item++) {
396 if (val == e->values[item])
397 break;
398 }
399
400 p->connect = 0;
401 for (i = 0; i < e->max; i++) {
402 if (!(strcmp(p->name, e->texts[i])) && item == i)
403 p->connect = 1;
404 }
405 }
406 break;
Mark Brown56563102011-10-03 22:41:09 +0100407 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200408 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600409 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200410 case snd_soc_dapm_output:
411 case snd_soc_dapm_adc:
412 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000413 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200414 case snd_soc_dapm_dac:
415 case snd_soc_dapm_micbias:
416 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100417 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000418 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200419 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100420 case snd_soc_dapm_aif_in:
421 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800422 case snd_soc_dapm_dai:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200423 case snd_soc_dapm_hp:
424 case snd_soc_dapm_mic:
425 case snd_soc_dapm_spk:
426 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100427 case snd_soc_dapm_dai_link:
Mark Brown56563102011-10-03 22:41:09 +0100428 p->connect = 1;
429 break;
430 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200431 case snd_soc_dapm_pre:
432 case snd_soc_dapm_post:
433 p->connect = 0;
434 break;
435 }
436}
437
Mark Brown74b8f952009-06-06 11:26:15 +0100438/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200439static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200440 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
441 struct snd_soc_dapm_path *path, const char *control_name,
442 const struct snd_kcontrol_new *kcontrol)
443{
444 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
445 int i;
446
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100447 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200448 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200449 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200450 list_add(&path->list_sink, &dest->sources);
451 list_add(&path->list_source, &src->sinks);
452 path->name = (char*)e->texts[i];
453 dapm_set_path_status(dest, path, 0);
454 return 0;
455 }
456 }
457
458 return -ENODEV;
459}
460
Mark Brown74b8f952009-06-06 11:26:15 +0100461/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200462static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200463 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
464 struct snd_soc_dapm_path *path, const char *control_name)
465{
466 int i;
467
468 /* search for mixer kcontrol */
469 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600470 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200471 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200472 list_add(&path->list_sink, &dest->sources);
473 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600474 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200475 dapm_set_path_status(dest, path, i);
476 return 0;
477 }
478 }
479 return -ENODEV;
480}
481
Stephen Warrenaf468002011-04-28 17:38:01 -0600482static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600483 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600484 const struct snd_kcontrol_new *kcontrol_new,
485 struct snd_kcontrol **kcontrol)
486{
487 struct snd_soc_dapm_widget *w;
488 int i;
489
490 *kcontrol = NULL;
491
492 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600493 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
494 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600495 for (i = 0; i < w->num_kcontrols; i++) {
496 if (&w->kcontrol_news[i] == kcontrol_new) {
497 if (w->kcontrols)
498 *kcontrol = w->kcontrols[i];
499 return 1;
500 }
501 }
502 }
503
504 return 0;
505}
506
Richard Purdie2b97eab2006-10-06 18:32:18 +0200507/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200508static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200509{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200510 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200511 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000512 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200513 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000514 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000515 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600516 struct snd_soc_dapm_widget_list *wlist;
517 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000518
519 if (dapm->codec)
520 prefix = dapm->codec->name_prefix;
521 else
522 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200523
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000524 if (prefix)
525 prefix_len = strlen(prefix) + 1;
526 else
527 prefix_len = 0;
528
Richard Purdie2b97eab2006-10-06 18:32:18 +0200529 /* add kcontrol */
530 for (i = 0; i < w->num_kcontrols; i++) {
531
532 /* match name */
533 list_for_each_entry(path, &w->sources, list_sink) {
534
535 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600536 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200537 continue;
538
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200539 if (w->kcontrols[i]) {
540 path->kcontrol = w->kcontrols[i];
541 continue;
542 }
543
Stephen Warrenfafd2172011-04-28 17:38:00 -0600544 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
545 sizeof(struct snd_soc_dapm_widget *),
546 wlist = kzalloc(wlistsize, GFP_KERNEL);
547 if (wlist == NULL) {
548 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000549 "ASoC: can't allocate widget list for %s\n",
Stephen Warrenfafd2172011-04-28 17:38:00 -0600550 w->name);
551 return -ENOMEM;
552 }
553 wlist->num_widgets = 1;
554 wlist->widgets[0] = w;
555
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000556 /* add dapm control with long name.
557 * for dapm_mixer this is the concatenation of the
558 * mixer and kcontrol name.
559 * for dapm_mixer_named_ctl this is simply the
560 * kcontrol name.
561 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600562 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000563 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000564 name_len += 1 + strlen(w->name);
565
Mark Brown219b93f2008-10-28 13:02:31 +0000566 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000567
Stephen Warrenfafd2172011-04-28 17:38:00 -0600568 if (path->long_name == NULL) {
569 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200570 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600571 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200572
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000573 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000574 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000575 /* The control will get a prefix from
576 * the control creation process but
577 * we're also using the same prefix
578 * for widgets so cut the prefix off
579 * the front of the widget name.
580 */
Mark Brown888df392012-02-16 19:37:51 -0800581 snprintf((char *)path->long_name, name_len,
582 "%s %s", w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600583 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000584 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000585 case snd_soc_dapm_mixer_named_ctl:
Mark Brown888df392012-02-16 19:37:51 -0800586 snprintf((char *)path->long_name, name_len,
587 "%s", w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000588 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000589 }
590
Mark Brown888df392012-02-16 19:37:51 -0800591 ((char *)path->long_name)[name_len - 1] = '\0';
Mark Brown219b93f2008-10-28 13:02:31 +0000592
Stephen Warrenfafd2172011-04-28 17:38:00 -0600593 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
594 wlist, path->long_name,
595 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200596 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200597 if (ret < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000598 dev_err(dapm->dev, "ASoC: failed to add widget"
599 " %s dapm kcontrol %s: %d\n",
600 w->name, path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600601 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200602 kfree(path->long_name);
603 path->long_name = NULL;
604 return ret;
605 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600606 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200607 }
608 }
609 return ret;
610}
611
612/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200613static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200614{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200615 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200616 struct snd_soc_dapm_path *path = NULL;
617 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000618 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000619 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000620 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600621 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600622 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600623 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600624 size_t wlistsize;
Mark Brown888df392012-02-16 19:37:51 -0800625 const char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200626
Stephen Warrenaf468002011-04-28 17:38:01 -0600627 if (w->num_kcontrols != 1) {
628 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000629 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600630 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200631 return -EINVAL;
632 }
633
Stephen Warren1007da02011-05-26 09:57:33 -0600634 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600635 &kcontrol);
636 if (kcontrol) {
637 wlist = kcontrol->private_data;
638 wlistentries = wlist->num_widgets + 1;
639 } else {
640 wlist = NULL;
641 wlistentries = 1;
642 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600643 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600644 wlistentries * sizeof(struct snd_soc_dapm_widget *),
645 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600646 if (wlist == NULL) {
647 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000648 "ASoC: can't allocate widget list for %s\n", w->name);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600649 return -ENOMEM;
650 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600651 wlist->num_widgets = wlistentries;
652 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600653
Stephen Warrenaf468002011-04-28 17:38:01 -0600654 if (!kcontrol) {
655 if (dapm->codec)
656 prefix = dapm->codec->name_prefix;
657 else
658 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000659
Stephen Warrenaf468002011-04-28 17:38:01 -0600660 if (shared) {
661 name = w->kcontrol_news[0].name;
662 prefix_len = 0;
663 } else {
664 name = w->name;
665 if (prefix)
666 prefix_len = strlen(prefix) + 1;
667 else
668 prefix_len = 0;
669 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000670
Stephen Warrenaf468002011-04-28 17:38:01 -0600671 /*
672 * The control will get a prefix from the control creation
673 * process but we're also using the same prefix for widgets so
674 * cut the prefix off the front of the widget name.
675 */
676 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
677 name + prefix_len, prefix);
678 ret = snd_ctl_add(card, kcontrol);
679 if (ret < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000680 dev_err(dapm->dev, "ASoC: failed to add kcontrol %s: %d\n",
Mark Brown53daf202011-09-05 10:51:05 -0700681 w->name, ret);
Stephen Warrenaf468002011-04-28 17:38:01 -0600682 kfree(wlist);
683 return ret;
684 }
685 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200686
Stephen Warrenaf468002011-04-28 17:38:01 -0600687 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200688
Stephen Warrenfad59882011-04-28 17:37:59 -0600689 w->kcontrols[0] = kcontrol;
690
Richard Purdie2b97eab2006-10-06 18:32:18 +0200691 list_for_each_entry(path, &w->sources, list_sink)
692 path->kcontrol = kcontrol;
693
Stephen Warrenaf468002011-04-28 17:38:01 -0600694 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200695}
696
697/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200698static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200699{
Mark Browna6c65732010-03-03 17:45:21 +0000700 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200701 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000702 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200703
Mark Browna6c65732010-03-03 17:45:21 +0000704 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200705}
706
707/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200708static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200709{
710 struct snd_soc_dapm_path *p;
711
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200712 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200713 p->walked = 0;
714}
715
Mark Brown99497882010-05-07 20:24:05 +0100716/* We implement power down on suspend by checking the power state of
717 * the ALSA card - when we are suspending the ALSA state for the card
718 * is set to D3.
719 */
720static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
721{
Mark Brown12ea2c72011-03-02 18:17:32 +0000722 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100723
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100725 case SNDRV_CTL_POWER_D3hot:
726 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100727 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000728 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200729 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100730 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100731 default:
732 return 1;
733 }
734}
735
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100736/* add widget to list if it's not already in the list */
737static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
738 struct snd_soc_dapm_widget *w)
739{
740 struct snd_soc_dapm_widget_list *wlist;
741 int wlistsize, wlistentries, i;
742
743 if (*list == NULL)
744 return -EINVAL;
745
746 wlist = *list;
747
748 /* is this widget already in the list */
749 for (i = 0; i < wlist->num_widgets; i++) {
750 if (wlist->widgets[i] == w)
751 return 0;
752 }
753
754 /* allocate some new space */
755 wlistentries = wlist->num_widgets + 1;
756 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
757 wlistentries * sizeof(struct snd_soc_dapm_widget *);
758 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
759 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000760 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100761 w->name);
762 return -ENOMEM;
763 }
764 wlist = *list;
765
766 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000767 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100768 w->name, wlist->num_widgets);
769
770 wlist->widgets[wlist->num_widgets] = w;
771 wlist->num_widgets++;
772 return 1;
773}
774
Richard Purdie2b97eab2006-10-06 18:32:18 +0200775/*
776 * Recursively check for a completed path to an active or physically connected
777 * output widget. Returns number of complete paths.
778 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100779static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
780 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200781{
782 struct snd_soc_dapm_path *path;
783 int con = 0;
784
Mark Brown024dc072011-10-09 11:52:05 +0100785 if (widget->outputs >= 0)
786 return widget->outputs;
787
Mark Brownde02d072011-09-20 21:43:24 +0100788 DAPM_UPDATE_STAT(widget, path_checks);
789
Mark Brown62ea8742012-01-21 21:14:48 +0000790 switch (widget->id) {
791 case snd_soc_dapm_supply:
792 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200793 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100794 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000795 default:
796 break;
797 }
Mark Brown246d0a12009-04-22 18:24:55 +0100798
Mark Brown010ff262009-08-17 17:39:22 +0100799 switch (widget->id) {
800 case snd_soc_dapm_adc:
801 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800802 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100803 if (widget->active) {
804 widget->outputs = snd_soc_dapm_suspend_check(widget);
805 return widget->outputs;
806 }
Mark Brown010ff262009-08-17 17:39:22 +0100807 default:
808 break;
809 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200810
811 if (widget->connected) {
812 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100813 if (widget->id == snd_soc_dapm_output && !widget->ext) {
814 widget->outputs = snd_soc_dapm_suspend_check(widget);
815 return widget->outputs;
816 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200817
818 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100819 if (widget->id == snd_soc_dapm_hp ||
820 widget->id == snd_soc_dapm_spk ||
821 (widget->id == snd_soc_dapm_line &&
822 !list_empty(&widget->sources))) {
823 widget->outputs = snd_soc_dapm_suspend_check(widget);
824 return widget->outputs;
825 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200826 }
827
828 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100829 DAPM_UPDATE_STAT(widget, neighbour_checks);
830
Mark Brownbf3a9e12011-06-13 16:42:29 +0100831 if (path->weak)
832 continue;
833
Mark Brown8af294b2013-02-22 17:48:15 +0000834 if (path->walking)
835 return 1;
836
Richard Purdie2b97eab2006-10-06 18:32:18 +0200837 if (path->walked)
838 continue;
839
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100840 trace_snd_soc_dapm_output_path(widget, path);
841
Richard Purdie2b97eab2006-10-06 18:32:18 +0200842 if (path->sink && path->connect) {
843 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000844 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100845
846 /* do we need to add this widget to the list ? */
847 if (list) {
848 int err;
849 err = dapm_list_add_widget(list, path->sink);
850 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000851 dev_err(widget->dapm->dev,
852 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100853 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000854 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100855 return con;
856 }
857 }
858
859 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000860
861 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200862 }
863 }
864
Mark Brown024dc072011-10-09 11:52:05 +0100865 widget->outputs = con;
866
Richard Purdie2b97eab2006-10-06 18:32:18 +0200867 return con;
868}
869
870/*
871 * Recursively check for a completed path to an active or physically connected
872 * input widget. Returns number of complete paths.
873 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100874static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
875 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200876{
877 struct snd_soc_dapm_path *path;
878 int con = 0;
879
Mark Brown024dc072011-10-09 11:52:05 +0100880 if (widget->inputs >= 0)
881 return widget->inputs;
882
Mark Brownde02d072011-09-20 21:43:24 +0100883 DAPM_UPDATE_STAT(widget, path_checks);
884
Mark Brown62ea8742012-01-21 21:14:48 +0000885 switch (widget->id) {
886 case snd_soc_dapm_supply:
887 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200888 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100889 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000890 default:
891 break;
892 }
Mark Brown246d0a12009-04-22 18:24:55 +0100893
Richard Purdie2b97eab2006-10-06 18:32:18 +0200894 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100895 switch (widget->id) {
896 case snd_soc_dapm_dac:
897 case snd_soc_dapm_aif_in:
Mark Brown888df392012-02-16 19:37:51 -0800898 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100899 if (widget->active) {
900 widget->inputs = snd_soc_dapm_suspend_check(widget);
901 return widget->inputs;
902 }
Mark Brown010ff262009-08-17 17:39:22 +0100903 default:
904 break;
905 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200906
907 if (widget->connected) {
908 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100909 if (widget->id == snd_soc_dapm_input && !widget->ext) {
910 widget->inputs = snd_soc_dapm_suspend_check(widget);
911 return widget->inputs;
912 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200913
914 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100915 if (widget->id == snd_soc_dapm_vmid) {
916 widget->inputs = snd_soc_dapm_suspend_check(widget);
917 return widget->inputs;
918 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200919
920 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300921 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100922 (widget->id == snd_soc_dapm_line &&
923 !list_empty(&widget->sinks))) {
924 widget->inputs = snd_soc_dapm_suspend_check(widget);
925 return widget->inputs;
926 }
927
Mark Brown1ab97c82011-11-27 16:21:51 +0000928 /* signal generator */
929 if (widget->id == snd_soc_dapm_siggen) {
930 widget->inputs = snd_soc_dapm_suspend_check(widget);
931 return widget->inputs;
932 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200933 }
934
935 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +0100936 DAPM_UPDATE_STAT(widget, neighbour_checks);
937
Mark Brownbf3a9e12011-06-13 16:42:29 +0100938 if (path->weak)
939 continue;
940
Mark Brown8af294b2013-02-22 17:48:15 +0000941 if (path->walking)
942 return 1;
943
Richard Purdie2b97eab2006-10-06 18:32:18 +0200944 if (path->walked)
945 continue;
946
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100947 trace_snd_soc_dapm_input_path(widget, path);
948
Richard Purdie2b97eab2006-10-06 18:32:18 +0200949 if (path->source && path->connect) {
950 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000951 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100952
953 /* do we need to add this widget to the list ? */
954 if (list) {
955 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +0100956 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100957 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000958 dev_err(widget->dapm->dev,
959 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100960 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000961 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100962 return con;
963 }
964 }
965
966 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000967
968 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200969 }
970 }
971
Mark Brown024dc072011-10-09 11:52:05 +0100972 widget->inputs = con;
973
Richard Purdie2b97eab2006-10-06 18:32:18 +0200974 return con;
975}
976
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100977/**
978 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
979 * @dai: the soc DAI.
980 * @stream: stream direction.
981 * @list: list of active widgets for this stream.
982 *
983 * Queries DAPM graph as to whether an valid audio stream path exists for
984 * the initial stream specified by name. This takes into account
985 * current mixer and mux kcontrol settings. Creates list of valid widgets.
986 *
987 * Returns the number of valid paths or negative error.
988 */
989int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
990 struct snd_soc_dapm_widget_list **list)
991{
992 struct snd_soc_card *card = dai->card;
993 int paths;
994
995 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
996 dapm_reset(card);
997
998 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
999 paths = is_connected_output_ep(dai->playback_widget, list);
1000 else
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001001 paths = is_connected_input_ep(dai->capture_widget, list);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001002
1003 trace_snd_soc_dapm_connected(paths, stream);
1004 dapm_clear_walk(&card->dapm);
1005 mutex_unlock(&card->dapm_mutex);
1006
1007 return paths;
1008}
1009
Richard Purdie2b97eab2006-10-06 18:32:18 +02001010/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001011 * Handler for generic register modifier widget.
1012 */
1013int dapm_reg_event(struct snd_soc_dapm_widget *w,
1014 struct snd_kcontrol *kcontrol, int event)
1015{
1016 unsigned int val;
1017
1018 if (SND_SOC_DAPM_EVENT_ON(event))
1019 val = w->on_val;
1020 else
1021 val = w->off_val;
1022
Liam Girdwood49575fb52012-03-06 18:16:19 +00001023 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001024 w->mask << w->shift, val << w->shift);
1025
1026 return 0;
1027}
Mark Brown11589412008-07-29 11:42:23 +01001028EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001029
Mark Brown62ea8742012-01-21 21:14:48 +00001030/*
1031 * Handler for regulator supply widget.
1032 */
1033int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1034 struct snd_kcontrol *kcontrol, int event)
1035{
Mark Brownc05b84d2012-09-07 12:57:11 +08001036 int ret;
1037
1038 if (SND_SOC_DAPM_EVENT_ON(event)) {
1039 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001040 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001041 if (ret != 0)
1042 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001043 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001044 w->name, ret);
1045 }
1046
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001047 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001048 } else {
1049 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001050 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001051 if (ret != 0)
1052 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001053 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001054 w->name, ret);
1055 }
1056
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001057 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001058 }
Mark Brown62ea8742012-01-21 21:14:48 +00001059}
1060EXPORT_SYMBOL_GPL(dapm_regulator_event);
1061
Ola Liljad7e7eb92012-05-24 15:26:25 +02001062/*
1063 * Handler for clock supply widget.
1064 */
1065int dapm_clock_event(struct snd_soc_dapm_widget *w,
1066 struct snd_kcontrol *kcontrol, int event)
1067{
1068 if (!w->clk)
1069 return -EIO;
1070
Mark Brownec029952012-06-04 08:16:20 +01001071#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001072 if (SND_SOC_DAPM_EVENT_ON(event)) {
1073 return clk_enable(w->clk);
1074 } else {
1075 clk_disable(w->clk);
1076 return 0;
1077 }
Mark Brownec029952012-06-04 08:16:20 +01001078#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001079 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001080}
1081EXPORT_SYMBOL_GPL(dapm_clock_event);
1082
Mark Brownd8050022011-09-28 18:28:23 +01001083static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1084{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001085 if (w->power_checked)
1086 return w->new_power;
1087
Mark Brownd8050022011-09-28 18:28:23 +01001088 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001089 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001090 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001091 w->new_power = w->power_check(w);
1092
1093 w->power_checked = true;
1094
1095 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001096}
1097
Mark Browncd0f2d42009-04-20 16:56:59 +01001098/* Generic check to see if a widget should be powered.
1099 */
1100static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1101{
1102 int in, out;
1103
Mark Brownde02d072011-09-20 21:43:24 +01001104 DAPM_UPDATE_STAT(w, power_checks);
1105
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001106 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001107 dapm_clear_walk(w->dapm);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001108 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001109 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +01001110 return out != 0 && in != 0;
1111}
1112
Mark Brown888df392012-02-16 19:37:51 -08001113static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
1114{
1115 DAPM_UPDATE_STAT(w, power_checks);
1116
Mark Brown1eee1b32012-04-10 13:57:46 +01001117 if (w->active)
1118 return w->active;
1119
1120 return dapm_generic_check_power(w);
Mark Brown888df392012-02-16 19:37:51 -08001121}
1122
Mark Brown6ea31b92009-04-20 17:15:41 +01001123/* Check to see if an ADC has power */
1124static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1125{
1126 int in;
1127
Mark Brownde02d072011-09-20 21:43:24 +01001128 DAPM_UPDATE_STAT(w, power_checks);
1129
Mark Brown6ea31b92009-04-20 17:15:41 +01001130 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001131 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001132 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +01001133 return in != 0;
1134 } else {
1135 return dapm_generic_check_power(w);
1136 }
1137}
1138
1139/* Check to see if a DAC has power */
1140static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1141{
1142 int out;
1143
Mark Brownde02d072011-09-20 21:43:24 +01001144 DAPM_UPDATE_STAT(w, power_checks);
1145
Mark Brown6ea31b92009-04-20 17:15:41 +01001146 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001147 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001148 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +01001149 return out != 0;
1150 } else {
1151 return dapm_generic_check_power(w);
1152 }
1153}
1154
Mark Brown246d0a12009-04-22 18:24:55 +01001155/* Check to see if a power supply is needed */
1156static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1157{
1158 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001159
Mark Brownde02d072011-09-20 21:43:24 +01001160 DAPM_UPDATE_STAT(w, power_checks);
1161
Mark Brown246d0a12009-04-22 18:24:55 +01001162 /* Check if one of our outputs is connected */
1163 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001164 DAPM_UPDATE_STAT(w, neighbour_checks);
1165
Mark Brownbf3a9e12011-06-13 16:42:29 +01001166 if (path->weak)
1167 continue;
1168
Mark Brown215edda2009-09-08 18:59:05 +01001169 if (path->connected &&
1170 !path->connected(path->source, path->sink))
1171 continue;
1172
Mark Brown30173582011-02-11 11:42:19 +00001173 if (!path->sink)
1174 continue;
1175
Mark Brownf68d7e12011-10-04 22:57:50 +01001176 if (dapm_widget_power_check(path->sink))
1177 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001178 }
1179
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001180 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +01001181
Mark Brownf68d7e12011-10-04 22:57:50 +01001182 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001183}
1184
Mark Brown35c64bc2011-09-28 18:23:53 +01001185static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1186{
1187 return 1;
1188}
1189
Mark Brown38357ab2009-06-06 19:03:23 +01001190static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1191 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001192 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001193{
Mark Brown828a8422011-01-15 13:14:30 +00001194 int *sort;
1195
1196 if (power_up)
1197 sort = dapm_up_seq;
1198 else
1199 sort = dapm_down_seq;
1200
Mark Brown38357ab2009-06-06 19:03:23 +01001201 if (sort[a->id] != sort[b->id])
1202 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001203 if (a->subseq != b->subseq) {
1204 if (power_up)
1205 return a->subseq - b->subseq;
1206 else
1207 return b->subseq - a->subseq;
1208 }
Mark Brownb22ead22009-06-07 12:51:26 +01001209 if (a->reg != b->reg)
1210 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001211 if (a->dapm != b->dapm)
1212 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001213
Mark Brown38357ab2009-06-06 19:03:23 +01001214 return 0;
1215}
Mark Brown42aa3412009-03-01 19:21:10 +00001216
Mark Brown38357ab2009-06-06 19:03:23 +01001217/* Insert a widget in order into a DAPM power sequence. */
1218static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1219 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001220 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001221{
1222 struct snd_soc_dapm_widget *w;
1223
1224 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001225 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001226 list_add_tail(&new_widget->power_list, &w->power_list);
1227 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001228 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001229
Mark Brown38357ab2009-06-06 19:03:23 +01001230 list_add_tail(&new_widget->power_list, list);
1231}
Mark Brown42aa3412009-03-01 19:21:10 +00001232
Mark Brown68f89ad2010-11-03 23:51:49 -04001233static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1234 struct snd_soc_dapm_widget *w, int event)
1235{
1236 struct snd_soc_card *card = dapm->card;
1237 const char *ev_name;
1238 int power, ret;
1239
1240 switch (event) {
1241 case SND_SOC_DAPM_PRE_PMU:
1242 ev_name = "PRE_PMU";
1243 power = 1;
1244 break;
1245 case SND_SOC_DAPM_POST_PMU:
1246 ev_name = "POST_PMU";
1247 power = 1;
1248 break;
1249 case SND_SOC_DAPM_PRE_PMD:
1250 ev_name = "PRE_PMD";
1251 power = 0;
1252 break;
1253 case SND_SOC_DAPM_POST_PMD:
1254 ev_name = "POST_PMD";
1255 power = 0;
1256 break;
1257 default:
1258 BUG();
1259 return;
1260 }
1261
1262 if (w->power != power)
1263 return;
1264
1265 if (w->event && (w->event_flags & event)) {
1266 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1267 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001268 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001269 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001270 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001271 if (ret < 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001272 dev_err(dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001273 ev_name, w->name, ret);
1274 }
1275}
1276
Mark Brownb22ead22009-06-07 12:51:26 +01001277/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001278static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001279 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001280{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001281 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001282 struct snd_soc_dapm_widget *w;
1283 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001284 unsigned int value = 0;
1285 unsigned int mask = 0;
1286 unsigned int cur_mask;
1287
1288 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1289 power_list)->reg;
1290
1291 list_for_each_entry(w, pending, power_list) {
1292 cur_mask = 1 << w->shift;
1293 BUG_ON(reg != w->reg);
1294
1295 if (w->invert)
1296 power = !w->power;
1297 else
1298 power = w->power;
1299
1300 mask |= cur_mask;
1301 if (power)
1302 value |= cur_mask;
1303
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001304 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001305 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1306 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001307
Mark Brown68f89ad2010-11-03 23:51:49 -04001308 /* Check for events */
1309 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1310 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001311 }
1312
Mark Brown81628102009-06-07 13:21:24 +01001313 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001314 /* Any widget will do, they should all be updating the
1315 * same register.
1316 */
1317 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1318 power_list);
1319
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001320 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001321 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001322 value, mask, reg, card->pop_time);
1323 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001324 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001325 }
1326
1327 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001328 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1329 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001330 }
Mark Brown42aa3412009-03-01 19:21:10 +00001331}
1332
Mark Brownb22ead22009-06-07 12:51:26 +01001333/* Apply a DAPM power sequence.
1334 *
1335 * We walk over a pre-sorted list of widgets to apply power to. In
1336 * order to minimise the number of writes to the device required
1337 * multiple widgets will be updated in a single write where possible.
1338 * Currently anything that requires more than a single write is not
1339 * handled.
1340 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001341static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001342 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001343{
1344 struct snd_soc_dapm_widget *w, *n;
1345 LIST_HEAD(pending);
1346 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001347 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001348 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001349 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001350 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001351 int *sort;
1352
1353 if (power_up)
1354 sort = dapm_up_seq;
1355 else
1356 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001357
Mark Brownb22ead22009-06-07 12:51:26 +01001358 list_for_each_entry_safe(w, n, list, power_list) {
1359 ret = 0;
1360
1361 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001362 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001363 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001364 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001365 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001366
Mark Brown474b62d2011-01-18 16:14:44 +00001367 if (cur_dapm && cur_dapm->seq_notifier) {
1368 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1369 if (sort[i] == cur_sort)
1370 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001371 i,
1372 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001373 }
1374
Mark Brownb22ead22009-06-07 12:51:26 +01001375 INIT_LIST_HEAD(&pending);
1376 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001377 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001378 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001379 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001380 }
1381
Mark Brown163cac02009-06-07 10:12:52 +01001382 switch (w->id) {
1383 case snd_soc_dapm_pre:
1384 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001385 list_for_each_entry_safe_continue(w, n, list,
1386 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001387
Mark Brownb22ead22009-06-07 12:51:26 +01001388 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001389 ret = w->event(w,
1390 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001391 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001392 ret = w->event(w,
1393 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001394 break;
1395
1396 case snd_soc_dapm_post:
1397 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001398 list_for_each_entry_safe_continue(w, n, list,
1399 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001400
Mark Brownb22ead22009-06-07 12:51:26 +01001401 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001402 ret = w->event(w,
1403 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001404 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001405 ret = w->event(w,
1406 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001407 break;
1408
Mark Brown163cac02009-06-07 10:12:52 +01001409 default:
Mark Brown81628102009-06-07 13:21:24 +01001410 /* Queue it up for application */
1411 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001412 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001413 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001414 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001415 list_move(&w->power_list, &pending);
1416 break;
Mark Brown163cac02009-06-07 10:12:52 +01001417 }
Mark Brownb22ead22009-06-07 12:51:26 +01001418
1419 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001420 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001421 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001422 }
Mark Brownb22ead22009-06-07 12:51:26 +01001423
1424 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001425 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001426
1427 if (cur_dapm && cur_dapm->seq_notifier) {
1428 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1429 if (sort[i] == cur_sort)
1430 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001431 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001432 }
Mark Brown163cac02009-06-07 10:12:52 +01001433}
1434
Mark Brown97404f22010-12-14 16:13:57 +00001435static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1436{
1437 struct snd_soc_dapm_update *update = dapm->update;
1438 struct snd_soc_dapm_widget *w;
1439 int ret;
1440
1441 if (!update)
1442 return;
1443
1444 w = update->widget;
1445
1446 if (w->event &&
1447 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1448 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1449 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001450 dev_err(dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Mark Brown97404f22010-12-14 16:13:57 +00001451 w->name, ret);
1452 }
1453
Liam Girdwood49575fb52012-03-06 18:16:19 +00001454 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001455 update->val);
1456 if (ret < 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001457 dev_err(dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1458 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001459
1460 if (w->event &&
1461 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1462 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1463 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001464 dev_err(dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Mark Brown97404f22010-12-14 16:13:57 +00001465 w->name, ret);
1466 }
1467}
1468
Mark Brown9d0624a2011-02-18 11:49:43 -08001469/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1470 * they're changing state.
1471 */
1472static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1473{
1474 struct snd_soc_dapm_context *d = data;
1475 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001476
Mark Brown56fba412011-06-04 11:25:10 +01001477 /* If we're off and we're not supposed to be go into STANDBY */
1478 if (d->bias_level == SND_SOC_BIAS_OFF &&
1479 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001480 if (d->dev)
1481 pm_runtime_get_sync(d->dev);
1482
Mark Brown9d0624a2011-02-18 11:49:43 -08001483 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1484 if (ret != 0)
1485 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001486 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001487 }
1488
Mark Brown56fba412011-06-04 11:25:10 +01001489 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1490 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001491 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1492 if (ret != 0)
1493 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001494 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001495 }
1496}
1497
1498/* Async callback run prior to DAPM sequences - brings to their final
1499 * state.
1500 */
1501static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1502{
1503 struct snd_soc_dapm_context *d = data;
1504 int ret;
1505
1506 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001507 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1508 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1509 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001510 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1511 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001512 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001513 ret);
1514 }
1515
1516 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001517 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1518 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001519 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1520 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001521 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1522 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001523
1524 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001525 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001526 }
1527
1528 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001529 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1530 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001531 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1532 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001533 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001534 ret);
1535 }
1536}
Mark Brown97404f22010-12-14 16:13:57 +00001537
Mark Brownfe4fda52011-10-03 22:36:57 +01001538static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1539 bool power, bool connect)
1540{
1541 /* If a connection is being made or broken then that update
1542 * will have marked the peer dirty, otherwise the widgets are
1543 * not connected and this update has no impact. */
1544 if (!connect)
1545 return;
1546
1547 /* If the peer is already in the state we're moving to then we
1548 * won't have an impact on it. */
1549 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001550 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001551}
1552
Mark Brown05623c42011-09-28 17:02:31 +01001553static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1554 struct list_head *up_list,
1555 struct list_head *down_list)
1556{
Mark Browndb432b42011-10-03 21:06:40 +01001557 struct snd_soc_dapm_path *path;
1558
Mark Brown05623c42011-09-28 17:02:31 +01001559 if (w->power == power)
1560 return;
1561
1562 trace_snd_soc_dapm_widget_power(w, power);
1563
Mark Browndb432b42011-10-03 21:06:40 +01001564 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001565 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001566 */
1567 list_for_each_entry(path, &w->sources, list_sink) {
1568 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001569 dapm_widget_set_peer_power(path->source, power,
1570 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001571 }
1572 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001573 switch (w->id) {
1574 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001575 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001576 case snd_soc_dapm_clock_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001577 /* Supplies can't affect their outputs, only their inputs */
1578 break;
1579 default:
1580 list_for_each_entry(path, &w->sinks, list_source) {
1581 if (path->sink) {
1582 dapm_widget_set_peer_power(path->sink, power,
1583 path->connect);
1584 }
Mark Browndb432b42011-10-03 21:06:40 +01001585 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001586 break;
Mark Browndb432b42011-10-03 21:06:40 +01001587 }
1588
Mark Brown05623c42011-09-28 17:02:31 +01001589 if (power)
1590 dapm_seq_insert(w, up_list, true);
1591 else
1592 dapm_seq_insert(w, down_list, false);
1593
1594 w->power = power;
1595}
1596
Mark Brown7c81beb2011-09-20 22:22:32 +01001597static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1598 struct list_head *up_list,
1599 struct list_head *down_list)
1600{
Mark Brown7c81beb2011-09-20 22:22:32 +01001601 int power;
1602
1603 switch (w->id) {
1604 case snd_soc_dapm_pre:
1605 dapm_seq_insert(w, down_list, false);
1606 break;
1607 case snd_soc_dapm_post:
1608 dapm_seq_insert(w, up_list, true);
1609 break;
1610
1611 default:
Mark Brownd8050022011-09-28 18:28:23 +01001612 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001613
Mark Brown05623c42011-09-28 17:02:31 +01001614 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001615 break;
1616 }
1617}
1618
Mark Brown42aa3412009-03-01 19:21:10 +00001619/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001620 * Scan each dapm widget for complete audio path.
1621 * A complete path is a route that has valid endpoints i.e.:-
1622 *
1623 * o DAC to output pin.
1624 * o Input Pin to ADC.
1625 * o Input pin to Output pin (bypass, sidetone)
1626 * o DAC to ADC (loopback).
1627 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001628static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001629{
Mark Brown12ea2c72011-03-02 18:17:32 +00001630 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001631 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001632 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001633 LIST_HEAD(up_list);
1634 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001635 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001636 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001637
Mark Brown84e90932010-11-04 00:07:02 -04001638 trace_snd_soc_dapm_start(card);
1639
Mark Brown56fba412011-06-04 11:25:10 +01001640 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001641 if (d->idle_bias_off)
1642 d->target_bias_level = SND_SOC_BIAS_OFF;
1643 else
1644 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001645 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001646
Liam Girdwood6c120e12012-02-15 15:15:34 +00001647 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001648
Mark Brown6d3ddc82009-05-16 17:47:29 +01001649 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001650 * lists indicating if they should be powered up or down. We
1651 * only check widgets that have been flagged as dirty but note
1652 * that new widgets may be added to the dirty list while we
1653 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001654 */
Mark Browndb432b42011-10-03 21:06:40 +01001655 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001656 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001657 }
1658
Mark Brownf9de6d72011-09-28 17:19:47 +01001659 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001660 switch (w->id) {
1661 case snd_soc_dapm_pre:
1662 case snd_soc_dapm_post:
1663 /* These widgets always need to be powered */
1664 break;
1665 default:
1666 list_del_init(&w->dirty);
1667 break;
1668 }
Mark Browndb432b42011-10-03 21:06:40 +01001669
Mark Brownf9de6d72011-09-28 17:19:47 +01001670 if (w->power) {
1671 d = w->dapm;
1672
1673 /* Supplies and micbiases only bring the
1674 * context up to STANDBY as unless something
1675 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001676 * generally don't require full power. Signal
1677 * generators are virtual pins and have no
1678 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001679 */
1680 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001681 case snd_soc_dapm_siggen:
1682 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001683 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001684 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001685 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001686 case snd_soc_dapm_micbias:
1687 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1688 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1689 break;
1690 default:
1691 d->target_bias_level = SND_SOC_BIAS_ON;
1692 break;
1693 }
1694 }
1695
1696 }
1697
Mark Brown85a843c2011-09-21 21:29:47 +01001698 /* Force all contexts in the card to the same bias state if
1699 * they're not ground referenced.
1700 */
Mark Brown56fba412011-06-04 11:25:10 +01001701 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001702 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001703 if (d->target_bias_level > bias)
1704 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001705 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001706 if (!d->idle_bias_off)
1707 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001708
Mark Brownde02d072011-09-20 21:43:24 +01001709 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001710
Mark Brown9d0624a2011-02-18 11:49:43 -08001711 /* Run all the bias changes in parallel */
1712 list_for_each_entry(d, &dapm->card->dapm_list, list)
1713 async_schedule_domain(dapm_pre_sequence_async, d,
1714 &async_domain);
1715 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001716
Mark Brown6d3ddc82009-05-16 17:47:29 +01001717 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001718 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001719
Mark Brown97404f22010-12-14 16:13:57 +00001720 dapm_widget_update(dapm);
1721
Mark Brown6d3ddc82009-05-16 17:47:29 +01001722 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001723 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001724
Mark Brown9d0624a2011-02-18 11:49:43 -08001725 /* Run all the bias changes in parallel */
1726 list_for_each_entry(d, &dapm->card->dapm_list, list)
1727 async_schedule_domain(dapm_post_sequence_async, d,
1728 &async_domain);
1729 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001730
Liam Girdwood8078d872012-02-15 15:15:35 +00001731 /* do we need to notify any clients that DAPM event is complete */
1732 list_for_each_entry(d, &card->dapm_list, list) {
1733 if (d->stream_event)
1734 d->stream_event(d, event);
1735 }
1736
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001737 pop_dbg(dapm->dev, card->pop_time,
1738 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001739 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001740
Mark Brown84e90932010-11-04 00:07:02 -04001741 trace_snd_soc_dapm_done(card);
1742
Mark Brown42aa3412009-03-01 19:21:10 +00001743 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001744}
1745
Mark Brown79fb9382009-08-21 16:38:13 +01001746#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001747static ssize_t dapm_widget_power_read_file(struct file *file,
1748 char __user *user_buf,
1749 size_t count, loff_t *ppos)
1750{
1751 struct snd_soc_dapm_widget *w = file->private_data;
1752 char *buf;
1753 int in, out;
1754 ssize_t ret;
1755 struct snd_soc_dapm_path *p = NULL;
1756
1757 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1758 if (!buf)
1759 return -ENOMEM;
1760
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001761 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001762 dapm_clear_walk(w->dapm);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001763 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001764 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001765
Mark Brownf13ebad2012-03-03 18:01:01 +00001766 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1767 w->name, w->power ? "On" : "Off",
1768 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001769
Mark Brownd033c362009-12-04 15:25:56 +00001770 if (w->reg >= 0)
1771 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1772 " - R%d(0x%x) bit %d",
1773 w->reg, w->reg, w->shift);
1774
1775 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1776
Mark Brown3eef08b2009-09-14 16:49:00 +01001777 if (w->sname)
1778 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1779 w->sname,
1780 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001781
1782 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001783 if (p->connected && !p->connected(w, p->sink))
1784 continue;
1785
Mark Brown79fb9382009-08-21 16:38:13 +01001786 if (p->connect)
1787 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001788 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001789 p->name ? p->name : "static",
1790 p->source->name);
1791 }
1792 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001793 if (p->connected && !p->connected(w, p->sink))
1794 continue;
1795
Mark Brown79fb9382009-08-21 16:38:13 +01001796 if (p->connect)
1797 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001798 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001799 p->name ? p->name : "static",
1800 p->sink->name);
1801 }
1802
1803 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1804
1805 kfree(buf);
1806 return ret;
1807}
1808
1809static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001810 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001811 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001812 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001813};
1814
Mark Brownef49e4f2011-04-04 20:48:13 +09001815static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1816 size_t count, loff_t *ppos)
1817{
1818 struct snd_soc_dapm_context *dapm = file->private_data;
1819 char *level;
1820
1821 switch (dapm->bias_level) {
1822 case SND_SOC_BIAS_ON:
1823 level = "On\n";
1824 break;
1825 case SND_SOC_BIAS_PREPARE:
1826 level = "Prepare\n";
1827 break;
1828 case SND_SOC_BIAS_STANDBY:
1829 level = "Standby\n";
1830 break;
1831 case SND_SOC_BIAS_OFF:
1832 level = "Off\n";
1833 break;
1834 default:
1835 BUG();
1836 level = "Unknown\n";
1837 break;
1838 }
1839
1840 return simple_read_from_buffer(user_buf, count, ppos, level,
1841 strlen(level));
1842}
1843
1844static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001845 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001846 .read = dapm_bias_read_file,
1847 .llseek = default_llseek,
1848};
1849
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001850void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1851 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001852{
Mark Brown79fb9382009-08-21 16:38:13 +01001853 struct dentry *d;
1854
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001855 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1856
1857 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001858 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001859 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001860 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001861 }
Mark Brown79fb9382009-08-21 16:38:13 +01001862
Mark Brownef49e4f2011-04-04 20:48:13 +09001863 d = debugfs_create_file("bias_level", 0444,
1864 dapm->debugfs_dapm, dapm,
1865 &dapm_bias_fops);
1866 if (!d)
1867 dev_warn(dapm->dev,
1868 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001869}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001870
1871static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1872{
1873 struct snd_soc_dapm_context *dapm = w->dapm;
1874 struct dentry *d;
1875
1876 if (!dapm->debugfs_dapm || !w->name)
1877 return;
1878
1879 d = debugfs_create_file(w->name, 0444,
1880 dapm->debugfs_dapm, w,
1881 &dapm_widget_power_fops);
1882 if (!d)
1883 dev_warn(w->dapm->dev,
1884 "ASoC: Failed to create %s debugfs file\n",
1885 w->name);
1886}
1887
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001888static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1889{
1890 debugfs_remove_recursive(dapm->debugfs_dapm);
1891}
1892
Mark Brown79fb9382009-08-21 16:38:13 +01001893#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001894void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1895 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001896{
1897}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001898
1899static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1900{
1901}
1902
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001903static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1904{
1905}
1906
Mark Brown79fb9382009-08-21 16:38:13 +01001907#endif
1908
Richard Purdie2b97eab2006-10-06 18:32:18 +02001909/* test and update the power status of a mux widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001910static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001911 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001912{
1913 struct snd_soc_dapm_path *path;
1914 int found = 0;
1915
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001916 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001917 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001918 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001919 return -ENODEV;
1920
Richard Purdie2b97eab2006-10-06 18:32:18 +02001921 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001922 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001923 if (path->kcontrol != kcontrol)
1924 continue;
1925
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001926 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001927 continue;
1928
1929 found = 1;
1930 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001931 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001932 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001933 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001934 } else {
1935 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001936 dapm_mark_dirty(path->source,
1937 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001938 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001939 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001940 }
1941
Mark Browndb432b42011-10-03 21:06:40 +01001942 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001943 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001944 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001945 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001946
Liam Girdwood618dae12012-04-25 12:12:51 +01001947 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001948}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001949
1950int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1951 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1952{
1953 struct snd_soc_card *card = widget->dapm->card;
1954 int ret;
1955
Liam Girdwood3cd04342012-03-09 12:02:08 +00001956 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001957 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1958 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001959 if (ret > 0)
1960 soc_dpcm_runtime_update(widget);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001961 return ret;
1962}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001963EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001964
Milan plzik1b075e32008-01-10 14:39:46 +01001965/* test and update the power status of a mixer or switch widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001966static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001967 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001968{
1969 struct snd_soc_dapm_path *path;
1970 int found = 0;
1971
Milan plzik1b075e32008-01-10 14:39:46 +01001972 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001973 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001974 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001975 return -ENODEV;
1976
Richard Purdie2b97eab2006-10-06 18:32:18 +02001977 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001978 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001979 if (path->kcontrol != kcontrol)
1980 continue;
1981
1982 /* found, now check type */
1983 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001984 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001985 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001986 }
1987
Mark Browndb432b42011-10-03 21:06:40 +01001988 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001989 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001990 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001991 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001992
Liam Girdwood618dae12012-04-25 12:12:51 +01001993 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001994}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001995
1996int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1997 struct snd_kcontrol *kcontrol, int connect)
1998{
1999 struct snd_soc_card *card = widget->dapm->card;
2000 int ret;
2001
Liam Girdwood3cd04342012-03-09 12:02:08 +00002002 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002003 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
2004 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002005 if (ret > 0)
2006 soc_dpcm_runtime_update(widget);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002007 return ret;
2008}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002009EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002010
2011/* show dapm widget status in sys fs */
2012static ssize_t dapm_widget_show(struct device *dev,
2013 struct device_attribute *attr, char *buf)
2014{
Mark Brown36ae1a92012-01-06 17:12:45 -08002015 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002016 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002017 struct snd_soc_dapm_widget *w;
2018 int count = 0;
2019 char *state = "not set";
2020
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002021 list_for_each_entry(w, &codec->card->widgets, list) {
2022 if (w->dapm != &codec->dapm)
2023 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002024
2025 /* only display widgets that burnm power */
2026 switch (w->id) {
2027 case snd_soc_dapm_hp:
2028 case snd_soc_dapm_mic:
2029 case snd_soc_dapm_spk:
2030 case snd_soc_dapm_line:
2031 case snd_soc_dapm_micbias:
2032 case snd_soc_dapm_dac:
2033 case snd_soc_dapm_adc:
2034 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002035 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002036 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002037 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002038 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002039 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002040 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002041 if (w->name)
2042 count += sprintf(buf + count, "%s: %s\n",
2043 w->name, w->power ? "On":"Off");
2044 break;
2045 default:
2046 break;
2047 }
2048 }
2049
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002050 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002051 case SND_SOC_BIAS_ON:
2052 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002053 break;
Mark Brown0be98982008-05-19 12:31:28 +02002054 case SND_SOC_BIAS_PREPARE:
2055 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002056 break;
Mark Brown0be98982008-05-19 12:31:28 +02002057 case SND_SOC_BIAS_STANDBY:
2058 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002059 break;
Mark Brown0be98982008-05-19 12:31:28 +02002060 case SND_SOC_BIAS_OFF:
2061 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002062 break;
2063 }
2064 count += sprintf(buf + count, "PM State: %s\n", state);
2065
2066 return count;
2067}
2068
2069static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2070
2071int snd_soc_dapm_sys_add(struct device *dev)
2072{
Troy Kisky12ef1932008-10-13 17:42:14 -07002073 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002074}
2075
2076static void snd_soc_dapm_sys_remove(struct device *dev)
2077{
Mark Brownaef90842009-05-16 17:53:16 +01002078 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002079}
2080
2081/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002082static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002083{
2084 struct snd_soc_dapm_widget *w, *next_w;
2085 struct snd_soc_dapm_path *p, *next_p;
2086
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002087 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2088 if (w->dapm != dapm)
2089 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002090 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002091 /*
2092 * remove source and sink paths associated to this widget.
2093 * While removing the path, remove reference to it from both
2094 * source and sink widgets so that path is removed only once.
2095 */
2096 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
2097 list_del(&p->list_sink);
2098 list_del(&p->list_source);
2099 list_del(&p->list);
2100 kfree(p->long_name);
2101 kfree(p);
2102 }
2103 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2104 list_del(&p->list_sink);
2105 list_del(&p->list_source);
2106 list_del(&p->list);
2107 kfree(p->long_name);
2108 kfree(p);
2109 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002110 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002111 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002112 kfree(w);
2113 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002114}
2115
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002116static struct snd_soc_dapm_widget *dapm_find_widget(
2117 struct snd_soc_dapm_context *dapm, const char *pin,
2118 bool search_other_contexts)
2119{
2120 struct snd_soc_dapm_widget *w;
2121 struct snd_soc_dapm_widget *fallback = NULL;
2122
2123 list_for_each_entry(w, &dapm->card->widgets, list) {
2124 if (!strcmp(w->name, pin)) {
2125 if (w->dapm == dapm)
2126 return w;
2127 else
2128 fallback = w;
2129 }
2130 }
2131
2132 if (search_other_contexts)
2133 return fallback;
2134
2135 return NULL;
2136}
2137
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002138static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002139 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002140{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002141 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002142
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002143 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002144 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002145 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002146 }
2147
Mark Brown1a8b2d92012-02-16 11:50:07 -08002148 if (w->connected != status)
2149 dapm_mark_dirty(w, "pin configuration");
2150
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002151 w->connected = status;
2152 if (status == 0)
2153 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002154
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002155 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002156}
2157
Richard Purdie2b97eab2006-10-06 18:32:18 +02002158/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002159 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002160 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002161 *
2162 * Walks all dapm audio paths and powers widgets according to their
2163 * stream or path usage.
2164 *
2165 * Returns 0 for success.
2166 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002167int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002168{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002169 int ret;
2170
Mark Brown4f4c0072011-10-07 14:29:19 +01002171 /*
2172 * Suppress early reports (eg, jacks syncing their state) to avoid
2173 * silly DAPM runs during card startup.
2174 */
2175 if (!dapm->card || !dapm->card->instantiated)
2176 return 0;
2177
Liam Girdwood3cd04342012-03-09 12:02:08 +00002178 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002179 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2180 mutex_unlock(&dapm->card->dapm_mutex);
2181 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002182}
Liam Girdwooda5302182008-07-07 13:35:17 +01002183EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002184
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002185static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002186 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002187{
2188 struct snd_soc_dapm_path *path;
2189 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002190 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002191 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002192 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002193 const char *source;
2194 char prefixed_sink[80];
2195 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002196 int ret = 0;
2197
Mark Brown88e8b9a2011-03-02 18:18:24 +00002198 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002199 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2200 dapm->codec->name_prefix, route->sink);
2201 sink = prefixed_sink;
2202 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2203 dapm->codec->name_prefix, route->source);
2204 source = prefixed_source;
2205 } else {
2206 sink = route->sink;
2207 source = route->source;
2208 }
2209
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002210 /*
2211 * find src and dest widgets over all widgets but favor a widget from
2212 * current DAPM context
2213 */
2214 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002215 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002216 wtsink = w;
2217 if (w->dapm == dapm)
2218 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002219 continue;
2220 }
2221 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002222 wtsource = w;
2223 if (w->dapm == dapm)
2224 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002225 }
2226 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002227 /* use widget from another DAPM context if not found from this */
2228 if (!wsink)
2229 wsink = wtsink;
2230 if (!wsource)
2231 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002232
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002233 if (wsource == NULL) {
2234 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2235 route->source);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002236 return -ENODEV;
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002237 }
2238 if (wsink == NULL) {
2239 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2240 route->sink);
2241 return -ENODEV;
2242 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002243
2244 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2245 if (!path)
2246 return -ENOMEM;
2247
2248 path->source = wsource;
2249 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002250 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002251 INIT_LIST_HEAD(&path->list);
2252 INIT_LIST_HEAD(&path->list_source);
2253 INIT_LIST_HEAD(&path->list_sink);
2254
2255 /* check for external widgets */
2256 if (wsink->id == snd_soc_dapm_input) {
2257 if (wsource->id == snd_soc_dapm_micbias ||
2258 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002259 wsource->id == snd_soc_dapm_line ||
2260 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002261 wsink->ext = 1;
2262 }
2263 if (wsource->id == snd_soc_dapm_output) {
2264 if (wsink->id == snd_soc_dapm_spk ||
2265 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002266 wsink->id == snd_soc_dapm_line ||
2267 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002268 wsource->ext = 1;
2269 }
2270
2271 /* connect static paths */
2272 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002273 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002274 list_add(&path->list_sink, &wsink->sources);
2275 list_add(&path->list_source, &wsource->sinks);
2276 path->connect = 1;
2277 return 0;
2278 }
2279
2280 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002281 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002282 case snd_soc_dapm_adc:
2283 case snd_soc_dapm_dac:
2284 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002285 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002286 case snd_soc_dapm_input:
2287 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002288 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002289 case snd_soc_dapm_micbias:
2290 case snd_soc_dapm_vmid:
2291 case snd_soc_dapm_pre:
2292 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002293 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002294 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002295 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002296 case snd_soc_dapm_aif_in:
2297 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -08002298 case snd_soc_dapm_dai:
Mark Brownc74184e2012-04-04 22:12:09 +01002299 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002300 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002301 list_add(&path->list_sink, &wsink->sources);
2302 list_add(&path->list_source, &wsource->sinks);
2303 path->connect = 1;
2304 return 0;
2305 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002306 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002307 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002308 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002309 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002310 if (ret != 0)
2311 goto err;
2312 break;
2313 case snd_soc_dapm_switch:
2314 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002315 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002316 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002317 if (ret != 0)
2318 goto err;
2319 break;
2320 case snd_soc_dapm_hp:
2321 case snd_soc_dapm_mic:
2322 case snd_soc_dapm_line:
2323 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002324 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002325 list_add(&path->list_sink, &wsink->sources);
2326 list_add(&path->list_source, &wsource->sinks);
2327 path->connect = 0;
2328 return 0;
2329 }
Mark Brownfabd0382012-07-05 17:20:06 +01002330
2331 dapm_mark_dirty(wsource, "Route added");
2332 dapm_mark_dirty(wsink, "Route added");
2333
Richard Purdie2b97eab2006-10-06 18:32:18 +02002334 return 0;
2335
2336err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002337 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002338 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002339 kfree(path);
2340 return ret;
2341}
Mark Brown105f1c22008-05-13 14:52:19 +02002342
Mark Brownefcc3c62012-07-05 17:24:19 +01002343static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2344 const struct snd_soc_dapm_route *route)
2345{
2346 struct snd_soc_dapm_path *path, *p;
2347 const char *sink;
2348 const char *source;
2349 char prefixed_sink[80];
2350 char prefixed_source[80];
2351
2352 if (route->control) {
2353 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002354 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002355 return -EINVAL;
2356 }
2357
2358 if (dapm->codec && dapm->codec->name_prefix) {
2359 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2360 dapm->codec->name_prefix, route->sink);
2361 sink = prefixed_sink;
2362 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2363 dapm->codec->name_prefix, route->source);
2364 source = prefixed_source;
2365 } else {
2366 sink = route->sink;
2367 source = route->source;
2368 }
2369
2370 path = NULL;
2371 list_for_each_entry(p, &dapm->card->paths, list) {
2372 if (strcmp(p->source->name, source) != 0)
2373 continue;
2374 if (strcmp(p->sink->name, sink) != 0)
2375 continue;
2376 path = p;
2377 break;
2378 }
2379
2380 if (path) {
2381 dapm_mark_dirty(path->source, "Route removed");
2382 dapm_mark_dirty(path->sink, "Route removed");
2383
2384 list_del(&path->list);
2385 list_del(&path->list_sink);
2386 list_del(&path->list_source);
2387 kfree(path);
2388 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002389 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002390 source, sink);
2391 }
2392
2393 return 0;
2394}
2395
Mark Brown105f1c22008-05-13 14:52:19 +02002396/**
Mark Brown105f1c22008-05-13 14:52:19 +02002397 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002398 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002399 * @route: audio routes
2400 * @num: number of routes
2401 *
2402 * Connects 2 dapm widgets together via a named audio path. The sink is
2403 * the widget receiving the audio signal, whilst the source is the sender
2404 * of the audio signal.
2405 *
2406 * Returns 0 for success else error. On error all resources can be freed
2407 * with a call to snd_soc_card_free().
2408 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002409int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002410 const struct snd_soc_dapm_route *route, int num)
2411{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002412 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002413
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002414 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002415 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002416 r = snd_soc_dapm_add_route(dapm, route);
2417 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002418 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2419 route->source,
2420 route->control ? route->control : "direct",
2421 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002422 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002423 }
2424 route++;
2425 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002426 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002427
Dan Carpenter60884c22012-04-13 22:25:43 +03002428 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002429}
2430EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2431
Mark Brownefcc3c62012-07-05 17:24:19 +01002432/**
2433 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2434 * @dapm: DAPM context
2435 * @route: audio routes
2436 * @num: number of routes
2437 *
2438 * Removes routes from the DAPM context.
2439 */
2440int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2441 const struct snd_soc_dapm_route *route, int num)
2442{
2443 int i, ret = 0;
2444
2445 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2446 for (i = 0; i < num; i++) {
2447 snd_soc_dapm_del_route(dapm, route);
2448 route++;
2449 }
2450 mutex_unlock(&dapm->card->dapm_mutex);
2451
2452 return ret;
2453}
2454EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2455
Mark Brownbf3a9e12011-06-13 16:42:29 +01002456static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2457 const struct snd_soc_dapm_route *route)
2458{
2459 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2460 route->source,
2461 true);
2462 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2463 route->sink,
2464 true);
2465 struct snd_soc_dapm_path *path;
2466 int count = 0;
2467
2468 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002469 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002470 route->source);
2471 return -ENODEV;
2472 }
2473
2474 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002475 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002476 route->sink);
2477 return -ENODEV;
2478 }
2479
2480 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002481 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002482 route->source, route->sink);
2483
2484 list_for_each_entry(path, &source->sinks, list_source) {
2485 if (path->sink == sink) {
2486 path->weak = 1;
2487 count++;
2488 }
2489 }
2490
2491 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002492 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002493 route->source, route->sink);
2494 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002495 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002496 count, route->source, route->sink);
2497
2498 return 0;
2499}
2500
2501/**
2502 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2503 * @dapm: DAPM context
2504 * @route: audio routes
2505 * @num: number of routes
2506 *
2507 * Mark existing routes matching those specified in the passed array
2508 * as being weak, meaning that they are ignored for the purpose of
2509 * power decisions. The main intended use case is for sidetone paths
2510 * which couple audio between other independent paths if they are both
2511 * active in order to make the combination work better at the user
2512 * level but which aren't intended to be "used".
2513 *
2514 * Note that CODEC drivers should not use this as sidetone type paths
2515 * can frequently also be used as bypass paths.
2516 */
2517int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2518 const struct snd_soc_dapm_route *route, int num)
2519{
2520 int i, err;
2521 int ret = 0;
2522
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002523 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002524 for (i = 0; i < num; i++) {
2525 err = snd_soc_dapm_weak_route(dapm, route);
2526 if (err)
2527 ret = err;
2528 route++;
2529 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002530 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002531
2532 return ret;
2533}
2534EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2535
Mark Brown105f1c22008-05-13 14:52:19 +02002536/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002537 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002538 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002539 *
2540 * Checks the codec for any new dapm widgets and creates them if found.
2541 *
2542 * Returns 0 for success.
2543 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002544int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002545{
2546 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002547 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002548
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002549 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2550
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002551 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002552 {
2553 if (w->new)
2554 continue;
2555
Stephen Warrenfad59882011-04-28 17:37:59 -06002556 if (w->num_kcontrols) {
2557 w->kcontrols = kzalloc(w->num_kcontrols *
2558 sizeof(struct snd_kcontrol *),
2559 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002560 if (!w->kcontrols) {
2561 mutex_unlock(&dapm->card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002562 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002563 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002564 }
2565
Richard Purdie2b97eab2006-10-06 18:32:18 +02002566 switch(w->id) {
2567 case snd_soc_dapm_switch:
2568 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002569 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002570 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002571 break;
2572 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002573 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002574 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002575 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002576 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002577 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002578 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002579 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002580 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002581 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002582 break;
2583 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002584
2585 /* Read the initial power state from the device */
2586 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002587 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002588 val &= 1 << w->shift;
2589 if (w->invert)
2590 val = !val;
2591
2592 if (val)
2593 w->power = 1;
2594 }
2595
Richard Purdie2b97eab2006-10-06 18:32:18 +02002596 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002597
Mark Brown7508b122011-10-05 12:09:12 +01002598 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002599 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002600 }
2601
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002602 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002603 mutex_unlock(&dapm->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002604 return 0;
2605}
2606EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2607
2608/**
2609 * snd_soc_dapm_get_volsw - dapm mixer get callback
2610 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002611 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002612 *
2613 * Callback to get the value of a dapm mixer control.
2614 *
2615 * Returns 0 for success.
2616 */
2617int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2618 struct snd_ctl_elem_value *ucontrol)
2619{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002620 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2621 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002622 struct soc_mixer_control *mc =
2623 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002624 unsigned int reg = mc->reg;
2625 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002626 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002627 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002628 unsigned int invert = mc->invert;
2629
2630 if (snd_soc_volsw_is_stereo(mc))
2631 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002632 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002633 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002634
Richard Purdie2b97eab2006-10-06 18:32:18 +02002635 ucontrol->value.integer.value[0] =
2636 (snd_soc_read(widget->codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002637 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002638 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002639 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002640
2641 return 0;
2642}
2643EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2644
2645/**
2646 * snd_soc_dapm_put_volsw - dapm mixer set callback
2647 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002648 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002649 *
2650 * Callback to set the value of a dapm mixer control.
2651 *
2652 * Returns 0 for success.
2653 */
2654int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2655 struct snd_ctl_elem_value *ucontrol)
2656{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002657 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2658 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2659 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002660 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002661 struct soc_mixer_control *mc =
2662 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002663 unsigned int reg = mc->reg;
2664 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002665 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002666 unsigned int mask = (1 << fls(max)) - 1;
2667 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002668 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002669 int connect, change;
2670 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002671 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002672
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002673 if (snd_soc_volsw_is_stereo(mc))
2674 dev_warn(widget->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002675 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002676 kcontrol->id.name);
2677
Richard Purdie2b97eab2006-10-06 18:32:18 +02002678 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002679 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002680
2681 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002682 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002683 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002684 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002685
Liam Girdwood3cd04342012-03-09 12:02:08 +00002686 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002687
Stephen Warrene9cf7042011-01-27 14:54:05 -07002688 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002689 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002690 for (wi = 0; wi < wlist->num_widgets; wi++) {
2691 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002692
Stephen Warrenfafd2172011-04-28 17:38:00 -06002693 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002694
Stephen Warrenfafd2172011-04-28 17:38:00 -06002695 update.kcontrol = kcontrol;
2696 update.widget = widget;
2697 update.reg = reg;
2698 update.mask = mask;
2699 update.val = val;
2700 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002701
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002702 soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002703
2704 widget->dapm->update = NULL;
2705 }
Mark Brown283375c2009-12-07 18:09:03 +00002706 }
2707
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002708 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002709 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002710}
2711EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2712
2713/**
2714 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2715 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002716 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002717 *
2718 * Callback to get the value of a dapm enumerated double mixer control.
2719 *
2720 * Returns 0 for success.
2721 */
2722int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2723 struct snd_ctl_elem_value *ucontrol)
2724{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002725 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2726 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002727 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002728 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002729
Richard Purdie2b97eab2006-10-06 18:32:18 +02002730 val = snd_soc_read(widget->codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002731 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002732 if (e->shift_l != e->shift_r)
2733 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002734 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002735
2736 return 0;
2737}
2738EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2739
2740/**
2741 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2742 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002743 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002744 *
2745 * Callback to set the value of a dapm enumerated double mixer control.
2746 *
2747 * Returns 0 for success.
2748 */
2749int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2750 struct snd_ctl_elem_value *ucontrol)
2751{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002752 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2753 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2754 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002755 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002756 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002757 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002758 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002759 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002760 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002761
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002762 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002763 return -EINVAL;
2764 mux = ucontrol->value.enumerated.item[0];
2765 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002766 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002767 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002768 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002769 return -EINVAL;
2770 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002771 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002772 }
2773
Liam Girdwood3cd04342012-03-09 12:02:08 +00002774 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002775
Mark Brown3a655772009-10-05 17:23:30 +01002776 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002777 if (change) {
2778 for (wi = 0; wi < wlist->num_widgets; wi++) {
2779 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002780
Stephen Warrenfafd2172011-04-28 17:38:00 -06002781 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002782
Stephen Warrenfafd2172011-04-28 17:38:00 -06002783 update.kcontrol = kcontrol;
2784 update.widget = widget;
2785 update.reg = e->reg;
2786 update.mask = mask;
2787 update.val = val;
2788 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002789
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002790 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002791
Stephen Warrenfafd2172011-04-28 17:38:00 -06002792 widget->dapm->update = NULL;
2793 }
2794 }
2795
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002796 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002797 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002798}
2799EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2800
2801/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002802 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2803 * @kcontrol: mixer control
2804 * @ucontrol: control element information
2805 *
2806 * Returns 0 for success.
2807 */
2808int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2809 struct snd_ctl_elem_value *ucontrol)
2810{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002811 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2812 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002813
2814 ucontrol->value.enumerated.item[0] = widget->value;
2815
2816 return 0;
2817}
2818EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2819
2820/**
2821 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2822 * @kcontrol: mixer control
2823 * @ucontrol: control element information
2824 *
2825 * Returns 0 for success.
2826 */
2827int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2828 struct snd_ctl_elem_value *ucontrol)
2829{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002830 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2831 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2832 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002833 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002834 struct soc_enum *e =
2835 (struct soc_enum *)kcontrol->private_value;
2836 int change;
2837 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002838 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002839
2840 if (ucontrol->value.enumerated.item[0] >= e->max)
2841 return -EINVAL;
2842
Liam Girdwood3cd04342012-03-09 12:02:08 +00002843 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002844
2845 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002846 if (change) {
2847 for (wi = 0; wi < wlist->num_widgets; wi++) {
2848 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002849
Stephen Warrenfafd2172011-04-28 17:38:00 -06002850 widget->value = ucontrol->value.enumerated.item[0];
2851
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002852 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002853 }
2854 }
2855
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002856 mutex_unlock(&card->dapm_mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002857 return ret;
2858}
2859EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2860
2861/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002862 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2863 * callback
2864 * @kcontrol: mixer control
2865 * @ucontrol: control element information
2866 *
2867 * Callback to get the value of a dapm semi enumerated double mixer control.
2868 *
2869 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2870 * used for handling bitfield coded enumeration for example.
2871 *
2872 * Returns 0 for success.
2873 */
2874int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2875 struct snd_ctl_elem_value *ucontrol)
2876{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002877 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2878 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002879 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002880 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002881
2882 reg_val = snd_soc_read(widget->codec, e->reg);
2883 val = (reg_val >> e->shift_l) & e->mask;
2884 for (mux = 0; mux < e->max; mux++) {
2885 if (val == e->values[mux])
2886 break;
2887 }
2888 ucontrol->value.enumerated.item[0] = mux;
2889 if (e->shift_l != e->shift_r) {
2890 val = (reg_val >> e->shift_r) & e->mask;
2891 for (mux = 0; mux < e->max; mux++) {
2892 if (val == e->values[mux])
2893 break;
2894 }
2895 ucontrol->value.enumerated.item[1] = mux;
2896 }
2897
2898 return 0;
2899}
2900EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2901
2902/**
2903 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2904 * callback
2905 * @kcontrol: mixer control
2906 * @ucontrol: control element information
2907 *
2908 * Callback to set the value of a dapm semi enumerated double mixer control.
2909 *
2910 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2911 * used for handling bitfield coded enumeration for example.
2912 *
2913 * Returns 0 for success.
2914 */
2915int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2916 struct snd_ctl_elem_value *ucontrol)
2917{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002918 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2919 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2920 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002921 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002922 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002923 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002924 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002925 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002926 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002927
2928 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2929 return -EINVAL;
2930 mux = ucontrol->value.enumerated.item[0];
2931 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2932 mask = e->mask << e->shift_l;
2933 if (e->shift_l != e->shift_r) {
2934 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2935 return -EINVAL;
2936 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2937 mask |= e->mask << e->shift_r;
2938 }
2939
Liam Girdwood3cd04342012-03-09 12:02:08 +00002940 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002941
Mark Brown3a655772009-10-05 17:23:30 +01002942 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002943 if (change) {
2944 for (wi = 0; wi < wlist->num_widgets; wi++) {
2945 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002946
Stephen Warrenfafd2172011-04-28 17:38:00 -06002947 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002948
Stephen Warrenfafd2172011-04-28 17:38:00 -06002949 update.kcontrol = kcontrol;
2950 update.widget = widget;
2951 update.reg = e->reg;
2952 update.mask = mask;
2953 update.val = val;
2954 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002955
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002956 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002957
Stephen Warrenfafd2172011-04-28 17:38:00 -06002958 widget->dapm->update = NULL;
2959 }
2960 }
2961
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002962 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002963 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002964}
2965EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2966
2967/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002968 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2969 *
2970 * @kcontrol: mixer control
2971 * @uinfo: control element information
2972 *
2973 * Callback to provide information about a pin switch control.
2974 */
2975int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2976 struct snd_ctl_elem_info *uinfo)
2977{
2978 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2979 uinfo->count = 1;
2980 uinfo->value.integer.min = 0;
2981 uinfo->value.integer.max = 1;
2982
2983 return 0;
2984}
2985EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2986
2987/**
2988 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2989 *
2990 * @kcontrol: mixer control
2991 * @ucontrol: Value
2992 */
2993int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2994 struct snd_ctl_elem_value *ucontrol)
2995{
Mark Brown48a8c392012-02-14 17:11:15 -08002996 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002997 const char *pin = (const char *)kcontrol->private_value;
2998
Liam Girdwood3cd04342012-03-09 12:02:08 +00002999 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003000
3001 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003002 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003003
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003004 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003005
3006 return 0;
3007}
3008EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3009
3010/**
3011 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3012 *
3013 * @kcontrol: mixer control
3014 * @ucontrol: Value
3015 */
3016int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3017 struct snd_ctl_elem_value *ucontrol)
3018{
Mark Brown48a8c392012-02-14 17:11:15 -08003019 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003020 const char *pin = (const char *)kcontrol->private_value;
3021
Liam Girdwood3cd04342012-03-09 12:02:08 +00003022 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003023
3024 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003025 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003026 else
Mark Brown48a8c392012-02-14 17:11:15 -08003027 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003028
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003029 mutex_unlock(&card->dapm_mutex);
3030
Mark Brown48a8c392012-02-14 17:11:15 -08003031 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003032 return 0;
3033}
3034EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3035
Mark Brown5ba06fc2012-02-16 11:07:13 -08003036static struct snd_soc_dapm_widget *
3037snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3038 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003039{
3040 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003041 size_t name_len;
Mark Brown62ea8742012-01-21 21:14:48 +00003042 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003043
3044 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003045 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003046
Mark Brown62ea8742012-01-21 21:14:48 +00003047 switch (w->id) {
3048 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003049 w->regulator = devm_regulator_get(dapm->dev, w->name);
3050 if (IS_ERR(w->regulator)) {
3051 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003052 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003053 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003054 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003055 }
Mark Brown8784c772013-01-10 19:33:47 +00003056
3057 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
3058 ret = regulator_allow_bypass(w->regulator, true);
3059 if (ret != 0)
3060 dev_warn(w->dapm->dev,
3061 "ASoC: Failed to unbypass %s: %d\n",
3062 w->name, ret);
3063 }
Mark Brown62ea8742012-01-21 21:14:48 +00003064 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003065 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003066#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003067 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003068 if (IS_ERR(w->clk)) {
3069 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003070 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003071 w->name, ret);
3072 return NULL;
3073 }
Mark Brownec029952012-06-04 08:16:20 +01003074#else
3075 return NULL;
3076#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003077 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003078 default:
3079 break;
3080 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003081
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003082 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00003083 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003084 name_len += 1 + strlen(dapm->codec->name_prefix);
3085 w->name = kmalloc(name_len, GFP_KERNEL);
3086 if (w->name == NULL) {
3087 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003088 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003089 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00003090 if (dapm->codec && dapm->codec->name_prefix)
Mark Brown888df392012-02-16 19:37:51 -08003091 snprintf((char *)w->name, name_len, "%s %s",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003092 dapm->codec->name_prefix, widget->name);
3093 else
Mark Brown888df392012-02-16 19:37:51 -08003094 snprintf((char *)w->name, name_len, "%s", widget->name);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003095
Mark Brown7ca3a182011-10-08 14:04:50 +01003096 switch (w->id) {
3097 case snd_soc_dapm_switch:
3098 case snd_soc_dapm_mixer:
3099 case snd_soc_dapm_mixer_named_ctl:
3100 w->power_check = dapm_generic_check_power;
3101 break;
3102 case snd_soc_dapm_mux:
3103 case snd_soc_dapm_virt_mux:
3104 case snd_soc_dapm_value_mux:
3105 w->power_check = dapm_generic_check_power;
3106 break;
3107 case snd_soc_dapm_adc:
3108 case snd_soc_dapm_aif_out:
3109 w->power_check = dapm_adc_check_power;
3110 break;
3111 case snd_soc_dapm_dac:
3112 case snd_soc_dapm_aif_in:
3113 w->power_check = dapm_dac_check_power;
3114 break;
3115 case snd_soc_dapm_pga:
3116 case snd_soc_dapm_out_drv:
3117 case snd_soc_dapm_input:
3118 case snd_soc_dapm_output:
3119 case snd_soc_dapm_micbias:
3120 case snd_soc_dapm_spk:
3121 case snd_soc_dapm_hp:
3122 case snd_soc_dapm_mic:
3123 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003124 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003125 w->power_check = dapm_generic_check_power;
3126 break;
3127 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003128 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003129 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003130 w->power_check = dapm_supply_check_power;
3131 break;
Mark Brown888df392012-02-16 19:37:51 -08003132 case snd_soc_dapm_dai:
3133 w->power_check = dapm_dai_check_power;
3134 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003135 default:
3136 w->power_check = dapm_always_on_check_power;
3137 break;
3138 }
3139
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003140 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003141 w->dapm = dapm;
3142 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003143 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003144 INIT_LIST_HEAD(&w->sources);
3145 INIT_LIST_HEAD(&w->sinks);
3146 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003147 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003148 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003149
3150 /* machine layer set ups unconnected pins and insertions */
3151 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003152 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003153}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003154
3155/**
Mark Brown4ba13272008-05-13 14:51:19 +02003156 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003157 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003158 * @widget: widget array
3159 * @num: number of widgets
3160 *
3161 * Creates new DAPM controls based upon the templates.
3162 *
3163 * Returns 0 for success else error.
3164 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003165int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003166 const struct snd_soc_dapm_widget *widget,
3167 int num)
3168{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003169 struct snd_soc_dapm_widget *w;
3170 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003171 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003172
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003173 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003174 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003175 w = snd_soc_dapm_new_control(dapm, widget);
3176 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003177 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003178 "ASoC: Failed to create DAPM control %s\n",
3179 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003180 ret = -ENOMEM;
3181 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003182 }
Mark Brown4ba13272008-05-13 14:51:19 +02003183 widget++;
3184 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003185 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003186 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003187}
3188EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3189
Mark Brownc74184e2012-04-04 22:12:09 +01003190static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3191 struct snd_kcontrol *kcontrol, int event)
3192{
3193 struct snd_soc_dapm_path *source_p, *sink_p;
3194 struct snd_soc_dai *source, *sink;
3195 const struct snd_soc_pcm_stream *config = w->params;
3196 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003197 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003198 u64 fmt;
3199 int ret;
3200
3201 BUG_ON(!config);
3202 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3203
3204 /* We only support a single source and sink, pick the first */
3205 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3206 list_sink);
3207 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3208 list_source);
3209
3210 BUG_ON(!source_p || !sink_p);
3211 BUG_ON(!sink_p->source || !source_p->sink);
3212 BUG_ON(!source_p->source || !sink_p->sink);
3213
3214 source = source_p->source->priv;
3215 sink = sink_p->sink->priv;
3216
3217 /* Be a little careful as we don't want to overflow the mask array */
3218 if (config->formats) {
3219 fmt = ffs(config->formats) - 1;
3220 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003221 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003222 config->formats);
3223 fmt = 0;
3224 }
3225
3226 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003227 params = kzalloc(sizeof(*params), GFP_KERNEL);
3228 if (!params) {
3229 ret = -ENOMEM;
3230 goto out;
3231 }
3232 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003233
Mark Brown9747cec2012-04-26 19:12:21 +01003234 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003235 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003236 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003237 config->rate_max;
3238
Mark Brown9747cec2012-04-26 19:12:21 +01003239 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003240 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003241 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003242 = config->channels_max;
3243
3244 memset(&substream, 0, sizeof(substream));
3245
3246 switch (event) {
3247 case SND_SOC_DAPM_PRE_PMU:
3248 if (source->driver->ops && source->driver->ops->hw_params) {
3249 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3250 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003251 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003252 if (ret != 0) {
3253 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003254 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003255 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003256 }
3257 }
3258
3259 if (sink->driver->ops && sink->driver->ops->hw_params) {
3260 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003261 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003262 sink);
3263 if (ret != 0) {
3264 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003265 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003266 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003267 }
3268 }
3269 break;
3270
3271 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003272 ret = snd_soc_dai_digital_mute(sink, 0,
3273 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003274 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003275 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003276 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003277 break;
3278
3279 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003280 ret = snd_soc_dai_digital_mute(sink, 1,
3281 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003282 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003283 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003284 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003285 break;
3286
3287 default:
3288 BUG();
3289 return -EINVAL;
3290 }
3291
Mark Brown9747cec2012-04-26 19:12:21 +01003292out:
3293 kfree(params);
3294 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003295}
3296
3297int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3298 const struct snd_soc_pcm_stream *params,
3299 struct snd_soc_dapm_widget *source,
3300 struct snd_soc_dapm_widget *sink)
3301{
3302 struct snd_soc_dapm_route routes[2];
3303 struct snd_soc_dapm_widget template;
3304 struct snd_soc_dapm_widget *w;
3305 size_t len;
3306 char *link_name;
3307
3308 len = strlen(source->name) + strlen(sink->name) + 2;
3309 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3310 if (!link_name)
3311 return -ENOMEM;
3312 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3313
3314 memset(&template, 0, sizeof(template));
3315 template.reg = SND_SOC_NOPM;
3316 template.id = snd_soc_dapm_dai_link;
3317 template.name = link_name;
3318 template.event = snd_soc_dai_link_event;
3319 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3320 SND_SOC_DAPM_PRE_PMD;
3321
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003322 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003323
3324 w = snd_soc_dapm_new_control(&card->dapm, &template);
3325 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003326 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003327 link_name);
3328 return -ENOMEM;
3329 }
3330
3331 w->params = params;
3332
3333 memset(&routes, 0, sizeof(routes));
3334
3335 routes[0].source = source->name;
3336 routes[0].sink = link_name;
3337 routes[1].source = link_name;
3338 routes[1].sink = sink->name;
3339
3340 return snd_soc_dapm_add_routes(&card->dapm, routes,
3341 ARRAY_SIZE(routes));
3342}
3343
Mark Brown888df392012-02-16 19:37:51 -08003344int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3345 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003346{
Mark Brown888df392012-02-16 19:37:51 -08003347 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003348 struct snd_soc_dapm_widget *w;
3349
Mark Brown888df392012-02-16 19:37:51 -08003350 WARN_ON(dapm->dev != dai->dev);
3351
3352 memset(&template, 0, sizeof(template));
3353 template.reg = SND_SOC_NOPM;
3354
3355 if (dai->driver->playback.stream_name) {
3356 template.id = snd_soc_dapm_dai;
3357 template.name = dai->driver->playback.stream_name;
3358 template.sname = dai->driver->playback.stream_name;
3359
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003360 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003361 template.name);
3362
3363 w = snd_soc_dapm_new_control(dapm, &template);
3364 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003365 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003366 dai->driver->playback.stream_name);
3367 }
3368
3369 w->priv = dai;
3370 dai->playback_widget = w;
3371 }
3372
3373 if (dai->driver->capture.stream_name) {
3374 template.id = snd_soc_dapm_dai;
3375 template.name = dai->driver->capture.stream_name;
3376 template.sname = dai->driver->capture.stream_name;
3377
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003378 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003379 template.name);
3380
3381 w = snd_soc_dapm_new_control(dapm, &template);
3382 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003383 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003384 dai->driver->capture.stream_name);
3385 }
3386
3387 w->priv = dai;
3388 dai->capture_widget = w;
3389 }
3390
3391 return 0;
3392}
3393
3394int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3395{
3396 struct snd_soc_dapm_widget *dai_w, *w;
3397 struct snd_soc_dai *dai;
3398 struct snd_soc_dapm_route r;
3399
3400 memset(&r, 0, sizeof(r));
3401
3402 /* For each DAI widget... */
3403 list_for_each_entry(dai_w, &card->widgets, list) {
3404 if (dai_w->id != snd_soc_dapm_dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003405 continue;
Mark Brown888df392012-02-16 19:37:51 -08003406
3407 dai = dai_w->priv;
3408
3409 /* ...find all widgets with the same stream and link them */
3410 list_for_each_entry(w, &card->widgets, list) {
3411 if (w->dapm != dai_w->dapm)
3412 continue;
3413
3414 if (w->id == snd_soc_dapm_dai)
3415 continue;
3416
3417 if (!w->sname)
3418 continue;
3419
3420 if (dai->driver->playback.stream_name &&
3421 strstr(w->sname,
3422 dai->driver->playback.stream_name)) {
3423 r.source = dai->playback_widget->name;
3424 r.sink = w->name;
3425 dev_dbg(dai->dev, "%s -> %s\n",
3426 r.source, r.sink);
3427
3428 snd_soc_dapm_add_route(w->dapm, &r);
3429 }
3430
3431 if (dai->driver->capture.stream_name &&
3432 strstr(w->sname,
3433 dai->driver->capture.stream_name)) {
3434 r.source = w->name;
3435 r.sink = dai->capture_widget->name;
3436 dev_dbg(dai->dev, "%s -> %s\n",
3437 r.source, r.sink);
3438
3439 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003440 }
3441 }
3442 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003443
Mark Brown888df392012-02-16 19:37:51 -08003444 return 0;
3445}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003446
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003447static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3448 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003449{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003450
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003451 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3452 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3453 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003454
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003455 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3456 w_cpu = cpu_dai->playback_widget;
3457 w_codec = codec_dai->playback_widget;
3458 } else {
3459 w_cpu = cpu_dai->capture_widget;
3460 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003461 }
3462
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003463 if (w_cpu) {
3464
3465 dapm_mark_dirty(w_cpu, "stream event");
3466
3467 switch (event) {
3468 case SND_SOC_DAPM_STREAM_START:
3469 w_cpu->active = 1;
3470 break;
3471 case SND_SOC_DAPM_STREAM_STOP:
3472 w_cpu->active = 0;
3473 break;
3474 case SND_SOC_DAPM_STREAM_SUSPEND:
3475 case SND_SOC_DAPM_STREAM_RESUME:
3476 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3477 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3478 break;
3479 }
3480 }
3481
3482 if (w_codec) {
3483
3484 dapm_mark_dirty(w_codec, "stream event");
3485
3486 switch (event) {
3487 case SND_SOC_DAPM_STREAM_START:
3488 w_codec->active = 1;
3489 break;
3490 case SND_SOC_DAPM_STREAM_STOP:
3491 w_codec->active = 0;
3492 break;
3493 case SND_SOC_DAPM_STREAM_SUSPEND:
3494 case SND_SOC_DAPM_STREAM_RESUME:
3495 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3496 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3497 break;
3498 }
3499 }
3500
3501 dapm_power_widgets(&rtd->card->dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003502}
3503
3504/**
3505 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3506 * @rtd: PCM runtime data
3507 * @stream: stream name
3508 * @event: stream event
3509 *
3510 * Sends a stream event to the dapm core. The core then makes any
3511 * necessary widget power changes.
3512 *
3513 * Returns 0 for success else error.
3514 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003515void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3516 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003517{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003518 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003519
Liam Girdwood3cd04342012-03-09 12:02:08 +00003520 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003521 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003522 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003523}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003524
3525/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003526 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003527 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003528 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003529 *
Mark Brown74b8f952009-06-06 11:26:15 +01003530 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003531 * a valid audio route and active audio stream.
3532 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3533 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003534 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003535int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003536{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003537 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003538}
Liam Girdwooda5302182008-07-07 13:35:17 +01003539EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003540
3541/**
Mark Brownda341832010-03-15 19:23:37 +00003542 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003543 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003544 * @pin: pin name
3545 *
3546 * Enables input/output pin regardless of any other state. This is
3547 * intended for use with microphone bias supplies used in microphone
3548 * jack detection.
3549 *
3550 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3551 * do any widget power switching.
3552 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003553int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3554 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003555{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003556 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003557
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003558 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003559 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003560 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003561 }
3562
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003563 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003564 w->connected = 1;
3565 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003566 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003567
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003568 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003569}
3570EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3571
3572/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003573 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003574 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003575 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003576 *
Mark Brown74b8f952009-06-06 11:26:15 +01003577 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003578 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3579 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003580 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003581int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3582 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003583{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003584 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003585}
3586EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3587
3588/**
Mark Brown5817b522008-09-24 11:23:11 +01003589 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003590 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003591 * @pin: pin name
3592 *
3593 * Marks the specified pin as being not connected, disabling it along
3594 * any parent or child widgets. At present this is identical to
3595 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3596 * additional things such as disabling controls which only affect
3597 * paths through the pin.
3598 *
3599 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3600 * do any widget power switching.
3601 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003602int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003603{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003604 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003605}
3606EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3607
3608/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003609 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003610 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003611 * @pin: audio signal pin endpoint (or start point)
3612 *
3613 * Get audio pin status - connected or disconnected.
3614 *
3615 * Returns 1 for connected otherwise 0.
3616 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003617int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3618 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003619{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003620 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003621
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003622 if (w)
3623 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003624
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003625 return 0;
3626}
Liam Girdwooda5302182008-07-07 13:35:17 +01003627EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003628
3629/**
Mark Brown1547aba2010-05-07 21:11:40 +01003630 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003631 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003632 * @pin: audio signal pin endpoint (or start point)
3633 *
3634 * Mark the given endpoint or pin as ignoring suspend. When the
3635 * system is disabled a path between two endpoints flagged as ignoring
3636 * suspend will not be disabled. The path must already be enabled via
3637 * normal means at suspend time, it will not be turned on if it was not
3638 * already enabled.
3639 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003640int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3641 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003642{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003643 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003644
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003645 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003646 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003647 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003648 }
3649
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003650 w->ignore_suspend = 1;
3651
3652 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003653}
3654EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3655
Stephen Warren16332812011-11-23 12:42:04 -07003656static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3657 struct snd_soc_dapm_widget *w)
3658{
3659 struct snd_soc_dapm_path *p;
3660
3661 list_for_each_entry(p, &card->paths, list) {
3662 if ((p->source == w) || (p->sink == w)) {
3663 dev_dbg(card->dev,
3664 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3665 p->source->name, p->source->id, p->source->dapm,
3666 p->sink->name, p->sink->id, p->sink->dapm);
3667
3668 /* Connected to something other than the codec */
3669 if (p->source->dapm != p->sink->dapm)
3670 return true;
3671 /*
3672 * Loopback connection from codec external pin to
3673 * codec external pin
3674 */
3675 if (p->sink->id == snd_soc_dapm_input) {
3676 switch (p->source->id) {
3677 case snd_soc_dapm_output:
3678 case snd_soc_dapm_micbias:
3679 return true;
3680 default:
3681 break;
3682 }
3683 }
3684 }
3685 }
3686
3687 return false;
3688}
3689
3690/**
3691 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3692 * @codec: The codec whose pins should be processed
3693 *
3694 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3695 * which are unused. Pins are used if they are connected externally to the
3696 * codec, whether that be to some other device, or a loop-back connection to
3697 * the codec itself.
3698 */
3699void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3700{
3701 struct snd_soc_card *card = codec->card;
3702 struct snd_soc_dapm_context *dapm = &codec->dapm;
3703 struct snd_soc_dapm_widget *w;
3704
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003705 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003706 &card->dapm, &codec->dapm);
3707
3708 list_for_each_entry(w, &card->widgets, list) {
3709 if (w->dapm != dapm)
3710 continue;
3711 switch (w->id) {
3712 case snd_soc_dapm_input:
3713 case snd_soc_dapm_output:
3714 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003715 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003716 w->name);
3717 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003718 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003719 "... Not in map; disabling\n");
3720 snd_soc_dapm_nc_pin(dapm, w->name);
3721 }
3722 break;
3723 default:
3724 break;
3725 }
3726 }
3727}
3728
Mark Brown1547aba2010-05-07 21:11:40 +01003729/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003730 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003731 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003732 *
3733 * Free all dapm widgets and resources.
3734 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003735void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003736{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003737 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003738 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003739 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003740 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003741}
3742EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3743
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003744static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003745{
Liam Girdwood01005a72012-07-06 16:57:05 +01003746 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003747 struct snd_soc_dapm_widget *w;
3748 LIST_HEAD(down_list);
3749 int powerdown = 0;
3750
Liam Girdwood01005a72012-07-06 16:57:05 +01003751 mutex_lock(&card->dapm_mutex);
3752
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003753 list_for_each_entry(w, &dapm->card->widgets, list) {
3754 if (w->dapm != dapm)
3755 continue;
Mark Brown51737472009-06-22 13:16:51 +01003756 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003757 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003758 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003759 powerdown = 1;
3760 }
3761 }
3762
3763 /* If there were no widgets to power down we're already in
3764 * standby.
3765 */
3766 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003767 if (dapm->bias_level == SND_SOC_BIAS_ON)
3768 snd_soc_dapm_set_bias_level(dapm,
3769 SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003770 dapm_seq_run(dapm, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003771 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3772 snd_soc_dapm_set_bias_level(dapm,
3773 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003774 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003775
3776 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003777}
Mark Brown51737472009-06-22 13:16:51 +01003778
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003779/*
3780 * snd_soc_dapm_shutdown - callback for system shutdown
3781 */
3782void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3783{
3784 struct snd_soc_codec *codec;
3785
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003786 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003787 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003788 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3789 snd_soc_dapm_set_bias_level(&codec->dapm,
3790 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003791 }
Mark Brown51737472009-06-22 13:16:51 +01003792}
3793
Richard Purdie2b97eab2006-10-06 18:32:18 +02003794/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003795MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003796MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3797MODULE_LICENSE("GPL");