blob: 145ec4b56ca9e1891d3835a64c2e249168aaa6ca [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
Liam Girdwood612a3fe2012-02-06 16:05:29 +000017 * mic/headphone insertion events.
Richard Purdie2b97eab2006-10-06 18:32:18 +020018 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Liam Girdwood612a3fe2012-02-06 16:05:29 +000021 * o Delayed power down of audio subsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020024 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080029#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020030#include <linux/delay.h>
31#include <linux/pm.h>
32#include <linux/bitops.h>
33#include <linux/platform_device.h>
34#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020035#include <linux/debugfs.h>
Mark Brownf1aac482011-12-05 15:17:06 +000036#include <linux/pm_runtime.h>
Mark Brown62ea8742012-01-21 21:14:48 +000037#include <linux/regulator/consumer.h>
Ola Liljad7e7eb92012-05-24 15:26:25 +020038#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020040#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020043#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020044#include <sound/initval.h>
45
Mark Brown84e90932010-11-04 00:07:02 -040046#include <trace/events/asoc.h>
47
Mark Brownde02d072011-09-20 21:43:24 +010048#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49
Richard Purdie2b97eab2006-10-06 18:32:18 +020050/* dapm power sequences - make this per codec in the future */
51static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010052 [snd_soc_dapm_pre] = 0,
53 [snd_soc_dapm_supply] = 1,
Mark Brown62ea8742012-01-21 21:14:48 +000054 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020055 [snd_soc_dapm_clock_supply] = 1,
Mark Brown38357ab2009-06-06 19:03:23 +010056 [snd_soc_dapm_micbias] = 2,
Mark Brownc74184e2012-04-04 22:12:09 +010057 [snd_soc_dapm_dai_link] = 2,
Mark Brown888df392012-02-16 19:37:51 -080058 [snd_soc_dapm_dai] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010059 [snd_soc_dapm_aif_in] = 3,
60 [snd_soc_dapm_aif_out] = 3,
61 [snd_soc_dapm_mic] = 4,
62 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000063 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010064 [snd_soc_dapm_value_mux] = 5,
65 [snd_soc_dapm_dac] = 6,
66 [snd_soc_dapm_mixer] = 7,
67 [snd_soc_dapm_mixer_named_ctl] = 7,
68 [snd_soc_dapm_pga] = 8,
69 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060070 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010071 [snd_soc_dapm_hp] = 10,
72 [snd_soc_dapm_spk] = 10,
Mark Brown7e1f7c82012-04-12 17:29:36 +010073 [snd_soc_dapm_line] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010074 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020075};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000076
Richard Purdie2b97eab2006-10-06 18:32:18 +020077static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010078 [snd_soc_dapm_pre] = 0,
79 [snd_soc_dapm_adc] = 1,
80 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010081 [snd_soc_dapm_spk] = 2,
Mark Brown7e1f7c82012-04-12 17:29:36 +010082 [snd_soc_dapm_line] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060083 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010084 [snd_soc_dapm_pga] = 4,
85 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010086 [snd_soc_dapm_mixer] = 5,
87 [snd_soc_dapm_dac] = 6,
88 [snd_soc_dapm_mic] = 7,
89 [snd_soc_dapm_micbias] = 8,
90 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000091 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010092 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010093 [snd_soc_dapm_aif_in] = 10,
94 [snd_soc_dapm_aif_out] = 10,
Mark Brown888df392012-02-16 19:37:51 -080095 [snd_soc_dapm_dai] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +010096 [snd_soc_dapm_dai_link] = 11,
Ola Liljad7e7eb92012-05-24 15:26:25 +020097 [snd_soc_dapm_clock_supply] = 12,
Mark Brownc74184e2012-04-04 22:12:09 +010098 [snd_soc_dapm_regulator_supply] = 12,
99 [snd_soc_dapm_supply] = 12,
100 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200101};
102
Troy Kisky12ef1932008-10-13 17:42:14 -0700103static void pop_wait(u32 pop_time)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100104{
105 if (pop_time)
106 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
107}
108
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200109static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100110{
111 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200112 char *buf;
113
114 if (!pop_time)
115 return;
116
117 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
118 if (buf == NULL)
119 return;
Mark Brown15e4c72f2008-07-02 11:51:20 +0100120
121 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200122 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100123 dev_info(dev, "%s", buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100124 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200125
126 kfree(buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100127}
128
Mark Browndb432b42011-10-03 21:06:40 +0100129static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
130{
131 return !list_empty(&w->dirty);
132}
133
Mark Brown25c77c52011-10-08 13:36:03 +0100134void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100135{
Mark Brown75c1f892011-10-04 22:28:08 +0100136 if (!dapm_dirty_widget(w)) {
137 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
138 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100139 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100140 }
Mark Browndb432b42011-10-03 21:06:40 +0100141}
Mark Brown25c77c52011-10-08 13:36:03 +0100142EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100143
Richard Purdie2b97eab2006-10-06 18:32:18 +0200144/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100145static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200146 const struct snd_soc_dapm_widget *_widget)
147{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100148 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200149}
150
Liam Girdwood48056082011-07-20 12:23:33 +0100151/* get snd_card from DAPM context */
152static inline struct snd_card *dapm_get_snd_card(
153 struct snd_soc_dapm_context *dapm)
154{
155 if (dapm->codec)
156 return dapm->codec->card->snd_card;
157 else if (dapm->platform)
158 return dapm->platform->card->snd_card;
159 else
160 BUG();
161
162 /* unreachable */
163 return NULL;
164}
165
166/* get soc_card from DAPM context */
167static inline struct snd_soc_card *dapm_get_soc_card(
168 struct snd_soc_dapm_context *dapm)
169{
170 if (dapm->codec)
171 return dapm->codec->card;
172 else if (dapm->platform)
173 return dapm->platform->card;
174 else
175 BUG();
176
177 /* unreachable */
178 return NULL;
179}
180
Liam Girdwood6c120e12012-02-15 15:15:34 +0000181static void dapm_reset(struct snd_soc_card *card)
182{
183 struct snd_soc_dapm_widget *w;
184
185 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
186
187 list_for_each_entry(w, &card->widgets, list) {
188 w->power_checked = false;
189 w->inputs = -1;
190 w->outputs = -1;
191 }
192}
193
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100194static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
195{
196 if (w->codec)
197 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100198 else if (w->platform)
199 return snd_soc_platform_read(w->platform, reg);
200
201 dev_err(w->dapm->dev, "no valid widget read method\n");
202 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100203}
204
205static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
206{
207 if (w->codec)
208 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100209 else if (w->platform)
210 return snd_soc_platform_write(w->platform, reg, val);
211
212 dev_err(w->dapm->dev, "no valid widget write method\n");
213 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100214}
215
Liam Girdwood49575fb52012-03-06 18:16:19 +0000216static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
217{
Mark Browne06ab3b2012-03-06 23:58:22 +0000218 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000219 mutex_lock(&w->codec->mutex);
220 else if (w->platform)
221 mutex_lock(&w->platform->mutex);
222}
223
224static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
225{
Mark Browne06ab3b2012-03-06 23:58:22 +0000226 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000227 mutex_unlock(&w->codec->mutex);
228 else if (w->platform)
229 mutex_unlock(&w->platform->mutex);
230}
231
232static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100233 unsigned short reg, unsigned int mask, unsigned int value)
234{
Mark Brown8a713da2011-12-03 12:33:55 +0000235 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100236 unsigned int old, new;
237 int ret;
238
Mark Brown8a713da2011-12-03 12:33:55 +0000239 if (w->codec && w->codec->using_regmap) {
240 ret = regmap_update_bits_check(w->codec->control_data,
241 reg, mask, value, &change);
242 if (ret != 0)
243 return ret;
244 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000245 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000246 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000247 if (ret < 0) {
248 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100249 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000250 }
Mark Brown8a713da2011-12-03 12:33:55 +0000251
252 old = ret;
253 new = (old & ~mask) | (value & mask);
254 change = old != new;
255 if (change) {
256 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000257 if (ret < 0) {
258 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000259 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000260 }
Mark Brown8a713da2011-12-03 12:33:55 +0000261 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000262 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100263 }
264
265 return change;
266}
267
Mark Brown452c5ea2009-05-17 21:41:23 +0100268/**
269 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800270 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100271 * @level: level to configure
272 *
273 * Configure the bias (power) levels for the SoC audio device.
274 *
275 * Returns 0 for success else error.
276 */
Mark Browned5a4c42011-02-18 11:12:42 -0800277static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200278 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100279{
Mark Browned5a4c42011-02-18 11:12:42 -0800280 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100281 int ret = 0;
282
Mark Brown84e90932010-11-04 00:07:02 -0400283 trace_snd_soc_bias_level_start(card, level);
284
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000285 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100286 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100287 if (ret != 0)
288 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100289
Mark Browncc4c6702011-06-06 19:03:34 +0100290 if (dapm->codec) {
291 if (dapm->codec->driver->set_bias_level)
292 ret = dapm->codec->driver->set_bias_level(dapm->codec,
293 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100294 else
295 dapm->bias_level = level;
Liam Girdwood41231282012-07-06 16:56:16 +0100296 } else
297 dapm->bias_level = level;
298
Mark Brown171ec6b2011-06-06 18:15:19 +0100299 if (ret != 0)
300 goto out;
301
302 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100303 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100304out:
Mark Brown84e90932010-11-04 00:07:02 -0400305 trace_snd_soc_bias_level_done(card, level);
306
Mark Brown452c5ea2009-05-17 21:41:23 +0100307 return ret;
308}
309
Richard Purdie2b97eab2006-10-06 18:32:18 +0200310/* set up initial codec paths */
311static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
312 struct snd_soc_dapm_path *p, int i)
313{
314 switch (w->id) {
315 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000316 case snd_soc_dapm_mixer:
317 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200318 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100319 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600320 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400321 unsigned int reg = mc->reg;
322 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100323 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400324 unsigned int mask = (1 << fls(max)) - 1;
325 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200326
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100327 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200328 val = (val >> shift) & mask;
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200329 if (invert)
330 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200331
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200332 p->connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200333 }
334 break;
335 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600336 struct soc_enum *e = (struct soc_enum *)
337 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200338 int val, item, bitmask;
339
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100340 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Mark Brown88d96082011-06-06 16:16:34 +0100341 ;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100342 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200343 item = (val >> e->shift_l) & (bitmask - 1);
344
345 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100346 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200347 if (!(strcmp(p->name, e->texts[i])) && item == i)
348 p->connect = 1;
349 }
350 }
351 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000352 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600353 struct soc_enum *e = (struct soc_enum *)
354 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000355
356 p->connect = 0;
357 /* since a virtual mux has no backing registers to
358 * decide which path to connect, it will try to match
359 * with the first enumeration. This is to ensure
360 * that the default mux choice (the first) will be
361 * correctly powered up during initialization.
362 */
363 if (!strcmp(p->name, e->texts[0]))
364 p->connect = 1;
365 }
366 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200367 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200368 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600369 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200370 int val, item;
371
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100372 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200373 val = (val >> e->shift_l) & e->mask;
374 for (item = 0; item < e->max; item++) {
375 if (val == e->values[item])
376 break;
377 }
378
379 p->connect = 0;
380 for (i = 0; i < e->max; i++) {
381 if (!(strcmp(p->name, e->texts[i])) && item == i)
382 p->connect = 1;
383 }
384 }
385 break;
Mark Brown56563102011-10-03 22:41:09 +0100386 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200387 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600388 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200389 case snd_soc_dapm_output:
390 case snd_soc_dapm_adc:
391 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000392 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200393 case snd_soc_dapm_dac:
394 case snd_soc_dapm_micbias:
395 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100396 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000397 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200398 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100399 case snd_soc_dapm_aif_in:
400 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800401 case snd_soc_dapm_dai:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200402 case snd_soc_dapm_hp:
403 case snd_soc_dapm_mic:
404 case snd_soc_dapm_spk:
405 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100406 case snd_soc_dapm_dai_link:
Mark Brown56563102011-10-03 22:41:09 +0100407 p->connect = 1;
408 break;
409 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200410 case snd_soc_dapm_pre:
411 case snd_soc_dapm_post:
412 p->connect = 0;
413 break;
414 }
415}
416
Mark Brown74b8f952009-06-06 11:26:15 +0100417/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200418static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200419 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
420 struct snd_soc_dapm_path *path, const char *control_name,
421 const struct snd_kcontrol_new *kcontrol)
422{
423 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
424 int i;
425
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100426 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200427 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200428 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200429 list_add(&path->list_sink, &dest->sources);
430 list_add(&path->list_source, &src->sinks);
431 path->name = (char*)e->texts[i];
432 dapm_set_path_status(dest, path, 0);
433 return 0;
434 }
435 }
436
437 return -ENODEV;
438}
439
Mark Brown74b8f952009-06-06 11:26:15 +0100440/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200441static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200442 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
443 struct snd_soc_dapm_path *path, const char *control_name)
444{
445 int i;
446
447 /* search for mixer kcontrol */
448 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600449 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200450 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200451 list_add(&path->list_sink, &dest->sources);
452 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600453 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200454 dapm_set_path_status(dest, path, i);
455 return 0;
456 }
457 }
458 return -ENODEV;
459}
460
Stephen Warrenaf468002011-04-28 17:38:01 -0600461static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600462 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600463 const struct snd_kcontrol_new *kcontrol_new,
464 struct snd_kcontrol **kcontrol)
465{
466 struct snd_soc_dapm_widget *w;
467 int i;
468
469 *kcontrol = NULL;
470
471 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600472 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
473 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600474 for (i = 0; i < w->num_kcontrols; i++) {
475 if (&w->kcontrol_news[i] == kcontrol_new) {
476 if (w->kcontrols)
477 *kcontrol = w->kcontrols[i];
478 return 1;
479 }
480 }
481 }
482
483 return 0;
484}
485
Richard Purdie2b97eab2006-10-06 18:32:18 +0200486/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200487static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200488{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200489 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200490 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000491 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200492 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000493 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000494 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600495 struct snd_soc_dapm_widget_list *wlist;
496 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000497
498 if (dapm->codec)
499 prefix = dapm->codec->name_prefix;
500 else
501 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200502
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000503 if (prefix)
504 prefix_len = strlen(prefix) + 1;
505 else
506 prefix_len = 0;
507
Richard Purdie2b97eab2006-10-06 18:32:18 +0200508 /* add kcontrol */
509 for (i = 0; i < w->num_kcontrols; i++) {
510
511 /* match name */
512 list_for_each_entry(path, &w->sources, list_sink) {
513
514 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600515 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200516 continue;
517
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200518 if (w->kcontrols[i]) {
519 path->kcontrol = w->kcontrols[i];
520 continue;
521 }
522
Stephen Warrenfafd2172011-04-28 17:38:00 -0600523 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
524 sizeof(struct snd_soc_dapm_widget *),
525 wlist = kzalloc(wlistsize, GFP_KERNEL);
526 if (wlist == NULL) {
527 dev_err(dapm->dev,
528 "asoc: can't allocate widget list for %s\n",
529 w->name);
530 return -ENOMEM;
531 }
532 wlist->num_widgets = 1;
533 wlist->widgets[0] = w;
534
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000535 /* add dapm control with long name.
536 * for dapm_mixer this is the concatenation of the
537 * mixer and kcontrol name.
538 * for dapm_mixer_named_ctl this is simply the
539 * kcontrol name.
540 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600541 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000542 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000543 name_len += 1 + strlen(w->name);
544
Mark Brown219b93f2008-10-28 13:02:31 +0000545 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000546
Stephen Warrenfafd2172011-04-28 17:38:00 -0600547 if (path->long_name == NULL) {
548 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200549 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600550 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200551
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000552 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000553 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000554 /* The control will get a prefix from
555 * the control creation process but
556 * we're also using the same prefix
557 * for widgets so cut the prefix off
558 * the front of the widget name.
559 */
Mark Brown888df392012-02-16 19:37:51 -0800560 snprintf((char *)path->long_name, name_len,
561 "%s %s", w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600562 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000563 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000564 case snd_soc_dapm_mixer_named_ctl:
Mark Brown888df392012-02-16 19:37:51 -0800565 snprintf((char *)path->long_name, name_len,
566 "%s", w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000567 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000568 }
569
Mark Brown888df392012-02-16 19:37:51 -0800570 ((char *)path->long_name)[name_len - 1] = '\0';
Mark Brown219b93f2008-10-28 13:02:31 +0000571
Stephen Warrenfafd2172011-04-28 17:38:00 -0600572 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
573 wlist, path->long_name,
574 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200575 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200576 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200577 dev_err(dapm->dev,
578 "asoc: failed to add dapm kcontrol %s: %d\n",
579 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600580 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200581 kfree(path->long_name);
582 path->long_name = NULL;
583 return ret;
584 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600585 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200586 }
587 }
588 return ret;
589}
590
591/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200592static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200593{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200594 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200595 struct snd_soc_dapm_path *path = NULL;
596 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000597 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000598 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000599 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600600 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600601 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600602 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600603 size_t wlistsize;
Mark Brown888df392012-02-16 19:37:51 -0800604 const char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200605
Stephen Warrenaf468002011-04-28 17:38:01 -0600606 if (w->num_kcontrols != 1) {
607 dev_err(dapm->dev,
608 "asoc: mux %s has incorrect number of controls\n",
609 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200610 return -EINVAL;
611 }
612
Stephen Warren1007da02011-05-26 09:57:33 -0600613 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600614 &kcontrol);
615 if (kcontrol) {
616 wlist = kcontrol->private_data;
617 wlistentries = wlist->num_widgets + 1;
618 } else {
619 wlist = NULL;
620 wlistentries = 1;
621 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600622 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600623 wlistentries * sizeof(struct snd_soc_dapm_widget *),
624 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600625 if (wlist == NULL) {
626 dev_err(dapm->dev,
627 "asoc: can't allocate widget list for %s\n", w->name);
628 return -ENOMEM;
629 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600630 wlist->num_widgets = wlistentries;
631 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600632
Stephen Warrenaf468002011-04-28 17:38:01 -0600633 if (!kcontrol) {
634 if (dapm->codec)
635 prefix = dapm->codec->name_prefix;
636 else
637 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000638
Stephen Warrenaf468002011-04-28 17:38:01 -0600639 if (shared) {
640 name = w->kcontrol_news[0].name;
641 prefix_len = 0;
642 } else {
643 name = w->name;
644 if (prefix)
645 prefix_len = strlen(prefix) + 1;
646 else
647 prefix_len = 0;
648 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000649
Stephen Warrenaf468002011-04-28 17:38:01 -0600650 /*
651 * The control will get a prefix from the control creation
652 * process but we're also using the same prefix for widgets so
653 * cut the prefix off the front of the widget name.
654 */
655 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
656 name + prefix_len, prefix);
657 ret = snd_ctl_add(card, kcontrol);
658 if (ret < 0) {
Mark Brown53daf202011-09-05 10:51:05 -0700659 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
660 w->name, ret);
Stephen Warrenaf468002011-04-28 17:38:01 -0600661 kfree(wlist);
662 return ret;
663 }
664 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200665
Stephen Warrenaf468002011-04-28 17:38:01 -0600666 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200667
Stephen Warrenfad59882011-04-28 17:37:59 -0600668 w->kcontrols[0] = kcontrol;
669
Richard Purdie2b97eab2006-10-06 18:32:18 +0200670 list_for_each_entry(path, &w->sources, list_sink)
671 path->kcontrol = kcontrol;
672
Stephen Warrenaf468002011-04-28 17:38:01 -0600673 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200674}
675
676/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200677static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200678{
Mark Browna6c65732010-03-03 17:45:21 +0000679 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200680 dev_err(w->dapm->dev,
681 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200682
Mark Browna6c65732010-03-03 17:45:21 +0000683 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200684}
685
686/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200687static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200688{
689 struct snd_soc_dapm_path *p;
690
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200691 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200692 p->walked = 0;
693}
694
Mark Brown9949788b2010-05-07 20:24:05 +0100695/* We implement power down on suspend by checking the power state of
696 * the ALSA card - when we are suspending the ALSA state for the card
697 * is set to D3.
698 */
699static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
700{
Mark Brown12ea2c72011-03-02 18:17:32 +0000701 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100702
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000703 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100704 case SNDRV_CTL_POWER_D3hot:
705 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100706 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200707 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
708 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100709 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100710 default:
711 return 1;
712 }
713}
714
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100715/* add widget to list if it's not already in the list */
716static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
717 struct snd_soc_dapm_widget *w)
718{
719 struct snd_soc_dapm_widget_list *wlist;
720 int wlistsize, wlistentries, i;
721
722 if (*list == NULL)
723 return -EINVAL;
724
725 wlist = *list;
726
727 /* is this widget already in the list */
728 for (i = 0; i < wlist->num_widgets; i++) {
729 if (wlist->widgets[i] == w)
730 return 0;
731 }
732
733 /* allocate some new space */
734 wlistentries = wlist->num_widgets + 1;
735 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
736 wlistentries * sizeof(struct snd_soc_dapm_widget *);
737 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
738 if (*list == NULL) {
739 dev_err(w->dapm->dev, "can't allocate widget list for %s\n",
740 w->name);
741 return -ENOMEM;
742 }
743 wlist = *list;
744
745 /* insert the widget */
746 dev_dbg(w->dapm->dev, "added %s in widget list pos %d\n",
747 w->name, wlist->num_widgets);
748
749 wlist->widgets[wlist->num_widgets] = w;
750 wlist->num_widgets++;
751 return 1;
752}
753
Richard Purdie2b97eab2006-10-06 18:32:18 +0200754/*
755 * Recursively check for a completed path to an active or physically connected
756 * output widget. Returns number of complete paths.
757 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100758static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
759 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200760{
761 struct snd_soc_dapm_path *path;
762 int con = 0;
763
Mark Brown024dc072011-10-09 11:52:05 +0100764 if (widget->outputs >= 0)
765 return widget->outputs;
766
Mark Brownde02d072011-09-20 21:43:24 +0100767 DAPM_UPDATE_STAT(widget, path_checks);
768
Mark Brown62ea8742012-01-21 21:14:48 +0000769 switch (widget->id) {
770 case snd_soc_dapm_supply:
771 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200772 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100773 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000774 default:
775 break;
776 }
Mark Brown246d0a12009-04-22 18:24:55 +0100777
Mark Brown010ff262009-08-17 17:39:22 +0100778 switch (widget->id) {
779 case snd_soc_dapm_adc:
780 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800781 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100782 if (widget->active) {
783 widget->outputs = snd_soc_dapm_suspend_check(widget);
784 return widget->outputs;
785 }
Mark Brown010ff262009-08-17 17:39:22 +0100786 default:
787 break;
788 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200789
790 if (widget->connected) {
791 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100792 if (widget->id == snd_soc_dapm_output && !widget->ext) {
793 widget->outputs = snd_soc_dapm_suspend_check(widget);
794 return widget->outputs;
795 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200796
797 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100798 if (widget->id == snd_soc_dapm_hp ||
799 widget->id == snd_soc_dapm_spk ||
800 (widget->id == snd_soc_dapm_line &&
801 !list_empty(&widget->sources))) {
802 widget->outputs = snd_soc_dapm_suspend_check(widget);
803 return widget->outputs;
804 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200805 }
806
807 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100808 DAPM_UPDATE_STAT(widget, neighbour_checks);
809
Mark Brownbf3a9e12011-06-13 16:42:29 +0100810 if (path->weak)
811 continue;
812
Richard Purdie2b97eab2006-10-06 18:32:18 +0200813 if (path->walked)
814 continue;
815
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100816 trace_snd_soc_dapm_output_path(widget, path);
817
Richard Purdie2b97eab2006-10-06 18:32:18 +0200818 if (path->sink && path->connect) {
819 path->walked = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100820
821 /* do we need to add this widget to the list ? */
822 if (list) {
823 int err;
824 err = dapm_list_add_widget(list, path->sink);
825 if (err < 0) {
826 dev_err(widget->dapm->dev, "could not add widget %s\n",
827 widget->name);
828 return con;
829 }
830 }
831
832 con += is_connected_output_ep(path->sink, list);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200833 }
834 }
835
Mark Brown024dc072011-10-09 11:52:05 +0100836 widget->outputs = con;
837
Richard Purdie2b97eab2006-10-06 18:32:18 +0200838 return con;
839}
840
841/*
842 * Recursively check for a completed path to an active or physically connected
843 * input widget. Returns number of complete paths.
844 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100845static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
846 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200847{
848 struct snd_soc_dapm_path *path;
849 int con = 0;
850
Mark Brown024dc072011-10-09 11:52:05 +0100851 if (widget->inputs >= 0)
852 return widget->inputs;
853
Mark Brownde02d072011-09-20 21:43:24 +0100854 DAPM_UPDATE_STAT(widget, path_checks);
855
Mark Brown62ea8742012-01-21 21:14:48 +0000856 switch (widget->id) {
857 case snd_soc_dapm_supply:
858 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200859 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100860 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000861 default:
862 break;
863 }
Mark Brown246d0a12009-04-22 18:24:55 +0100864
Richard Purdie2b97eab2006-10-06 18:32:18 +0200865 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100866 switch (widget->id) {
867 case snd_soc_dapm_dac:
868 case snd_soc_dapm_aif_in:
Mark Brown888df392012-02-16 19:37:51 -0800869 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100870 if (widget->active) {
871 widget->inputs = snd_soc_dapm_suspend_check(widget);
872 return widget->inputs;
873 }
Mark Brown010ff262009-08-17 17:39:22 +0100874 default:
875 break;
876 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200877
878 if (widget->connected) {
879 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100880 if (widget->id == snd_soc_dapm_input && !widget->ext) {
881 widget->inputs = snd_soc_dapm_suspend_check(widget);
882 return widget->inputs;
883 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200884
885 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100886 if (widget->id == snd_soc_dapm_vmid) {
887 widget->inputs = snd_soc_dapm_suspend_check(widget);
888 return widget->inputs;
889 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200890
891 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300892 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100893 (widget->id == snd_soc_dapm_line &&
894 !list_empty(&widget->sinks))) {
895 widget->inputs = snd_soc_dapm_suspend_check(widget);
896 return widget->inputs;
897 }
898
Mark Brown1ab97c82011-11-27 16:21:51 +0000899 /* signal generator */
900 if (widget->id == snd_soc_dapm_siggen) {
901 widget->inputs = snd_soc_dapm_suspend_check(widget);
902 return widget->inputs;
903 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200904 }
905
906 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +0100907 DAPM_UPDATE_STAT(widget, neighbour_checks);
908
Mark Brownbf3a9e12011-06-13 16:42:29 +0100909 if (path->weak)
910 continue;
911
Richard Purdie2b97eab2006-10-06 18:32:18 +0200912 if (path->walked)
913 continue;
914
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100915 trace_snd_soc_dapm_input_path(widget, path);
916
Richard Purdie2b97eab2006-10-06 18:32:18 +0200917 if (path->source && path->connect) {
918 path->walked = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100919
920 /* do we need to add this widget to the list ? */
921 if (list) {
922 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +0100923 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100924 if (err < 0) {
925 dev_err(widget->dapm->dev, "could not add widget %s\n",
926 widget->name);
927 return con;
928 }
929 }
930
931 con += is_connected_input_ep(path->source, list);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200932 }
933 }
934
Mark Brown024dc072011-10-09 11:52:05 +0100935 widget->inputs = con;
936
Richard Purdie2b97eab2006-10-06 18:32:18 +0200937 return con;
938}
939
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100940/**
941 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
942 * @dai: the soc DAI.
943 * @stream: stream direction.
944 * @list: list of active widgets for this stream.
945 *
946 * Queries DAPM graph as to whether an valid audio stream path exists for
947 * the initial stream specified by name. This takes into account
948 * current mixer and mux kcontrol settings. Creates list of valid widgets.
949 *
950 * Returns the number of valid paths or negative error.
951 */
952int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
953 struct snd_soc_dapm_widget_list **list)
954{
955 struct snd_soc_card *card = dai->card;
956 int paths;
957
958 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
959 dapm_reset(card);
960
961 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
962 paths = is_connected_output_ep(dai->playback_widget, list);
963 else
Liam Girdwoodd298caa2012-06-01 18:03:00 +0100964 paths = is_connected_input_ep(dai->capture_widget, list);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100965
966 trace_snd_soc_dapm_connected(paths, stream);
967 dapm_clear_walk(&card->dapm);
968 mutex_unlock(&card->dapm_mutex);
969
970 return paths;
971}
972
Richard Purdie2b97eab2006-10-06 18:32:18 +0200973/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300974 * Handler for generic register modifier widget.
975 */
976int dapm_reg_event(struct snd_soc_dapm_widget *w,
977 struct snd_kcontrol *kcontrol, int event)
978{
979 unsigned int val;
980
981 if (SND_SOC_DAPM_EVENT_ON(event))
982 val = w->on_val;
983 else
984 val = w->off_val;
985
Liam Girdwood49575fb52012-03-06 18:16:19 +0000986 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300987 w->mask << w->shift, val << w->shift);
988
989 return 0;
990}
Mark Brown11589412008-07-29 11:42:23 +0100991EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300992
Mark Brown62ea8742012-01-21 21:14:48 +0000993/*
994 * Handler for regulator supply widget.
995 */
996int dapm_regulator_event(struct snd_soc_dapm_widget *w,
997 struct snd_kcontrol *kcontrol, int event)
998{
999 if (SND_SOC_DAPM_EVENT_ON(event))
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001000 return regulator_enable(w->regulator);
Mark Brown62ea8742012-01-21 21:14:48 +00001001 else
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001002 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brown62ea8742012-01-21 21:14:48 +00001003}
1004EXPORT_SYMBOL_GPL(dapm_regulator_event);
1005
Ola Liljad7e7eb92012-05-24 15:26:25 +02001006/*
1007 * Handler for clock supply widget.
1008 */
1009int dapm_clock_event(struct snd_soc_dapm_widget *w,
1010 struct snd_kcontrol *kcontrol, int event)
1011{
1012 if (!w->clk)
1013 return -EIO;
1014
Mark Brownec029952012-06-04 08:16:20 +01001015#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001016 if (SND_SOC_DAPM_EVENT_ON(event)) {
1017 return clk_enable(w->clk);
1018 } else {
1019 clk_disable(w->clk);
1020 return 0;
1021 }
Mark Brownec029952012-06-04 08:16:20 +01001022#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001023 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001024}
1025EXPORT_SYMBOL_GPL(dapm_clock_event);
1026
Mark Brownd805002b2011-09-28 18:28:23 +01001027static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1028{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001029 if (w->power_checked)
1030 return w->new_power;
1031
Mark Brownd805002b2011-09-28 18:28:23 +01001032 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001033 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +01001034 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001035 w->new_power = w->power_check(w);
1036
1037 w->power_checked = true;
1038
1039 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +01001040}
1041
Mark Browncd0f2d42009-04-20 16:56:59 +01001042/* Generic check to see if a widget should be powered.
1043 */
1044static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1045{
1046 int in, out;
1047
Mark Brownde02d072011-09-20 21:43:24 +01001048 DAPM_UPDATE_STAT(w, power_checks);
1049
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001050 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001051 dapm_clear_walk(w->dapm);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001052 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001053 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +01001054 return out != 0 && in != 0;
1055}
1056
Mark Brown888df392012-02-16 19:37:51 -08001057static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
1058{
1059 DAPM_UPDATE_STAT(w, power_checks);
1060
Mark Brown1eee1b32012-04-10 13:57:46 +01001061 if (w->active)
1062 return w->active;
1063
1064 return dapm_generic_check_power(w);
Mark Brown888df392012-02-16 19:37:51 -08001065}
1066
Mark Brown6ea31b92009-04-20 17:15:41 +01001067/* Check to see if an ADC has power */
1068static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1069{
1070 int in;
1071
Mark Brownde02d072011-09-20 21:43:24 +01001072 DAPM_UPDATE_STAT(w, power_checks);
1073
Mark Brown6ea31b92009-04-20 17:15:41 +01001074 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001075 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001076 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +01001077 return in != 0;
1078 } else {
1079 return dapm_generic_check_power(w);
1080 }
1081}
1082
1083/* Check to see if a DAC has power */
1084static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1085{
1086 int out;
1087
Mark Brownde02d072011-09-20 21:43:24 +01001088 DAPM_UPDATE_STAT(w, power_checks);
1089
Mark Brown6ea31b92009-04-20 17:15:41 +01001090 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001091 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001092 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +01001093 return out != 0;
1094 } else {
1095 return dapm_generic_check_power(w);
1096 }
1097}
1098
Mark Brown246d0a12009-04-22 18:24:55 +01001099/* Check to see if a power supply is needed */
1100static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1101{
1102 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001103
Mark Brownde02d072011-09-20 21:43:24 +01001104 DAPM_UPDATE_STAT(w, power_checks);
1105
Mark Brown246d0a12009-04-22 18:24:55 +01001106 /* Check if one of our outputs is connected */
1107 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001108 DAPM_UPDATE_STAT(w, neighbour_checks);
1109
Mark Brownbf3a9e12011-06-13 16:42:29 +01001110 if (path->weak)
1111 continue;
1112
Mark Brown215edda2009-09-08 18:59:05 +01001113 if (path->connected &&
1114 !path->connected(path->source, path->sink))
1115 continue;
1116
Mark Brown30173582011-02-11 11:42:19 +00001117 if (!path->sink)
1118 continue;
1119
Mark Brownf68d7e12011-10-04 22:57:50 +01001120 if (dapm_widget_power_check(path->sink))
1121 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001122 }
1123
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001124 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +01001125
Mark Brownf68d7e12011-10-04 22:57:50 +01001126 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001127}
1128
Mark Brown35c64bc2011-09-28 18:23:53 +01001129static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1130{
1131 return 1;
1132}
1133
Mark Brown38357ab2009-06-06 19:03:23 +01001134static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1135 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001136 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001137{
Mark Brown828a8422011-01-15 13:14:30 +00001138 int *sort;
1139
1140 if (power_up)
1141 sort = dapm_up_seq;
1142 else
1143 sort = dapm_down_seq;
1144
Mark Brown38357ab2009-06-06 19:03:23 +01001145 if (sort[a->id] != sort[b->id])
1146 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001147 if (a->subseq != b->subseq) {
1148 if (power_up)
1149 return a->subseq - b->subseq;
1150 else
1151 return b->subseq - a->subseq;
1152 }
Mark Brownb22ead22009-06-07 12:51:26 +01001153 if (a->reg != b->reg)
1154 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001155 if (a->dapm != b->dapm)
1156 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001157
Mark Brown38357ab2009-06-06 19:03:23 +01001158 return 0;
1159}
Mark Brown42aa3412009-03-01 19:21:10 +00001160
Mark Brown38357ab2009-06-06 19:03:23 +01001161/* Insert a widget in order into a DAPM power sequence. */
1162static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1163 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001164 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001165{
1166 struct snd_soc_dapm_widget *w;
1167
1168 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001169 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001170 list_add_tail(&new_widget->power_list, &w->power_list);
1171 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001172 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001173
Mark Brown38357ab2009-06-06 19:03:23 +01001174 list_add_tail(&new_widget->power_list, list);
1175}
Mark Brown42aa3412009-03-01 19:21:10 +00001176
Mark Brown68f89ad2010-11-03 23:51:49 -04001177static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1178 struct snd_soc_dapm_widget *w, int event)
1179{
1180 struct snd_soc_card *card = dapm->card;
1181 const char *ev_name;
1182 int power, ret;
1183
1184 switch (event) {
1185 case SND_SOC_DAPM_PRE_PMU:
1186 ev_name = "PRE_PMU";
1187 power = 1;
1188 break;
1189 case SND_SOC_DAPM_POST_PMU:
1190 ev_name = "POST_PMU";
1191 power = 1;
1192 break;
1193 case SND_SOC_DAPM_PRE_PMD:
1194 ev_name = "PRE_PMD";
1195 power = 0;
1196 break;
1197 case SND_SOC_DAPM_POST_PMD:
1198 ev_name = "POST_PMD";
1199 power = 0;
1200 break;
1201 default:
1202 BUG();
1203 return;
1204 }
1205
1206 if (w->power != power)
1207 return;
1208
1209 if (w->event && (w->event_flags & event)) {
1210 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1211 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001212 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001213 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001214 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001215 if (ret < 0)
1216 pr_err("%s: %s event failed: %d\n",
1217 ev_name, w->name, ret);
1218 }
1219}
1220
Mark Brownb22ead22009-06-07 12:51:26 +01001221/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001222static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001223 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001224{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001225 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001226 struct snd_soc_dapm_widget *w;
1227 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001228 unsigned int value = 0;
1229 unsigned int mask = 0;
1230 unsigned int cur_mask;
1231
1232 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1233 power_list)->reg;
1234
1235 list_for_each_entry(w, pending, power_list) {
1236 cur_mask = 1 << w->shift;
1237 BUG_ON(reg != w->reg);
1238
1239 if (w->invert)
1240 power = !w->power;
1241 else
1242 power = w->power;
1243
1244 mask |= cur_mask;
1245 if (power)
1246 value |= cur_mask;
1247
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001248 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001249 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1250 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001251
Mark Brown68f89ad2010-11-03 23:51:49 -04001252 /* Check for events */
1253 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1254 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001255 }
1256
Mark Brown81628102009-06-07 13:21:24 +01001257 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001258 /* Any widget will do, they should all be updating the
1259 * same register.
1260 */
1261 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1262 power_list);
1263
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001264 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001265 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001266 value, mask, reg, card->pop_time);
1267 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001268 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001269 }
1270
1271 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001272 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1273 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001274 }
Mark Brown42aa3412009-03-01 19:21:10 +00001275}
1276
Mark Brownb22ead22009-06-07 12:51:26 +01001277/* Apply a DAPM power sequence.
1278 *
1279 * We walk over a pre-sorted list of widgets to apply power to. In
1280 * order to minimise the number of writes to the device required
1281 * multiple widgets will be updated in a single write where possible.
1282 * Currently anything that requires more than a single write is not
1283 * handled.
1284 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001285static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001286 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001287{
1288 struct snd_soc_dapm_widget *w, *n;
1289 LIST_HEAD(pending);
1290 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001291 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001292 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001293 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001294 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001295 int *sort;
1296
1297 if (power_up)
1298 sort = dapm_up_seq;
1299 else
1300 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001301
Mark Brownb22ead22009-06-07 12:51:26 +01001302 list_for_each_entry_safe(w, n, list, power_list) {
1303 ret = 0;
1304
1305 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001306 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001307 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001308 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001309 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001310
Mark Brown474b62d2011-01-18 16:14:44 +00001311 if (cur_dapm && cur_dapm->seq_notifier) {
1312 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1313 if (sort[i] == cur_sort)
1314 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001315 i,
1316 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001317 }
1318
Mark Brownb22ead22009-06-07 12:51:26 +01001319 INIT_LIST_HEAD(&pending);
1320 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001321 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001322 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001323 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001324 }
1325
Mark Brown163cac02009-06-07 10:12:52 +01001326 switch (w->id) {
1327 case snd_soc_dapm_pre:
1328 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001329 list_for_each_entry_safe_continue(w, n, list,
1330 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001331
Mark Brownb22ead22009-06-07 12:51:26 +01001332 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001333 ret = w->event(w,
1334 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001335 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001336 ret = w->event(w,
1337 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001338 break;
1339
1340 case snd_soc_dapm_post:
1341 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001342 list_for_each_entry_safe_continue(w, n, list,
1343 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001344
Mark Brownb22ead22009-06-07 12:51:26 +01001345 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001346 ret = w->event(w,
1347 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001348 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001349 ret = w->event(w,
1350 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001351 break;
1352
Mark Brown163cac02009-06-07 10:12:52 +01001353 default:
Mark Brown81628102009-06-07 13:21:24 +01001354 /* Queue it up for application */
1355 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001356 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001357 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001358 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001359 list_move(&w->power_list, &pending);
1360 break;
Mark Brown163cac02009-06-07 10:12:52 +01001361 }
Mark Brownb22ead22009-06-07 12:51:26 +01001362
1363 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001364 dev_err(w->dapm->dev,
1365 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001366 }
Mark Brownb22ead22009-06-07 12:51:26 +01001367
1368 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001369 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001370
1371 if (cur_dapm && cur_dapm->seq_notifier) {
1372 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1373 if (sort[i] == cur_sort)
1374 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001375 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001376 }
Mark Brown163cac02009-06-07 10:12:52 +01001377}
1378
Mark Brown97404f22010-12-14 16:13:57 +00001379static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1380{
1381 struct snd_soc_dapm_update *update = dapm->update;
1382 struct snd_soc_dapm_widget *w;
1383 int ret;
1384
1385 if (!update)
1386 return;
1387
1388 w = update->widget;
1389
1390 if (w->event &&
1391 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1392 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1393 if (ret != 0)
1394 pr_err("%s DAPM pre-event failed: %d\n",
1395 w->name, ret);
1396 }
1397
Liam Girdwood49575fb52012-03-06 18:16:19 +00001398 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001399 update->val);
1400 if (ret < 0)
1401 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1402
1403 if (w->event &&
1404 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1405 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1406 if (ret != 0)
1407 pr_err("%s DAPM post-event failed: %d\n",
1408 w->name, ret);
1409 }
1410}
1411
Mark Brown9d0624a2011-02-18 11:49:43 -08001412/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1413 * they're changing state.
1414 */
1415static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1416{
1417 struct snd_soc_dapm_context *d = data;
1418 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001419
Mark Brown56fba412011-06-04 11:25:10 +01001420 /* If we're off and we're not supposed to be go into STANDBY */
1421 if (d->bias_level == SND_SOC_BIAS_OFF &&
1422 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001423 if (d->dev)
1424 pm_runtime_get_sync(d->dev);
1425
Mark Brown9d0624a2011-02-18 11:49:43 -08001426 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1427 if (ret != 0)
1428 dev_err(d->dev,
1429 "Failed to turn on bias: %d\n", ret);
1430 }
1431
Mark Brown56fba412011-06-04 11:25:10 +01001432 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1433 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001434 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1435 if (ret != 0)
1436 dev_err(d->dev,
1437 "Failed to prepare bias: %d\n", ret);
1438 }
1439}
1440
1441/* Async callback run prior to DAPM sequences - brings to their final
1442 * state.
1443 */
1444static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1445{
1446 struct snd_soc_dapm_context *d = data;
1447 int ret;
1448
1449 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001450 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1451 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1452 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001453 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1454 if (ret != 0)
1455 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1456 ret);
1457 }
1458
1459 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001460 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1461 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001462 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1463 if (ret != 0)
1464 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001465
1466 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001467 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001468 }
1469
1470 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001471 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1472 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001473 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1474 if (ret != 0)
1475 dev_err(d->dev, "Failed to apply active bias: %d\n",
1476 ret);
1477 }
1478}
Mark Brown97404f22010-12-14 16:13:57 +00001479
Mark Brownfe4fda52011-10-03 22:36:57 +01001480static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1481 bool power, bool connect)
1482{
1483 /* If a connection is being made or broken then that update
1484 * will have marked the peer dirty, otherwise the widgets are
1485 * not connected and this update has no impact. */
1486 if (!connect)
1487 return;
1488
1489 /* If the peer is already in the state we're moving to then we
1490 * won't have an impact on it. */
1491 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001492 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001493}
1494
Mark Brown05623c42011-09-28 17:02:31 +01001495static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1496 struct list_head *up_list,
1497 struct list_head *down_list)
1498{
Mark Browndb432b42011-10-03 21:06:40 +01001499 struct snd_soc_dapm_path *path;
1500
Mark Brown05623c42011-09-28 17:02:31 +01001501 if (w->power == power)
1502 return;
1503
1504 trace_snd_soc_dapm_widget_power(w, power);
1505
Mark Browndb432b42011-10-03 21:06:40 +01001506 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001507 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001508 */
1509 list_for_each_entry(path, &w->sources, list_sink) {
1510 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001511 dapm_widget_set_peer_power(path->source, power,
1512 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001513 }
1514 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001515 switch (w->id) {
1516 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001517 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001518 case snd_soc_dapm_clock_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001519 /* Supplies can't affect their outputs, only their inputs */
1520 break;
1521 default:
1522 list_for_each_entry(path, &w->sinks, list_source) {
1523 if (path->sink) {
1524 dapm_widget_set_peer_power(path->sink, power,
1525 path->connect);
1526 }
Mark Browndb432b42011-10-03 21:06:40 +01001527 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001528 break;
Mark Browndb432b42011-10-03 21:06:40 +01001529 }
1530
Mark Brown05623c42011-09-28 17:02:31 +01001531 if (power)
1532 dapm_seq_insert(w, up_list, true);
1533 else
1534 dapm_seq_insert(w, down_list, false);
1535
1536 w->power = power;
1537}
1538
Mark Brown7c81beb2011-09-20 22:22:32 +01001539static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1540 struct list_head *up_list,
1541 struct list_head *down_list)
1542{
Mark Brown7c81beb2011-09-20 22:22:32 +01001543 int power;
1544
1545 switch (w->id) {
1546 case snd_soc_dapm_pre:
1547 dapm_seq_insert(w, down_list, false);
1548 break;
1549 case snd_soc_dapm_post:
1550 dapm_seq_insert(w, up_list, true);
1551 break;
1552
1553 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001554 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001555
Mark Brown05623c42011-09-28 17:02:31 +01001556 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001557 break;
1558 }
1559}
1560
Mark Brown42aa3412009-03-01 19:21:10 +00001561/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001562 * Scan each dapm widget for complete audio path.
1563 * A complete path is a route that has valid endpoints i.e.:-
1564 *
1565 * o DAC to output pin.
1566 * o Input Pin to ADC.
1567 * o Input pin to Output pin (bypass, sidetone)
1568 * o DAC to ADC (loopback).
1569 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001570static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001571{
Mark Brown12ea2c72011-03-02 18:17:32 +00001572 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001573 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001574 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001575 LIST_HEAD(up_list);
1576 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001577 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001578 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001579
Mark Brown84e90932010-11-04 00:07:02 -04001580 trace_snd_soc_dapm_start(card);
1581
Mark Brown56fba412011-06-04 11:25:10 +01001582 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001583 if (d->idle_bias_off)
1584 d->target_bias_level = SND_SOC_BIAS_OFF;
1585 else
1586 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001587 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001588
Liam Girdwood6c120e12012-02-15 15:15:34 +00001589 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001590
Mark Brown6d3ddc82009-05-16 17:47:29 +01001591 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001592 * lists indicating if they should be powered up or down. We
1593 * only check widgets that have been flagged as dirty but note
1594 * that new widgets may be added to the dirty list while we
1595 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001596 */
Mark Browndb432b42011-10-03 21:06:40 +01001597 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001598 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001599 }
1600
Mark Brownf9de6d72011-09-28 17:19:47 +01001601 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001602 switch (w->id) {
1603 case snd_soc_dapm_pre:
1604 case snd_soc_dapm_post:
1605 /* These widgets always need to be powered */
1606 break;
1607 default:
1608 list_del_init(&w->dirty);
1609 break;
1610 }
Mark Browndb432b42011-10-03 21:06:40 +01001611
Mark Brownf9de6d72011-09-28 17:19:47 +01001612 if (w->power) {
1613 d = w->dapm;
1614
1615 /* Supplies and micbiases only bring the
1616 * context up to STANDBY as unless something
1617 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001618 * generally don't require full power. Signal
1619 * generators are virtual pins and have no
1620 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001621 */
1622 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001623 case snd_soc_dapm_siggen:
1624 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001625 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001626 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001627 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001628 case snd_soc_dapm_micbias:
1629 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1630 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1631 break;
1632 default:
1633 d->target_bias_level = SND_SOC_BIAS_ON;
1634 break;
1635 }
1636 }
1637
1638 }
1639
Mark Brown85a843c2011-09-21 21:29:47 +01001640 /* Force all contexts in the card to the same bias state if
1641 * they're not ground referenced.
1642 */
Mark Brown56fba412011-06-04 11:25:10 +01001643 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001644 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001645 if (d->target_bias_level > bias)
1646 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001647 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001648 if (!d->idle_bias_off)
1649 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001650
Mark Brownde02d072011-09-20 21:43:24 +01001651 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001652
Mark Brown9d0624a2011-02-18 11:49:43 -08001653 /* Run all the bias changes in parallel */
1654 list_for_each_entry(d, &dapm->card->dapm_list, list)
1655 async_schedule_domain(dapm_pre_sequence_async, d,
1656 &async_domain);
1657 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001658
Mark Brown6d3ddc82009-05-16 17:47:29 +01001659 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001660 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001661
Mark Brown97404f22010-12-14 16:13:57 +00001662 dapm_widget_update(dapm);
1663
Mark Brown6d3ddc82009-05-16 17:47:29 +01001664 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001665 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001666
Mark Brown9d0624a2011-02-18 11:49:43 -08001667 /* Run all the bias changes in parallel */
1668 list_for_each_entry(d, &dapm->card->dapm_list, list)
1669 async_schedule_domain(dapm_post_sequence_async, d,
1670 &async_domain);
1671 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001672
Liam Girdwood8078d872012-02-15 15:15:35 +00001673 /* do we need to notify any clients that DAPM event is complete */
1674 list_for_each_entry(d, &card->dapm_list, list) {
1675 if (d->stream_event)
1676 d->stream_event(d, event);
1677 }
1678
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001679 pop_dbg(dapm->dev, card->pop_time,
1680 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001681 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001682
Mark Brown84e90932010-11-04 00:07:02 -04001683 trace_snd_soc_dapm_done(card);
1684
Mark Brown42aa3412009-03-01 19:21:10 +00001685 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001686}
1687
Mark Brown79fb9382009-08-21 16:38:13 +01001688#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001689static ssize_t dapm_widget_power_read_file(struct file *file,
1690 char __user *user_buf,
1691 size_t count, loff_t *ppos)
1692{
1693 struct snd_soc_dapm_widget *w = file->private_data;
1694 char *buf;
1695 int in, out;
1696 ssize_t ret;
1697 struct snd_soc_dapm_path *p = NULL;
1698
1699 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1700 if (!buf)
1701 return -ENOMEM;
1702
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001703 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001704 dapm_clear_walk(w->dapm);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001705 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001706 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001707
Mark Brownf13ebad2012-03-03 18:01:01 +00001708 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1709 w->name, w->power ? "On" : "Off",
1710 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001711
Mark Brownd033c362009-12-04 15:25:56 +00001712 if (w->reg >= 0)
1713 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1714 " - R%d(0x%x) bit %d",
1715 w->reg, w->reg, w->shift);
1716
1717 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1718
Mark Brown3eef08b2009-09-14 16:49:00 +01001719 if (w->sname)
1720 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1721 w->sname,
1722 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001723
1724 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001725 if (p->connected && !p->connected(w, p->sink))
1726 continue;
1727
Mark Brown79fb9382009-08-21 16:38:13 +01001728 if (p->connect)
1729 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001730 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001731 p->name ? p->name : "static",
1732 p->source->name);
1733 }
1734 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001735 if (p->connected && !p->connected(w, p->sink))
1736 continue;
1737
Mark Brown79fb9382009-08-21 16:38:13 +01001738 if (p->connect)
1739 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001740 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001741 p->name ? p->name : "static",
1742 p->sink->name);
1743 }
1744
1745 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1746
1747 kfree(buf);
1748 return ret;
1749}
1750
1751static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001752 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001753 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001754 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001755};
1756
Mark Brownef49e4f2011-04-04 20:48:13 +09001757static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1758 size_t count, loff_t *ppos)
1759{
1760 struct snd_soc_dapm_context *dapm = file->private_data;
1761 char *level;
1762
1763 switch (dapm->bias_level) {
1764 case SND_SOC_BIAS_ON:
1765 level = "On\n";
1766 break;
1767 case SND_SOC_BIAS_PREPARE:
1768 level = "Prepare\n";
1769 break;
1770 case SND_SOC_BIAS_STANDBY:
1771 level = "Standby\n";
1772 break;
1773 case SND_SOC_BIAS_OFF:
1774 level = "Off\n";
1775 break;
1776 default:
1777 BUG();
1778 level = "Unknown\n";
1779 break;
1780 }
1781
1782 return simple_read_from_buffer(user_buf, count, ppos, level,
1783 strlen(level));
1784}
1785
1786static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001787 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001788 .read = dapm_bias_read_file,
1789 .llseek = default_llseek,
1790};
1791
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001792void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1793 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001794{
Mark Brown79fb9382009-08-21 16:38:13 +01001795 struct dentry *d;
1796
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001797 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1798
1799 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001800 dev_warn(dapm->dev,
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001801 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001802 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001803 }
Mark Brown79fb9382009-08-21 16:38:13 +01001804
Mark Brownef49e4f2011-04-04 20:48:13 +09001805 d = debugfs_create_file("bias_level", 0444,
1806 dapm->debugfs_dapm, dapm,
1807 &dapm_bias_fops);
1808 if (!d)
1809 dev_warn(dapm->dev,
1810 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001811}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001812
1813static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1814{
1815 struct snd_soc_dapm_context *dapm = w->dapm;
1816 struct dentry *d;
1817
1818 if (!dapm->debugfs_dapm || !w->name)
1819 return;
1820
1821 d = debugfs_create_file(w->name, 0444,
1822 dapm->debugfs_dapm, w,
1823 &dapm_widget_power_fops);
1824 if (!d)
1825 dev_warn(w->dapm->dev,
1826 "ASoC: Failed to create %s debugfs file\n",
1827 w->name);
1828}
1829
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001830static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1831{
1832 debugfs_remove_recursive(dapm->debugfs_dapm);
1833}
1834
Mark Brown79fb9382009-08-21 16:38:13 +01001835#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001836void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1837 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001838{
1839}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001840
1841static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1842{
1843}
1844
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001845static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1846{
1847}
1848
Mark Brown79fb9382009-08-21 16:38:13 +01001849#endif
1850
Richard Purdie2b97eab2006-10-06 18:32:18 +02001851/* test and update the power status of a mux widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001852static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001853 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001854{
1855 struct snd_soc_dapm_path *path;
1856 int found = 0;
1857
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001858 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001859 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001860 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001861 return -ENODEV;
1862
Richard Purdie2b97eab2006-10-06 18:32:18 +02001863 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001864 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001865 if (path->kcontrol != kcontrol)
1866 continue;
1867
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001868 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001869 continue;
1870
1871 found = 1;
1872 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001873 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001874 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001875 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001876 } else {
1877 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001878 dapm_mark_dirty(path->source,
1879 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001880 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001881 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001882 }
1883
Mark Browndb432b42011-10-03 21:06:40 +01001884 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001885 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001886 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001887 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001888
Liam Girdwood618dae12012-04-25 12:12:51 +01001889 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001890}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001891
1892int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1893 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1894{
1895 struct snd_soc_card *card = widget->dapm->card;
1896 int ret;
1897
Liam Girdwood3cd04342012-03-09 12:02:08 +00001898 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001899 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1900 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001901 if (ret > 0)
1902 soc_dpcm_runtime_update(widget);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001903 return ret;
1904}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001905EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001906
Milan plzik1b075e32008-01-10 14:39:46 +01001907/* test and update the power status of a mixer or switch widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001908static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001909 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001910{
1911 struct snd_soc_dapm_path *path;
1912 int found = 0;
1913
Milan plzik1b075e32008-01-10 14:39:46 +01001914 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001915 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001916 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001917 return -ENODEV;
1918
Richard Purdie2b97eab2006-10-06 18:32:18 +02001919 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001920 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001921 if (path->kcontrol != kcontrol)
1922 continue;
1923
1924 /* found, now check type */
1925 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001926 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001927 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001928 }
1929
Mark Browndb432b42011-10-03 21:06:40 +01001930 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001931 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001932 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001933 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001934
Liam Girdwood618dae12012-04-25 12:12:51 +01001935 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001936}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001937
1938int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1939 struct snd_kcontrol *kcontrol, int connect)
1940{
1941 struct snd_soc_card *card = widget->dapm->card;
1942 int ret;
1943
Liam Girdwood3cd04342012-03-09 12:02:08 +00001944 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001945 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
1946 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001947 if (ret > 0)
1948 soc_dpcm_runtime_update(widget);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001949 return ret;
1950}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001951EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001952
1953/* show dapm widget status in sys fs */
1954static ssize_t dapm_widget_show(struct device *dev,
1955 struct device_attribute *attr, char *buf)
1956{
Mark Brown36ae1a92012-01-06 17:12:45 -08001957 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001958 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001959 struct snd_soc_dapm_widget *w;
1960 int count = 0;
1961 char *state = "not set";
1962
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001963 list_for_each_entry(w, &codec->card->widgets, list) {
1964 if (w->dapm != &codec->dapm)
1965 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001966
1967 /* only display widgets that burnm power */
1968 switch (w->id) {
1969 case snd_soc_dapm_hp:
1970 case snd_soc_dapm_mic:
1971 case snd_soc_dapm_spk:
1972 case snd_soc_dapm_line:
1973 case snd_soc_dapm_micbias:
1974 case snd_soc_dapm_dac:
1975 case snd_soc_dapm_adc:
1976 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001977 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001978 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001979 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001980 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001981 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001982 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001983 if (w->name)
1984 count += sprintf(buf + count, "%s: %s\n",
1985 w->name, w->power ? "On":"Off");
1986 break;
1987 default:
1988 break;
1989 }
1990 }
1991
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001992 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001993 case SND_SOC_BIAS_ON:
1994 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001995 break;
Mark Brown0be98982008-05-19 12:31:28 +02001996 case SND_SOC_BIAS_PREPARE:
1997 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001998 break;
Mark Brown0be98982008-05-19 12:31:28 +02001999 case SND_SOC_BIAS_STANDBY:
2000 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002001 break;
Mark Brown0be98982008-05-19 12:31:28 +02002002 case SND_SOC_BIAS_OFF:
2003 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002004 break;
2005 }
2006 count += sprintf(buf + count, "PM State: %s\n", state);
2007
2008 return count;
2009}
2010
2011static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2012
2013int snd_soc_dapm_sys_add(struct device *dev)
2014{
Troy Kisky12ef1932008-10-13 17:42:14 -07002015 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002016}
2017
2018static void snd_soc_dapm_sys_remove(struct device *dev)
2019{
Mark Brownaef90842009-05-16 17:53:16 +01002020 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002021}
2022
2023/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002024static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002025{
2026 struct snd_soc_dapm_widget *w, *next_w;
2027 struct snd_soc_dapm_path *p, *next_p;
2028
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002029 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2030 if (w->dapm != dapm)
2031 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002032 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002033 /*
2034 * remove source and sink paths associated to this widget.
2035 * While removing the path, remove reference to it from both
2036 * source and sink widgets so that path is removed only once.
2037 */
2038 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
2039 list_del(&p->list_sink);
2040 list_del(&p->list_source);
2041 list_del(&p->list);
2042 kfree(p->long_name);
2043 kfree(p);
2044 }
2045 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2046 list_del(&p->list_sink);
2047 list_del(&p->list_source);
2048 list_del(&p->list);
2049 kfree(p->long_name);
2050 kfree(p);
2051 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002052 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002053 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002054 kfree(w);
2055 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002056}
2057
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002058static struct snd_soc_dapm_widget *dapm_find_widget(
2059 struct snd_soc_dapm_context *dapm, const char *pin,
2060 bool search_other_contexts)
2061{
2062 struct snd_soc_dapm_widget *w;
2063 struct snd_soc_dapm_widget *fallback = NULL;
2064
2065 list_for_each_entry(w, &dapm->card->widgets, list) {
2066 if (!strcmp(w->name, pin)) {
2067 if (w->dapm == dapm)
2068 return w;
2069 else
2070 fallback = w;
2071 }
2072 }
2073
2074 if (search_other_contexts)
2075 return fallback;
2076
2077 return NULL;
2078}
2079
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002080static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002081 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002082{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002083 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002084
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002085 if (!w) {
2086 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2087 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002088 }
2089
Mark Brown1a8b2d92012-02-16 11:50:07 -08002090 if (w->connected != status)
2091 dapm_mark_dirty(w, "pin configuration");
2092
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002093 w->connected = status;
2094 if (status == 0)
2095 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002096
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002097 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002098}
2099
Richard Purdie2b97eab2006-10-06 18:32:18 +02002100/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002101 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002102 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002103 *
2104 * Walks all dapm audio paths and powers widgets according to their
2105 * stream or path usage.
2106 *
2107 * Returns 0 for success.
2108 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002109int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002110{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002111 int ret;
2112
Mark Brown4f4c0072011-10-07 14:29:19 +01002113 /*
2114 * Suppress early reports (eg, jacks syncing their state) to avoid
2115 * silly DAPM runs during card startup.
2116 */
2117 if (!dapm->card || !dapm->card->instantiated)
2118 return 0;
2119
Liam Girdwood3cd04342012-03-09 12:02:08 +00002120 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002121 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2122 mutex_unlock(&dapm->card->dapm_mutex);
2123 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002124}
Liam Girdwooda5302182008-07-07 13:35:17 +01002125EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002126
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002127static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002128 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002129{
2130 struct snd_soc_dapm_path *path;
2131 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002132 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002133 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002134 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002135 const char *source;
2136 char prefixed_sink[80];
2137 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002138 int ret = 0;
2139
Mark Brown88e8b9a2011-03-02 18:18:24 +00002140 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002141 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2142 dapm->codec->name_prefix, route->sink);
2143 sink = prefixed_sink;
2144 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2145 dapm->codec->name_prefix, route->source);
2146 source = prefixed_source;
2147 } else {
2148 sink = route->sink;
2149 source = route->source;
2150 }
2151
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002152 /*
2153 * find src and dest widgets over all widgets but favor a widget from
2154 * current DAPM context
2155 */
2156 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002157 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002158 wtsink = w;
2159 if (w->dapm == dapm)
2160 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002161 continue;
2162 }
2163 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002164 wtsource = w;
2165 if (w->dapm == dapm)
2166 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002167 }
2168 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002169 /* use widget from another DAPM context if not found from this */
2170 if (!wsink)
2171 wsink = wtsink;
2172 if (!wsource)
2173 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002174
2175 if (wsource == NULL || wsink == NULL)
2176 return -ENODEV;
2177
2178 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2179 if (!path)
2180 return -ENOMEM;
2181
2182 path->source = wsource;
2183 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002184 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002185 INIT_LIST_HEAD(&path->list);
2186 INIT_LIST_HEAD(&path->list_source);
2187 INIT_LIST_HEAD(&path->list_sink);
2188
2189 /* check for external widgets */
2190 if (wsink->id == snd_soc_dapm_input) {
2191 if (wsource->id == snd_soc_dapm_micbias ||
2192 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002193 wsource->id == snd_soc_dapm_line ||
2194 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002195 wsink->ext = 1;
2196 }
2197 if (wsource->id == snd_soc_dapm_output) {
2198 if (wsink->id == snd_soc_dapm_spk ||
2199 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002200 wsink->id == snd_soc_dapm_line ||
2201 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002202 wsource->ext = 1;
2203 }
2204
2205 /* connect static paths */
2206 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002207 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002208 list_add(&path->list_sink, &wsink->sources);
2209 list_add(&path->list_source, &wsource->sinks);
2210 path->connect = 1;
2211 return 0;
2212 }
2213
2214 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002215 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002216 case snd_soc_dapm_adc:
2217 case snd_soc_dapm_dac:
2218 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002219 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002220 case snd_soc_dapm_input:
2221 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002222 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002223 case snd_soc_dapm_micbias:
2224 case snd_soc_dapm_vmid:
2225 case snd_soc_dapm_pre:
2226 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002227 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002228 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002229 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002230 case snd_soc_dapm_aif_in:
2231 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -08002232 case snd_soc_dapm_dai:
Mark Brownc74184e2012-04-04 22:12:09 +01002233 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002234 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002235 list_add(&path->list_sink, &wsink->sources);
2236 list_add(&path->list_source, &wsource->sinks);
2237 path->connect = 1;
2238 return 0;
2239 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002240 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002241 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002242 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002243 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244 if (ret != 0)
2245 goto err;
2246 break;
2247 case snd_soc_dapm_switch:
2248 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002249 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002250 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002251 if (ret != 0)
2252 goto err;
2253 break;
2254 case snd_soc_dapm_hp:
2255 case snd_soc_dapm_mic:
2256 case snd_soc_dapm_line:
2257 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002258 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002259 list_add(&path->list_sink, &wsink->sources);
2260 list_add(&path->list_source, &wsource->sinks);
2261 path->connect = 0;
2262 return 0;
2263 }
Mark Brownfabd0382012-07-05 17:20:06 +01002264
2265 dapm_mark_dirty(wsource, "Route added");
2266 dapm_mark_dirty(wsink, "Route added");
2267
Richard Purdie2b97eab2006-10-06 18:32:18 +02002268 return 0;
2269
2270err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002271 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2272 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002273 kfree(path);
2274 return ret;
2275}
Mark Brown105f1c22008-05-13 14:52:19 +02002276
Mark Brownefcc3c62012-07-05 17:24:19 +01002277static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2278 const struct snd_soc_dapm_route *route)
2279{
2280 struct snd_soc_dapm_path *path, *p;
2281 const char *sink;
2282 const char *source;
2283 char prefixed_sink[80];
2284 char prefixed_source[80];
2285
2286 if (route->control) {
2287 dev_err(dapm->dev,
2288 "Removal of routes with controls not supported\n");
2289 return -EINVAL;
2290 }
2291
2292 if (dapm->codec && dapm->codec->name_prefix) {
2293 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2294 dapm->codec->name_prefix, route->sink);
2295 sink = prefixed_sink;
2296 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2297 dapm->codec->name_prefix, route->source);
2298 source = prefixed_source;
2299 } else {
2300 sink = route->sink;
2301 source = route->source;
2302 }
2303
2304 path = NULL;
2305 list_for_each_entry(p, &dapm->card->paths, list) {
2306 if (strcmp(p->source->name, source) != 0)
2307 continue;
2308 if (strcmp(p->sink->name, sink) != 0)
2309 continue;
2310 path = p;
2311 break;
2312 }
2313
2314 if (path) {
2315 dapm_mark_dirty(path->source, "Route removed");
2316 dapm_mark_dirty(path->sink, "Route removed");
2317
2318 list_del(&path->list);
2319 list_del(&path->list_sink);
2320 list_del(&path->list_source);
2321 kfree(path);
2322 } else {
2323 dev_warn(dapm->dev, "Route %s->%s does not exist\n",
2324 source, sink);
2325 }
2326
2327 return 0;
2328}
2329
Mark Brown105f1c22008-05-13 14:52:19 +02002330/**
Mark Brown105f1c22008-05-13 14:52:19 +02002331 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002332 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002333 * @route: audio routes
2334 * @num: number of routes
2335 *
2336 * Connects 2 dapm widgets together via a named audio path. The sink is
2337 * the widget receiving the audio signal, whilst the source is the sender
2338 * of the audio signal.
2339 *
2340 * Returns 0 for success else error. On error all resources can be freed
2341 * with a call to snd_soc_card_free().
2342 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002343int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002344 const struct snd_soc_dapm_route *route, int num)
2345{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002346 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002347
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002348 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002349 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002350 r = snd_soc_dapm_add_route(dapm, route);
2351 if (r < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002352 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2353 route->source, route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002354 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002355 }
2356 route++;
2357 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002358 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002359
Dan Carpenter60884c22012-04-13 22:25:43 +03002360 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002361}
2362EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2363
Mark Brownefcc3c62012-07-05 17:24:19 +01002364/**
2365 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2366 * @dapm: DAPM context
2367 * @route: audio routes
2368 * @num: number of routes
2369 *
2370 * Removes routes from the DAPM context.
2371 */
2372int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2373 const struct snd_soc_dapm_route *route, int num)
2374{
2375 int i, ret = 0;
2376
2377 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2378 for (i = 0; i < num; i++) {
2379 snd_soc_dapm_del_route(dapm, route);
2380 route++;
2381 }
2382 mutex_unlock(&dapm->card->dapm_mutex);
2383
2384 return ret;
2385}
2386EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2387
Mark Brownbf3a9e12011-06-13 16:42:29 +01002388static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2389 const struct snd_soc_dapm_route *route)
2390{
2391 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2392 route->source,
2393 true);
2394 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2395 route->sink,
2396 true);
2397 struct snd_soc_dapm_path *path;
2398 int count = 0;
2399
2400 if (!source) {
2401 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2402 route->source);
2403 return -ENODEV;
2404 }
2405
2406 if (!sink) {
2407 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2408 route->sink);
2409 return -ENODEV;
2410 }
2411
2412 if (route->control || route->connected)
2413 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2414 route->source, route->sink);
2415
2416 list_for_each_entry(path, &source->sinks, list_source) {
2417 if (path->sink == sink) {
2418 path->weak = 1;
2419 count++;
2420 }
2421 }
2422
2423 if (count == 0)
2424 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2425 route->source, route->sink);
2426 if (count > 1)
2427 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2428 count, route->source, route->sink);
2429
2430 return 0;
2431}
2432
2433/**
2434 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2435 * @dapm: DAPM context
2436 * @route: audio routes
2437 * @num: number of routes
2438 *
2439 * Mark existing routes matching those specified in the passed array
2440 * as being weak, meaning that they are ignored for the purpose of
2441 * power decisions. The main intended use case is for sidetone paths
2442 * which couple audio between other independent paths if they are both
2443 * active in order to make the combination work better at the user
2444 * level but which aren't intended to be "used".
2445 *
2446 * Note that CODEC drivers should not use this as sidetone type paths
2447 * can frequently also be used as bypass paths.
2448 */
2449int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2450 const struct snd_soc_dapm_route *route, int num)
2451{
2452 int i, err;
2453 int ret = 0;
2454
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002455 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002456 for (i = 0; i < num; i++) {
2457 err = snd_soc_dapm_weak_route(dapm, route);
2458 if (err)
2459 ret = err;
2460 route++;
2461 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002462 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002463
2464 return ret;
2465}
2466EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2467
Mark Brown105f1c22008-05-13 14:52:19 +02002468/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002469 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002470 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002471 *
2472 * Checks the codec for any new dapm widgets and creates them if found.
2473 *
2474 * Returns 0 for success.
2475 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002476int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002477{
2478 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002479 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002480
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002481 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2482
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002483 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002484 {
2485 if (w->new)
2486 continue;
2487
Stephen Warrenfad59882011-04-28 17:37:59 -06002488 if (w->num_kcontrols) {
2489 w->kcontrols = kzalloc(w->num_kcontrols *
2490 sizeof(struct snd_kcontrol *),
2491 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002492 if (!w->kcontrols) {
2493 mutex_unlock(&dapm->card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002494 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002495 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002496 }
2497
Richard Purdie2b97eab2006-10-06 18:32:18 +02002498 switch(w->id) {
2499 case snd_soc_dapm_switch:
2500 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002501 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002502 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002503 break;
2504 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002505 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002506 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002507 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002508 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002509 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002510 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002511 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002512 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002513 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002514 break;
2515 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002516
2517 /* Read the initial power state from the device */
2518 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002519 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002520 val &= 1 << w->shift;
2521 if (w->invert)
2522 val = !val;
2523
2524 if (val)
2525 w->power = 1;
2526 }
2527
Richard Purdie2b97eab2006-10-06 18:32:18 +02002528 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002529
Mark Brown7508b122011-10-05 12:09:12 +01002530 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002531 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002532 }
2533
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002534 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002535 mutex_unlock(&dapm->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002536 return 0;
2537}
2538EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2539
2540/**
2541 * snd_soc_dapm_get_volsw - dapm mixer get callback
2542 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002543 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002544 *
2545 * Callback to get the value of a dapm mixer control.
2546 *
2547 * Returns 0 for success.
2548 */
2549int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2550 struct snd_ctl_elem_value *ucontrol)
2551{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002552 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2553 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002554 struct soc_mixer_control *mc =
2555 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002556 unsigned int reg = mc->reg;
2557 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002558 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002559 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002560 unsigned int invert = mc->invert;
2561
2562 if (snd_soc_volsw_is_stereo(mc))
2563 dev_warn(widget->dapm->dev,
2564 "Control '%s' is stereo, which is not supported\n",
2565 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002566
Richard Purdie2b97eab2006-10-06 18:32:18 +02002567 ucontrol->value.integer.value[0] =
2568 (snd_soc_read(widget->codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002569 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002570 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002571 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002572
2573 return 0;
2574}
2575EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2576
2577/**
2578 * snd_soc_dapm_put_volsw - dapm mixer set callback
2579 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002580 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002581 *
2582 * Callback to set the value of a dapm mixer control.
2583 *
2584 * Returns 0 for success.
2585 */
2586int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2587 struct snd_ctl_elem_value *ucontrol)
2588{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002589 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2590 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2591 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002592 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002593 struct soc_mixer_control *mc =
2594 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002595 unsigned int reg = mc->reg;
2596 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002597 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002598 unsigned int mask = (1 << fls(max)) - 1;
2599 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002600 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002601 int connect, change;
2602 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002603 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002604
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002605 if (snd_soc_volsw_is_stereo(mc))
2606 dev_warn(widget->dapm->dev,
2607 "Control '%s' is stereo, which is not supported\n",
2608 kcontrol->id.name);
2609
Richard Purdie2b97eab2006-10-06 18:32:18 +02002610 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002611 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002612
2613 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002614 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002615 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002616 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002617
Liam Girdwood3cd04342012-03-09 12:02:08 +00002618 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002619
Stephen Warrene9cf7042011-01-27 14:54:05 -07002620 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002621 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002622 for (wi = 0; wi < wlist->num_widgets; wi++) {
2623 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002624
Stephen Warrenfafd2172011-04-28 17:38:00 -06002625 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002626
Stephen Warrenfafd2172011-04-28 17:38:00 -06002627 update.kcontrol = kcontrol;
2628 update.widget = widget;
2629 update.reg = reg;
2630 update.mask = mask;
2631 update.val = val;
2632 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002633
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002634 soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002635
2636 widget->dapm->update = NULL;
2637 }
Mark Brown283375c2009-12-07 18:09:03 +00002638 }
2639
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002640 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002641 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002642}
2643EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2644
2645/**
2646 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2647 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002648 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002649 *
2650 * Callback to get the value of a dapm enumerated double mixer control.
2651 *
2652 * Returns 0 for success.
2653 */
2654int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2655 struct snd_ctl_elem_value *ucontrol)
2656{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002657 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2658 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002659 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002660 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002661
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002662 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002663 ;
2664 val = snd_soc_read(widget->codec, e->reg);
2665 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2666 if (e->shift_l != e->shift_r)
2667 ucontrol->value.enumerated.item[1] =
2668 (val >> e->shift_r) & (bitmask - 1);
2669
2670 return 0;
2671}
2672EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2673
2674/**
2675 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2676 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002677 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002678 *
2679 * Callback to set the value of a dapm enumerated double mixer control.
2680 *
2681 * Returns 0 for success.
2682 */
2683int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2684 struct snd_ctl_elem_value *ucontrol)
2685{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002686 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2687 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2688 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002689 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002690 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002691 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002692 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002693 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002694 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002695
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002696 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002697 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002698 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002699 return -EINVAL;
2700 mux = ucontrol->value.enumerated.item[0];
2701 val = mux << e->shift_l;
2702 mask = (bitmask - 1) << e->shift_l;
2703 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002704 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002705 return -EINVAL;
2706 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2707 mask |= (bitmask - 1) << e->shift_r;
2708 }
2709
Liam Girdwood3cd04342012-03-09 12:02:08 +00002710 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002711
Mark Brown3a655772009-10-05 17:23:30 +01002712 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002713 if (change) {
2714 for (wi = 0; wi < wlist->num_widgets; wi++) {
2715 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002716
Stephen Warrenfafd2172011-04-28 17:38:00 -06002717 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002718
Stephen Warrenfafd2172011-04-28 17:38:00 -06002719 update.kcontrol = kcontrol;
2720 update.widget = widget;
2721 update.reg = e->reg;
2722 update.mask = mask;
2723 update.val = val;
2724 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002725
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002726 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002727
Stephen Warrenfafd2172011-04-28 17:38:00 -06002728 widget->dapm->update = NULL;
2729 }
2730 }
2731
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002732 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002733 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002734}
2735EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2736
2737/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002738 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2739 * @kcontrol: mixer control
2740 * @ucontrol: control element information
2741 *
2742 * Returns 0 for success.
2743 */
2744int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2745 struct snd_ctl_elem_value *ucontrol)
2746{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002747 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2748 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002749
2750 ucontrol->value.enumerated.item[0] = widget->value;
2751
2752 return 0;
2753}
2754EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2755
2756/**
2757 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2758 * @kcontrol: mixer control
2759 * @ucontrol: control element information
2760 *
2761 * Returns 0 for success.
2762 */
2763int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2764 struct snd_ctl_elem_value *ucontrol)
2765{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002766 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2767 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2768 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002769 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002770 struct soc_enum *e =
2771 (struct soc_enum *)kcontrol->private_value;
2772 int change;
2773 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002774 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002775
2776 if (ucontrol->value.enumerated.item[0] >= e->max)
2777 return -EINVAL;
2778
Liam Girdwood3cd04342012-03-09 12:02:08 +00002779 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002780
2781 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002782 if (change) {
2783 for (wi = 0; wi < wlist->num_widgets; wi++) {
2784 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002785
Stephen Warrenfafd2172011-04-28 17:38:00 -06002786 widget->value = ucontrol->value.enumerated.item[0];
2787
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002788 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002789 }
2790 }
2791
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002792 mutex_unlock(&card->dapm_mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002793 return ret;
2794}
2795EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2796
2797/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002798 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2799 * callback
2800 * @kcontrol: mixer control
2801 * @ucontrol: control element information
2802 *
2803 * Callback to get the value of a dapm semi enumerated double mixer control.
2804 *
2805 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2806 * used for handling bitfield coded enumeration for example.
2807 *
2808 * Returns 0 for success.
2809 */
2810int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2811 struct snd_ctl_elem_value *ucontrol)
2812{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002813 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2814 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002815 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002816 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002817
2818 reg_val = snd_soc_read(widget->codec, e->reg);
2819 val = (reg_val >> e->shift_l) & e->mask;
2820 for (mux = 0; mux < e->max; mux++) {
2821 if (val == e->values[mux])
2822 break;
2823 }
2824 ucontrol->value.enumerated.item[0] = mux;
2825 if (e->shift_l != e->shift_r) {
2826 val = (reg_val >> e->shift_r) & e->mask;
2827 for (mux = 0; mux < e->max; mux++) {
2828 if (val == e->values[mux])
2829 break;
2830 }
2831 ucontrol->value.enumerated.item[1] = mux;
2832 }
2833
2834 return 0;
2835}
2836EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2837
2838/**
2839 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2840 * callback
2841 * @kcontrol: mixer control
2842 * @ucontrol: control element information
2843 *
2844 * Callback to set the value of a dapm semi enumerated double mixer control.
2845 *
2846 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2847 * used for handling bitfield coded enumeration for example.
2848 *
2849 * Returns 0 for success.
2850 */
2851int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2852 struct snd_ctl_elem_value *ucontrol)
2853{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002854 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2855 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2856 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002857 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002858 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002859 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002860 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002861 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002862 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002863
2864 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2865 return -EINVAL;
2866 mux = ucontrol->value.enumerated.item[0];
2867 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2868 mask = e->mask << e->shift_l;
2869 if (e->shift_l != e->shift_r) {
2870 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2871 return -EINVAL;
2872 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2873 mask |= e->mask << e->shift_r;
2874 }
2875
Liam Girdwood3cd04342012-03-09 12:02:08 +00002876 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002877
Mark Brown3a655772009-10-05 17:23:30 +01002878 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002879 if (change) {
2880 for (wi = 0; wi < wlist->num_widgets; wi++) {
2881 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002882
Stephen Warrenfafd2172011-04-28 17:38:00 -06002883 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002884
Stephen Warrenfafd2172011-04-28 17:38:00 -06002885 update.kcontrol = kcontrol;
2886 update.widget = widget;
2887 update.reg = e->reg;
2888 update.mask = mask;
2889 update.val = val;
2890 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002891
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002892 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002893
Stephen Warrenfafd2172011-04-28 17:38:00 -06002894 widget->dapm->update = NULL;
2895 }
2896 }
2897
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002898 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002899 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002900}
2901EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2902
2903/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002904 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2905 *
2906 * @kcontrol: mixer control
2907 * @uinfo: control element information
2908 *
2909 * Callback to provide information about a pin switch control.
2910 */
2911int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2912 struct snd_ctl_elem_info *uinfo)
2913{
2914 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2915 uinfo->count = 1;
2916 uinfo->value.integer.min = 0;
2917 uinfo->value.integer.max = 1;
2918
2919 return 0;
2920}
2921EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2922
2923/**
2924 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2925 *
2926 * @kcontrol: mixer control
2927 * @ucontrol: Value
2928 */
2929int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2930 struct snd_ctl_elem_value *ucontrol)
2931{
Mark Brown48a8c392012-02-14 17:11:15 -08002932 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002933 const char *pin = (const char *)kcontrol->private_value;
2934
Liam Girdwood3cd04342012-03-09 12:02:08 +00002935 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002936
2937 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002938 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002939
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002940 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002941
2942 return 0;
2943}
2944EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2945
2946/**
2947 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2948 *
2949 * @kcontrol: mixer control
2950 * @ucontrol: Value
2951 */
2952int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2953 struct snd_ctl_elem_value *ucontrol)
2954{
Mark Brown48a8c392012-02-14 17:11:15 -08002955 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002956 const char *pin = (const char *)kcontrol->private_value;
2957
Liam Girdwood3cd04342012-03-09 12:02:08 +00002958 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002959
2960 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002961 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002962 else
Mark Brown48a8c392012-02-14 17:11:15 -08002963 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002964
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002965 mutex_unlock(&card->dapm_mutex);
2966
Mark Brown48a8c392012-02-14 17:11:15 -08002967 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002968 return 0;
2969}
2970EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2971
Mark Brown5ba06fc2012-02-16 11:07:13 -08002972static struct snd_soc_dapm_widget *
2973snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2974 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002975{
2976 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002977 size_t name_len;
Mark Brown62ea8742012-01-21 21:14:48 +00002978 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002979
2980 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08002981 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002982
Mark Brown62ea8742012-01-21 21:14:48 +00002983 switch (w->id) {
2984 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00002985 w->regulator = devm_regulator_get(dapm->dev, w->name);
2986 if (IS_ERR(w->regulator)) {
2987 ret = PTR_ERR(w->regulator);
Mark Brown62ea8742012-01-21 21:14:48 +00002988 dev_err(dapm->dev, "Failed to request %s: %d\n",
2989 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002990 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00002991 }
2992 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02002993 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01002994#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01002995 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02002996 if (IS_ERR(w->clk)) {
2997 ret = PTR_ERR(w->clk);
2998 dev_err(dapm->dev, "Failed to request %s: %d\n",
2999 w->name, ret);
3000 return NULL;
3001 }
Mark Brownec029952012-06-04 08:16:20 +01003002#else
3003 return NULL;
3004#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003005 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003006 default:
3007 break;
3008 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003009
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003010 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00003011 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003012 name_len += 1 + strlen(dapm->codec->name_prefix);
3013 w->name = kmalloc(name_len, GFP_KERNEL);
3014 if (w->name == NULL) {
3015 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003016 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003017 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00003018 if (dapm->codec && dapm->codec->name_prefix)
Mark Brown888df392012-02-16 19:37:51 -08003019 snprintf((char *)w->name, name_len, "%s %s",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003020 dapm->codec->name_prefix, widget->name);
3021 else
Mark Brown888df392012-02-16 19:37:51 -08003022 snprintf((char *)w->name, name_len, "%s", widget->name);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003023
Mark Brown7ca3a182011-10-08 14:04:50 +01003024 switch (w->id) {
3025 case snd_soc_dapm_switch:
3026 case snd_soc_dapm_mixer:
3027 case snd_soc_dapm_mixer_named_ctl:
3028 w->power_check = dapm_generic_check_power;
3029 break;
3030 case snd_soc_dapm_mux:
3031 case snd_soc_dapm_virt_mux:
3032 case snd_soc_dapm_value_mux:
3033 w->power_check = dapm_generic_check_power;
3034 break;
3035 case snd_soc_dapm_adc:
3036 case snd_soc_dapm_aif_out:
3037 w->power_check = dapm_adc_check_power;
3038 break;
3039 case snd_soc_dapm_dac:
3040 case snd_soc_dapm_aif_in:
3041 w->power_check = dapm_dac_check_power;
3042 break;
3043 case snd_soc_dapm_pga:
3044 case snd_soc_dapm_out_drv:
3045 case snd_soc_dapm_input:
3046 case snd_soc_dapm_output:
3047 case snd_soc_dapm_micbias:
3048 case snd_soc_dapm_spk:
3049 case snd_soc_dapm_hp:
3050 case snd_soc_dapm_mic:
3051 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003052 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003053 w->power_check = dapm_generic_check_power;
3054 break;
3055 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003056 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003057 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003058 w->power_check = dapm_supply_check_power;
3059 break;
Mark Brown888df392012-02-16 19:37:51 -08003060 case snd_soc_dapm_dai:
3061 w->power_check = dapm_dai_check_power;
3062 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003063 default:
3064 w->power_check = dapm_always_on_check_power;
3065 break;
3066 }
3067
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003068 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003069 w->dapm = dapm;
3070 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003071 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003072 INIT_LIST_HEAD(&w->sources);
3073 INIT_LIST_HEAD(&w->sinks);
3074 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003075 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003076 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003077
3078 /* machine layer set ups unconnected pins and insertions */
3079 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003080 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003081}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003082
3083/**
Mark Brown4ba13272008-05-13 14:51:19 +02003084 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003085 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003086 * @widget: widget array
3087 * @num: number of widgets
3088 *
3089 * Creates new DAPM controls based upon the templates.
3090 *
3091 * Returns 0 for success else error.
3092 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003093int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003094 const struct snd_soc_dapm_widget *widget,
3095 int num)
3096{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003097 struct snd_soc_dapm_widget *w;
3098 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003099 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003100
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003101 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003102 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003103 w = snd_soc_dapm_new_control(dapm, widget);
3104 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003105 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003106 "ASoC: Failed to create DAPM control %s\n",
3107 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003108 ret = -ENOMEM;
3109 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003110 }
Mark Brown4ba13272008-05-13 14:51:19 +02003111 widget++;
3112 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003113 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003114 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003115}
3116EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3117
Mark Brownc74184e2012-04-04 22:12:09 +01003118static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3119 struct snd_kcontrol *kcontrol, int event)
3120{
3121 struct snd_soc_dapm_path *source_p, *sink_p;
3122 struct snd_soc_dai *source, *sink;
3123 const struct snd_soc_pcm_stream *config = w->params;
3124 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003125 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003126 u64 fmt;
3127 int ret;
3128
3129 BUG_ON(!config);
3130 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3131
3132 /* We only support a single source and sink, pick the first */
3133 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3134 list_sink);
3135 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3136 list_source);
3137
3138 BUG_ON(!source_p || !sink_p);
3139 BUG_ON(!sink_p->source || !source_p->sink);
3140 BUG_ON(!source_p->source || !sink_p->sink);
3141
3142 source = source_p->source->priv;
3143 sink = sink_p->sink->priv;
3144
3145 /* Be a little careful as we don't want to overflow the mask array */
3146 if (config->formats) {
3147 fmt = ffs(config->formats) - 1;
3148 } else {
Fabio Estevam516541a2012-04-16 10:53:09 -03003149 dev_warn(w->dapm->dev, "Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003150 config->formats);
3151 fmt = 0;
3152 }
3153
3154 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003155 params = kzalloc(sizeof(*params), GFP_KERNEL);
3156 if (!params) {
3157 ret = -ENOMEM;
3158 goto out;
3159 }
3160 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003161
Mark Brown9747cec2012-04-26 19:12:21 +01003162 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003163 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003164 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003165 config->rate_max;
3166
Mark Brown9747cec2012-04-26 19:12:21 +01003167 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003168 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003169 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003170 = config->channels_max;
3171
3172 memset(&substream, 0, sizeof(substream));
3173
3174 switch (event) {
3175 case SND_SOC_DAPM_PRE_PMU:
3176 if (source->driver->ops && source->driver->ops->hw_params) {
3177 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3178 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003179 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003180 if (ret != 0) {
3181 dev_err(source->dev,
3182 "hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003183 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003184 }
3185 }
3186
3187 if (sink->driver->ops && sink->driver->ops->hw_params) {
3188 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003189 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003190 sink);
3191 if (ret != 0) {
3192 dev_err(sink->dev,
3193 "hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003194 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003195 }
3196 }
3197 break;
3198
3199 case SND_SOC_DAPM_POST_PMU:
3200 ret = snd_soc_dai_digital_mute(sink, 0);
3201 if (ret != 0 && ret != -ENOTSUPP)
3202 dev_warn(sink->dev, "Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003203 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003204 break;
3205
3206 case SND_SOC_DAPM_PRE_PMD:
3207 ret = snd_soc_dai_digital_mute(sink, 1);
3208 if (ret != 0 && ret != -ENOTSUPP)
3209 dev_warn(sink->dev, "Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003210 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003211 break;
3212
3213 default:
3214 BUG();
3215 return -EINVAL;
3216 }
3217
Mark Brown9747cec2012-04-26 19:12:21 +01003218out:
3219 kfree(params);
3220 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003221}
3222
3223int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3224 const struct snd_soc_pcm_stream *params,
3225 struct snd_soc_dapm_widget *source,
3226 struct snd_soc_dapm_widget *sink)
3227{
3228 struct snd_soc_dapm_route routes[2];
3229 struct snd_soc_dapm_widget template;
3230 struct snd_soc_dapm_widget *w;
3231 size_t len;
3232 char *link_name;
3233
3234 len = strlen(source->name) + strlen(sink->name) + 2;
3235 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3236 if (!link_name)
3237 return -ENOMEM;
3238 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3239
3240 memset(&template, 0, sizeof(template));
3241 template.reg = SND_SOC_NOPM;
3242 template.id = snd_soc_dapm_dai_link;
3243 template.name = link_name;
3244 template.event = snd_soc_dai_link_event;
3245 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3246 SND_SOC_DAPM_PRE_PMD;
3247
3248 dev_dbg(card->dev, "adding %s widget\n", link_name);
3249
3250 w = snd_soc_dapm_new_control(&card->dapm, &template);
3251 if (!w) {
3252 dev_err(card->dev, "Failed to create %s widget\n",
3253 link_name);
3254 return -ENOMEM;
3255 }
3256
3257 w->params = params;
3258
3259 memset(&routes, 0, sizeof(routes));
3260
3261 routes[0].source = source->name;
3262 routes[0].sink = link_name;
3263 routes[1].source = link_name;
3264 routes[1].sink = sink->name;
3265
3266 return snd_soc_dapm_add_routes(&card->dapm, routes,
3267 ARRAY_SIZE(routes));
3268}
3269
Mark Brown888df392012-02-16 19:37:51 -08003270int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3271 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003272{
Mark Brown888df392012-02-16 19:37:51 -08003273 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003274 struct snd_soc_dapm_widget *w;
3275
Mark Brown888df392012-02-16 19:37:51 -08003276 WARN_ON(dapm->dev != dai->dev);
3277
3278 memset(&template, 0, sizeof(template));
3279 template.reg = SND_SOC_NOPM;
3280
3281 if (dai->driver->playback.stream_name) {
3282 template.id = snd_soc_dapm_dai;
3283 template.name = dai->driver->playback.stream_name;
3284 template.sname = dai->driver->playback.stream_name;
3285
3286 dev_dbg(dai->dev, "adding %s widget\n",
3287 template.name);
3288
3289 w = snd_soc_dapm_new_control(dapm, &template);
3290 if (!w) {
3291 dev_err(dapm->dev, "Failed to create %s widget\n",
3292 dai->driver->playback.stream_name);
3293 }
3294
3295 w->priv = dai;
3296 dai->playback_widget = w;
3297 }
3298
3299 if (dai->driver->capture.stream_name) {
3300 template.id = snd_soc_dapm_dai;
3301 template.name = dai->driver->capture.stream_name;
3302 template.sname = dai->driver->capture.stream_name;
3303
3304 dev_dbg(dai->dev, "adding %s widget\n",
3305 template.name);
3306
3307 w = snd_soc_dapm_new_control(dapm, &template);
3308 if (!w) {
3309 dev_err(dapm->dev, "Failed to create %s widget\n",
3310 dai->driver->capture.stream_name);
3311 }
3312
3313 w->priv = dai;
3314 dai->capture_widget = w;
3315 }
3316
3317 return 0;
3318}
3319
3320int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3321{
3322 struct snd_soc_dapm_widget *dai_w, *w;
3323 struct snd_soc_dai *dai;
3324 struct snd_soc_dapm_route r;
3325
3326 memset(&r, 0, sizeof(r));
3327
3328 /* For each DAI widget... */
3329 list_for_each_entry(dai_w, &card->widgets, list) {
3330 if (dai_w->id != snd_soc_dapm_dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003331 continue;
Mark Brown888df392012-02-16 19:37:51 -08003332
3333 dai = dai_w->priv;
3334
3335 /* ...find all widgets with the same stream and link them */
3336 list_for_each_entry(w, &card->widgets, list) {
3337 if (w->dapm != dai_w->dapm)
3338 continue;
3339
3340 if (w->id == snd_soc_dapm_dai)
3341 continue;
3342
3343 if (!w->sname)
3344 continue;
3345
3346 if (dai->driver->playback.stream_name &&
3347 strstr(w->sname,
3348 dai->driver->playback.stream_name)) {
3349 r.source = dai->playback_widget->name;
3350 r.sink = w->name;
3351 dev_dbg(dai->dev, "%s -> %s\n",
3352 r.source, r.sink);
3353
3354 snd_soc_dapm_add_route(w->dapm, &r);
3355 }
3356
3357 if (dai->driver->capture.stream_name &&
3358 strstr(w->sname,
3359 dai->driver->capture.stream_name)) {
3360 r.source = w->name;
3361 r.sink = dai->capture_widget->name;
3362 dev_dbg(dai->dev, "%s -> %s\n",
3363 r.source, r.sink);
3364
3365 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003366 }
3367 }
3368 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003369
Mark Brown888df392012-02-16 19:37:51 -08003370 return 0;
3371}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003372
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003373static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3374 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003375{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003376
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003377 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3378 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3379 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003380
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003381 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3382 w_cpu = cpu_dai->playback_widget;
3383 w_codec = codec_dai->playback_widget;
3384 } else {
3385 w_cpu = cpu_dai->capture_widget;
3386 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003387 }
3388
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003389 if (w_cpu) {
3390
3391 dapm_mark_dirty(w_cpu, "stream event");
3392
3393 switch (event) {
3394 case SND_SOC_DAPM_STREAM_START:
3395 w_cpu->active = 1;
3396 break;
3397 case SND_SOC_DAPM_STREAM_STOP:
3398 w_cpu->active = 0;
3399 break;
3400 case SND_SOC_DAPM_STREAM_SUSPEND:
3401 case SND_SOC_DAPM_STREAM_RESUME:
3402 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3403 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3404 break;
3405 }
3406 }
3407
3408 if (w_codec) {
3409
3410 dapm_mark_dirty(w_codec, "stream event");
3411
3412 switch (event) {
3413 case SND_SOC_DAPM_STREAM_START:
3414 w_codec->active = 1;
3415 break;
3416 case SND_SOC_DAPM_STREAM_STOP:
3417 w_codec->active = 0;
3418 break;
3419 case SND_SOC_DAPM_STREAM_SUSPEND:
3420 case SND_SOC_DAPM_STREAM_RESUME:
3421 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3422 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3423 break;
3424 }
3425 }
3426
3427 dapm_power_widgets(&rtd->card->dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003428}
3429
3430/**
3431 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3432 * @rtd: PCM runtime data
3433 * @stream: stream name
3434 * @event: stream event
3435 *
3436 * Sends a stream event to the dapm core. The core then makes any
3437 * necessary widget power changes.
3438 *
3439 * Returns 0 for success else error.
3440 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003441void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3442 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003443{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003444 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003445
Liam Girdwood3cd04342012-03-09 12:02:08 +00003446 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003447 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003448 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003449}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003450
3451/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003452 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003453 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003454 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003455 *
Mark Brown74b8f952009-06-06 11:26:15 +01003456 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003457 * a valid audio route and active audio stream.
3458 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3459 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003460 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003461int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003462{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003463 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003464}
Liam Girdwooda5302182008-07-07 13:35:17 +01003465EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003466
3467/**
Mark Brownda341832010-03-15 19:23:37 +00003468 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003469 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003470 * @pin: pin name
3471 *
3472 * Enables input/output pin regardless of any other state. This is
3473 * intended for use with microphone bias supplies used in microphone
3474 * jack detection.
3475 *
3476 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3477 * do any widget power switching.
3478 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003479int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3480 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003481{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003482 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003483
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003484 if (!w) {
3485 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3486 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003487 }
3488
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003489 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3490 w->connected = 1;
3491 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003492 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003493
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003494 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003495}
3496EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3497
3498/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003499 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003500 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003501 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003502 *
Mark Brown74b8f952009-06-06 11:26:15 +01003503 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003504 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3505 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003506 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003507int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3508 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003509{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003510 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003511}
3512EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3513
3514/**
Mark Brown5817b522008-09-24 11:23:11 +01003515 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003516 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003517 * @pin: pin name
3518 *
3519 * Marks the specified pin as being not connected, disabling it along
3520 * any parent or child widgets. At present this is identical to
3521 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3522 * additional things such as disabling controls which only affect
3523 * paths through the pin.
3524 *
3525 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3526 * do any widget power switching.
3527 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003528int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003529{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003530 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003531}
3532EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3533
3534/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003535 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003536 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003537 * @pin: audio signal pin endpoint (or start point)
3538 *
3539 * Get audio pin status - connected or disconnected.
3540 *
3541 * Returns 1 for connected otherwise 0.
3542 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003543int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3544 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003545{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003546 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003547
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003548 if (w)
3549 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003550
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003551 return 0;
3552}
Liam Girdwooda5302182008-07-07 13:35:17 +01003553EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003554
3555/**
Mark Brown1547aba2010-05-07 21:11:40 +01003556 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003557 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003558 * @pin: audio signal pin endpoint (or start point)
3559 *
3560 * Mark the given endpoint or pin as ignoring suspend. When the
3561 * system is disabled a path between two endpoints flagged as ignoring
3562 * suspend will not be disabled. The path must already be enabled via
3563 * normal means at suspend time, it will not be turned on if it was not
3564 * already enabled.
3565 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003566int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3567 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003568{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003569 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003570
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003571 if (!w) {
3572 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3573 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003574 }
3575
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003576 w->ignore_suspend = 1;
3577
3578 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003579}
3580EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3581
Stephen Warren16332812011-11-23 12:42:04 -07003582static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3583 struct snd_soc_dapm_widget *w)
3584{
3585 struct snd_soc_dapm_path *p;
3586
3587 list_for_each_entry(p, &card->paths, list) {
3588 if ((p->source == w) || (p->sink == w)) {
3589 dev_dbg(card->dev,
3590 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3591 p->source->name, p->source->id, p->source->dapm,
3592 p->sink->name, p->sink->id, p->sink->dapm);
3593
3594 /* Connected to something other than the codec */
3595 if (p->source->dapm != p->sink->dapm)
3596 return true;
3597 /*
3598 * Loopback connection from codec external pin to
3599 * codec external pin
3600 */
3601 if (p->sink->id == snd_soc_dapm_input) {
3602 switch (p->source->id) {
3603 case snd_soc_dapm_output:
3604 case snd_soc_dapm_micbias:
3605 return true;
3606 default:
3607 break;
3608 }
3609 }
3610 }
3611 }
3612
3613 return false;
3614}
3615
3616/**
3617 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3618 * @codec: The codec whose pins should be processed
3619 *
3620 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3621 * which are unused. Pins are used if they are connected externally to the
3622 * codec, whether that be to some other device, or a loop-back connection to
3623 * the codec itself.
3624 */
3625void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3626{
3627 struct snd_soc_card *card = codec->card;
3628 struct snd_soc_dapm_context *dapm = &codec->dapm;
3629 struct snd_soc_dapm_widget *w;
3630
Mark Browna094b802011-11-27 19:42:20 +00003631 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003632 &card->dapm, &codec->dapm);
3633
3634 list_for_each_entry(w, &card->widgets, list) {
3635 if (w->dapm != dapm)
3636 continue;
3637 switch (w->id) {
3638 case snd_soc_dapm_input:
3639 case snd_soc_dapm_output:
3640 case snd_soc_dapm_micbias:
Mark Browna094b802011-11-27 19:42:20 +00003641 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003642 w->name);
3643 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003644 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003645 "... Not in map; disabling\n");
3646 snd_soc_dapm_nc_pin(dapm, w->name);
3647 }
3648 break;
3649 default:
3650 break;
3651 }
3652 }
3653}
3654
Mark Brown1547aba2010-05-07 21:11:40 +01003655/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003656 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003657 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003658 *
3659 * Free all dapm widgets and resources.
3660 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003661void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003662{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003663 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003664 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003665 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003666 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003667}
3668EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3669
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003670static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003671{
Liam Girdwood01005a72012-07-06 16:57:05 +01003672 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003673 struct snd_soc_dapm_widget *w;
3674 LIST_HEAD(down_list);
3675 int powerdown = 0;
3676
Liam Girdwood01005a72012-07-06 16:57:05 +01003677 mutex_lock(&card->dapm_mutex);
3678
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003679 list_for_each_entry(w, &dapm->card->widgets, list) {
3680 if (w->dapm != dapm)
3681 continue;
Mark Brown51737472009-06-22 13:16:51 +01003682 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003683 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003684 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003685 powerdown = 1;
3686 }
3687 }
3688
3689 /* If there were no widgets to power down we're already in
3690 * standby.
3691 */
3692 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003693 if (dapm->bias_level == SND_SOC_BIAS_ON)
3694 snd_soc_dapm_set_bias_level(dapm,
3695 SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003696 dapm_seq_run(dapm, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003697 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3698 snd_soc_dapm_set_bias_level(dapm,
3699 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003700 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003701
3702 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003703}
Mark Brown51737472009-06-22 13:16:51 +01003704
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003705/*
3706 * snd_soc_dapm_shutdown - callback for system shutdown
3707 */
3708void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3709{
3710 struct snd_soc_codec *codec;
3711
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003712 list_for_each_entry(codec, &card->codec_dev_list, list) {
3713 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003714 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3715 snd_soc_dapm_set_bias_level(&codec->dapm,
3716 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003717 }
Mark Brown51737472009-06-22 13:16:51 +01003718}
3719
Richard Purdie2b97eab2006-10-06 18:32:18 +02003720/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003721MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003722MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3723MODULE_LICENSE("GPL");