blob: 02db2c09076b84eab8ea0b09e3bad30646eefc4f [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 Brown99497882010-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 Brown99497882010-05-07 20:24:05 +0100695
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000696 switch (level) {
Mark Brown99497882010-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 Brown99497882010-05-07 20:24:05 +0100703 default:
704 return 1;
705 }
706}
707
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100708/* add widget to list if it's not already in the list */
709static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
710 struct snd_soc_dapm_widget *w)
711{
712 struct snd_soc_dapm_widget_list *wlist;
713 int wlistsize, wlistentries, i;
714
715 if (*list == NULL)
716 return -EINVAL;
717
718 wlist = *list;
719
720 /* is this widget already in the list */
721 for (i = 0; i < wlist->num_widgets; i++) {
722 if (wlist->widgets[i] == w)
723 return 0;
724 }
725
726 /* allocate some new space */
727 wlistentries = wlist->num_widgets + 1;
728 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
729 wlistentries * sizeof(struct snd_soc_dapm_widget *);
730 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
731 if (*list == NULL) {
732 dev_err(w->dapm->dev, "can't allocate widget list for %s\n",
733 w->name);
734 return -ENOMEM;
735 }
736 wlist = *list;
737
738 /* insert the widget */
739 dev_dbg(w->dapm->dev, "added %s in widget list pos %d\n",
740 w->name, wlist->num_widgets);
741
742 wlist->widgets[wlist->num_widgets] = w;
743 wlist->num_widgets++;
744 return 1;
745}
746
Richard Purdie2b97eab2006-10-06 18:32:18 +0200747/*
748 * Recursively check for a completed path to an active or physically connected
749 * output widget. Returns number of complete paths.
750 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100751static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
752 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200753{
754 struct snd_soc_dapm_path *path;
755 int con = 0;
756
Mark Brown024dc072011-10-09 11:52:05 +0100757 if (widget->outputs >= 0)
758 return widget->outputs;
759
Mark Brownde02d072011-09-20 21:43:24 +0100760 DAPM_UPDATE_STAT(widget, path_checks);
761
Mark Brown62ea8742012-01-21 21:14:48 +0000762 switch (widget->id) {
763 case snd_soc_dapm_supply:
764 case snd_soc_dapm_regulator_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100765 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000766 default:
767 break;
768 }
Mark Brown246d0a12009-04-22 18:24:55 +0100769
Mark Brown010ff262009-08-17 17:39:22 +0100770 switch (widget->id) {
771 case snd_soc_dapm_adc:
772 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800773 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100774 if (widget->active) {
775 widget->outputs = snd_soc_dapm_suspend_check(widget);
776 return widget->outputs;
777 }
Mark Brown010ff262009-08-17 17:39:22 +0100778 default:
779 break;
780 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200781
782 if (widget->connected) {
783 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100784 if (widget->id == snd_soc_dapm_output && !widget->ext) {
785 widget->outputs = snd_soc_dapm_suspend_check(widget);
786 return widget->outputs;
787 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200788
789 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100790 if (widget->id == snd_soc_dapm_hp ||
791 widget->id == snd_soc_dapm_spk ||
792 (widget->id == snd_soc_dapm_line &&
793 !list_empty(&widget->sources))) {
794 widget->outputs = snd_soc_dapm_suspend_check(widget);
795 return widget->outputs;
796 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200797 }
798
799 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100800 DAPM_UPDATE_STAT(widget, neighbour_checks);
801
Mark Brownbf3a9e12011-06-13 16:42:29 +0100802 if (path->weak)
803 continue;
804
Richard Purdie2b97eab2006-10-06 18:32:18 +0200805 if (path->walked)
806 continue;
807
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100808 trace_snd_soc_dapm_output_path(widget, path);
809
Richard Purdie2b97eab2006-10-06 18:32:18 +0200810 if (path->sink && path->connect) {
811 path->walked = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100812
813 /* do we need to add this widget to the list ? */
814 if (list) {
815 int err;
816 err = dapm_list_add_widget(list, path->sink);
817 if (err < 0) {
818 dev_err(widget->dapm->dev, "could not add widget %s\n",
819 widget->name);
820 return con;
821 }
822 }
823
824 con += is_connected_output_ep(path->sink, list);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200825 }
826 }
827
Mark Brown024dc072011-10-09 11:52:05 +0100828 widget->outputs = con;
829
Richard Purdie2b97eab2006-10-06 18:32:18 +0200830 return con;
831}
832
833/*
834 * Recursively check for a completed path to an active or physically connected
835 * input widget. Returns number of complete paths.
836 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100837static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
838 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200839{
840 struct snd_soc_dapm_path *path;
841 int con = 0;
842
Mark Brown024dc072011-10-09 11:52:05 +0100843 if (widget->inputs >= 0)
844 return widget->inputs;
845
Mark Brownde02d072011-09-20 21:43:24 +0100846 DAPM_UPDATE_STAT(widget, path_checks);
847
Mark Brown62ea8742012-01-21 21:14:48 +0000848 switch (widget->id) {
849 case snd_soc_dapm_supply:
850 case snd_soc_dapm_regulator_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100851 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000852 default:
853 break;
854 }
Mark Brown246d0a12009-04-22 18:24:55 +0100855
Richard Purdie2b97eab2006-10-06 18:32:18 +0200856 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100857 switch (widget->id) {
858 case snd_soc_dapm_dac:
859 case snd_soc_dapm_aif_in:
Mark Brown888df392012-02-16 19:37:51 -0800860 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100861 if (widget->active) {
862 widget->inputs = snd_soc_dapm_suspend_check(widget);
863 return widget->inputs;
864 }
Mark Brown010ff262009-08-17 17:39:22 +0100865 default:
866 break;
867 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200868
869 if (widget->connected) {
870 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100871 if (widget->id == snd_soc_dapm_input && !widget->ext) {
872 widget->inputs = snd_soc_dapm_suspend_check(widget);
873 return widget->inputs;
874 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200875
876 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100877 if (widget->id == snd_soc_dapm_vmid) {
878 widget->inputs = snd_soc_dapm_suspend_check(widget);
879 return widget->inputs;
880 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200881
882 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300883 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100884 (widget->id == snd_soc_dapm_line &&
885 !list_empty(&widget->sinks))) {
886 widget->inputs = snd_soc_dapm_suspend_check(widget);
887 return widget->inputs;
888 }
889
Mark Brown1ab97c82011-11-27 16:21:51 +0000890 /* signal generator */
891 if (widget->id == snd_soc_dapm_siggen) {
892 widget->inputs = snd_soc_dapm_suspend_check(widget);
893 return widget->inputs;
894 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200895 }
896
897 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +0100898 DAPM_UPDATE_STAT(widget, neighbour_checks);
899
Mark Brownbf3a9e12011-06-13 16:42:29 +0100900 if (path->weak)
901 continue;
902
Richard Purdie2b97eab2006-10-06 18:32:18 +0200903 if (path->walked)
904 continue;
905
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100906 trace_snd_soc_dapm_input_path(widget, path);
907
Richard Purdie2b97eab2006-10-06 18:32:18 +0200908 if (path->source && path->connect) {
909 path->walked = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100910
911 /* do we need to add this widget to the list ? */
912 if (list) {
913 int err;
914 err = dapm_list_add_widget(list, path->sink);
915 if (err < 0) {
916 dev_err(widget->dapm->dev, "could not add widget %s\n",
917 widget->name);
918 return con;
919 }
920 }
921
922 con += is_connected_input_ep(path->source, list);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200923 }
924 }
925
Mark Brown024dc072011-10-09 11:52:05 +0100926 widget->inputs = con;
927
Richard Purdie2b97eab2006-10-06 18:32:18 +0200928 return con;
929}
930
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100931/**
932 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
933 * @dai: the soc DAI.
934 * @stream: stream direction.
935 * @list: list of active widgets for this stream.
936 *
937 * Queries DAPM graph as to whether an valid audio stream path exists for
938 * the initial stream specified by name. This takes into account
939 * current mixer and mux kcontrol settings. Creates list of valid widgets.
940 *
941 * Returns the number of valid paths or negative error.
942 */
943int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
944 struct snd_soc_dapm_widget_list **list)
945{
946 struct snd_soc_card *card = dai->card;
947 int paths;
948
949 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
950 dapm_reset(card);
951
952 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
953 paths = is_connected_output_ep(dai->playback_widget, list);
954 else
955 paths = is_connected_input_ep(dai->playback_widget, list);
956
957 trace_snd_soc_dapm_connected(paths, stream);
958 dapm_clear_walk(&card->dapm);
959 mutex_unlock(&card->dapm_mutex);
960
961 return paths;
962}
963
Richard Purdie2b97eab2006-10-06 18:32:18 +0200964/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300965 * Handler for generic register modifier widget.
966 */
967int dapm_reg_event(struct snd_soc_dapm_widget *w,
968 struct snd_kcontrol *kcontrol, int event)
969{
970 unsigned int val;
971
972 if (SND_SOC_DAPM_EVENT_ON(event))
973 val = w->on_val;
974 else
975 val = w->off_val;
976
Liam Girdwood49575fb52012-03-06 18:16:19 +0000977 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300978 w->mask << w->shift, val << w->shift);
979
980 return 0;
981}
Mark Brown11589412008-07-29 11:42:23 +0100982EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300983
Mark Brown62ea8742012-01-21 21:14:48 +0000984/*
985 * Handler for regulator supply widget.
986 */
987int dapm_regulator_event(struct snd_soc_dapm_widget *w,
988 struct snd_kcontrol *kcontrol, int event)
989{
990 if (SND_SOC_DAPM_EVENT_ON(event))
Liam Girdwooda3cc0562012-03-09 17:20:16 +0000991 return regulator_enable(w->regulator);
Mark Brown62ea8742012-01-21 21:14:48 +0000992 else
Liam Girdwooda3cc0562012-03-09 17:20:16 +0000993 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brown62ea8742012-01-21 21:14:48 +0000994}
995EXPORT_SYMBOL_GPL(dapm_regulator_event);
996
Mark Brownd8050022011-09-28 18:28:23 +0100997static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
998{
Mark Brown9b8a83b2011-10-04 22:15:59 +0100999 if (w->power_checked)
1000 return w->new_power;
1001
Mark Brownd8050022011-09-28 18:28:23 +01001002 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001003 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001004 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001005 w->new_power = w->power_check(w);
1006
1007 w->power_checked = true;
1008
1009 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001010}
1011
Mark Browncd0f2d42009-04-20 16:56:59 +01001012/* Generic check to see if a widget should be powered.
1013 */
1014static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1015{
1016 int in, out;
1017
Mark Brownde02d072011-09-20 21:43:24 +01001018 DAPM_UPDATE_STAT(w, power_checks);
1019
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001020 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001021 dapm_clear_walk(w->dapm);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001022 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001023 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +01001024 return out != 0 && in != 0;
1025}
1026
Mark Brown888df392012-02-16 19:37:51 -08001027static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
1028{
1029 DAPM_UPDATE_STAT(w, power_checks);
1030
Mark Brown1eee1b32012-04-10 13:57:46 +01001031 if (w->active)
1032 return w->active;
1033
1034 return dapm_generic_check_power(w);
Mark Brown888df392012-02-16 19:37:51 -08001035}
1036
Mark Brown6ea31b92009-04-20 17:15:41 +01001037/* Check to see if an ADC has power */
1038static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1039{
1040 int in;
1041
Mark Brownde02d072011-09-20 21:43:24 +01001042 DAPM_UPDATE_STAT(w, power_checks);
1043
Mark Brown6ea31b92009-04-20 17:15:41 +01001044 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001045 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001046 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +01001047 return in != 0;
1048 } else {
1049 return dapm_generic_check_power(w);
1050 }
1051}
1052
1053/* Check to see if a DAC has power */
1054static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1055{
1056 int out;
1057
Mark Brownde02d072011-09-20 21:43:24 +01001058 DAPM_UPDATE_STAT(w, power_checks);
1059
Mark Brown6ea31b92009-04-20 17:15:41 +01001060 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001061 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001062 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +01001063 return out != 0;
1064 } else {
1065 return dapm_generic_check_power(w);
1066 }
1067}
1068
Mark Brown246d0a12009-04-22 18:24:55 +01001069/* Check to see if a power supply is needed */
1070static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1071{
1072 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001073
Mark Brownde02d072011-09-20 21:43:24 +01001074 DAPM_UPDATE_STAT(w, power_checks);
1075
Mark Brown246d0a12009-04-22 18:24:55 +01001076 /* Check if one of our outputs is connected */
1077 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001078 DAPM_UPDATE_STAT(w, neighbour_checks);
1079
Mark Brownbf3a9e12011-06-13 16:42:29 +01001080 if (path->weak)
1081 continue;
1082
Mark Brown215edda2009-09-08 18:59:05 +01001083 if (path->connected &&
1084 !path->connected(path->source, path->sink))
1085 continue;
1086
Mark Brown30173582011-02-11 11:42:19 +00001087 if (!path->sink)
1088 continue;
1089
Mark Brownf68d7e12011-10-04 22:57:50 +01001090 if (dapm_widget_power_check(path->sink))
1091 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001092 }
1093
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001094 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +01001095
Mark Brownf68d7e12011-10-04 22:57:50 +01001096 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001097}
1098
Mark Brown35c64bc2011-09-28 18:23:53 +01001099static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1100{
1101 return 1;
1102}
1103
Mark Brown38357ab2009-06-06 19:03:23 +01001104static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1105 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001106 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001107{
Mark Brown828a8422011-01-15 13:14:30 +00001108 int *sort;
1109
1110 if (power_up)
1111 sort = dapm_up_seq;
1112 else
1113 sort = dapm_down_seq;
1114
Mark Brown38357ab2009-06-06 19:03:23 +01001115 if (sort[a->id] != sort[b->id])
1116 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001117 if (a->subseq != b->subseq) {
1118 if (power_up)
1119 return a->subseq - b->subseq;
1120 else
1121 return b->subseq - a->subseq;
1122 }
Mark Brownb22ead22009-06-07 12:51:26 +01001123 if (a->reg != b->reg)
1124 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001125 if (a->dapm != b->dapm)
1126 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001127
Mark Brown38357ab2009-06-06 19:03:23 +01001128 return 0;
1129}
Mark Brown42aa3412009-03-01 19:21:10 +00001130
Mark Brown38357ab2009-06-06 19:03:23 +01001131/* Insert a widget in order into a DAPM power sequence. */
1132static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1133 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001134 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001135{
1136 struct snd_soc_dapm_widget *w;
1137
1138 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001139 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001140 list_add_tail(&new_widget->power_list, &w->power_list);
1141 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001142 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001143
Mark Brown38357ab2009-06-06 19:03:23 +01001144 list_add_tail(&new_widget->power_list, list);
1145}
Mark Brown42aa3412009-03-01 19:21:10 +00001146
Mark Brown68f89ad2010-11-03 23:51:49 -04001147static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1148 struct snd_soc_dapm_widget *w, int event)
1149{
1150 struct snd_soc_card *card = dapm->card;
1151 const char *ev_name;
1152 int power, ret;
1153
1154 switch (event) {
1155 case SND_SOC_DAPM_PRE_PMU:
1156 ev_name = "PRE_PMU";
1157 power = 1;
1158 break;
1159 case SND_SOC_DAPM_POST_PMU:
1160 ev_name = "POST_PMU";
1161 power = 1;
1162 break;
1163 case SND_SOC_DAPM_PRE_PMD:
1164 ev_name = "PRE_PMD";
1165 power = 0;
1166 break;
1167 case SND_SOC_DAPM_POST_PMD:
1168 ev_name = "POST_PMD";
1169 power = 0;
1170 break;
1171 default:
1172 BUG();
1173 return;
1174 }
1175
1176 if (w->power != power)
1177 return;
1178
1179 if (w->event && (w->event_flags & event)) {
1180 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1181 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001182 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001183 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001184 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001185 if (ret < 0)
1186 pr_err("%s: %s event failed: %d\n",
1187 ev_name, w->name, ret);
1188 }
1189}
1190
Mark Brownb22ead22009-06-07 12:51:26 +01001191/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001192static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001193 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001194{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001195 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001196 struct snd_soc_dapm_widget *w;
1197 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001198 unsigned int value = 0;
1199 unsigned int mask = 0;
1200 unsigned int cur_mask;
1201
1202 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1203 power_list)->reg;
1204
1205 list_for_each_entry(w, pending, power_list) {
1206 cur_mask = 1 << w->shift;
1207 BUG_ON(reg != w->reg);
1208
1209 if (w->invert)
1210 power = !w->power;
1211 else
1212 power = w->power;
1213
1214 mask |= cur_mask;
1215 if (power)
1216 value |= cur_mask;
1217
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001218 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001219 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1220 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001221
Mark Brown68f89ad2010-11-03 23:51:49 -04001222 /* Check for events */
1223 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1224 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001225 }
1226
Mark Brown81628102009-06-07 13:21:24 +01001227 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001228 /* Any widget will do, they should all be updating the
1229 * same register.
1230 */
1231 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1232 power_list);
1233
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001234 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001235 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001236 value, mask, reg, card->pop_time);
1237 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001238 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001239 }
1240
1241 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001242 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1243 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001244 }
Mark Brown42aa3412009-03-01 19:21:10 +00001245}
1246
Mark Brownb22ead22009-06-07 12:51:26 +01001247/* Apply a DAPM power sequence.
1248 *
1249 * We walk over a pre-sorted list of widgets to apply power to. In
1250 * order to minimise the number of writes to the device required
1251 * multiple widgets will be updated in a single write where possible.
1252 * Currently anything that requires more than a single write is not
1253 * handled.
1254 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001255static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001256 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001257{
1258 struct snd_soc_dapm_widget *w, *n;
1259 LIST_HEAD(pending);
1260 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001261 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001262 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001263 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001264 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001265 int *sort;
1266
1267 if (power_up)
1268 sort = dapm_up_seq;
1269 else
1270 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001271
Mark Brownb22ead22009-06-07 12:51:26 +01001272 list_for_each_entry_safe(w, n, list, power_list) {
1273 ret = 0;
1274
1275 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001276 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001277 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001278 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001279 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001280
Mark Brown474b62d2011-01-18 16:14:44 +00001281 if (cur_dapm && cur_dapm->seq_notifier) {
1282 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1283 if (sort[i] == cur_sort)
1284 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001285 i,
1286 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001287 }
1288
Mark Brownb22ead22009-06-07 12:51:26 +01001289 INIT_LIST_HEAD(&pending);
1290 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001291 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001292 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001293 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001294 }
1295
Mark Brown163cac02009-06-07 10:12:52 +01001296 switch (w->id) {
1297 case snd_soc_dapm_pre:
1298 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001299 list_for_each_entry_safe_continue(w, n, list,
1300 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001301
Mark Brownb22ead22009-06-07 12:51:26 +01001302 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001303 ret = w->event(w,
1304 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001305 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001306 ret = w->event(w,
1307 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001308 break;
1309
1310 case snd_soc_dapm_post:
1311 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001312 list_for_each_entry_safe_continue(w, n, list,
1313 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001314
Mark Brownb22ead22009-06-07 12:51:26 +01001315 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001316 ret = w->event(w,
1317 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001318 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001319 ret = w->event(w,
1320 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001321 break;
1322
Mark Brown163cac02009-06-07 10:12:52 +01001323 default:
Mark Brown81628102009-06-07 13:21:24 +01001324 /* Queue it up for application */
1325 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001326 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001327 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001328 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001329 list_move(&w->power_list, &pending);
1330 break;
Mark Brown163cac02009-06-07 10:12:52 +01001331 }
Mark Brownb22ead22009-06-07 12:51:26 +01001332
1333 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001334 dev_err(w->dapm->dev,
1335 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001336 }
Mark Brownb22ead22009-06-07 12:51:26 +01001337
1338 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001339 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001340
1341 if (cur_dapm && cur_dapm->seq_notifier) {
1342 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1343 if (sort[i] == cur_sort)
1344 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001345 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001346 }
Mark Brown163cac02009-06-07 10:12:52 +01001347}
1348
Mark Brown97404f22010-12-14 16:13:57 +00001349static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1350{
1351 struct snd_soc_dapm_update *update = dapm->update;
1352 struct snd_soc_dapm_widget *w;
1353 int ret;
1354
1355 if (!update)
1356 return;
1357
1358 w = update->widget;
1359
1360 if (w->event &&
1361 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1362 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1363 if (ret != 0)
1364 pr_err("%s DAPM pre-event failed: %d\n",
1365 w->name, ret);
1366 }
1367
Liam Girdwood49575fb52012-03-06 18:16:19 +00001368 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001369 update->val);
1370 if (ret < 0)
1371 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1372
1373 if (w->event &&
1374 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1375 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1376 if (ret != 0)
1377 pr_err("%s DAPM post-event failed: %d\n",
1378 w->name, ret);
1379 }
1380}
1381
Mark Brown9d0624a2011-02-18 11:49:43 -08001382/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1383 * they're changing state.
1384 */
1385static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1386{
1387 struct snd_soc_dapm_context *d = data;
1388 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001389
Mark Brown56fba412011-06-04 11:25:10 +01001390 /* If we're off and we're not supposed to be go into STANDBY */
1391 if (d->bias_level == SND_SOC_BIAS_OFF &&
1392 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001393 if (d->dev)
1394 pm_runtime_get_sync(d->dev);
1395
Mark Brown9d0624a2011-02-18 11:49:43 -08001396 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1397 if (ret != 0)
1398 dev_err(d->dev,
1399 "Failed to turn on bias: %d\n", ret);
1400 }
1401
Mark Brown56fba412011-06-04 11:25:10 +01001402 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1403 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001404 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1405 if (ret != 0)
1406 dev_err(d->dev,
1407 "Failed to prepare bias: %d\n", ret);
1408 }
1409}
1410
1411/* Async callback run prior to DAPM sequences - brings to their final
1412 * state.
1413 */
1414static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1415{
1416 struct snd_soc_dapm_context *d = data;
1417 int ret;
1418
1419 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001420 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1421 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1422 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001423 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1424 if (ret != 0)
1425 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1426 ret);
1427 }
1428
1429 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001430 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1431 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001432 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1433 if (ret != 0)
1434 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001435
1436 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001437 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001438 }
1439
1440 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001441 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1442 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001443 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1444 if (ret != 0)
1445 dev_err(d->dev, "Failed to apply active bias: %d\n",
1446 ret);
1447 }
1448}
Mark Brown97404f22010-12-14 16:13:57 +00001449
Mark Brownfe4fda52011-10-03 22:36:57 +01001450static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1451 bool power, bool connect)
1452{
1453 /* If a connection is being made or broken then that update
1454 * will have marked the peer dirty, otherwise the widgets are
1455 * not connected and this update has no impact. */
1456 if (!connect)
1457 return;
1458
1459 /* If the peer is already in the state we're moving to then we
1460 * won't have an impact on it. */
1461 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001462 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001463}
1464
Mark Brown05623c42011-09-28 17:02:31 +01001465static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1466 struct list_head *up_list,
1467 struct list_head *down_list)
1468{
Mark Browndb432b42011-10-03 21:06:40 +01001469 struct snd_soc_dapm_path *path;
1470
Mark Brown05623c42011-09-28 17:02:31 +01001471 if (w->power == power)
1472 return;
1473
1474 trace_snd_soc_dapm_widget_power(w, power);
1475
Mark Browndb432b42011-10-03 21:06:40 +01001476 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001477 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001478 */
1479 list_for_each_entry(path, &w->sources, list_sink) {
1480 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001481 dapm_widget_set_peer_power(path->source, power,
1482 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001483 }
1484 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001485 switch (w->id) {
1486 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001487 case snd_soc_dapm_regulator_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001488 /* Supplies can't affect their outputs, only their inputs */
1489 break;
1490 default:
1491 list_for_each_entry(path, &w->sinks, list_source) {
1492 if (path->sink) {
1493 dapm_widget_set_peer_power(path->sink, power,
1494 path->connect);
1495 }
Mark Browndb432b42011-10-03 21:06:40 +01001496 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001497 break;
Mark Browndb432b42011-10-03 21:06:40 +01001498 }
1499
Mark Brown05623c42011-09-28 17:02:31 +01001500 if (power)
1501 dapm_seq_insert(w, up_list, true);
1502 else
1503 dapm_seq_insert(w, down_list, false);
1504
1505 w->power = power;
1506}
1507
Mark Brown7c81beb2011-09-20 22:22:32 +01001508static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1509 struct list_head *up_list,
1510 struct list_head *down_list)
1511{
Mark Brown7c81beb2011-09-20 22:22:32 +01001512 int power;
1513
1514 switch (w->id) {
1515 case snd_soc_dapm_pre:
1516 dapm_seq_insert(w, down_list, false);
1517 break;
1518 case snd_soc_dapm_post:
1519 dapm_seq_insert(w, up_list, true);
1520 break;
1521
1522 default:
Mark Brownd8050022011-09-28 18:28:23 +01001523 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001524
Mark Brown05623c42011-09-28 17:02:31 +01001525 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001526 break;
1527 }
1528}
1529
Mark Brown42aa3412009-03-01 19:21:10 +00001530/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001531 * Scan each dapm widget for complete audio path.
1532 * A complete path is a route that has valid endpoints i.e.:-
1533 *
1534 * o DAC to output pin.
1535 * o Input Pin to ADC.
1536 * o Input pin to Output pin (bypass, sidetone)
1537 * o DAC to ADC (loopback).
1538 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001539static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001540{
Mark Brown12ea2c72011-03-02 18:17:32 +00001541 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001542 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001543 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001544 LIST_HEAD(up_list);
1545 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001546 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001547 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001548
Mark Brown84e90932010-11-04 00:07:02 -04001549 trace_snd_soc_dapm_start(card);
1550
Mark Brown56fba412011-06-04 11:25:10 +01001551 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001552 if (d->idle_bias_off)
1553 d->target_bias_level = SND_SOC_BIAS_OFF;
1554 else
1555 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001556 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001557
Liam Girdwood6c120e12012-02-15 15:15:34 +00001558 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001559
Mark Brown6d3ddc82009-05-16 17:47:29 +01001560 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001561 * lists indicating if they should be powered up or down. We
1562 * only check widgets that have been flagged as dirty but note
1563 * that new widgets may be added to the dirty list while we
1564 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001565 */
Mark Browndb432b42011-10-03 21:06:40 +01001566 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001567 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001568 }
1569
Mark Brownf9de6d72011-09-28 17:19:47 +01001570 list_for_each_entry(w, &card->widgets, list) {
Mark Browndb432b42011-10-03 21:06:40 +01001571 list_del_init(&w->dirty);
1572
Mark Brownf9de6d72011-09-28 17:19:47 +01001573 if (w->power) {
1574 d = w->dapm;
1575
1576 /* Supplies and micbiases only bring the
1577 * context up to STANDBY as unless something
1578 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001579 * generally don't require full power. Signal
1580 * generators are virtual pins and have no
1581 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001582 */
1583 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001584 case snd_soc_dapm_siggen:
1585 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001586 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001587 case snd_soc_dapm_regulator_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001588 case snd_soc_dapm_micbias:
1589 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1590 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1591 break;
1592 default:
1593 d->target_bias_level = SND_SOC_BIAS_ON;
1594 break;
1595 }
1596 }
1597
1598 }
1599
Mark Brown85a843c2011-09-21 21:29:47 +01001600 /* Force all contexts in the card to the same bias state if
1601 * they're not ground referenced.
1602 */
Mark Brown56fba412011-06-04 11:25:10 +01001603 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001604 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001605 if (d->target_bias_level > bias)
1606 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001607 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001608 if (!d->idle_bias_off)
1609 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001610
Mark Brownde02d072011-09-20 21:43:24 +01001611 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001612
Mark Brown9d0624a2011-02-18 11:49:43 -08001613 /* Run all the bias changes in parallel */
1614 list_for_each_entry(d, &dapm->card->dapm_list, list)
1615 async_schedule_domain(dapm_pre_sequence_async, d,
1616 &async_domain);
1617 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001618
Mark Brown6d3ddc82009-05-16 17:47:29 +01001619 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001620 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001621
Mark Brown97404f22010-12-14 16:13:57 +00001622 dapm_widget_update(dapm);
1623
Mark Brown6d3ddc82009-05-16 17:47:29 +01001624 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001625 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001626
Mark Brown9d0624a2011-02-18 11:49:43 -08001627 /* Run all the bias changes in parallel */
1628 list_for_each_entry(d, &dapm->card->dapm_list, list)
1629 async_schedule_domain(dapm_post_sequence_async, d,
1630 &async_domain);
1631 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001632
Liam Girdwood8078d872012-02-15 15:15:35 +00001633 /* do we need to notify any clients that DAPM event is complete */
1634 list_for_each_entry(d, &card->dapm_list, list) {
1635 if (d->stream_event)
1636 d->stream_event(d, event);
1637 }
1638
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001639 pop_dbg(dapm->dev, card->pop_time,
1640 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001641 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001642
Mark Brown84e90932010-11-04 00:07:02 -04001643 trace_snd_soc_dapm_done(card);
1644
Mark Brown42aa3412009-03-01 19:21:10 +00001645 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001646}
1647
Mark Brown79fb9382009-08-21 16:38:13 +01001648#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001649static ssize_t dapm_widget_power_read_file(struct file *file,
1650 char __user *user_buf,
1651 size_t count, loff_t *ppos)
1652{
1653 struct snd_soc_dapm_widget *w = file->private_data;
1654 char *buf;
1655 int in, out;
1656 ssize_t ret;
1657 struct snd_soc_dapm_path *p = NULL;
1658
1659 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1660 if (!buf)
1661 return -ENOMEM;
1662
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001663 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001664 dapm_clear_walk(w->dapm);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001665 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001666 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001667
Mark Brownf13ebad2012-03-03 18:01:01 +00001668 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1669 w->name, w->power ? "On" : "Off",
1670 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001671
Mark Brownd033c362009-12-04 15:25:56 +00001672 if (w->reg >= 0)
1673 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1674 " - R%d(0x%x) bit %d",
1675 w->reg, w->reg, w->shift);
1676
1677 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1678
Mark Brown3eef08b2009-09-14 16:49:00 +01001679 if (w->sname)
1680 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1681 w->sname,
1682 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001683
1684 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001685 if (p->connected && !p->connected(w, p->sink))
1686 continue;
1687
Mark Brown79fb9382009-08-21 16:38:13 +01001688 if (p->connect)
1689 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001690 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001691 p->name ? p->name : "static",
1692 p->source->name);
1693 }
1694 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001695 if (p->connected && !p->connected(w, p->sink))
1696 continue;
1697
Mark Brown79fb9382009-08-21 16:38:13 +01001698 if (p->connect)
1699 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001700 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001701 p->name ? p->name : "static",
1702 p->sink->name);
1703 }
1704
1705 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1706
1707 kfree(buf);
1708 return ret;
1709}
1710
1711static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001712 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001713 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001714 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001715};
1716
Mark Brownef49e4f2011-04-04 20:48:13 +09001717static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1718 size_t count, loff_t *ppos)
1719{
1720 struct snd_soc_dapm_context *dapm = file->private_data;
1721 char *level;
1722
1723 switch (dapm->bias_level) {
1724 case SND_SOC_BIAS_ON:
1725 level = "On\n";
1726 break;
1727 case SND_SOC_BIAS_PREPARE:
1728 level = "Prepare\n";
1729 break;
1730 case SND_SOC_BIAS_STANDBY:
1731 level = "Standby\n";
1732 break;
1733 case SND_SOC_BIAS_OFF:
1734 level = "Off\n";
1735 break;
1736 default:
1737 BUG();
1738 level = "Unknown\n";
1739 break;
1740 }
1741
1742 return simple_read_from_buffer(user_buf, count, ppos, level,
1743 strlen(level));
1744}
1745
1746static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001747 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001748 .read = dapm_bias_read_file,
1749 .llseek = default_llseek,
1750};
1751
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001752void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1753 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001754{
Mark Brown79fb9382009-08-21 16:38:13 +01001755 struct dentry *d;
1756
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001757 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1758
1759 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001760 dev_warn(dapm->dev,
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001761 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001762 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001763 }
Mark Brown79fb9382009-08-21 16:38:13 +01001764
Mark Brownef49e4f2011-04-04 20:48:13 +09001765 d = debugfs_create_file("bias_level", 0444,
1766 dapm->debugfs_dapm, dapm,
1767 &dapm_bias_fops);
1768 if (!d)
1769 dev_warn(dapm->dev,
1770 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001771}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001772
1773static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1774{
1775 struct snd_soc_dapm_context *dapm = w->dapm;
1776 struct dentry *d;
1777
1778 if (!dapm->debugfs_dapm || !w->name)
1779 return;
1780
1781 d = debugfs_create_file(w->name, 0444,
1782 dapm->debugfs_dapm, w,
1783 &dapm_widget_power_fops);
1784 if (!d)
1785 dev_warn(w->dapm->dev,
1786 "ASoC: Failed to create %s debugfs file\n",
1787 w->name);
1788}
1789
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001790static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1791{
1792 debugfs_remove_recursive(dapm->debugfs_dapm);
1793}
1794
Mark Brown79fb9382009-08-21 16:38:13 +01001795#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001796void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1797 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001798{
1799}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001800
1801static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1802{
1803}
1804
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001805static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1806{
1807}
1808
Mark Brown79fb9382009-08-21 16:38:13 +01001809#endif
1810
Richard Purdie2b97eab2006-10-06 18:32:18 +02001811/* test and update the power status of a mux widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001812static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001813 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001814{
1815 struct snd_soc_dapm_path *path;
1816 int found = 0;
1817
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001818 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001819 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001820 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001821 return -ENODEV;
1822
Richard Purdie2b97eab2006-10-06 18:32:18 +02001823 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001824 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001825 if (path->kcontrol != kcontrol)
1826 continue;
1827
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001828 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001829 continue;
1830
1831 found = 1;
1832 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001833 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001834 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001835 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001836 } else {
1837 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001838 dapm_mark_dirty(path->source,
1839 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001840 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001841 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001842 }
1843
Mark Browndb432b42011-10-03 21:06:40 +01001844 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001845 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001846 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001847 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001848
Liam Girdwood618dae12012-04-25 12:12:51 +01001849 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001850}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001851
1852int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1853 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1854{
1855 struct snd_soc_card *card = widget->dapm->card;
1856 int ret;
1857
Liam Girdwood3cd04342012-03-09 12:02:08 +00001858 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001859 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1860 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001861 if (ret > 0)
1862 soc_dpcm_runtime_update(widget);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001863 return ret;
1864}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001865EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001866
Milan plzik1b075e32008-01-10 14:39:46 +01001867/* test and update the power status of a mixer or switch widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001868static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001869 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001870{
1871 struct snd_soc_dapm_path *path;
1872 int found = 0;
1873
Milan plzik1b075e32008-01-10 14:39:46 +01001874 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001875 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001876 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001877 return -ENODEV;
1878
Richard Purdie2b97eab2006-10-06 18:32:18 +02001879 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001880 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001881 if (path->kcontrol != kcontrol)
1882 continue;
1883
1884 /* found, now check type */
1885 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001886 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001887 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001888 }
1889
Mark Browndb432b42011-10-03 21:06:40 +01001890 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001891 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001892 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001893 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001894
Liam Girdwood618dae12012-04-25 12:12:51 +01001895 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001896}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001897
1898int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1899 struct snd_kcontrol *kcontrol, int connect)
1900{
1901 struct snd_soc_card *card = widget->dapm->card;
1902 int ret;
1903
Liam Girdwood3cd04342012-03-09 12:02:08 +00001904 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001905 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
1906 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001907 if (ret > 0)
1908 soc_dpcm_runtime_update(widget);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001909 return ret;
1910}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001911EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001912
1913/* show dapm widget status in sys fs */
1914static ssize_t dapm_widget_show(struct device *dev,
1915 struct device_attribute *attr, char *buf)
1916{
Mark Brown36ae1a92012-01-06 17:12:45 -08001917 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001918 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001919 struct snd_soc_dapm_widget *w;
1920 int count = 0;
1921 char *state = "not set";
1922
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001923 list_for_each_entry(w, &codec->card->widgets, list) {
1924 if (w->dapm != &codec->dapm)
1925 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001926
1927 /* only display widgets that burnm power */
1928 switch (w->id) {
1929 case snd_soc_dapm_hp:
1930 case snd_soc_dapm_mic:
1931 case snd_soc_dapm_spk:
1932 case snd_soc_dapm_line:
1933 case snd_soc_dapm_micbias:
1934 case snd_soc_dapm_dac:
1935 case snd_soc_dapm_adc:
1936 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001937 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001938 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001939 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001940 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001941 case snd_soc_dapm_regulator_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001942 if (w->name)
1943 count += sprintf(buf + count, "%s: %s\n",
1944 w->name, w->power ? "On":"Off");
1945 break;
1946 default:
1947 break;
1948 }
1949 }
1950
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001951 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001952 case SND_SOC_BIAS_ON:
1953 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001954 break;
Mark Brown0be98982008-05-19 12:31:28 +02001955 case SND_SOC_BIAS_PREPARE:
1956 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001957 break;
Mark Brown0be98982008-05-19 12:31:28 +02001958 case SND_SOC_BIAS_STANDBY:
1959 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001960 break;
Mark Brown0be98982008-05-19 12:31:28 +02001961 case SND_SOC_BIAS_OFF:
1962 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001963 break;
1964 }
1965 count += sprintf(buf + count, "PM State: %s\n", state);
1966
1967 return count;
1968}
1969
1970static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1971
1972int snd_soc_dapm_sys_add(struct device *dev)
1973{
Troy Kisky12ef1932008-10-13 17:42:14 -07001974 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001975}
1976
1977static void snd_soc_dapm_sys_remove(struct device *dev)
1978{
Mark Brownaef90842009-05-16 17:53:16 +01001979 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001980}
1981
1982/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001983static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001984{
1985 struct snd_soc_dapm_widget *w, *next_w;
1986 struct snd_soc_dapm_path *p, *next_p;
1987
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001988 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1989 if (w->dapm != dapm)
1990 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001991 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001992 /*
1993 * remove source and sink paths associated to this widget.
1994 * While removing the path, remove reference to it from both
1995 * source and sink widgets so that path is removed only once.
1996 */
1997 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1998 list_del(&p->list_sink);
1999 list_del(&p->list_source);
2000 list_del(&p->list);
2001 kfree(p->long_name);
2002 kfree(p);
2003 }
2004 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2005 list_del(&p->list_sink);
2006 list_del(&p->list_source);
2007 list_del(&p->list);
2008 kfree(p->long_name);
2009 kfree(p);
2010 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002011 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002012 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002013 kfree(w);
2014 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002015}
2016
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002017static struct snd_soc_dapm_widget *dapm_find_widget(
2018 struct snd_soc_dapm_context *dapm, const char *pin,
2019 bool search_other_contexts)
2020{
2021 struct snd_soc_dapm_widget *w;
2022 struct snd_soc_dapm_widget *fallback = NULL;
2023
2024 list_for_each_entry(w, &dapm->card->widgets, list) {
2025 if (!strcmp(w->name, pin)) {
2026 if (w->dapm == dapm)
2027 return w;
2028 else
2029 fallback = w;
2030 }
2031 }
2032
2033 if (search_other_contexts)
2034 return fallback;
2035
2036 return NULL;
2037}
2038
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002039static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002040 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002041{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002042 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002043
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002044 if (!w) {
2045 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2046 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002047 }
2048
Mark Brown1a8b2d92012-02-16 11:50:07 -08002049 if (w->connected != status)
2050 dapm_mark_dirty(w, "pin configuration");
2051
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002052 w->connected = status;
2053 if (status == 0)
2054 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002055
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002056 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002057}
2058
Richard Purdie2b97eab2006-10-06 18:32:18 +02002059/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002060 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002061 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002062 *
2063 * Walks all dapm audio paths and powers widgets according to their
2064 * stream or path usage.
2065 *
2066 * Returns 0 for success.
2067 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002068int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002069{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002070 int ret;
2071
Mark Brown4f4c0072011-10-07 14:29:19 +01002072 /*
2073 * Suppress early reports (eg, jacks syncing their state) to avoid
2074 * silly DAPM runs during card startup.
2075 */
2076 if (!dapm->card || !dapm->card->instantiated)
2077 return 0;
2078
Liam Girdwood3cd04342012-03-09 12:02:08 +00002079 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002080 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2081 mutex_unlock(&dapm->card->dapm_mutex);
2082 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002083}
Liam Girdwooda5302182008-07-07 13:35:17 +01002084EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002085
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002086static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002087 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002088{
2089 struct snd_soc_dapm_path *path;
2090 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002091 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002092 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002093 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002094 const char *source;
2095 char prefixed_sink[80];
2096 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002097 int ret = 0;
2098
Mark Brown88e8b9a2011-03-02 18:18:24 +00002099 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002100 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2101 dapm->codec->name_prefix, route->sink);
2102 sink = prefixed_sink;
2103 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2104 dapm->codec->name_prefix, route->source);
2105 source = prefixed_source;
2106 } else {
2107 sink = route->sink;
2108 source = route->source;
2109 }
2110
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002111 /*
2112 * find src and dest widgets over all widgets but favor a widget from
2113 * current DAPM context
2114 */
2115 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002116 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002117 wtsink = w;
2118 if (w->dapm == dapm)
2119 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002120 continue;
2121 }
2122 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002123 wtsource = w;
2124 if (w->dapm == dapm)
2125 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002126 }
2127 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002128 /* use widget from another DAPM context if not found from this */
2129 if (!wsink)
2130 wsink = wtsink;
2131 if (!wsource)
2132 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002133
2134 if (wsource == NULL || wsink == NULL)
2135 return -ENODEV;
2136
2137 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2138 if (!path)
2139 return -ENOMEM;
2140
2141 path->source = wsource;
2142 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002143 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002144 INIT_LIST_HEAD(&path->list);
2145 INIT_LIST_HEAD(&path->list_source);
2146 INIT_LIST_HEAD(&path->list_sink);
2147
2148 /* check for external widgets */
2149 if (wsink->id == snd_soc_dapm_input) {
2150 if (wsource->id == snd_soc_dapm_micbias ||
2151 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002152 wsource->id == snd_soc_dapm_line ||
2153 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002154 wsink->ext = 1;
2155 }
2156 if (wsource->id == snd_soc_dapm_output) {
2157 if (wsink->id == snd_soc_dapm_spk ||
2158 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002159 wsink->id == snd_soc_dapm_line ||
2160 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002161 wsource->ext = 1;
2162 }
2163
2164 /* connect static paths */
2165 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002166 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002167 list_add(&path->list_sink, &wsink->sources);
2168 list_add(&path->list_source, &wsource->sinks);
2169 path->connect = 1;
2170 return 0;
2171 }
2172
2173 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002174 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002175 case snd_soc_dapm_adc:
2176 case snd_soc_dapm_dac:
2177 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002178 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002179 case snd_soc_dapm_input:
2180 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002181 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002182 case snd_soc_dapm_micbias:
2183 case snd_soc_dapm_vmid:
2184 case snd_soc_dapm_pre:
2185 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002186 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002187 case snd_soc_dapm_regulator_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002188 case snd_soc_dapm_aif_in:
2189 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -08002190 case snd_soc_dapm_dai:
Mark Brownc74184e2012-04-04 22:12:09 +01002191 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002192 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002193 list_add(&path->list_sink, &wsink->sources);
2194 list_add(&path->list_source, &wsource->sinks);
2195 path->connect = 1;
2196 return 0;
2197 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002198 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002199 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002200 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002201 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002202 if (ret != 0)
2203 goto err;
2204 break;
2205 case snd_soc_dapm_switch:
2206 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002207 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002208 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002209 if (ret != 0)
2210 goto err;
2211 break;
2212 case snd_soc_dapm_hp:
2213 case snd_soc_dapm_mic:
2214 case snd_soc_dapm_line:
2215 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002216 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002217 list_add(&path->list_sink, &wsink->sources);
2218 list_add(&path->list_source, &wsource->sinks);
2219 path->connect = 0;
2220 return 0;
2221 }
2222 return 0;
2223
2224err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002225 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2226 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002227 kfree(path);
2228 return ret;
2229}
Mark Brown105f1c22008-05-13 14:52:19 +02002230
2231/**
Mark Brown105f1c22008-05-13 14:52:19 +02002232 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002233 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002234 * @route: audio routes
2235 * @num: number of routes
2236 *
2237 * Connects 2 dapm widgets together via a named audio path. The sink is
2238 * the widget receiving the audio signal, whilst the source is the sender
2239 * of the audio signal.
2240 *
2241 * Returns 0 for success else error. On error all resources can be freed
2242 * with a call to snd_soc_card_free().
2243 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002244int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002245 const struct snd_soc_dapm_route *route, int num)
2246{
Dan Carpenter60884c22012-04-13 22:25:43 +03002247 int i, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002248
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002249 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002250 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002251 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02002252 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002253 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2254 route->source, route->sink);
Dan Carpenter60884c22012-04-13 22:25:43 +03002255 break;
Mark Brown105f1c22008-05-13 14:52:19 +02002256 }
2257 route++;
2258 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002259 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002260
Dan Carpenter60884c22012-04-13 22:25:43 +03002261 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002262}
2263EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2264
Mark Brownbf3a9e12011-06-13 16:42:29 +01002265static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2266 const struct snd_soc_dapm_route *route)
2267{
2268 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2269 route->source,
2270 true);
2271 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2272 route->sink,
2273 true);
2274 struct snd_soc_dapm_path *path;
2275 int count = 0;
2276
2277 if (!source) {
2278 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2279 route->source);
2280 return -ENODEV;
2281 }
2282
2283 if (!sink) {
2284 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2285 route->sink);
2286 return -ENODEV;
2287 }
2288
2289 if (route->control || route->connected)
2290 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2291 route->source, route->sink);
2292
2293 list_for_each_entry(path, &source->sinks, list_source) {
2294 if (path->sink == sink) {
2295 path->weak = 1;
2296 count++;
2297 }
2298 }
2299
2300 if (count == 0)
2301 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2302 route->source, route->sink);
2303 if (count > 1)
2304 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2305 count, route->source, route->sink);
2306
2307 return 0;
2308}
2309
2310/**
2311 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2312 * @dapm: DAPM context
2313 * @route: audio routes
2314 * @num: number of routes
2315 *
2316 * Mark existing routes matching those specified in the passed array
2317 * as being weak, meaning that they are ignored for the purpose of
2318 * power decisions. The main intended use case is for sidetone paths
2319 * which couple audio between other independent paths if they are both
2320 * active in order to make the combination work better at the user
2321 * level but which aren't intended to be "used".
2322 *
2323 * Note that CODEC drivers should not use this as sidetone type paths
2324 * can frequently also be used as bypass paths.
2325 */
2326int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2327 const struct snd_soc_dapm_route *route, int num)
2328{
2329 int i, err;
2330 int ret = 0;
2331
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002332 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002333 for (i = 0; i < num; i++) {
2334 err = snd_soc_dapm_weak_route(dapm, route);
2335 if (err)
2336 ret = err;
2337 route++;
2338 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002339 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002340
2341 return ret;
2342}
2343EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2344
Mark Brown105f1c22008-05-13 14:52:19 +02002345/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002346 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002347 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002348 *
2349 * Checks the codec for any new dapm widgets and creates them if found.
2350 *
2351 * Returns 0 for success.
2352 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002353int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002354{
2355 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002356 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002357
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002358 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2359
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002360 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002361 {
2362 if (w->new)
2363 continue;
2364
Stephen Warrenfad59882011-04-28 17:37:59 -06002365 if (w->num_kcontrols) {
2366 w->kcontrols = kzalloc(w->num_kcontrols *
2367 sizeof(struct snd_kcontrol *),
2368 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002369 if (!w->kcontrols) {
2370 mutex_unlock(&dapm->card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002371 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002372 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002373 }
2374
Richard Purdie2b97eab2006-10-06 18:32:18 +02002375 switch(w->id) {
2376 case snd_soc_dapm_switch:
2377 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002378 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002379 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002380 break;
2381 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002382 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002383 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002384 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002385 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002386 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002387 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002388 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002389 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002390 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002391 break;
2392 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002393
2394 /* Read the initial power state from the device */
2395 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002396 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002397 val &= 1 << w->shift;
2398 if (w->invert)
2399 val = !val;
2400
2401 if (val)
2402 w->power = 1;
2403 }
2404
Richard Purdie2b97eab2006-10-06 18:32:18 +02002405 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002406
Mark Brown7508b122011-10-05 12:09:12 +01002407 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002408 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002409 }
2410
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002411 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002412 mutex_unlock(&dapm->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002413 return 0;
2414}
2415EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2416
2417/**
2418 * snd_soc_dapm_get_volsw - dapm mixer get callback
2419 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002420 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002421 *
2422 * Callback to get the value of a dapm mixer control.
2423 *
2424 * Returns 0 for success.
2425 */
2426int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2427 struct snd_ctl_elem_value *ucontrol)
2428{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002429 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2430 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002431 struct soc_mixer_control *mc =
2432 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002433 unsigned int reg = mc->reg;
2434 unsigned int shift = mc->shift;
2435 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002436 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002437 unsigned int invert = mc->invert;
2438 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002439
Richard Purdie2b97eab2006-10-06 18:32:18 +02002440 ucontrol->value.integer.value[0] =
2441 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2442 if (shift != rshift)
2443 ucontrol->value.integer.value[1] =
2444 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2445 if (invert) {
2446 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002447 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002448 if (shift != rshift)
2449 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002450 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002451 }
2452
2453 return 0;
2454}
2455EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2456
2457/**
2458 * snd_soc_dapm_put_volsw - dapm mixer set callback
2459 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002460 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002461 *
2462 * Callback to set the value of a dapm mixer control.
2463 *
2464 * Returns 0 for success.
2465 */
2466int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2467 struct snd_ctl_elem_value *ucontrol)
2468{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002469 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2470 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2471 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002472 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002473 struct soc_mixer_control *mc =
2474 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002475 unsigned int reg = mc->reg;
2476 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002477 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002478 unsigned int mask = (1 << fls(max)) - 1;
2479 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002480 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002481 int connect, change;
2482 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002483 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002484
2485 val = (ucontrol->value.integer.value[0] & mask);
2486
2487 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002488 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002489 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002490 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002491
Stephen Warrenfafd2172011-04-28 17:38:00 -06002492 if (val)
2493 /* new connection */
2494 connect = invert ? 0 : 1;
2495 else
2496 /* old connection must be powered down */
2497 connect = invert ? 1 : 0;
2498
Liam Girdwood3cd04342012-03-09 12:02:08 +00002499 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002500
Stephen Warrene9cf7042011-01-27 14:54:05 -07002501 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002502 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002503 for (wi = 0; wi < wlist->num_widgets; wi++) {
2504 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002505
Stephen Warrenfafd2172011-04-28 17:38:00 -06002506 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002507
Stephen Warrenfafd2172011-04-28 17:38:00 -06002508 update.kcontrol = kcontrol;
2509 update.widget = widget;
2510 update.reg = reg;
2511 update.mask = mask;
2512 update.val = val;
2513 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002514
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002515 soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002516
2517 widget->dapm->update = NULL;
2518 }
Mark Brown283375c2009-12-07 18:09:03 +00002519 }
2520
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002521 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002522 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002523}
2524EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2525
2526/**
2527 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2528 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002529 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002530 *
2531 * Callback to get the value of a dapm enumerated double mixer control.
2532 *
2533 * Returns 0 for success.
2534 */
2535int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2536 struct snd_ctl_elem_value *ucontrol)
2537{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002538 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2539 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002540 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002541 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002542
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002543 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002544 ;
2545 val = snd_soc_read(widget->codec, e->reg);
2546 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2547 if (e->shift_l != e->shift_r)
2548 ucontrol->value.enumerated.item[1] =
2549 (val >> e->shift_r) & (bitmask - 1);
2550
2551 return 0;
2552}
2553EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2554
2555/**
2556 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2557 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002558 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002559 *
2560 * Callback to set the value of a dapm enumerated double mixer control.
2561 *
2562 * Returns 0 for success.
2563 */
2564int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2565 struct snd_ctl_elem_value *ucontrol)
2566{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002567 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2568 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2569 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002570 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002571 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002572 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002573 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002574 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002575 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002576
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002577 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002578 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002579 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002580 return -EINVAL;
2581 mux = ucontrol->value.enumerated.item[0];
2582 val = mux << e->shift_l;
2583 mask = (bitmask - 1) << e->shift_l;
2584 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002585 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002586 return -EINVAL;
2587 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2588 mask |= (bitmask - 1) << e->shift_r;
2589 }
2590
Liam Girdwood3cd04342012-03-09 12:02:08 +00002591 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002592
Mark Brown3a655772009-10-05 17:23:30 +01002593 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002594 if (change) {
2595 for (wi = 0; wi < wlist->num_widgets; wi++) {
2596 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002597
Stephen Warrenfafd2172011-04-28 17:38:00 -06002598 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002599
Stephen Warrenfafd2172011-04-28 17:38:00 -06002600 update.kcontrol = kcontrol;
2601 update.widget = widget;
2602 update.reg = e->reg;
2603 update.mask = mask;
2604 update.val = val;
2605 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002606
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002607 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002608
Stephen Warrenfafd2172011-04-28 17:38:00 -06002609 widget->dapm->update = NULL;
2610 }
2611 }
2612
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002613 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002614 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002615}
2616EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2617
2618/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002619 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2620 * @kcontrol: mixer control
2621 * @ucontrol: control element information
2622 *
2623 * Returns 0 for success.
2624 */
2625int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2626 struct snd_ctl_elem_value *ucontrol)
2627{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002628 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2629 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002630
2631 ucontrol->value.enumerated.item[0] = widget->value;
2632
2633 return 0;
2634}
2635EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2636
2637/**
2638 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2639 * @kcontrol: mixer control
2640 * @ucontrol: control element information
2641 *
2642 * Returns 0 for success.
2643 */
2644int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2645 struct snd_ctl_elem_value *ucontrol)
2646{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002647 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2648 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2649 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002650 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002651 struct soc_enum *e =
2652 (struct soc_enum *)kcontrol->private_value;
2653 int change;
2654 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002655 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002656
2657 if (ucontrol->value.enumerated.item[0] >= e->max)
2658 return -EINVAL;
2659
Liam Girdwood3cd04342012-03-09 12:02:08 +00002660 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002661
2662 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002663 if (change) {
2664 for (wi = 0; wi < wlist->num_widgets; wi++) {
2665 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002666
Stephen Warrenfafd2172011-04-28 17:38:00 -06002667 widget->value = ucontrol->value.enumerated.item[0];
2668
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002669 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002670 }
2671 }
2672
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002673 mutex_unlock(&card->dapm_mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002674 return ret;
2675}
2676EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2677
2678/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002679 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2680 * callback
2681 * @kcontrol: mixer control
2682 * @ucontrol: control element information
2683 *
2684 * Callback to get the value of a dapm semi enumerated double mixer control.
2685 *
2686 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2687 * used for handling bitfield coded enumeration for example.
2688 *
2689 * Returns 0 for success.
2690 */
2691int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2692 struct snd_ctl_elem_value *ucontrol)
2693{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002694 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2695 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002696 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002697 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002698
2699 reg_val = snd_soc_read(widget->codec, e->reg);
2700 val = (reg_val >> e->shift_l) & e->mask;
2701 for (mux = 0; mux < e->max; mux++) {
2702 if (val == e->values[mux])
2703 break;
2704 }
2705 ucontrol->value.enumerated.item[0] = mux;
2706 if (e->shift_l != e->shift_r) {
2707 val = (reg_val >> e->shift_r) & e->mask;
2708 for (mux = 0; mux < e->max; mux++) {
2709 if (val == e->values[mux])
2710 break;
2711 }
2712 ucontrol->value.enumerated.item[1] = mux;
2713 }
2714
2715 return 0;
2716}
2717EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2718
2719/**
2720 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2721 * callback
2722 * @kcontrol: mixer control
2723 * @ucontrol: control element information
2724 *
2725 * Callback to set the value of a dapm semi enumerated double mixer control.
2726 *
2727 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2728 * used for handling bitfield coded enumeration for example.
2729 *
2730 * Returns 0 for success.
2731 */
2732int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2733 struct snd_ctl_elem_value *ucontrol)
2734{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002735 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2736 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2737 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002738 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002739 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002740 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002741 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002742 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002743 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002744
2745 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2746 return -EINVAL;
2747 mux = ucontrol->value.enumerated.item[0];
2748 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2749 mask = e->mask << e->shift_l;
2750 if (e->shift_l != e->shift_r) {
2751 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2752 return -EINVAL;
2753 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2754 mask |= e->mask << e->shift_r;
2755 }
2756
Liam Girdwood3cd04342012-03-09 12:02:08 +00002757 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002758
Mark Brown3a655772009-10-05 17:23:30 +01002759 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002760 if (change) {
2761 for (wi = 0; wi < wlist->num_widgets; wi++) {
2762 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002763
Stephen Warrenfafd2172011-04-28 17:38:00 -06002764 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002765
Stephen Warrenfafd2172011-04-28 17:38:00 -06002766 update.kcontrol = kcontrol;
2767 update.widget = widget;
2768 update.reg = e->reg;
2769 update.mask = mask;
2770 update.val = val;
2771 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002772
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002773 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002774
Stephen Warrenfafd2172011-04-28 17:38:00 -06002775 widget->dapm->update = NULL;
2776 }
2777 }
2778
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002779 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002780 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002781}
2782EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2783
2784/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002785 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2786 *
2787 * @kcontrol: mixer control
2788 * @uinfo: control element information
2789 *
2790 * Callback to provide information about a pin switch control.
2791 */
2792int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2793 struct snd_ctl_elem_info *uinfo)
2794{
2795 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2796 uinfo->count = 1;
2797 uinfo->value.integer.min = 0;
2798 uinfo->value.integer.max = 1;
2799
2800 return 0;
2801}
2802EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2803
2804/**
2805 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2806 *
2807 * @kcontrol: mixer control
2808 * @ucontrol: Value
2809 */
2810int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2811 struct snd_ctl_elem_value *ucontrol)
2812{
Mark Brown48a8c392012-02-14 17:11:15 -08002813 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002814 const char *pin = (const char *)kcontrol->private_value;
2815
Liam Girdwood3cd04342012-03-09 12:02:08 +00002816 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002817
2818 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002819 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002820
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002821 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002822
2823 return 0;
2824}
2825EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2826
2827/**
2828 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2829 *
2830 * @kcontrol: mixer control
2831 * @ucontrol: Value
2832 */
2833int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2834 struct snd_ctl_elem_value *ucontrol)
2835{
Mark Brown48a8c392012-02-14 17:11:15 -08002836 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002837 const char *pin = (const char *)kcontrol->private_value;
2838
Liam Girdwood3cd04342012-03-09 12:02:08 +00002839 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002840
2841 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002842 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002843 else
Mark Brown48a8c392012-02-14 17:11:15 -08002844 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002845
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002846 mutex_unlock(&card->dapm_mutex);
2847
Mark Brown48a8c392012-02-14 17:11:15 -08002848 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002849 return 0;
2850}
2851EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2852
Mark Brown5ba06fc2012-02-16 11:07:13 -08002853static struct snd_soc_dapm_widget *
2854snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2855 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002856{
2857 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002858 size_t name_len;
Mark Brown62ea8742012-01-21 21:14:48 +00002859 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002860
2861 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08002862 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002863
Mark Brown62ea8742012-01-21 21:14:48 +00002864 switch (w->id) {
2865 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00002866 w->regulator = devm_regulator_get(dapm->dev, w->name);
2867 if (IS_ERR(w->regulator)) {
2868 ret = PTR_ERR(w->regulator);
Mark Brown62ea8742012-01-21 21:14:48 +00002869 dev_err(dapm->dev, "Failed to request %s: %d\n",
2870 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002871 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00002872 }
2873 break;
2874 default:
2875 break;
2876 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002877
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002878 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002879 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002880 name_len += 1 + strlen(dapm->codec->name_prefix);
2881 w->name = kmalloc(name_len, GFP_KERNEL);
2882 if (w->name == NULL) {
2883 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002884 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002885 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002886 if (dapm->codec && dapm->codec->name_prefix)
Mark Brown888df392012-02-16 19:37:51 -08002887 snprintf((char *)w->name, name_len, "%s %s",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002888 dapm->codec->name_prefix, widget->name);
2889 else
Mark Brown888df392012-02-16 19:37:51 -08002890 snprintf((char *)w->name, name_len, "%s", widget->name);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002891
Mark Brown7ca3a182011-10-08 14:04:50 +01002892 switch (w->id) {
2893 case snd_soc_dapm_switch:
2894 case snd_soc_dapm_mixer:
2895 case snd_soc_dapm_mixer_named_ctl:
2896 w->power_check = dapm_generic_check_power;
2897 break;
2898 case snd_soc_dapm_mux:
2899 case snd_soc_dapm_virt_mux:
2900 case snd_soc_dapm_value_mux:
2901 w->power_check = dapm_generic_check_power;
2902 break;
2903 case snd_soc_dapm_adc:
2904 case snd_soc_dapm_aif_out:
2905 w->power_check = dapm_adc_check_power;
2906 break;
2907 case snd_soc_dapm_dac:
2908 case snd_soc_dapm_aif_in:
2909 w->power_check = dapm_dac_check_power;
2910 break;
2911 case snd_soc_dapm_pga:
2912 case snd_soc_dapm_out_drv:
2913 case snd_soc_dapm_input:
2914 case snd_soc_dapm_output:
2915 case snd_soc_dapm_micbias:
2916 case snd_soc_dapm_spk:
2917 case snd_soc_dapm_hp:
2918 case snd_soc_dapm_mic:
2919 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01002920 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01002921 w->power_check = dapm_generic_check_power;
2922 break;
2923 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002924 case snd_soc_dapm_regulator_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01002925 w->power_check = dapm_supply_check_power;
2926 break;
Mark Brown888df392012-02-16 19:37:51 -08002927 case snd_soc_dapm_dai:
2928 w->power_check = dapm_dai_check_power;
2929 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002930 default:
2931 w->power_check = dapm_always_on_check_power;
2932 break;
2933 }
2934
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002935 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002936 w->dapm = dapm;
2937 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002938 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002939 INIT_LIST_HEAD(&w->sources);
2940 INIT_LIST_HEAD(&w->sinks);
2941 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01002942 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002943 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002944
2945 /* machine layer set ups unconnected pins and insertions */
2946 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08002947 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002948}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002949
2950/**
Mark Brown4ba13272008-05-13 14:51:19 +02002951 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002952 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002953 * @widget: widget array
2954 * @num: number of widgets
2955 *
2956 * Creates new DAPM controls based upon the templates.
2957 *
2958 * Returns 0 for success else error.
2959 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002960int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002961 const struct snd_soc_dapm_widget *widget,
2962 int num)
2963{
Mark Brown5ba06fc2012-02-16 11:07:13 -08002964 struct snd_soc_dapm_widget *w;
2965 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03002966 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02002967
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002968 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02002969 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08002970 w = snd_soc_dapm_new_control(dapm, widget);
2971 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002972 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08002973 "ASoC: Failed to create DAPM control %s\n",
2974 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03002975 ret = -ENOMEM;
2976 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002977 }
Mark Brown4ba13272008-05-13 14:51:19 +02002978 widget++;
2979 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002980 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03002981 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02002982}
2983EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2984
Mark Brownc74184e2012-04-04 22:12:09 +01002985static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
2986 struct snd_kcontrol *kcontrol, int event)
2987{
2988 struct snd_soc_dapm_path *source_p, *sink_p;
2989 struct snd_soc_dai *source, *sink;
2990 const struct snd_soc_pcm_stream *config = w->params;
2991 struct snd_pcm_substream substream;
2992 struct snd_pcm_hw_params params;
2993 u64 fmt;
2994 int ret;
2995
2996 BUG_ON(!config);
2997 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
2998
2999 /* We only support a single source and sink, pick the first */
3000 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3001 list_sink);
3002 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3003 list_source);
3004
3005 BUG_ON(!source_p || !sink_p);
3006 BUG_ON(!sink_p->source || !source_p->sink);
3007 BUG_ON(!source_p->source || !sink_p->sink);
3008
3009 source = source_p->source->priv;
3010 sink = sink_p->sink->priv;
3011
3012 /* Be a little careful as we don't want to overflow the mask array */
3013 if (config->formats) {
3014 fmt = ffs(config->formats) - 1;
3015 } else {
Fabio Estevam516541a2012-04-16 10:53:09 -03003016 dev_warn(w->dapm->dev, "Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003017 config->formats);
3018 fmt = 0;
3019 }
3020
3021 /* Currently very limited parameter selection */
3022 memset(&params, 0, sizeof(params));
3023 snd_mask_set(hw_param_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3024
3025 hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE)->min =
3026 config->rate_min;
3027 hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE)->max =
3028 config->rate_max;
3029
3030 hw_param_interval(&params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3031 = config->channels_min;
3032 hw_param_interval(&params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3033 = config->channels_max;
3034
3035 memset(&substream, 0, sizeof(substream));
3036
3037 switch (event) {
3038 case SND_SOC_DAPM_PRE_PMU:
3039 if (source->driver->ops && source->driver->ops->hw_params) {
3040 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3041 ret = source->driver->ops->hw_params(&substream,
3042 &params, source);
3043 if (ret != 0) {
3044 dev_err(source->dev,
3045 "hw_params() failed: %d\n", ret);
3046 return ret;
3047 }
3048 }
3049
3050 if (sink->driver->ops && sink->driver->ops->hw_params) {
3051 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3052 ret = sink->driver->ops->hw_params(&substream, &params,
3053 sink);
3054 if (ret != 0) {
3055 dev_err(sink->dev,
3056 "hw_params() failed: %d\n", ret);
3057 return ret;
3058 }
3059 }
3060 break;
3061
3062 case SND_SOC_DAPM_POST_PMU:
3063 ret = snd_soc_dai_digital_mute(sink, 0);
3064 if (ret != 0 && ret != -ENOTSUPP)
3065 dev_warn(sink->dev, "Failed to unmute: %d\n", ret);
3066 break;
3067
3068 case SND_SOC_DAPM_PRE_PMD:
3069 ret = snd_soc_dai_digital_mute(sink, 1);
3070 if (ret != 0 && ret != -ENOTSUPP)
3071 dev_warn(sink->dev, "Failed to mute: %d\n", ret);
3072 break;
3073
3074 default:
3075 BUG();
3076 return -EINVAL;
3077 }
3078
3079 return 0;
3080}
3081
3082int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3083 const struct snd_soc_pcm_stream *params,
3084 struct snd_soc_dapm_widget *source,
3085 struct snd_soc_dapm_widget *sink)
3086{
3087 struct snd_soc_dapm_route routes[2];
3088 struct snd_soc_dapm_widget template;
3089 struct snd_soc_dapm_widget *w;
3090 size_t len;
3091 char *link_name;
3092
3093 len = strlen(source->name) + strlen(sink->name) + 2;
3094 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3095 if (!link_name)
3096 return -ENOMEM;
3097 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3098
3099 memset(&template, 0, sizeof(template));
3100 template.reg = SND_SOC_NOPM;
3101 template.id = snd_soc_dapm_dai_link;
3102 template.name = link_name;
3103 template.event = snd_soc_dai_link_event;
3104 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3105 SND_SOC_DAPM_PRE_PMD;
3106
3107 dev_dbg(card->dev, "adding %s widget\n", link_name);
3108
3109 w = snd_soc_dapm_new_control(&card->dapm, &template);
3110 if (!w) {
3111 dev_err(card->dev, "Failed to create %s widget\n",
3112 link_name);
3113 return -ENOMEM;
3114 }
3115
3116 w->params = params;
3117
3118 memset(&routes, 0, sizeof(routes));
3119
3120 routes[0].source = source->name;
3121 routes[0].sink = link_name;
3122 routes[1].source = link_name;
3123 routes[1].sink = sink->name;
3124
3125 return snd_soc_dapm_add_routes(&card->dapm, routes,
3126 ARRAY_SIZE(routes));
3127}
3128
Mark Brown888df392012-02-16 19:37:51 -08003129int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3130 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003131{
Mark Brown888df392012-02-16 19:37:51 -08003132 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003133 struct snd_soc_dapm_widget *w;
3134
Mark Brown888df392012-02-16 19:37:51 -08003135 WARN_ON(dapm->dev != dai->dev);
3136
3137 memset(&template, 0, sizeof(template));
3138 template.reg = SND_SOC_NOPM;
3139
3140 if (dai->driver->playback.stream_name) {
3141 template.id = snd_soc_dapm_dai;
3142 template.name = dai->driver->playback.stream_name;
3143 template.sname = dai->driver->playback.stream_name;
3144
3145 dev_dbg(dai->dev, "adding %s widget\n",
3146 template.name);
3147
3148 w = snd_soc_dapm_new_control(dapm, &template);
3149 if (!w) {
3150 dev_err(dapm->dev, "Failed to create %s widget\n",
3151 dai->driver->playback.stream_name);
3152 }
3153
3154 w->priv = dai;
3155 dai->playback_widget = w;
3156 }
3157
3158 if (dai->driver->capture.stream_name) {
3159 template.id = snd_soc_dapm_dai;
3160 template.name = dai->driver->capture.stream_name;
3161 template.sname = dai->driver->capture.stream_name;
3162
3163 dev_dbg(dai->dev, "adding %s widget\n",
3164 template.name);
3165
3166 w = snd_soc_dapm_new_control(dapm, &template);
3167 if (!w) {
3168 dev_err(dapm->dev, "Failed to create %s widget\n",
3169 dai->driver->capture.stream_name);
3170 }
3171
3172 w->priv = dai;
3173 dai->capture_widget = w;
3174 }
3175
3176 return 0;
3177}
3178
3179int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3180{
3181 struct snd_soc_dapm_widget *dai_w, *w;
3182 struct snd_soc_dai *dai;
3183 struct snd_soc_dapm_route r;
3184
3185 memset(&r, 0, sizeof(r));
3186
3187 /* For each DAI widget... */
3188 list_for_each_entry(dai_w, &card->widgets, list) {
3189 if (dai_w->id != snd_soc_dapm_dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003190 continue;
Mark Brown888df392012-02-16 19:37:51 -08003191
3192 dai = dai_w->priv;
3193
3194 /* ...find all widgets with the same stream and link them */
3195 list_for_each_entry(w, &card->widgets, list) {
3196 if (w->dapm != dai_w->dapm)
3197 continue;
3198
3199 if (w->id == snd_soc_dapm_dai)
3200 continue;
3201
3202 if (!w->sname)
3203 continue;
3204
3205 if (dai->driver->playback.stream_name &&
3206 strstr(w->sname,
3207 dai->driver->playback.stream_name)) {
3208 r.source = dai->playback_widget->name;
3209 r.sink = w->name;
3210 dev_dbg(dai->dev, "%s -> %s\n",
3211 r.source, r.sink);
3212
3213 snd_soc_dapm_add_route(w->dapm, &r);
3214 }
3215
3216 if (dai->driver->capture.stream_name &&
3217 strstr(w->sname,
3218 dai->driver->capture.stream_name)) {
3219 r.source = w->name;
3220 r.sink = dai->capture_widget->name;
3221 dev_dbg(dai->dev, "%s -> %s\n",
3222 r.source, r.sink);
3223
3224 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003225 }
3226 }
3227 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003228
Mark Brown888df392012-02-16 19:37:51 -08003229 return 0;
3230}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003231
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003232static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3233 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003234{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003235
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003236 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3237 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3238 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003239
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003240 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3241 w_cpu = cpu_dai->playback_widget;
3242 w_codec = codec_dai->playback_widget;
3243 } else {
3244 w_cpu = cpu_dai->capture_widget;
3245 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003246 }
3247
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003248 if (w_cpu) {
3249
3250 dapm_mark_dirty(w_cpu, "stream event");
3251
3252 switch (event) {
3253 case SND_SOC_DAPM_STREAM_START:
3254 w_cpu->active = 1;
3255 break;
3256 case SND_SOC_DAPM_STREAM_STOP:
3257 w_cpu->active = 0;
3258 break;
3259 case SND_SOC_DAPM_STREAM_SUSPEND:
3260 case SND_SOC_DAPM_STREAM_RESUME:
3261 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3262 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3263 break;
3264 }
3265 }
3266
3267 if (w_codec) {
3268
3269 dapm_mark_dirty(w_codec, "stream event");
3270
3271 switch (event) {
3272 case SND_SOC_DAPM_STREAM_START:
3273 w_codec->active = 1;
3274 break;
3275 case SND_SOC_DAPM_STREAM_STOP:
3276 w_codec->active = 0;
3277 break;
3278 case SND_SOC_DAPM_STREAM_SUSPEND:
3279 case SND_SOC_DAPM_STREAM_RESUME:
3280 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3281 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3282 break;
3283 }
3284 }
3285
3286 dapm_power_widgets(&rtd->card->dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003287}
3288
3289/**
3290 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3291 * @rtd: PCM runtime data
3292 * @stream: stream name
3293 * @event: stream event
3294 *
3295 * Sends a stream event to the dapm core. The core then makes any
3296 * necessary widget power changes.
3297 *
3298 * Returns 0 for success else error.
3299 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003300void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3301 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003302{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003303 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003304
Liam Girdwood3cd04342012-03-09 12:02:08 +00003305 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003306 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003307 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003308}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003309
3310/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003311 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003312 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003313 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003314 *
Mark Brown74b8f952009-06-06 11:26:15 +01003315 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003316 * a valid audio route and active audio stream.
3317 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3318 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003319 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003320int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003321{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003322 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003323}
Liam Girdwooda5302182008-07-07 13:35:17 +01003324EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003325
3326/**
Mark Brownda341832010-03-15 19:23:37 +00003327 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003328 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003329 * @pin: pin name
3330 *
3331 * Enables input/output pin regardless of any other state. This is
3332 * intended for use with microphone bias supplies used in microphone
3333 * jack detection.
3334 *
3335 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3336 * do any widget power switching.
3337 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003338int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3339 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003340{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003341 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003342
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003343 if (!w) {
3344 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3345 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003346 }
3347
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003348 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3349 w->connected = 1;
3350 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003351 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003352
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003353 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003354}
3355EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3356
3357/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003358 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003359 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003360 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003361 *
Mark Brown74b8f952009-06-06 11:26:15 +01003362 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003363 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3364 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003365 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003366int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3367 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003368{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003369 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003370}
3371EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3372
3373/**
Mark Brown5817b522008-09-24 11:23:11 +01003374 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003375 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003376 * @pin: pin name
3377 *
3378 * Marks the specified pin as being not connected, disabling it along
3379 * any parent or child widgets. At present this is identical to
3380 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3381 * additional things such as disabling controls which only affect
3382 * paths through the pin.
3383 *
3384 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3385 * do any widget power switching.
3386 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003387int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003388{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003389 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003390}
3391EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3392
3393/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003394 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003395 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003396 * @pin: audio signal pin endpoint (or start point)
3397 *
3398 * Get audio pin status - connected or disconnected.
3399 *
3400 * Returns 1 for connected otherwise 0.
3401 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003402int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3403 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003404{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003405 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003406
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003407 if (w)
3408 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003409
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003410 return 0;
3411}
Liam Girdwooda5302182008-07-07 13:35:17 +01003412EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003413
3414/**
Mark Brown1547aba2010-05-07 21:11:40 +01003415 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003416 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003417 * @pin: audio signal pin endpoint (or start point)
3418 *
3419 * Mark the given endpoint or pin as ignoring suspend. When the
3420 * system is disabled a path between two endpoints flagged as ignoring
3421 * suspend will not be disabled. The path must already be enabled via
3422 * normal means at suspend time, it will not be turned on if it was not
3423 * already enabled.
3424 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003425int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3426 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003427{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003428 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003429
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003430 if (!w) {
3431 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3432 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003433 }
3434
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003435 w->ignore_suspend = 1;
3436
3437 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003438}
3439EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3440
Stephen Warren16332812011-11-23 12:42:04 -07003441static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3442 struct snd_soc_dapm_widget *w)
3443{
3444 struct snd_soc_dapm_path *p;
3445
3446 list_for_each_entry(p, &card->paths, list) {
3447 if ((p->source == w) || (p->sink == w)) {
3448 dev_dbg(card->dev,
3449 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3450 p->source->name, p->source->id, p->source->dapm,
3451 p->sink->name, p->sink->id, p->sink->dapm);
3452
3453 /* Connected to something other than the codec */
3454 if (p->source->dapm != p->sink->dapm)
3455 return true;
3456 /*
3457 * Loopback connection from codec external pin to
3458 * codec external pin
3459 */
3460 if (p->sink->id == snd_soc_dapm_input) {
3461 switch (p->source->id) {
3462 case snd_soc_dapm_output:
3463 case snd_soc_dapm_micbias:
3464 return true;
3465 default:
3466 break;
3467 }
3468 }
3469 }
3470 }
3471
3472 return false;
3473}
3474
3475/**
3476 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3477 * @codec: The codec whose pins should be processed
3478 *
3479 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3480 * which are unused. Pins are used if they are connected externally to the
3481 * codec, whether that be to some other device, or a loop-back connection to
3482 * the codec itself.
3483 */
3484void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3485{
3486 struct snd_soc_card *card = codec->card;
3487 struct snd_soc_dapm_context *dapm = &codec->dapm;
3488 struct snd_soc_dapm_widget *w;
3489
Mark Browna094b802011-11-27 19:42:20 +00003490 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003491 &card->dapm, &codec->dapm);
3492
3493 list_for_each_entry(w, &card->widgets, list) {
3494 if (w->dapm != dapm)
3495 continue;
3496 switch (w->id) {
3497 case snd_soc_dapm_input:
3498 case snd_soc_dapm_output:
3499 case snd_soc_dapm_micbias:
Mark Browna094b802011-11-27 19:42:20 +00003500 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003501 w->name);
3502 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003503 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003504 "... Not in map; disabling\n");
3505 snd_soc_dapm_nc_pin(dapm, w->name);
3506 }
3507 break;
3508 default:
3509 break;
3510 }
3511 }
3512}
3513
Mark Brown1547aba2010-05-07 21:11:40 +01003514/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003515 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003516 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003517 *
3518 * Free all dapm widgets and resources.
3519 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003520void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003521{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003522 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003523 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003524 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003525 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003526}
3527EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3528
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003529static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003530{
Mark Brown51737472009-06-22 13:16:51 +01003531 struct snd_soc_dapm_widget *w;
3532 LIST_HEAD(down_list);
3533 int powerdown = 0;
3534
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003535 list_for_each_entry(w, &dapm->card->widgets, list) {
3536 if (w->dapm != dapm)
3537 continue;
Mark Brown51737472009-06-22 13:16:51 +01003538 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003539 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003540 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003541 powerdown = 1;
3542 }
3543 }
3544
3545 /* If there were no widgets to power down we're already in
3546 * standby.
3547 */
3548 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003549 if (dapm->bias_level == SND_SOC_BIAS_ON)
3550 snd_soc_dapm_set_bias_level(dapm,
3551 SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003552 dapm_seq_run(dapm, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003553 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3554 snd_soc_dapm_set_bias_level(dapm,
3555 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003556 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003557}
Mark Brown51737472009-06-22 13:16:51 +01003558
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003559/*
3560 * snd_soc_dapm_shutdown - callback for system shutdown
3561 */
3562void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3563{
3564 struct snd_soc_codec *codec;
3565
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003566 list_for_each_entry(codec, &card->codec_dev_list, list) {
3567 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003568 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3569 snd_soc_dapm_set_bias_level(&codec->dapm,
3570 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003571 }
Mark Brown51737472009-06-22 13:16:51 +01003572}
3573
Richard Purdie2b97eab2006-10-06 18:32:18 +02003574/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003575MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003576MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3577MODULE_LICENSE("GPL");