blob: d7ee73a60ca556a1d41ee9c576d1ca0ae50cb127 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020039#include <sound/core.h>
40#include <sound/pcm.h>
41#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020042#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020043#include <sound/initval.h>
44
Mark Brown84e90932010-11-04 00:07:02 -040045#include <trace/events/asoc.h>
46
Mark Brownde02d072011-09-20 21:43:24 +010047#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
48
Richard Purdie2b97eab2006-10-06 18:32:18 +020049/* dapm power sequences - make this per codec in the future */
50static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010051 [snd_soc_dapm_pre] = 0,
52 [snd_soc_dapm_supply] = 1,
Mark Brown62ea8742012-01-21 21:14:48 +000053 [snd_soc_dapm_regulator_supply] = 1,
Mark Brown38357ab2009-06-06 19:03:23 +010054 [snd_soc_dapm_micbias] = 2,
Mark Brownc74184e2012-04-04 22:12:09 +010055 [snd_soc_dapm_dai_link] = 2,
Mark Brown888df392012-02-16 19:37:51 -080056 [snd_soc_dapm_dai] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010057 [snd_soc_dapm_aif_in] = 3,
58 [snd_soc_dapm_aif_out] = 3,
59 [snd_soc_dapm_mic] = 4,
60 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000061 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010062 [snd_soc_dapm_value_mux] = 5,
63 [snd_soc_dapm_dac] = 6,
64 [snd_soc_dapm_mixer] = 7,
65 [snd_soc_dapm_mixer_named_ctl] = 7,
66 [snd_soc_dapm_pga] = 8,
67 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060068 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010069 [snd_soc_dapm_hp] = 10,
70 [snd_soc_dapm_spk] = 10,
71 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020072};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000073
Richard Purdie2b97eab2006-10-06 18:32:18 +020074static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010075 [snd_soc_dapm_pre] = 0,
76 [snd_soc_dapm_adc] = 1,
77 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010078 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060079 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010080 [snd_soc_dapm_pga] = 4,
81 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010082 [snd_soc_dapm_mixer] = 5,
83 [snd_soc_dapm_dac] = 6,
84 [snd_soc_dapm_mic] = 7,
85 [snd_soc_dapm_micbias] = 8,
86 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000087 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010088 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010089 [snd_soc_dapm_aif_in] = 10,
90 [snd_soc_dapm_aif_out] = 10,
Mark Brown888df392012-02-16 19:37:51 -080091 [snd_soc_dapm_dai] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +010092 [snd_soc_dapm_dai_link] = 11,
93 [snd_soc_dapm_regulator_supply] = 12,
94 [snd_soc_dapm_supply] = 12,
95 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +020096};
97
Troy Kisky12ef1932008-10-13 17:42:14 -070098static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010099{
100 if (pop_time)
101 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
102}
103
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200104static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100105{
106 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200107 char *buf;
108
109 if (!pop_time)
110 return;
111
112 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
113 if (buf == NULL)
114 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100115
116 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200117 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100118 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100119 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200120
121 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100122}
123
Mark Browndb432b42011-10-03 21:06:40 +0100124static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
125{
126 return !list_empty(&w->dirty);
127}
128
Mark Brown25c77c52011-10-08 13:36:03 +0100129void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100130{
Mark Brown75c1f892011-10-04 22:28:08 +0100131 if (!dapm_dirty_widget(w)) {
132 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
133 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100134 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100135 }
Mark Browndb432b42011-10-03 21:06:40 +0100136}
Mark Brown25c77c52011-10-08 13:36:03 +0100137EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100138
Richard Purdie2b97eab2006-10-06 18:32:18 +0200139/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100140static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200141 const struct snd_soc_dapm_widget *_widget)
142{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100143 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200144}
145
Liam Girdwood48056082011-07-20 12:23:33 +0100146/* get snd_card from DAPM context */
147static inline struct snd_card *dapm_get_snd_card(
148 struct snd_soc_dapm_context *dapm)
149{
150 if (dapm->codec)
151 return dapm->codec->card->snd_card;
152 else if (dapm->platform)
153 return dapm->platform->card->snd_card;
154 else
155 BUG();
156
157 /* unreachable */
158 return NULL;
159}
160
161/* get soc_card from DAPM context */
162static inline struct snd_soc_card *dapm_get_soc_card(
163 struct snd_soc_dapm_context *dapm)
164{
165 if (dapm->codec)
166 return dapm->codec->card;
167 else if (dapm->platform)
168 return dapm->platform->card;
169 else
170 BUG();
171
172 /* unreachable */
173 return NULL;
174}
175
Liam Girdwood6c120e12012-02-15 15:15:34 +0000176static void dapm_reset(struct snd_soc_card *card)
177{
178 struct snd_soc_dapm_widget *w;
179
180 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
181
182 list_for_each_entry(w, &card->widgets, list) {
183 w->power_checked = false;
184 w->inputs = -1;
185 w->outputs = -1;
186 }
187}
188
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100189static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
190{
191 if (w->codec)
192 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100193 else if (w->platform)
194 return snd_soc_platform_read(w->platform, reg);
195
196 dev_err(w->dapm->dev, "no valid widget read method\n");
197 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100198}
199
200static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
201{
202 if (w->codec)
203 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100204 else if (w->platform)
205 return snd_soc_platform_write(w->platform, reg, val);
206
207 dev_err(w->dapm->dev, "no valid widget write method\n");
208 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100209}
210
Liam Girdwood49575fb52012-03-06 18:16:19 +0000211static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
212{
Mark Browne06ab3b2012-03-06 23:58:22 +0000213 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000214 mutex_lock(&w->codec->mutex);
215 else if (w->platform)
216 mutex_lock(&w->platform->mutex);
217}
218
219static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
220{
Mark Browne06ab3b2012-03-06 23:58:22 +0000221 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000222 mutex_unlock(&w->codec->mutex);
223 else if (w->platform)
224 mutex_unlock(&w->platform->mutex);
225}
226
227static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100228 unsigned short reg, unsigned int mask, unsigned int value)
229{
Mark Brown8a713da2011-12-03 12:33:55 +0000230 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100231 unsigned int old, new;
232 int ret;
233
Mark Brown8a713da2011-12-03 12:33:55 +0000234 if (w->codec && w->codec->using_regmap) {
235 ret = regmap_update_bits_check(w->codec->control_data,
236 reg, mask, value, &change);
237 if (ret != 0)
238 return ret;
239 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000240 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000241 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000242 if (ret < 0) {
243 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100244 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000245 }
Mark Brown8a713da2011-12-03 12:33:55 +0000246
247 old = ret;
248 new = (old & ~mask) | (value & mask);
249 change = old != new;
250 if (change) {
251 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000252 if (ret < 0) {
253 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000254 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000255 }
Mark Brown8a713da2011-12-03 12:33:55 +0000256 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000257 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100258 }
259
260 return change;
261}
262
Mark Brown452c5ea2009-05-17 21:41:23 +0100263/**
264 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800265 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100266 * @level: level to configure
267 *
268 * Configure the bias (power) levels for the SoC audio device.
269 *
270 * Returns 0 for success else error.
271 */
Mark Browned5a4c42011-02-18 11:12:42 -0800272static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200273 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100274{
Mark Browned5a4c42011-02-18 11:12:42 -0800275 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100276 int ret = 0;
277
Mark Brown84e90932010-11-04 00:07:02 -0400278 trace_snd_soc_bias_level_start(card, level);
279
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000280 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100281 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100282 if (ret != 0)
283 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100284
Mark Browncc4c6702011-06-06 19:03:34 +0100285 if (dapm->codec) {
286 if (dapm->codec->driver->set_bias_level)
287 ret = dapm->codec->driver->set_bias_level(dapm->codec,
288 level);
289 else
290 dapm->bias_level = level;
291 }
Mark Brown171ec6b2011-06-06 18:15:19 +0100292 if (ret != 0)
293 goto out;
294
295 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100296 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100297out:
Mark Brown84e90932010-11-04 00:07:02 -0400298 trace_snd_soc_bias_level_done(card, level);
299
Mark Brown452c5ea2009-05-17 21:41:23 +0100300 return ret;
301}
302
Richard Purdie2b97eab2006-10-06 18:32:18 +0200303/* set up initial codec paths */
304static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
305 struct snd_soc_dapm_path *p, int i)
306{
307 switch (w->id) {
308 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000309 case snd_soc_dapm_mixer:
310 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200311 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100312 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600313 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400314 unsigned int reg = mc->reg;
315 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100316 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400317 unsigned int mask = (1 << fls(max)) - 1;
318 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200319
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100320 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200321 val = (val >> shift) & mask;
322
323 if ((invert && !val) || (!invert && val))
324 p->connect = 1;
325 else
326 p->connect = 0;
327 }
328 break;
329 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600330 struct soc_enum *e = (struct soc_enum *)
331 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200332 int val, item, bitmask;
333
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100334 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Mark Brown88d96082011-06-06 16:16:34 +0100335 ;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100336 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200337 item = (val >> e->shift_l) & (bitmask - 1);
338
339 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100340 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200341 if (!(strcmp(p->name, e->texts[i])) && item == i)
342 p->connect = 1;
343 }
344 }
345 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000346 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600347 struct soc_enum *e = (struct soc_enum *)
348 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000349
350 p->connect = 0;
351 /* since a virtual mux has no backing registers to
352 * decide which path to connect, it will try to match
353 * with the first enumeration. This is to ensure
354 * that the default mux choice (the first) will be
355 * correctly powered up during initialization.
356 */
357 if (!strcmp(p->name, e->texts[0]))
358 p->connect = 1;
359 }
360 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200361 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200362 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600363 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200364 int val, item;
365
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100366 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200367 val = (val >> e->shift_l) & e->mask;
368 for (item = 0; item < e->max; item++) {
369 if (val == e->values[item])
370 break;
371 }
372
373 p->connect = 0;
374 for (i = 0; i < e->max; i++) {
375 if (!(strcmp(p->name, e->texts[i])) && item == i)
376 p->connect = 1;
377 }
378 }
379 break;
Mark Brown56563102011-10-03 22:41:09 +0100380 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200381 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600382 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200383 case snd_soc_dapm_output:
384 case snd_soc_dapm_adc:
385 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000386 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200387 case snd_soc_dapm_dac:
388 case snd_soc_dapm_micbias:
389 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100390 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000391 case snd_soc_dapm_regulator_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100392 case snd_soc_dapm_aif_in:
393 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800394 case snd_soc_dapm_dai:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200395 case snd_soc_dapm_hp:
396 case snd_soc_dapm_mic:
397 case snd_soc_dapm_spk:
398 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100399 case snd_soc_dapm_dai_link:
Mark Brown56563102011-10-03 22:41:09 +0100400 p->connect = 1;
401 break;
402 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200403 case snd_soc_dapm_pre:
404 case snd_soc_dapm_post:
405 p->connect = 0;
406 break;
407 }
408}
409
Mark Brown74b8f952009-06-06 11:26:15 +0100410/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200411static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200412 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
413 struct snd_soc_dapm_path *path, const char *control_name,
414 const struct snd_kcontrol_new *kcontrol)
415{
416 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
417 int i;
418
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100419 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200420 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200421 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200422 list_add(&path->list_sink, &dest->sources);
423 list_add(&path->list_source, &src->sinks);
424 path->name = (char*)e->texts[i];
425 dapm_set_path_status(dest, path, 0);
426 return 0;
427 }
428 }
429
430 return -ENODEV;
431}
432
Mark Brown74b8f952009-06-06 11:26:15 +0100433/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200434static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200435 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
436 struct snd_soc_dapm_path *path, const char *control_name)
437{
438 int i;
439
440 /* search for mixer kcontrol */
441 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600442 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200443 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200444 list_add(&path->list_sink, &dest->sources);
445 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600446 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200447 dapm_set_path_status(dest, path, i);
448 return 0;
449 }
450 }
451 return -ENODEV;
452}
453
Stephen Warrenaf468002011-04-28 17:38:01 -0600454static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600455 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600456 const struct snd_kcontrol_new *kcontrol_new,
457 struct snd_kcontrol **kcontrol)
458{
459 struct snd_soc_dapm_widget *w;
460 int i;
461
462 *kcontrol = NULL;
463
464 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600465 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
466 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600467 for (i = 0; i < w->num_kcontrols; i++) {
468 if (&w->kcontrol_news[i] == kcontrol_new) {
469 if (w->kcontrols)
470 *kcontrol = w->kcontrols[i];
471 return 1;
472 }
473 }
474 }
475
476 return 0;
477}
478
Richard Purdie2b97eab2006-10-06 18:32:18 +0200479/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200480static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200481{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200482 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200483 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000484 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200485 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000486 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000487 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600488 struct snd_soc_dapm_widget_list *wlist;
489 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000490
491 if (dapm->codec)
492 prefix = dapm->codec->name_prefix;
493 else
494 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200495
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000496 if (prefix)
497 prefix_len = strlen(prefix) + 1;
498 else
499 prefix_len = 0;
500
Richard Purdie2b97eab2006-10-06 18:32:18 +0200501 /* add kcontrol */
502 for (i = 0; i < w->num_kcontrols; i++) {
503
504 /* match name */
505 list_for_each_entry(path, &w->sources, list_sink) {
506
507 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600508 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200509 continue;
510
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200511 if (w->kcontrols[i]) {
512 path->kcontrol = w->kcontrols[i];
513 continue;
514 }
515
Stephen Warrenfafd2172011-04-28 17:38:00 -0600516 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
517 sizeof(struct snd_soc_dapm_widget *),
518 wlist = kzalloc(wlistsize, GFP_KERNEL);
519 if (wlist == NULL) {
520 dev_err(dapm->dev,
521 "asoc: can't allocate widget list for %s\n",
522 w->name);
523 return -ENOMEM;
524 }
525 wlist->num_widgets = 1;
526 wlist->widgets[0] = w;
527
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000528 /* add dapm control with long name.
529 * for dapm_mixer this is the concatenation of the
530 * mixer and kcontrol name.
531 * for dapm_mixer_named_ctl this is simply the
532 * kcontrol name.
533 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600534 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000535 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000536 name_len += 1 + strlen(w->name);
537
Mark Brown219b93f2008-10-28 13:02:31 +0000538 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000539
Stephen Warrenfafd2172011-04-28 17:38:00 -0600540 if (path->long_name == NULL) {
541 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200542 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600543 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200544
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000545 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000546 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000547 /* The control will get a prefix from
548 * the control creation process but
549 * we're also using the same prefix
550 * for widgets so cut the prefix off
551 * the front of the widget name.
552 */
Mark Brown888df392012-02-16 19:37:51 -0800553 snprintf((char *)path->long_name, name_len,
554 "%s %s", w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600555 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000556 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000557 case snd_soc_dapm_mixer_named_ctl:
Mark Brown888df392012-02-16 19:37:51 -0800558 snprintf((char *)path->long_name, name_len,
559 "%s", w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000560 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000561 }
562
Mark Brown888df392012-02-16 19:37:51 -0800563 ((char *)path->long_name)[name_len - 1] = '\0';
Mark Brown219b93f2008-10-28 13:02:31 +0000564
Stephen Warrenfafd2172011-04-28 17:38:00 -0600565 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
566 wlist, path->long_name,
567 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200568 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200569 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200570 dev_err(dapm->dev,
571 "asoc: failed to add dapm kcontrol %s: %d\n",
572 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600573 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200574 kfree(path->long_name);
575 path->long_name = NULL;
576 return ret;
577 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600578 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200579 }
580 }
581 return ret;
582}
583
584/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200585static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200586{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200587 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200588 struct snd_soc_dapm_path *path = NULL;
589 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000590 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000591 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000592 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600593 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600594 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600595 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600596 size_t wlistsize;
Mark Brown888df392012-02-16 19:37:51 -0800597 const char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200598
Stephen Warrenaf468002011-04-28 17:38:01 -0600599 if (w->num_kcontrols != 1) {
600 dev_err(dapm->dev,
601 "asoc: mux %s has incorrect number of controls\n",
602 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200603 return -EINVAL;
604 }
605
Stephen Warren1007da02011-05-26 09:57:33 -0600606 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600607 &kcontrol);
608 if (kcontrol) {
609 wlist = kcontrol->private_data;
610 wlistentries = wlist->num_widgets + 1;
611 } else {
612 wlist = NULL;
613 wlistentries = 1;
614 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600615 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600616 wlistentries * sizeof(struct snd_soc_dapm_widget *),
617 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600618 if (wlist == NULL) {
619 dev_err(dapm->dev,
620 "asoc: can't allocate widget list for %s\n", w->name);
621 return -ENOMEM;
622 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600623 wlist->num_widgets = wlistentries;
624 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600625
Stephen Warrenaf468002011-04-28 17:38:01 -0600626 if (!kcontrol) {
627 if (dapm->codec)
628 prefix = dapm->codec->name_prefix;
629 else
630 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000631
Stephen Warrenaf468002011-04-28 17:38:01 -0600632 if (shared) {
633 name = w->kcontrol_news[0].name;
634 prefix_len = 0;
635 } else {
636 name = w->name;
637 if (prefix)
638 prefix_len = strlen(prefix) + 1;
639 else
640 prefix_len = 0;
641 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000642
Stephen Warrenaf468002011-04-28 17:38:01 -0600643 /*
644 * The control will get a prefix from the control creation
645 * process but we're also using the same prefix for widgets so
646 * cut the prefix off the front of the widget name.
647 */
648 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
649 name + prefix_len, prefix);
650 ret = snd_ctl_add(card, kcontrol);
651 if (ret < 0) {
Mark Brown53daf202011-09-05 10:51:05 -0700652 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
653 w->name, ret);
Stephen Warrenaf468002011-04-28 17:38:01 -0600654 kfree(wlist);
655 return ret;
656 }
657 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200658
Stephen Warrenaf468002011-04-28 17:38:01 -0600659 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200660
Stephen Warrenfad59882011-04-28 17:37:59 -0600661 w->kcontrols[0] = kcontrol;
662
Richard Purdie2b97eab2006-10-06 18:32:18 +0200663 list_for_each_entry(path, &w->sources, list_sink)
664 path->kcontrol = kcontrol;
665
Stephen Warrenaf468002011-04-28 17:38:01 -0600666 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200667}
668
669/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200670static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200671{
Mark Browna6c65732010-03-03 17:45:21 +0000672 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200673 dev_err(w->dapm->dev,
674 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200675
Mark Browna6c65732010-03-03 17:45:21 +0000676 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200677}
678
679/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200680static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200681{
682 struct snd_soc_dapm_path *p;
683
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200684 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200685 p->walked = 0;
686}
687
Mark Brown9949788b2010-05-07 20:24:05 +0100688/* We implement power down on suspend by checking the power state of
689 * the ALSA card - when we are suspending the ALSA state for the card
690 * is set to D3.
691 */
692static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
693{
Mark Brown12ea2c72011-03-02 18:17:32 +0000694 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100695
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000696 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100697 case SNDRV_CTL_POWER_D3hot:
698 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100699 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200700 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
701 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100702 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100703 default:
704 return 1;
705 }
706}
707
Richard Purdie2b97eab2006-10-06 18:32:18 +0200708/*
709 * Recursively check for a completed path to an active or physically connected
710 * output widget. Returns number of complete paths.
711 */
712static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
713{
714 struct snd_soc_dapm_path *path;
715 int con = 0;
716
Mark Brown024dc072011-10-09 11:52:05 +0100717 if (widget->outputs >= 0)
718 return widget->outputs;
719
Mark Brownde02d072011-09-20 21:43:24 +0100720 DAPM_UPDATE_STAT(widget, path_checks);
721
Mark Brown62ea8742012-01-21 21:14:48 +0000722 switch (widget->id) {
723 case snd_soc_dapm_supply:
724 case snd_soc_dapm_regulator_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100725 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000726 default:
727 break;
728 }
Mark Brown246d0a12009-04-22 18:24:55 +0100729
Mark Brown010ff262009-08-17 17:39:22 +0100730 switch (widget->id) {
731 case snd_soc_dapm_adc:
732 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800733 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100734 if (widget->active) {
735 widget->outputs = snd_soc_dapm_suspend_check(widget);
736 return widget->outputs;
737 }
Mark Brown010ff262009-08-17 17:39:22 +0100738 default:
739 break;
740 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200741
742 if (widget->connected) {
743 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100744 if (widget->id == snd_soc_dapm_output && !widget->ext) {
745 widget->outputs = snd_soc_dapm_suspend_check(widget);
746 return widget->outputs;
747 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200748
749 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100750 if (widget->id == snd_soc_dapm_hp ||
751 widget->id == snd_soc_dapm_spk ||
752 (widget->id == snd_soc_dapm_line &&
753 !list_empty(&widget->sources))) {
754 widget->outputs = snd_soc_dapm_suspend_check(widget);
755 return widget->outputs;
756 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200757 }
758
759 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100760 DAPM_UPDATE_STAT(widget, neighbour_checks);
761
Mark Brownbf3a9e12011-06-13 16:42:29 +0100762 if (path->weak)
763 continue;
764
Richard Purdie2b97eab2006-10-06 18:32:18 +0200765 if (path->walked)
766 continue;
767
768 if (path->sink && path->connect) {
769 path->walked = 1;
770 con += is_connected_output_ep(path->sink);
771 }
772 }
773
Mark Brown024dc072011-10-09 11:52:05 +0100774 widget->outputs = con;
775
Richard Purdie2b97eab2006-10-06 18:32:18 +0200776 return con;
777}
778
779/*
780 * Recursively check for a completed path to an active or physically connected
781 * input widget. Returns number of complete paths.
782 */
783static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
784{
785 struct snd_soc_dapm_path *path;
786 int con = 0;
787
Mark Brown024dc072011-10-09 11:52:05 +0100788 if (widget->inputs >= 0)
789 return widget->inputs;
790
Mark Brownde02d072011-09-20 21:43:24 +0100791 DAPM_UPDATE_STAT(widget, path_checks);
792
Mark Brown62ea8742012-01-21 21:14:48 +0000793 switch (widget->id) {
794 case snd_soc_dapm_supply:
795 case snd_soc_dapm_regulator_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100796 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000797 default:
798 break;
799 }
Mark Brown246d0a12009-04-22 18:24:55 +0100800
Richard Purdie2b97eab2006-10-06 18:32:18 +0200801 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100802 switch (widget->id) {
803 case snd_soc_dapm_dac:
804 case snd_soc_dapm_aif_in:
Mark Brown888df392012-02-16 19:37:51 -0800805 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100806 if (widget->active) {
807 widget->inputs = snd_soc_dapm_suspend_check(widget);
808 return widget->inputs;
809 }
Mark Brown010ff262009-08-17 17:39:22 +0100810 default:
811 break;
812 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200813
814 if (widget->connected) {
815 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100816 if (widget->id == snd_soc_dapm_input && !widget->ext) {
817 widget->inputs = snd_soc_dapm_suspend_check(widget);
818 return widget->inputs;
819 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200820
821 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100822 if (widget->id == snd_soc_dapm_vmid) {
823 widget->inputs = snd_soc_dapm_suspend_check(widget);
824 return widget->inputs;
825 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200826
827 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300828 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100829 (widget->id == snd_soc_dapm_line &&
830 !list_empty(&widget->sinks))) {
831 widget->inputs = snd_soc_dapm_suspend_check(widget);
832 return widget->inputs;
833 }
834
Mark Brown1ab97c82011-11-27 16:21:51 +0000835 /* signal generator */
836 if (widget->id == snd_soc_dapm_siggen) {
837 widget->inputs = snd_soc_dapm_suspend_check(widget);
838 return widget->inputs;
839 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200840 }
841
842 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +0100843 DAPM_UPDATE_STAT(widget, neighbour_checks);
844
Mark Brownbf3a9e12011-06-13 16:42:29 +0100845 if (path->weak)
846 continue;
847
Richard Purdie2b97eab2006-10-06 18:32:18 +0200848 if (path->walked)
849 continue;
850
851 if (path->source && path->connect) {
852 path->walked = 1;
853 con += is_connected_input_ep(path->source);
854 }
855 }
856
Mark Brown024dc072011-10-09 11:52:05 +0100857 widget->inputs = con;
858
Richard Purdie2b97eab2006-10-06 18:32:18 +0200859 return con;
860}
861
862/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300863 * Handler for generic register modifier widget.
864 */
865int dapm_reg_event(struct snd_soc_dapm_widget *w,
866 struct snd_kcontrol *kcontrol, int event)
867{
868 unsigned int val;
869
870 if (SND_SOC_DAPM_EVENT_ON(event))
871 val = w->on_val;
872 else
873 val = w->off_val;
874
Liam Girdwood49575fb52012-03-06 18:16:19 +0000875 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300876 w->mask << w->shift, val << w->shift);
877
878 return 0;
879}
Mark Brown11589412008-07-29 11:42:23 +0100880EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300881
Mark Brown62ea8742012-01-21 21:14:48 +0000882/*
883 * Handler for regulator supply widget.
884 */
885int dapm_regulator_event(struct snd_soc_dapm_widget *w,
886 struct snd_kcontrol *kcontrol, int event)
887{
888 if (SND_SOC_DAPM_EVENT_ON(event))
Liam Girdwooda3cc0562012-03-09 17:20:16 +0000889 return regulator_enable(w->regulator);
Mark Brown62ea8742012-01-21 21:14:48 +0000890 else
Liam Girdwooda3cc0562012-03-09 17:20:16 +0000891 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brown62ea8742012-01-21 21:14:48 +0000892}
893EXPORT_SYMBOL_GPL(dapm_regulator_event);
894
Mark Brownd8050022011-09-28 18:28:23 +0100895static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
896{
Mark Brown9b8a83b2011-10-04 22:15:59 +0100897 if (w->power_checked)
898 return w->new_power;
899
Mark Brownd8050022011-09-28 18:28:23 +0100900 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +0100901 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +0100902 else
Mark Brown9b8a83b2011-10-04 22:15:59 +0100903 w->new_power = w->power_check(w);
904
905 w->power_checked = true;
906
907 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +0100908}
909
Mark Browncd0f2d42009-04-20 16:56:59 +0100910/* Generic check to see if a widget should be powered.
911 */
912static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
913{
914 int in, out;
915
Mark Brownde02d072011-09-20 21:43:24 +0100916 DAPM_UPDATE_STAT(w, power_checks);
917
Mark Browncd0f2d42009-04-20 16:56:59 +0100918 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200919 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100920 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200921 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100922 return out != 0 && in != 0;
923}
924
Mark Brown888df392012-02-16 19:37:51 -0800925static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
926{
927 DAPM_UPDATE_STAT(w, power_checks);
928
Mark Brown1eee1b32012-04-10 13:57:46 +0100929 if (w->active)
930 return w->active;
931
932 return dapm_generic_check_power(w);
Mark Brown888df392012-02-16 19:37:51 -0800933}
934
Mark Brown6ea31b92009-04-20 17:15:41 +0100935/* Check to see if an ADC has power */
936static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
937{
938 int in;
939
Mark Brownde02d072011-09-20 21:43:24 +0100940 DAPM_UPDATE_STAT(w, power_checks);
941
Mark Brown6ea31b92009-04-20 17:15:41 +0100942 if (w->active) {
943 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200944 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100945 return in != 0;
946 } else {
947 return dapm_generic_check_power(w);
948 }
949}
950
951/* Check to see if a DAC has power */
952static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
953{
954 int out;
955
Mark Brownde02d072011-09-20 21:43:24 +0100956 DAPM_UPDATE_STAT(w, power_checks);
957
Mark Brown6ea31b92009-04-20 17:15:41 +0100958 if (w->active) {
959 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200960 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100961 return out != 0;
962 } else {
963 return dapm_generic_check_power(w);
964 }
965}
966
Mark Brown246d0a12009-04-22 18:24:55 +0100967/* Check to see if a power supply is needed */
968static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
969{
970 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +0100971
Mark Brownde02d072011-09-20 21:43:24 +0100972 DAPM_UPDATE_STAT(w, power_checks);
973
Mark Brown246d0a12009-04-22 18:24:55 +0100974 /* Check if one of our outputs is connected */
975 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +0100976 DAPM_UPDATE_STAT(w, neighbour_checks);
977
Mark Brownbf3a9e12011-06-13 16:42:29 +0100978 if (path->weak)
979 continue;
980
Mark Brown215edda2009-09-08 18:59:05 +0100981 if (path->connected &&
982 !path->connected(path->source, path->sink))
983 continue;
984
Mark Brown30173582011-02-11 11:42:19 +0000985 if (!path->sink)
986 continue;
987
Mark Brownf68d7e12011-10-04 22:57:50 +0100988 if (dapm_widget_power_check(path->sink))
989 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +0100990 }
991
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200992 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100993
Mark Brownf68d7e12011-10-04 22:57:50 +0100994 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +0100995}
996
Mark Brown35c64bc2011-09-28 18:23:53 +0100997static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
998{
999 return 1;
1000}
1001
Mark Brown38357ab2009-06-06 19:03:23 +01001002static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1003 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001004 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001005{
Mark Brown828a8422011-01-15 13:14:30 +00001006 int *sort;
1007
1008 if (power_up)
1009 sort = dapm_up_seq;
1010 else
1011 sort = dapm_down_seq;
1012
Mark Brown38357ab2009-06-06 19:03:23 +01001013 if (sort[a->id] != sort[b->id])
1014 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001015 if (a->subseq != b->subseq) {
1016 if (power_up)
1017 return a->subseq - b->subseq;
1018 else
1019 return b->subseq - a->subseq;
1020 }
Mark Brownb22ead22009-06-07 12:51:26 +01001021 if (a->reg != b->reg)
1022 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001023 if (a->dapm != b->dapm)
1024 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001025
Mark Brown38357ab2009-06-06 19:03:23 +01001026 return 0;
1027}
Mark Brown42aa3412009-03-01 19:21:10 +00001028
Mark Brown38357ab2009-06-06 19:03:23 +01001029/* Insert a widget in order into a DAPM power sequence. */
1030static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1031 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001032 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001033{
1034 struct snd_soc_dapm_widget *w;
1035
1036 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001037 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001038 list_add_tail(&new_widget->power_list, &w->power_list);
1039 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001040 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001041
Mark Brown38357ab2009-06-06 19:03:23 +01001042 list_add_tail(&new_widget->power_list, list);
1043}
Mark Brown42aa3412009-03-01 19:21:10 +00001044
Mark Brown68f89ad2010-11-03 23:51:49 -04001045static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1046 struct snd_soc_dapm_widget *w, int event)
1047{
1048 struct snd_soc_card *card = dapm->card;
1049 const char *ev_name;
1050 int power, ret;
1051
1052 switch (event) {
1053 case SND_SOC_DAPM_PRE_PMU:
1054 ev_name = "PRE_PMU";
1055 power = 1;
1056 break;
1057 case SND_SOC_DAPM_POST_PMU:
1058 ev_name = "POST_PMU";
1059 power = 1;
1060 break;
1061 case SND_SOC_DAPM_PRE_PMD:
1062 ev_name = "PRE_PMD";
1063 power = 0;
1064 break;
1065 case SND_SOC_DAPM_POST_PMD:
1066 ev_name = "POST_PMD";
1067 power = 0;
1068 break;
1069 default:
1070 BUG();
1071 return;
1072 }
1073
1074 if (w->power != power)
1075 return;
1076
1077 if (w->event && (w->event_flags & event)) {
1078 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1079 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001080 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001081 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001082 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001083 if (ret < 0)
1084 pr_err("%s: %s event failed: %d\n",
1085 ev_name, w->name, ret);
1086 }
1087}
1088
Mark Brownb22ead22009-06-07 12:51:26 +01001089/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001090static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001091 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001092{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001093 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001094 struct snd_soc_dapm_widget *w;
1095 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001096 unsigned int value = 0;
1097 unsigned int mask = 0;
1098 unsigned int cur_mask;
1099
1100 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1101 power_list)->reg;
1102
1103 list_for_each_entry(w, pending, power_list) {
1104 cur_mask = 1 << w->shift;
1105 BUG_ON(reg != w->reg);
1106
1107 if (w->invert)
1108 power = !w->power;
1109 else
1110 power = w->power;
1111
1112 mask |= cur_mask;
1113 if (power)
1114 value |= cur_mask;
1115
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001116 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001117 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1118 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001119
Mark Brown68f89ad2010-11-03 23:51:49 -04001120 /* Check for events */
1121 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1122 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001123 }
1124
Mark Brown81628102009-06-07 13:21:24 +01001125 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001126 /* Any widget will do, they should all be updating the
1127 * same register.
1128 */
1129 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1130 power_list);
1131
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001132 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001133 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001134 value, mask, reg, card->pop_time);
1135 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001136 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001137 }
1138
1139 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001140 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1141 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001142 }
Mark Brown42aa3412009-03-01 19:21:10 +00001143}
1144
Mark Brownb22ead22009-06-07 12:51:26 +01001145/* Apply a DAPM power sequence.
1146 *
1147 * We walk over a pre-sorted list of widgets to apply power to. In
1148 * order to minimise the number of writes to the device required
1149 * multiple widgets will be updated in a single write where possible.
1150 * Currently anything that requires more than a single write is not
1151 * handled.
1152 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001153static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001154 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001155{
1156 struct snd_soc_dapm_widget *w, *n;
1157 LIST_HEAD(pending);
1158 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001159 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001160 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001161 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001162 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001163 int *sort;
1164
1165 if (power_up)
1166 sort = dapm_up_seq;
1167 else
1168 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001169
Mark Brownb22ead22009-06-07 12:51:26 +01001170 list_for_each_entry_safe(w, n, list, power_list) {
1171 ret = 0;
1172
1173 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001174 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001175 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001176 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001177 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001178
Mark Brown474b62d2011-01-18 16:14:44 +00001179 if (cur_dapm && cur_dapm->seq_notifier) {
1180 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1181 if (sort[i] == cur_sort)
1182 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001183 i,
1184 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001185 }
1186
Mark Brownb22ead22009-06-07 12:51:26 +01001187 INIT_LIST_HEAD(&pending);
1188 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001189 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001190 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001191 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001192 }
1193
Mark Brown163cac02009-06-07 10:12:52 +01001194 switch (w->id) {
1195 case snd_soc_dapm_pre:
1196 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001197 list_for_each_entry_safe_continue(w, n, list,
1198 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001199
Mark Brownb22ead22009-06-07 12:51:26 +01001200 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001201 ret = w->event(w,
1202 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001203 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001204 ret = w->event(w,
1205 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001206 break;
1207
1208 case snd_soc_dapm_post:
1209 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001210 list_for_each_entry_safe_continue(w, n, list,
1211 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001212
Mark Brownb22ead22009-06-07 12:51:26 +01001213 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001214 ret = w->event(w,
1215 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001216 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001217 ret = w->event(w,
1218 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001219 break;
1220
Mark Brown163cac02009-06-07 10:12:52 +01001221 default:
Mark Brown81628102009-06-07 13:21:24 +01001222 /* Queue it up for application */
1223 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001224 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001225 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001226 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001227 list_move(&w->power_list, &pending);
1228 break;
Mark Brown163cac02009-06-07 10:12:52 +01001229 }
Mark Brownb22ead22009-06-07 12:51:26 +01001230
1231 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001232 dev_err(w->dapm->dev,
1233 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001234 }
Mark Brownb22ead22009-06-07 12:51:26 +01001235
1236 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001237 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001238
1239 if (cur_dapm && cur_dapm->seq_notifier) {
1240 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1241 if (sort[i] == cur_sort)
1242 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001243 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001244 }
Mark Brown163cac02009-06-07 10:12:52 +01001245}
1246
Mark Brown97404f22010-12-14 16:13:57 +00001247static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1248{
1249 struct snd_soc_dapm_update *update = dapm->update;
1250 struct snd_soc_dapm_widget *w;
1251 int ret;
1252
1253 if (!update)
1254 return;
1255
1256 w = update->widget;
1257
1258 if (w->event &&
1259 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1260 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1261 if (ret != 0)
1262 pr_err("%s DAPM pre-event failed: %d\n",
1263 w->name, ret);
1264 }
1265
Liam Girdwood49575fb52012-03-06 18:16:19 +00001266 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001267 update->val);
1268 if (ret < 0)
1269 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1270
1271 if (w->event &&
1272 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1273 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1274 if (ret != 0)
1275 pr_err("%s DAPM post-event failed: %d\n",
1276 w->name, ret);
1277 }
1278}
1279
Mark Brown9d0624a2011-02-18 11:49:43 -08001280/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1281 * they're changing state.
1282 */
1283static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1284{
1285 struct snd_soc_dapm_context *d = data;
1286 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001287
Mark Brown56fba412011-06-04 11:25:10 +01001288 /* If we're off and we're not supposed to be go into STANDBY */
1289 if (d->bias_level == SND_SOC_BIAS_OFF &&
1290 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001291 if (d->dev)
1292 pm_runtime_get_sync(d->dev);
1293
Mark Brown9d0624a2011-02-18 11:49:43 -08001294 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1295 if (ret != 0)
1296 dev_err(d->dev,
1297 "Failed to turn on bias: %d\n", ret);
1298 }
1299
Mark Brown56fba412011-06-04 11:25:10 +01001300 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1301 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001302 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1303 if (ret != 0)
1304 dev_err(d->dev,
1305 "Failed to prepare bias: %d\n", ret);
1306 }
1307}
1308
1309/* Async callback run prior to DAPM sequences - brings to their final
1310 * state.
1311 */
1312static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1313{
1314 struct snd_soc_dapm_context *d = data;
1315 int ret;
1316
1317 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001318 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1319 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1320 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001321 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1322 if (ret != 0)
1323 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1324 ret);
1325 }
1326
1327 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001328 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1329 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001330 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1331 if (ret != 0)
1332 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001333
1334 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001335 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001336 }
1337
1338 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001339 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1340 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001341 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1342 if (ret != 0)
1343 dev_err(d->dev, "Failed to apply active bias: %d\n",
1344 ret);
1345 }
1346}
Mark Brown97404f22010-12-14 16:13:57 +00001347
Mark Brownfe4fda52011-10-03 22:36:57 +01001348static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1349 bool power, bool connect)
1350{
1351 /* If a connection is being made or broken then that update
1352 * will have marked the peer dirty, otherwise the widgets are
1353 * not connected and this update has no impact. */
1354 if (!connect)
1355 return;
1356
1357 /* If the peer is already in the state we're moving to then we
1358 * won't have an impact on it. */
1359 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001360 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001361}
1362
Mark Brown05623c42011-09-28 17:02:31 +01001363static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1364 struct list_head *up_list,
1365 struct list_head *down_list)
1366{
Mark Browndb432b42011-10-03 21:06:40 +01001367 struct snd_soc_dapm_path *path;
1368
Mark Brown05623c42011-09-28 17:02:31 +01001369 if (w->power == power)
1370 return;
1371
1372 trace_snd_soc_dapm_widget_power(w, power);
1373
Mark Browndb432b42011-10-03 21:06:40 +01001374 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001375 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001376 */
1377 list_for_each_entry(path, &w->sources, list_sink) {
1378 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001379 dapm_widget_set_peer_power(path->source, power,
1380 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001381 }
1382 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001383 switch (w->id) {
1384 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001385 case snd_soc_dapm_regulator_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001386 /* Supplies can't affect their outputs, only their inputs */
1387 break;
1388 default:
1389 list_for_each_entry(path, &w->sinks, list_source) {
1390 if (path->sink) {
1391 dapm_widget_set_peer_power(path->sink, power,
1392 path->connect);
1393 }
Mark Browndb432b42011-10-03 21:06:40 +01001394 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001395 break;
Mark Browndb432b42011-10-03 21:06:40 +01001396 }
1397
Mark Brown05623c42011-09-28 17:02:31 +01001398 if (power)
1399 dapm_seq_insert(w, up_list, true);
1400 else
1401 dapm_seq_insert(w, down_list, false);
1402
1403 w->power = power;
1404}
1405
Mark Brown7c81beb2011-09-20 22:22:32 +01001406static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1407 struct list_head *up_list,
1408 struct list_head *down_list)
1409{
Mark Brown7c81beb2011-09-20 22:22:32 +01001410 int power;
1411
1412 switch (w->id) {
1413 case snd_soc_dapm_pre:
1414 dapm_seq_insert(w, down_list, false);
1415 break;
1416 case snd_soc_dapm_post:
1417 dapm_seq_insert(w, up_list, true);
1418 break;
1419
1420 default:
Mark Brownd8050022011-09-28 18:28:23 +01001421 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001422
Mark Brown05623c42011-09-28 17:02:31 +01001423 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001424 break;
1425 }
1426}
1427
Mark Brown42aa3412009-03-01 19:21:10 +00001428/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001429 * Scan each dapm widget for complete audio path.
1430 * A complete path is a route that has valid endpoints i.e.:-
1431 *
1432 * o DAC to output pin.
1433 * o Input Pin to ADC.
1434 * o Input pin to Output pin (bypass, sidetone)
1435 * o DAC to ADC (loopback).
1436 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001437static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001438{
Mark Brown12ea2c72011-03-02 18:17:32 +00001439 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001440 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001441 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001442 LIST_HEAD(up_list);
1443 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001444 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001445 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001446
Mark Brown84e90932010-11-04 00:07:02 -04001447 trace_snd_soc_dapm_start(card);
1448
Mark Brown56fba412011-06-04 11:25:10 +01001449 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001450 if (d->idle_bias_off)
1451 d->target_bias_level = SND_SOC_BIAS_OFF;
1452 else
1453 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001454 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001455
Liam Girdwood6c120e12012-02-15 15:15:34 +00001456 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001457
Mark Brown6d3ddc82009-05-16 17:47:29 +01001458 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001459 * lists indicating if they should be powered up or down. We
1460 * only check widgets that have been flagged as dirty but note
1461 * that new widgets may be added to the dirty list while we
1462 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001463 */
Mark Browndb432b42011-10-03 21:06:40 +01001464 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001465 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001466 }
1467
Mark Brownf9de6d72011-09-28 17:19:47 +01001468 list_for_each_entry(w, &card->widgets, list) {
Mark Browndb432b42011-10-03 21:06:40 +01001469 list_del_init(&w->dirty);
1470
Mark Brownf9de6d72011-09-28 17:19:47 +01001471 if (w->power) {
1472 d = w->dapm;
1473
1474 /* Supplies and micbiases only bring the
1475 * context up to STANDBY as unless something
1476 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001477 * generally don't require full power. Signal
1478 * generators are virtual pins and have no
1479 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001480 */
1481 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001482 case snd_soc_dapm_siggen:
1483 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001484 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001485 case snd_soc_dapm_regulator_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001486 case snd_soc_dapm_micbias:
1487 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1488 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1489 break;
1490 default:
1491 d->target_bias_level = SND_SOC_BIAS_ON;
1492 break;
1493 }
1494 }
1495
1496 }
1497
Mark Brown85a843c2011-09-21 21:29:47 +01001498 /* Force all contexts in the card to the same bias state if
1499 * they're not ground referenced.
1500 */
Mark Brown56fba412011-06-04 11:25:10 +01001501 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001502 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001503 if (d->target_bias_level > bias)
1504 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001505 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001506 if (!d->idle_bias_off)
1507 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001508
Mark Brownde02d072011-09-20 21:43:24 +01001509 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001510
Mark Brown9d0624a2011-02-18 11:49:43 -08001511 /* Run all the bias changes in parallel */
1512 list_for_each_entry(d, &dapm->card->dapm_list, list)
1513 async_schedule_domain(dapm_pre_sequence_async, d,
1514 &async_domain);
1515 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001516
Mark Brown6d3ddc82009-05-16 17:47:29 +01001517 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001518 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001519
Mark Brown97404f22010-12-14 16:13:57 +00001520 dapm_widget_update(dapm);
1521
Mark Brown6d3ddc82009-05-16 17:47:29 +01001522 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001523 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001524
Mark Brown9d0624a2011-02-18 11:49:43 -08001525 /* Run all the bias changes in parallel */
1526 list_for_each_entry(d, &dapm->card->dapm_list, list)
1527 async_schedule_domain(dapm_post_sequence_async, d,
1528 &async_domain);
1529 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001530
Liam Girdwood8078d872012-02-15 15:15:35 +00001531 /* do we need to notify any clients that DAPM event is complete */
1532 list_for_each_entry(d, &card->dapm_list, list) {
1533 if (d->stream_event)
1534 d->stream_event(d, event);
1535 }
1536
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001537 pop_dbg(dapm->dev, card->pop_time,
1538 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001539 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001540
Mark Brown84e90932010-11-04 00:07:02 -04001541 trace_snd_soc_dapm_done(card);
1542
Mark Brown42aa3412009-03-01 19:21:10 +00001543 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001544}
1545
Mark Brown79fb9382009-08-21 16:38:13 +01001546#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001547static ssize_t dapm_widget_power_read_file(struct file *file,
1548 char __user *user_buf,
1549 size_t count, loff_t *ppos)
1550{
1551 struct snd_soc_dapm_widget *w = file->private_data;
1552 char *buf;
1553 int in, out;
1554 ssize_t ret;
1555 struct snd_soc_dapm_path *p = NULL;
1556
1557 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1558 if (!buf)
1559 return -ENOMEM;
1560
1561 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001562 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001563 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001564 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001565
Mark Brownf13ebad2012-03-03 18:01:01 +00001566 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1567 w->name, w->power ? "On" : "Off",
1568 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001569
Mark Brownd033c362009-12-04 15:25:56 +00001570 if (w->reg >= 0)
1571 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1572 " - R%d(0x%x) bit %d",
1573 w->reg, w->reg, w->shift);
1574
1575 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1576
Mark Brown3eef08b2009-09-14 16:49:00 +01001577 if (w->sname)
1578 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1579 w->sname,
1580 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001581
1582 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001583 if (p->connected && !p->connected(w, p->sink))
1584 continue;
1585
Mark Brown79fb9382009-08-21 16:38:13 +01001586 if (p->connect)
1587 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001588 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001589 p->name ? p->name : "static",
1590 p->source->name);
1591 }
1592 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001593 if (p->connected && !p->connected(w, p->sink))
1594 continue;
1595
Mark Brown79fb9382009-08-21 16:38:13 +01001596 if (p->connect)
1597 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001598 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001599 p->name ? p->name : "static",
1600 p->sink->name);
1601 }
1602
1603 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1604
1605 kfree(buf);
1606 return ret;
1607}
1608
1609static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001610 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001611 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001612 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001613};
1614
Mark Brownef49e4f2011-04-04 20:48:13 +09001615static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1616 size_t count, loff_t *ppos)
1617{
1618 struct snd_soc_dapm_context *dapm = file->private_data;
1619 char *level;
1620
1621 switch (dapm->bias_level) {
1622 case SND_SOC_BIAS_ON:
1623 level = "On\n";
1624 break;
1625 case SND_SOC_BIAS_PREPARE:
1626 level = "Prepare\n";
1627 break;
1628 case SND_SOC_BIAS_STANDBY:
1629 level = "Standby\n";
1630 break;
1631 case SND_SOC_BIAS_OFF:
1632 level = "Off\n";
1633 break;
1634 default:
1635 BUG();
1636 level = "Unknown\n";
1637 break;
1638 }
1639
1640 return simple_read_from_buffer(user_buf, count, ppos, level,
1641 strlen(level));
1642}
1643
1644static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001645 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001646 .read = dapm_bias_read_file,
1647 .llseek = default_llseek,
1648};
1649
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001650void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1651 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001652{
Mark Brown79fb9382009-08-21 16:38:13 +01001653 struct dentry *d;
1654
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001655 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1656
1657 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001658 dev_warn(dapm->dev,
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001659 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001660 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001661 }
Mark Brown79fb9382009-08-21 16:38:13 +01001662
Mark Brownef49e4f2011-04-04 20:48:13 +09001663 d = debugfs_create_file("bias_level", 0444,
1664 dapm->debugfs_dapm, dapm,
1665 &dapm_bias_fops);
1666 if (!d)
1667 dev_warn(dapm->dev,
1668 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001669}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001670
1671static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1672{
1673 struct snd_soc_dapm_context *dapm = w->dapm;
1674 struct dentry *d;
1675
1676 if (!dapm->debugfs_dapm || !w->name)
1677 return;
1678
1679 d = debugfs_create_file(w->name, 0444,
1680 dapm->debugfs_dapm, w,
1681 &dapm_widget_power_fops);
1682 if (!d)
1683 dev_warn(w->dapm->dev,
1684 "ASoC: Failed to create %s debugfs file\n",
1685 w->name);
1686}
1687
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001688static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1689{
1690 debugfs_remove_recursive(dapm->debugfs_dapm);
1691}
1692
Mark Brown79fb9382009-08-21 16:38:13 +01001693#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001694void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1695 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001696{
1697}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001698
1699static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1700{
1701}
1702
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001703static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1704{
1705}
1706
Mark Brown79fb9382009-08-21 16:38:13 +01001707#endif
1708
Richard Purdie2b97eab2006-10-06 18:32:18 +02001709/* test and update the power status of a mux widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001710static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001711 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001712{
1713 struct snd_soc_dapm_path *path;
1714 int found = 0;
1715
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001716 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001717 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001718 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001719 return -ENODEV;
1720
Richard Purdie2b97eab2006-10-06 18:32:18 +02001721 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001722 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001723 if (path->kcontrol != kcontrol)
1724 continue;
1725
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001726 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001727 continue;
1728
1729 found = 1;
1730 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001731 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001732 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001733 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001734 } else {
1735 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001736 dapm_mark_dirty(path->source,
1737 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001738 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001739 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001740 }
1741
Mark Browndb432b42011-10-03 21:06:40 +01001742 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001743 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001744 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001745 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001746
1747 return 0;
1748}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001749
1750int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1751 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1752{
1753 struct snd_soc_card *card = widget->dapm->card;
1754 int ret;
1755
Liam Girdwood3cd04342012-03-09 12:02:08 +00001756 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001757 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1758 mutex_unlock(&card->dapm_mutex);
1759 return ret;
1760}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001761EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001762
Milan plzik1b075e32008-01-10 14:39:46 +01001763/* test and update the power status of a mixer or switch widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001764static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001765 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001766{
1767 struct snd_soc_dapm_path *path;
1768 int found = 0;
1769
Milan plzik1b075e32008-01-10 14:39:46 +01001770 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001771 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001772 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001773 return -ENODEV;
1774
Richard Purdie2b97eab2006-10-06 18:32:18 +02001775 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001776 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001777 if (path->kcontrol != kcontrol)
1778 continue;
1779
1780 /* found, now check type */
1781 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001782 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001783 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001784 }
1785
Mark Browndb432b42011-10-03 21:06:40 +01001786 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001787 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001788 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001789 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001790
1791 return 0;
1792}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001793
1794int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1795 struct snd_kcontrol *kcontrol, int connect)
1796{
1797 struct snd_soc_card *card = widget->dapm->card;
1798 int ret;
1799
Liam Girdwood3cd04342012-03-09 12:02:08 +00001800 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001801 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
1802 mutex_unlock(&card->dapm_mutex);
1803 return ret;
1804}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001805EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001806
1807/* show dapm widget status in sys fs */
1808static ssize_t dapm_widget_show(struct device *dev,
1809 struct device_attribute *attr, char *buf)
1810{
Mark Brown36ae1a92012-01-06 17:12:45 -08001811 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001812 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001813 struct snd_soc_dapm_widget *w;
1814 int count = 0;
1815 char *state = "not set";
1816
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001817 list_for_each_entry(w, &codec->card->widgets, list) {
1818 if (w->dapm != &codec->dapm)
1819 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001820
1821 /* only display widgets that burnm power */
1822 switch (w->id) {
1823 case snd_soc_dapm_hp:
1824 case snd_soc_dapm_mic:
1825 case snd_soc_dapm_spk:
1826 case snd_soc_dapm_line:
1827 case snd_soc_dapm_micbias:
1828 case snd_soc_dapm_dac:
1829 case snd_soc_dapm_adc:
1830 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001831 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001832 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001833 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001834 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001835 case snd_soc_dapm_regulator_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001836 if (w->name)
1837 count += sprintf(buf + count, "%s: %s\n",
1838 w->name, w->power ? "On":"Off");
1839 break;
1840 default:
1841 break;
1842 }
1843 }
1844
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001845 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001846 case SND_SOC_BIAS_ON:
1847 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001848 break;
Mark Brown0be98982008-05-19 12:31:28 +02001849 case SND_SOC_BIAS_PREPARE:
1850 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001851 break;
Mark Brown0be98982008-05-19 12:31:28 +02001852 case SND_SOC_BIAS_STANDBY:
1853 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001854 break;
Mark Brown0be98982008-05-19 12:31:28 +02001855 case SND_SOC_BIAS_OFF:
1856 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001857 break;
1858 }
1859 count += sprintf(buf + count, "PM State: %s\n", state);
1860
1861 return count;
1862}
1863
1864static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1865
1866int snd_soc_dapm_sys_add(struct device *dev)
1867{
Troy Kisky12ef1932008-10-13 17:42:14 -07001868 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001869}
1870
1871static void snd_soc_dapm_sys_remove(struct device *dev)
1872{
Mark Brownaef90842009-05-16 17:53:16 +01001873 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001874}
1875
1876/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001877static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001878{
1879 struct snd_soc_dapm_widget *w, *next_w;
1880 struct snd_soc_dapm_path *p, *next_p;
1881
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001882 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1883 if (w->dapm != dapm)
1884 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001885 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001886 /*
1887 * remove source and sink paths associated to this widget.
1888 * While removing the path, remove reference to it from both
1889 * source and sink widgets so that path is removed only once.
1890 */
1891 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1892 list_del(&p->list_sink);
1893 list_del(&p->list_source);
1894 list_del(&p->list);
1895 kfree(p->long_name);
1896 kfree(p);
1897 }
1898 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1899 list_del(&p->list_sink);
1900 list_del(&p->list_source);
1901 list_del(&p->list);
1902 kfree(p->long_name);
1903 kfree(p);
1904 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001905 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001906 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001907 kfree(w);
1908 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001909}
1910
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001911static struct snd_soc_dapm_widget *dapm_find_widget(
1912 struct snd_soc_dapm_context *dapm, const char *pin,
1913 bool search_other_contexts)
1914{
1915 struct snd_soc_dapm_widget *w;
1916 struct snd_soc_dapm_widget *fallback = NULL;
1917
1918 list_for_each_entry(w, &dapm->card->widgets, list) {
1919 if (!strcmp(w->name, pin)) {
1920 if (w->dapm == dapm)
1921 return w;
1922 else
1923 fallback = w;
1924 }
1925 }
1926
1927 if (search_other_contexts)
1928 return fallback;
1929
1930 return NULL;
1931}
1932
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001933static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001934 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001935{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001936 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001937
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001938 if (!w) {
1939 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1940 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001941 }
1942
Mark Brown1a8b2d92012-02-16 11:50:07 -08001943 if (w->connected != status)
1944 dapm_mark_dirty(w, "pin configuration");
1945
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001946 w->connected = status;
1947 if (status == 0)
1948 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001949
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001950 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001951}
1952
Richard Purdie2b97eab2006-10-06 18:32:18 +02001953/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001954 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001955 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001956 *
1957 * Walks all dapm audio paths and powers widgets according to their
1958 * stream or path usage.
1959 *
1960 * Returns 0 for success.
1961 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001962int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001963{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00001964 int ret;
1965
Mark Brown4f4c0072011-10-07 14:29:19 +01001966 /*
1967 * Suppress early reports (eg, jacks syncing their state) to avoid
1968 * silly DAPM runs during card startup.
1969 */
1970 if (!dapm->card || !dapm->card->instantiated)
1971 return 0;
1972
Liam Girdwood3cd04342012-03-09 12:02:08 +00001973 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00001974 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1975 mutex_unlock(&dapm->card->dapm_mutex);
1976 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001977}
Liam Girdwooda5302182008-07-07 13:35:17 +01001978EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001979
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001980static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001981 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001982{
1983 struct snd_soc_dapm_path *path;
1984 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001985 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001986 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001987 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001988 const char *source;
1989 char prefixed_sink[80];
1990 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001991 int ret = 0;
1992
Mark Brown88e8b9a2011-03-02 18:18:24 +00001993 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001994 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1995 dapm->codec->name_prefix, route->sink);
1996 sink = prefixed_sink;
1997 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1998 dapm->codec->name_prefix, route->source);
1999 source = prefixed_source;
2000 } else {
2001 sink = route->sink;
2002 source = route->source;
2003 }
2004
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002005 /*
2006 * find src and dest widgets over all widgets but favor a widget from
2007 * current DAPM context
2008 */
2009 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002010 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002011 wtsink = w;
2012 if (w->dapm == dapm)
2013 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002014 continue;
2015 }
2016 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002017 wtsource = w;
2018 if (w->dapm == dapm)
2019 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002020 }
2021 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002022 /* use widget from another DAPM context if not found from this */
2023 if (!wsink)
2024 wsink = wtsink;
2025 if (!wsource)
2026 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002027
2028 if (wsource == NULL || wsink == NULL)
2029 return -ENODEV;
2030
2031 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2032 if (!path)
2033 return -ENOMEM;
2034
2035 path->source = wsource;
2036 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002037 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002038 INIT_LIST_HEAD(&path->list);
2039 INIT_LIST_HEAD(&path->list_source);
2040 INIT_LIST_HEAD(&path->list_sink);
2041
2042 /* check for external widgets */
2043 if (wsink->id == snd_soc_dapm_input) {
2044 if (wsource->id == snd_soc_dapm_micbias ||
2045 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002046 wsource->id == snd_soc_dapm_line ||
2047 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002048 wsink->ext = 1;
2049 }
2050 if (wsource->id == snd_soc_dapm_output) {
2051 if (wsink->id == snd_soc_dapm_spk ||
2052 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002053 wsink->id == snd_soc_dapm_line ||
2054 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002055 wsource->ext = 1;
2056 }
2057
2058 /* connect static paths */
2059 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002060 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002061 list_add(&path->list_sink, &wsink->sources);
2062 list_add(&path->list_source, &wsource->sinks);
2063 path->connect = 1;
2064 return 0;
2065 }
2066
2067 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002068 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002069 case snd_soc_dapm_adc:
2070 case snd_soc_dapm_dac:
2071 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002072 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002073 case snd_soc_dapm_input:
2074 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002075 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002076 case snd_soc_dapm_micbias:
2077 case snd_soc_dapm_vmid:
2078 case snd_soc_dapm_pre:
2079 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002080 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002081 case snd_soc_dapm_regulator_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002082 case snd_soc_dapm_aif_in:
2083 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -08002084 case snd_soc_dapm_dai:
Mark Brownc74184e2012-04-04 22:12:09 +01002085 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002086 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002087 list_add(&path->list_sink, &wsink->sources);
2088 list_add(&path->list_source, &wsource->sinks);
2089 path->connect = 1;
2090 return 0;
2091 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002092 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002093 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002094 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002095 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002096 if (ret != 0)
2097 goto err;
2098 break;
2099 case snd_soc_dapm_switch:
2100 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002101 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002102 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002103 if (ret != 0)
2104 goto err;
2105 break;
2106 case snd_soc_dapm_hp:
2107 case snd_soc_dapm_mic:
2108 case snd_soc_dapm_line:
2109 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002110 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002111 list_add(&path->list_sink, &wsink->sources);
2112 list_add(&path->list_source, &wsource->sinks);
2113 path->connect = 0;
2114 return 0;
2115 }
2116 return 0;
2117
2118err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002119 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2120 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002121 kfree(path);
2122 return ret;
2123}
Mark Brown105f1c22008-05-13 14:52:19 +02002124
2125/**
Mark Brown105f1c22008-05-13 14:52:19 +02002126 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002127 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002128 * @route: audio routes
2129 * @num: number of routes
2130 *
2131 * Connects 2 dapm widgets together via a named audio path. The sink is
2132 * the widget receiving the audio signal, whilst the source is the sender
2133 * of the audio signal.
2134 *
2135 * Returns 0 for success else error. On error all resources can be freed
2136 * with a call to snd_soc_card_free().
2137 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002138int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002139 const struct snd_soc_dapm_route *route, int num)
2140{
Dan Carpenter60884c22012-04-13 22:25:43 +03002141 int i, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002142
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002143 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002144 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002145 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02002146 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002147 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2148 route->source, route->sink);
Dan Carpenter60884c22012-04-13 22:25:43 +03002149 break;
Mark Brown105f1c22008-05-13 14:52:19 +02002150 }
2151 route++;
2152 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002153 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002154
Dan Carpenter60884c22012-04-13 22:25:43 +03002155 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002156}
2157EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2158
Mark Brownbf3a9e12011-06-13 16:42:29 +01002159static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2160 const struct snd_soc_dapm_route *route)
2161{
2162 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2163 route->source,
2164 true);
2165 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2166 route->sink,
2167 true);
2168 struct snd_soc_dapm_path *path;
2169 int count = 0;
2170
2171 if (!source) {
2172 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2173 route->source);
2174 return -ENODEV;
2175 }
2176
2177 if (!sink) {
2178 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2179 route->sink);
2180 return -ENODEV;
2181 }
2182
2183 if (route->control || route->connected)
2184 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2185 route->source, route->sink);
2186
2187 list_for_each_entry(path, &source->sinks, list_source) {
2188 if (path->sink == sink) {
2189 path->weak = 1;
2190 count++;
2191 }
2192 }
2193
2194 if (count == 0)
2195 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2196 route->source, route->sink);
2197 if (count > 1)
2198 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2199 count, route->source, route->sink);
2200
2201 return 0;
2202}
2203
2204/**
2205 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2206 * @dapm: DAPM context
2207 * @route: audio routes
2208 * @num: number of routes
2209 *
2210 * Mark existing routes matching those specified in the passed array
2211 * as being weak, meaning that they are ignored for the purpose of
2212 * power decisions. The main intended use case is for sidetone paths
2213 * which couple audio between other independent paths if they are both
2214 * active in order to make the combination work better at the user
2215 * level but which aren't intended to be "used".
2216 *
2217 * Note that CODEC drivers should not use this as sidetone type paths
2218 * can frequently also be used as bypass paths.
2219 */
2220int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2221 const struct snd_soc_dapm_route *route, int num)
2222{
2223 int i, err;
2224 int ret = 0;
2225
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002226 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002227 for (i = 0; i < num; i++) {
2228 err = snd_soc_dapm_weak_route(dapm, route);
2229 if (err)
2230 ret = err;
2231 route++;
2232 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002233 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002234
2235 return ret;
2236}
2237EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2238
Mark Brown105f1c22008-05-13 14:52:19 +02002239/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002240 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002241 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002242 *
2243 * Checks the codec for any new dapm widgets and creates them if found.
2244 *
2245 * Returns 0 for success.
2246 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002247int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002248{
2249 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002250 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002251
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002252 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2253
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002254 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002255 {
2256 if (w->new)
2257 continue;
2258
Stephen Warrenfad59882011-04-28 17:37:59 -06002259 if (w->num_kcontrols) {
2260 w->kcontrols = kzalloc(w->num_kcontrols *
2261 sizeof(struct snd_kcontrol *),
2262 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002263 if (!w->kcontrols) {
2264 mutex_unlock(&dapm->card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002265 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002266 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002267 }
2268
Richard Purdie2b97eab2006-10-06 18:32:18 +02002269 switch(w->id) {
2270 case snd_soc_dapm_switch:
2271 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002272 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002273 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002274 break;
2275 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002276 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002277 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002278 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002279 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002280 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002281 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002282 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002283 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002284 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002285 break;
2286 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002287
2288 /* Read the initial power state from the device */
2289 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002290 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002291 val &= 1 << w->shift;
2292 if (w->invert)
2293 val = !val;
2294
2295 if (val)
2296 w->power = 1;
2297 }
2298
Richard Purdie2b97eab2006-10-06 18:32:18 +02002299 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002300
Mark Brown7508b122011-10-05 12:09:12 +01002301 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002302 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002303 }
2304
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002305 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002306 mutex_unlock(&dapm->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002307 return 0;
2308}
2309EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2310
2311/**
2312 * snd_soc_dapm_get_volsw - dapm mixer get callback
2313 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002314 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002315 *
2316 * Callback to get the value of a dapm mixer control.
2317 *
2318 * Returns 0 for success.
2319 */
2320int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2321 struct snd_ctl_elem_value *ucontrol)
2322{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002323 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2324 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002325 struct soc_mixer_control *mc =
2326 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002327 unsigned int reg = mc->reg;
2328 unsigned int shift = mc->shift;
2329 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002330 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002331 unsigned int invert = mc->invert;
2332 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002333
Richard Purdie2b97eab2006-10-06 18:32:18 +02002334 ucontrol->value.integer.value[0] =
2335 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2336 if (shift != rshift)
2337 ucontrol->value.integer.value[1] =
2338 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2339 if (invert) {
2340 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002341 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002342 if (shift != rshift)
2343 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002344 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002345 }
2346
2347 return 0;
2348}
2349EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2350
2351/**
2352 * snd_soc_dapm_put_volsw - dapm mixer set callback
2353 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002354 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002355 *
2356 * Callback to set the value of a dapm mixer control.
2357 *
2358 * Returns 0 for success.
2359 */
2360int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2361 struct snd_ctl_elem_value *ucontrol)
2362{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002363 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2364 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2365 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002366 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002367 struct soc_mixer_control *mc =
2368 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002369 unsigned int reg = mc->reg;
2370 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002371 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002372 unsigned int mask = (1 << fls(max)) - 1;
2373 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002374 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002375 int connect, change;
2376 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002377 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002378
2379 val = (ucontrol->value.integer.value[0] & mask);
2380
2381 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002382 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002383 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002384 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002385
Stephen Warrenfafd2172011-04-28 17:38:00 -06002386 if (val)
2387 /* new connection */
2388 connect = invert ? 0 : 1;
2389 else
2390 /* old connection must be powered down */
2391 connect = invert ? 1 : 0;
2392
Liam Girdwood3cd04342012-03-09 12:02:08 +00002393 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002394
Stephen Warrene9cf7042011-01-27 14:54:05 -07002395 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002396 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002397 for (wi = 0; wi < wlist->num_widgets; wi++) {
2398 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002399
Stephen Warrenfafd2172011-04-28 17:38:00 -06002400 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002401
Stephen Warrenfafd2172011-04-28 17:38:00 -06002402 update.kcontrol = kcontrol;
2403 update.widget = widget;
2404 update.reg = reg;
2405 update.mask = mask;
2406 update.val = val;
2407 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002408
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002409 soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002410
2411 widget->dapm->update = NULL;
2412 }
Mark Brown283375c2009-12-07 18:09:03 +00002413 }
2414
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002415 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002416 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002417}
2418EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2419
2420/**
2421 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2422 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002423 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002424 *
2425 * Callback to get the value of a dapm enumerated double mixer control.
2426 *
2427 * Returns 0 for success.
2428 */
2429int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2430 struct snd_ctl_elem_value *ucontrol)
2431{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002432 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2433 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002434 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002435 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002436
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002437 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002438 ;
2439 val = snd_soc_read(widget->codec, e->reg);
2440 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2441 if (e->shift_l != e->shift_r)
2442 ucontrol->value.enumerated.item[1] =
2443 (val >> e->shift_r) & (bitmask - 1);
2444
2445 return 0;
2446}
2447EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2448
2449/**
2450 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2451 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002452 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002453 *
2454 * Callback to set the value of a dapm enumerated double mixer control.
2455 *
2456 * Returns 0 for success.
2457 */
2458int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2459 struct snd_ctl_elem_value *ucontrol)
2460{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002461 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2462 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2463 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002464 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002465 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002466 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002467 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002468 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002469 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002470
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002471 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002472 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002473 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002474 return -EINVAL;
2475 mux = ucontrol->value.enumerated.item[0];
2476 val = mux << e->shift_l;
2477 mask = (bitmask - 1) << e->shift_l;
2478 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002479 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002480 return -EINVAL;
2481 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2482 mask |= (bitmask - 1) << e->shift_r;
2483 }
2484
Liam Girdwood3cd04342012-03-09 12:02:08 +00002485 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002486
Mark Brown3a655772009-10-05 17:23:30 +01002487 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002488 if (change) {
2489 for (wi = 0; wi < wlist->num_widgets; wi++) {
2490 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002491
Stephen Warrenfafd2172011-04-28 17:38:00 -06002492 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002493
Stephen Warrenfafd2172011-04-28 17:38:00 -06002494 update.kcontrol = kcontrol;
2495 update.widget = widget;
2496 update.reg = e->reg;
2497 update.mask = mask;
2498 update.val = val;
2499 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002500
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002501 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002502
Stephen Warrenfafd2172011-04-28 17:38:00 -06002503 widget->dapm->update = NULL;
2504 }
2505 }
2506
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002507 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002508 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002509}
2510EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2511
2512/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002513 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2514 * @kcontrol: mixer control
2515 * @ucontrol: control element information
2516 *
2517 * Returns 0 for success.
2518 */
2519int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2520 struct snd_ctl_elem_value *ucontrol)
2521{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002522 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2523 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002524
2525 ucontrol->value.enumerated.item[0] = widget->value;
2526
2527 return 0;
2528}
2529EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2530
2531/**
2532 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2533 * @kcontrol: mixer control
2534 * @ucontrol: control element information
2535 *
2536 * Returns 0 for success.
2537 */
2538int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2539 struct snd_ctl_elem_value *ucontrol)
2540{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002541 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2542 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2543 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002544 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002545 struct soc_enum *e =
2546 (struct soc_enum *)kcontrol->private_value;
2547 int change;
2548 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002549 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002550
2551 if (ucontrol->value.enumerated.item[0] >= e->max)
2552 return -EINVAL;
2553
Liam Girdwood3cd04342012-03-09 12:02:08 +00002554 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002555
2556 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002557 if (change) {
2558 for (wi = 0; wi < wlist->num_widgets; wi++) {
2559 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002560
Stephen Warrenfafd2172011-04-28 17:38:00 -06002561 widget->value = ucontrol->value.enumerated.item[0];
2562
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002563 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002564 }
2565 }
2566
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002567 mutex_unlock(&card->dapm_mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002568 return ret;
2569}
2570EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2571
2572/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002573 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2574 * callback
2575 * @kcontrol: mixer control
2576 * @ucontrol: control element information
2577 *
2578 * Callback to get the value of a dapm semi enumerated double mixer control.
2579 *
2580 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2581 * used for handling bitfield coded enumeration for example.
2582 *
2583 * Returns 0 for success.
2584 */
2585int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2586 struct snd_ctl_elem_value *ucontrol)
2587{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002588 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2589 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002590 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002591 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002592
2593 reg_val = snd_soc_read(widget->codec, e->reg);
2594 val = (reg_val >> e->shift_l) & e->mask;
2595 for (mux = 0; mux < e->max; mux++) {
2596 if (val == e->values[mux])
2597 break;
2598 }
2599 ucontrol->value.enumerated.item[0] = mux;
2600 if (e->shift_l != e->shift_r) {
2601 val = (reg_val >> e->shift_r) & e->mask;
2602 for (mux = 0; mux < e->max; mux++) {
2603 if (val == e->values[mux])
2604 break;
2605 }
2606 ucontrol->value.enumerated.item[1] = mux;
2607 }
2608
2609 return 0;
2610}
2611EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2612
2613/**
2614 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2615 * callback
2616 * @kcontrol: mixer control
2617 * @ucontrol: control element information
2618 *
2619 * Callback to set the value of a dapm semi enumerated double mixer control.
2620 *
2621 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2622 * used for handling bitfield coded enumeration for example.
2623 *
2624 * Returns 0 for success.
2625 */
2626int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2627 struct snd_ctl_elem_value *ucontrol)
2628{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002629 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2630 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2631 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002632 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002633 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002634 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002635 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002636 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002637 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002638
2639 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2640 return -EINVAL;
2641 mux = ucontrol->value.enumerated.item[0];
2642 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2643 mask = e->mask << e->shift_l;
2644 if (e->shift_l != e->shift_r) {
2645 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2646 return -EINVAL;
2647 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2648 mask |= e->mask << e->shift_r;
2649 }
2650
Liam Girdwood3cd04342012-03-09 12:02:08 +00002651 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002652
Mark Brown3a655772009-10-05 17:23:30 +01002653 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002654 if (change) {
2655 for (wi = 0; wi < wlist->num_widgets; wi++) {
2656 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002657
Stephen Warrenfafd2172011-04-28 17:38:00 -06002658 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002659
Stephen Warrenfafd2172011-04-28 17:38:00 -06002660 update.kcontrol = kcontrol;
2661 update.widget = widget;
2662 update.reg = e->reg;
2663 update.mask = mask;
2664 update.val = val;
2665 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002666
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002667 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002668
Stephen Warrenfafd2172011-04-28 17:38:00 -06002669 widget->dapm->update = NULL;
2670 }
2671 }
2672
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002673 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002674 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002675}
2676EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2677
2678/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002679 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2680 *
2681 * @kcontrol: mixer control
2682 * @uinfo: control element information
2683 *
2684 * Callback to provide information about a pin switch control.
2685 */
2686int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2687 struct snd_ctl_elem_info *uinfo)
2688{
2689 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2690 uinfo->count = 1;
2691 uinfo->value.integer.min = 0;
2692 uinfo->value.integer.max = 1;
2693
2694 return 0;
2695}
2696EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2697
2698/**
2699 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2700 *
2701 * @kcontrol: mixer control
2702 * @ucontrol: Value
2703 */
2704int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2705 struct snd_ctl_elem_value *ucontrol)
2706{
Mark Brown48a8c392012-02-14 17:11:15 -08002707 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002708 const char *pin = (const char *)kcontrol->private_value;
2709
Liam Girdwood3cd04342012-03-09 12:02:08 +00002710 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002711
2712 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002713 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002714
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002715 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002716
2717 return 0;
2718}
2719EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2720
2721/**
2722 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2723 *
2724 * @kcontrol: mixer control
2725 * @ucontrol: Value
2726 */
2727int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2728 struct snd_ctl_elem_value *ucontrol)
2729{
Mark Brown48a8c392012-02-14 17:11:15 -08002730 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002731 const char *pin = (const char *)kcontrol->private_value;
2732
Liam Girdwood3cd04342012-03-09 12:02:08 +00002733 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002734
2735 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002736 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002737 else
Mark Brown48a8c392012-02-14 17:11:15 -08002738 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002739
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002740 mutex_unlock(&card->dapm_mutex);
2741
Mark Brown48a8c392012-02-14 17:11:15 -08002742 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002743 return 0;
2744}
2745EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2746
Mark Brown5ba06fc2012-02-16 11:07:13 -08002747static struct snd_soc_dapm_widget *
2748snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2749 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002750{
2751 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002752 size_t name_len;
Mark Brown62ea8742012-01-21 21:14:48 +00002753 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002754
2755 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08002756 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002757
Mark Brown62ea8742012-01-21 21:14:48 +00002758 switch (w->id) {
2759 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00002760 w->regulator = devm_regulator_get(dapm->dev, w->name);
2761 if (IS_ERR(w->regulator)) {
2762 ret = PTR_ERR(w->regulator);
Mark Brown62ea8742012-01-21 21:14:48 +00002763 dev_err(dapm->dev, "Failed to request %s: %d\n",
2764 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002765 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00002766 }
2767 break;
2768 default:
2769 break;
2770 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002771
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002772 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002773 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002774 name_len += 1 + strlen(dapm->codec->name_prefix);
2775 w->name = kmalloc(name_len, GFP_KERNEL);
2776 if (w->name == NULL) {
2777 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002778 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002779 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002780 if (dapm->codec && dapm->codec->name_prefix)
Mark Brown888df392012-02-16 19:37:51 -08002781 snprintf((char *)w->name, name_len, "%s %s",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002782 dapm->codec->name_prefix, widget->name);
2783 else
Mark Brown888df392012-02-16 19:37:51 -08002784 snprintf((char *)w->name, name_len, "%s", widget->name);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002785
Mark Brown7ca3a182011-10-08 14:04:50 +01002786 switch (w->id) {
2787 case snd_soc_dapm_switch:
2788 case snd_soc_dapm_mixer:
2789 case snd_soc_dapm_mixer_named_ctl:
2790 w->power_check = dapm_generic_check_power;
2791 break;
2792 case snd_soc_dapm_mux:
2793 case snd_soc_dapm_virt_mux:
2794 case snd_soc_dapm_value_mux:
2795 w->power_check = dapm_generic_check_power;
2796 break;
2797 case snd_soc_dapm_adc:
2798 case snd_soc_dapm_aif_out:
2799 w->power_check = dapm_adc_check_power;
2800 break;
2801 case snd_soc_dapm_dac:
2802 case snd_soc_dapm_aif_in:
2803 w->power_check = dapm_dac_check_power;
2804 break;
2805 case snd_soc_dapm_pga:
2806 case snd_soc_dapm_out_drv:
2807 case snd_soc_dapm_input:
2808 case snd_soc_dapm_output:
2809 case snd_soc_dapm_micbias:
2810 case snd_soc_dapm_spk:
2811 case snd_soc_dapm_hp:
2812 case snd_soc_dapm_mic:
2813 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01002814 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01002815 w->power_check = dapm_generic_check_power;
2816 break;
2817 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002818 case snd_soc_dapm_regulator_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01002819 w->power_check = dapm_supply_check_power;
2820 break;
Mark Brown888df392012-02-16 19:37:51 -08002821 case snd_soc_dapm_dai:
2822 w->power_check = dapm_dai_check_power;
2823 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002824 default:
2825 w->power_check = dapm_always_on_check_power;
2826 break;
2827 }
2828
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002829 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002830 w->dapm = dapm;
2831 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002832 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002833 INIT_LIST_HEAD(&w->sources);
2834 INIT_LIST_HEAD(&w->sinks);
2835 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01002836 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002837 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002838
2839 /* machine layer set ups unconnected pins and insertions */
2840 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08002841 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002842}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002843
2844/**
Mark Brown4ba13272008-05-13 14:51:19 +02002845 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002846 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002847 * @widget: widget array
2848 * @num: number of widgets
2849 *
2850 * Creates new DAPM controls based upon the templates.
2851 *
2852 * Returns 0 for success else error.
2853 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002854int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002855 const struct snd_soc_dapm_widget *widget,
2856 int num)
2857{
Mark Brown5ba06fc2012-02-16 11:07:13 -08002858 struct snd_soc_dapm_widget *w;
2859 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03002860 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02002861
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002862 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02002863 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08002864 w = snd_soc_dapm_new_control(dapm, widget);
2865 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002866 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08002867 "ASoC: Failed to create DAPM control %s\n",
2868 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03002869 ret = -ENOMEM;
2870 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002871 }
Mark Brown4ba13272008-05-13 14:51:19 +02002872 widget++;
2873 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002874 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03002875 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02002876}
2877EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2878
Mark Brownc74184e2012-04-04 22:12:09 +01002879static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
2880 struct snd_kcontrol *kcontrol, int event)
2881{
2882 struct snd_soc_dapm_path *source_p, *sink_p;
2883 struct snd_soc_dai *source, *sink;
2884 const struct snd_soc_pcm_stream *config = w->params;
2885 struct snd_pcm_substream substream;
2886 struct snd_pcm_hw_params params;
2887 u64 fmt;
2888 int ret;
2889
2890 BUG_ON(!config);
2891 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
2892
2893 /* We only support a single source and sink, pick the first */
2894 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
2895 list_sink);
2896 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
2897 list_source);
2898
2899 BUG_ON(!source_p || !sink_p);
2900 BUG_ON(!sink_p->source || !source_p->sink);
2901 BUG_ON(!source_p->source || !sink_p->sink);
2902
2903 source = source_p->source->priv;
2904 sink = sink_p->sink->priv;
2905
2906 /* Be a little careful as we don't want to overflow the mask array */
2907 if (config->formats) {
2908 fmt = ffs(config->formats) - 1;
2909 } else {
Fabio Estevam516541a2012-04-16 10:53:09 -03002910 dev_warn(w->dapm->dev, "Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01002911 config->formats);
2912 fmt = 0;
2913 }
2914
2915 /* Currently very limited parameter selection */
2916 memset(&params, 0, sizeof(params));
2917 snd_mask_set(hw_param_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
2918
2919 hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE)->min =
2920 config->rate_min;
2921 hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE)->max =
2922 config->rate_max;
2923
2924 hw_param_interval(&params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
2925 = config->channels_min;
2926 hw_param_interval(&params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
2927 = config->channels_max;
2928
2929 memset(&substream, 0, sizeof(substream));
2930
2931 switch (event) {
2932 case SND_SOC_DAPM_PRE_PMU:
2933 if (source->driver->ops && source->driver->ops->hw_params) {
2934 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
2935 ret = source->driver->ops->hw_params(&substream,
2936 &params, source);
2937 if (ret != 0) {
2938 dev_err(source->dev,
2939 "hw_params() failed: %d\n", ret);
2940 return ret;
2941 }
2942 }
2943
2944 if (sink->driver->ops && sink->driver->ops->hw_params) {
2945 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
2946 ret = sink->driver->ops->hw_params(&substream, &params,
2947 sink);
2948 if (ret != 0) {
2949 dev_err(sink->dev,
2950 "hw_params() failed: %d\n", ret);
2951 return ret;
2952 }
2953 }
2954 break;
2955
2956 case SND_SOC_DAPM_POST_PMU:
2957 ret = snd_soc_dai_digital_mute(sink, 0);
2958 if (ret != 0 && ret != -ENOTSUPP)
2959 dev_warn(sink->dev, "Failed to unmute: %d\n", ret);
2960 break;
2961
2962 case SND_SOC_DAPM_PRE_PMD:
2963 ret = snd_soc_dai_digital_mute(sink, 1);
2964 if (ret != 0 && ret != -ENOTSUPP)
2965 dev_warn(sink->dev, "Failed to mute: %d\n", ret);
2966 break;
2967
2968 default:
2969 BUG();
2970 return -EINVAL;
2971 }
2972
2973 return 0;
2974}
2975
2976int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
2977 const struct snd_soc_pcm_stream *params,
2978 struct snd_soc_dapm_widget *source,
2979 struct snd_soc_dapm_widget *sink)
2980{
2981 struct snd_soc_dapm_route routes[2];
2982 struct snd_soc_dapm_widget template;
2983 struct snd_soc_dapm_widget *w;
2984 size_t len;
2985 char *link_name;
2986
2987 len = strlen(source->name) + strlen(sink->name) + 2;
2988 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
2989 if (!link_name)
2990 return -ENOMEM;
2991 snprintf(link_name, len, "%s-%s", source->name, sink->name);
2992
2993 memset(&template, 0, sizeof(template));
2994 template.reg = SND_SOC_NOPM;
2995 template.id = snd_soc_dapm_dai_link;
2996 template.name = link_name;
2997 template.event = snd_soc_dai_link_event;
2998 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
2999 SND_SOC_DAPM_PRE_PMD;
3000
3001 dev_dbg(card->dev, "adding %s widget\n", link_name);
3002
3003 w = snd_soc_dapm_new_control(&card->dapm, &template);
3004 if (!w) {
3005 dev_err(card->dev, "Failed to create %s widget\n",
3006 link_name);
3007 return -ENOMEM;
3008 }
3009
3010 w->params = params;
3011
3012 memset(&routes, 0, sizeof(routes));
3013
3014 routes[0].source = source->name;
3015 routes[0].sink = link_name;
3016 routes[1].source = link_name;
3017 routes[1].sink = sink->name;
3018
3019 return snd_soc_dapm_add_routes(&card->dapm, routes,
3020 ARRAY_SIZE(routes));
3021}
3022
Mark Brown888df392012-02-16 19:37:51 -08003023int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3024 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003025{
Mark Brown888df392012-02-16 19:37:51 -08003026 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003027 struct snd_soc_dapm_widget *w;
3028
Mark Brown888df392012-02-16 19:37:51 -08003029 WARN_ON(dapm->dev != dai->dev);
3030
3031 memset(&template, 0, sizeof(template));
3032 template.reg = SND_SOC_NOPM;
3033
3034 if (dai->driver->playback.stream_name) {
3035 template.id = snd_soc_dapm_dai;
3036 template.name = dai->driver->playback.stream_name;
3037 template.sname = dai->driver->playback.stream_name;
3038
3039 dev_dbg(dai->dev, "adding %s widget\n",
3040 template.name);
3041
3042 w = snd_soc_dapm_new_control(dapm, &template);
3043 if (!w) {
3044 dev_err(dapm->dev, "Failed to create %s widget\n",
3045 dai->driver->playback.stream_name);
3046 }
3047
3048 w->priv = dai;
3049 dai->playback_widget = w;
3050 }
3051
3052 if (dai->driver->capture.stream_name) {
3053 template.id = snd_soc_dapm_dai;
3054 template.name = dai->driver->capture.stream_name;
3055 template.sname = dai->driver->capture.stream_name;
3056
3057 dev_dbg(dai->dev, "adding %s widget\n",
3058 template.name);
3059
3060 w = snd_soc_dapm_new_control(dapm, &template);
3061 if (!w) {
3062 dev_err(dapm->dev, "Failed to create %s widget\n",
3063 dai->driver->capture.stream_name);
3064 }
3065
3066 w->priv = dai;
3067 dai->capture_widget = w;
3068 }
3069
3070 return 0;
3071}
3072
3073int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3074{
3075 struct snd_soc_dapm_widget *dai_w, *w;
3076 struct snd_soc_dai *dai;
3077 struct snd_soc_dapm_route r;
3078
3079 memset(&r, 0, sizeof(r));
3080
3081 /* For each DAI widget... */
3082 list_for_each_entry(dai_w, &card->widgets, list) {
3083 if (dai_w->id != snd_soc_dapm_dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003084 continue;
Mark Brown888df392012-02-16 19:37:51 -08003085
3086 dai = dai_w->priv;
3087
3088 /* ...find all widgets with the same stream and link them */
3089 list_for_each_entry(w, &card->widgets, list) {
3090 if (w->dapm != dai_w->dapm)
3091 continue;
3092
3093 if (w->id == snd_soc_dapm_dai)
3094 continue;
3095
3096 if (!w->sname)
3097 continue;
3098
3099 if (dai->driver->playback.stream_name &&
3100 strstr(w->sname,
3101 dai->driver->playback.stream_name)) {
3102 r.source = dai->playback_widget->name;
3103 r.sink = w->name;
3104 dev_dbg(dai->dev, "%s -> %s\n",
3105 r.source, r.sink);
3106
3107 snd_soc_dapm_add_route(w->dapm, &r);
3108 }
3109
3110 if (dai->driver->capture.stream_name &&
3111 strstr(w->sname,
3112 dai->driver->capture.stream_name)) {
3113 r.source = w->name;
3114 r.sink = dai->capture_widget->name;
3115 dev_dbg(dai->dev, "%s -> %s\n",
3116 r.source, r.sink);
3117
3118 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003119 }
3120 }
3121 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003122
Mark Brown888df392012-02-16 19:37:51 -08003123 return 0;
3124}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003125
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003126static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3127 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003128{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003129
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003130 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3131 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3132 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003133
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003134 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3135 w_cpu = cpu_dai->playback_widget;
3136 w_codec = codec_dai->playback_widget;
3137 } else {
3138 w_cpu = cpu_dai->capture_widget;
3139 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003140 }
3141
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003142 if (w_cpu) {
3143
3144 dapm_mark_dirty(w_cpu, "stream event");
3145
3146 switch (event) {
3147 case SND_SOC_DAPM_STREAM_START:
3148 w_cpu->active = 1;
3149 break;
3150 case SND_SOC_DAPM_STREAM_STOP:
3151 w_cpu->active = 0;
3152 break;
3153 case SND_SOC_DAPM_STREAM_SUSPEND:
3154 case SND_SOC_DAPM_STREAM_RESUME:
3155 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3156 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3157 break;
3158 }
3159 }
3160
3161 if (w_codec) {
3162
3163 dapm_mark_dirty(w_codec, "stream event");
3164
3165 switch (event) {
3166 case SND_SOC_DAPM_STREAM_START:
3167 w_codec->active = 1;
3168 break;
3169 case SND_SOC_DAPM_STREAM_STOP:
3170 w_codec->active = 0;
3171 break;
3172 case SND_SOC_DAPM_STREAM_SUSPEND:
3173 case SND_SOC_DAPM_STREAM_RESUME:
3174 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3175 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3176 break;
3177 }
3178 }
3179
3180 dapm_power_widgets(&rtd->card->dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003181}
3182
3183/**
3184 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3185 * @rtd: PCM runtime data
3186 * @stream: stream name
3187 * @event: stream event
3188 *
3189 * Sends a stream event to the dapm core. The core then makes any
3190 * necessary widget power changes.
3191 *
3192 * Returns 0 for success else error.
3193 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003194void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3195 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003196{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003197 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003198
Liam Girdwood3cd04342012-03-09 12:02:08 +00003199 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003200 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003201 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003202}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003203
3204/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003205 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003206 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003207 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003208 *
Mark Brown74b8f952009-06-06 11:26:15 +01003209 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003210 * a valid audio route and active audio stream.
3211 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3212 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003213 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003214int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003215{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003216 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003217}
Liam Girdwooda5302182008-07-07 13:35:17 +01003218EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003219
3220/**
Mark Brownda341832010-03-15 19:23:37 +00003221 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003222 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003223 * @pin: pin name
3224 *
3225 * Enables input/output pin regardless of any other state. This is
3226 * intended for use with microphone bias supplies used in microphone
3227 * jack detection.
3228 *
3229 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3230 * do any widget power switching.
3231 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003232int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3233 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003234{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003235 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003236
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003237 if (!w) {
3238 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3239 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003240 }
3241
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003242 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3243 w->connected = 1;
3244 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003245 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003246
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003247 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003248}
3249EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3250
3251/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003252 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003253 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003254 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003255 *
Mark Brown74b8f952009-06-06 11:26:15 +01003256 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003257 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3258 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003259 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003260int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3261 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003262{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003263 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003264}
3265EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3266
3267/**
Mark Brown5817b522008-09-24 11:23:11 +01003268 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003269 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003270 * @pin: pin name
3271 *
3272 * Marks the specified pin as being not connected, disabling it along
3273 * any parent or child widgets. At present this is identical to
3274 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3275 * additional things such as disabling controls which only affect
3276 * paths through the pin.
3277 *
3278 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3279 * do any widget power switching.
3280 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003281int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003282{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003283 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003284}
3285EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3286
3287/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003288 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003289 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003290 * @pin: audio signal pin endpoint (or start point)
3291 *
3292 * Get audio pin status - connected or disconnected.
3293 *
3294 * Returns 1 for connected otherwise 0.
3295 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003296int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3297 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003298{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003299 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003300
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003301 if (w)
3302 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003303
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003304 return 0;
3305}
Liam Girdwooda5302182008-07-07 13:35:17 +01003306EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003307
3308/**
Mark Brown1547aba2010-05-07 21:11:40 +01003309 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003310 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003311 * @pin: audio signal pin endpoint (or start point)
3312 *
3313 * Mark the given endpoint or pin as ignoring suspend. When the
3314 * system is disabled a path between two endpoints flagged as ignoring
3315 * suspend will not be disabled. The path must already be enabled via
3316 * normal means at suspend time, it will not be turned on if it was not
3317 * already enabled.
3318 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003319int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3320 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003321{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003322 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003323
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003324 if (!w) {
3325 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3326 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003327 }
3328
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003329 w->ignore_suspend = 1;
3330
3331 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003332}
3333EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3334
Stephen Warren16332812011-11-23 12:42:04 -07003335static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3336 struct snd_soc_dapm_widget *w)
3337{
3338 struct snd_soc_dapm_path *p;
3339
3340 list_for_each_entry(p, &card->paths, list) {
3341 if ((p->source == w) || (p->sink == w)) {
3342 dev_dbg(card->dev,
3343 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3344 p->source->name, p->source->id, p->source->dapm,
3345 p->sink->name, p->sink->id, p->sink->dapm);
3346
3347 /* Connected to something other than the codec */
3348 if (p->source->dapm != p->sink->dapm)
3349 return true;
3350 /*
3351 * Loopback connection from codec external pin to
3352 * codec external pin
3353 */
3354 if (p->sink->id == snd_soc_dapm_input) {
3355 switch (p->source->id) {
3356 case snd_soc_dapm_output:
3357 case snd_soc_dapm_micbias:
3358 return true;
3359 default:
3360 break;
3361 }
3362 }
3363 }
3364 }
3365
3366 return false;
3367}
3368
3369/**
3370 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3371 * @codec: The codec whose pins should be processed
3372 *
3373 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3374 * which are unused. Pins are used if they are connected externally to the
3375 * codec, whether that be to some other device, or a loop-back connection to
3376 * the codec itself.
3377 */
3378void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3379{
3380 struct snd_soc_card *card = codec->card;
3381 struct snd_soc_dapm_context *dapm = &codec->dapm;
3382 struct snd_soc_dapm_widget *w;
3383
Mark Browna094b802011-11-27 19:42:20 +00003384 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003385 &card->dapm, &codec->dapm);
3386
3387 list_for_each_entry(w, &card->widgets, list) {
3388 if (w->dapm != dapm)
3389 continue;
3390 switch (w->id) {
3391 case snd_soc_dapm_input:
3392 case snd_soc_dapm_output:
3393 case snd_soc_dapm_micbias:
Mark Browna094b802011-11-27 19:42:20 +00003394 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003395 w->name);
3396 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003397 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003398 "... Not in map; disabling\n");
3399 snd_soc_dapm_nc_pin(dapm, w->name);
3400 }
3401 break;
3402 default:
3403 break;
3404 }
3405 }
3406}
3407
Mark Brown1547aba2010-05-07 21:11:40 +01003408/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003409 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003410 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003411 *
3412 * Free all dapm widgets and resources.
3413 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003414void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003415{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003416 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003417 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003418 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003419 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003420}
3421EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3422
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003423static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003424{
Mark Brown51737472009-06-22 13:16:51 +01003425 struct snd_soc_dapm_widget *w;
3426 LIST_HEAD(down_list);
3427 int powerdown = 0;
3428
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003429 list_for_each_entry(w, &dapm->card->widgets, list) {
3430 if (w->dapm != dapm)
3431 continue;
Mark Brown51737472009-06-22 13:16:51 +01003432 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003433 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003434 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003435 powerdown = 1;
3436 }
3437 }
3438
3439 /* If there were no widgets to power down we're already in
3440 * standby.
3441 */
3442 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003443 if (dapm->bias_level == SND_SOC_BIAS_ON)
3444 snd_soc_dapm_set_bias_level(dapm,
3445 SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003446 dapm_seq_run(dapm, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003447 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3448 snd_soc_dapm_set_bias_level(dapm,
3449 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003450 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003451}
Mark Brown51737472009-06-22 13:16:51 +01003452
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003453/*
3454 * snd_soc_dapm_shutdown - callback for system shutdown
3455 */
3456void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3457{
3458 struct snd_soc_codec *codec;
3459
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003460 list_for_each_entry(codec, &card->codec_dev_list, list) {
3461 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003462 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3463 snd_soc_dapm_set_bias_level(&codec->dapm,
3464 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003465 }
Mark Brown51737472009-06-22 13:16:51 +01003466}
3467
Richard Purdie2b97eab2006-10-06 18:32:18 +02003468/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003469MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003470MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3471MODULE_LICENSE("GPL");