blob: bad6f6db74c93c34b2b561de46b7af091eee9c7d [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 Brown46162742013-06-05 19:36:11 +010058 [snd_soc_dapm_dai_in] = 3,
59 [snd_soc_dapm_dai_out] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010060 [snd_soc_dapm_aif_in] = 3,
61 [snd_soc_dapm_aif_out] = 3,
62 [snd_soc_dapm_mic] = 4,
63 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000064 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010065 [snd_soc_dapm_value_mux] = 5,
66 [snd_soc_dapm_dac] = 6,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020067 [snd_soc_dapm_switch] = 7,
Mark Brown010ff262009-08-17 17:39:22 +010068 [snd_soc_dapm_mixer] = 7,
69 [snd_soc_dapm_mixer_named_ctl] = 7,
70 [snd_soc_dapm_pga] = 8,
71 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060072 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010073 [snd_soc_dapm_hp] = 10,
74 [snd_soc_dapm_spk] = 10,
Mark Brown7e1f7c82012-04-12 17:29:36 +010075 [snd_soc_dapm_line] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010076 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020077};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000078
Richard Purdie2b97eab2006-10-06 18:32:18 +020079static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010080 [snd_soc_dapm_pre] = 0,
81 [snd_soc_dapm_adc] = 1,
82 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010083 [snd_soc_dapm_spk] = 2,
Mark Brown7e1f7c82012-04-12 17:29:36 +010084 [snd_soc_dapm_line] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060085 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010086 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020087 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +010088 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010089 [snd_soc_dapm_mixer] = 5,
90 [snd_soc_dapm_dac] = 6,
91 [snd_soc_dapm_mic] = 7,
92 [snd_soc_dapm_micbias] = 8,
93 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000094 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010095 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010096 [snd_soc_dapm_aif_in] = 10,
97 [snd_soc_dapm_aif_out] = 10,
Mark Brown46162742013-06-05 19:36:11 +010098 [snd_soc_dapm_dai_in] = 10,
99 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100100 [snd_soc_dapm_dai_link] = 11,
Ola Liljad7e7eb92012-05-24 15:26:25 +0200101 [snd_soc_dapm_clock_supply] = 12,
Mark Brownc74184e2012-04-04 22:12:09 +0100102 [snd_soc_dapm_regulator_supply] = 12,
103 [snd_soc_dapm_supply] = 12,
104 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200105};
106
Troy Kisky12ef1932008-10-13 17:42:14 -0700107static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100108{
109 if (pop_time)
110 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
111}
112
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200113static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100114{
115 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200116 char *buf;
117
118 if (!pop_time)
119 return;
120
121 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
122 if (buf == NULL)
123 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100124
125 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200126 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100127 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100128 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200129
130 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100131}
132
Mark Browndb432b42011-10-03 21:06:40 +0100133static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
134{
135 return !list_empty(&w->dirty);
136}
137
Mark Brown25c77c52011-10-08 13:36:03 +0100138void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100139{
Mark Brown75c1f892011-10-04 22:28:08 +0100140 if (!dapm_dirty_widget(w)) {
141 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
142 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100143 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100144 }
Mark Browndb432b42011-10-03 21:06:40 +0100145}
Mark Brown25c77c52011-10-08 13:36:03 +0100146EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100147
Mark Browne2d32ff2012-08-31 17:38:32 -0700148void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
149{
150 struct snd_soc_card *card = dapm->card;
151 struct snd_soc_dapm_widget *w;
152
153 mutex_lock(&card->dapm_mutex);
154
155 list_for_each_entry(w, &card->widgets, list) {
156 switch (w->id) {
157 case snd_soc_dapm_input:
158 case snd_soc_dapm_output:
159 dapm_mark_dirty(w, "Rechecking inputs and outputs");
160 break;
161 default:
162 break;
163 }
164 }
165
166 mutex_unlock(&card->dapm_mutex);
167}
168EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
169
Richard Purdie2b97eab2006-10-06 18:32:18 +0200170/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100171static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200172 const struct snd_soc_dapm_widget *_widget)
173{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100174 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200175}
176
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200177struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200178 unsigned int value;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200179 struct snd_soc_dapm_widget_list wlist;
180};
181
182static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
183 struct snd_kcontrol *kcontrol)
184{
185 struct dapm_kcontrol_data *data;
186
187 data = kzalloc(sizeof(*data) + sizeof(widget), GFP_KERNEL);
188 if (!data) {
189 dev_err(widget->dapm->dev,
190 "ASoC: can't allocate kcontrol data for %s\n",
191 widget->name);
192 return -ENOMEM;
193 }
194
195 data->wlist.widgets[0] = widget;
196 data->wlist.num_widgets = 1;
197
198 kcontrol->private_data = data;
199
200 return 0;
201}
202
203static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
204{
205 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
206 kfree(data);
207}
208
209static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
210 const struct snd_kcontrol *kcontrol)
211{
212 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
213
214 return &data->wlist;
215}
216
217static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
218 struct snd_soc_dapm_widget *widget)
219{
220 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
221 struct dapm_kcontrol_data *new_data;
222 unsigned int n = data->wlist.num_widgets + 1;
223
224 new_data = krealloc(data, sizeof(*data) + sizeof(widget) * n,
225 GFP_KERNEL);
226 if (!data)
227 return -ENOMEM;
228
229 data->wlist.widgets[n - 1] = widget;
230 data->wlist.num_widgets = n;
231
232 kcontrol->private_data = data;
233
234 return 0;
235}
236
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200237static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
238{
239 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
240
241 return data->value;
242}
243
244static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
245 unsigned int value)
246{
247 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
248
249 if (data->value == value)
250 return false;
251
252 data->value = value;
253
254 return true;
255}
256
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200257/**
258 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
259 * @kcontrol: The kcontrol
260 */
261struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
262{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200263 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200264}
265EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
266
Liam Girdwood6c120e12012-02-15 15:15:34 +0000267static void dapm_reset(struct snd_soc_card *card)
268{
269 struct snd_soc_dapm_widget *w;
270
271 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
272
273 list_for_each_entry(w, &card->widgets, list) {
274 w->power_checked = false;
275 w->inputs = -1;
276 w->outputs = -1;
277 }
278}
279
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100280static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
281{
282 if (w->codec)
283 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100284 else if (w->platform)
285 return snd_soc_platform_read(w->platform, reg);
286
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000287 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100288 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100289}
290
291static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
292{
293 if (w->codec)
294 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100295 else if (w->platform)
296 return snd_soc_platform_write(w->platform, reg, val);
297
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000298 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100299 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100300}
301
Liam Girdwood49575fb52012-03-06 18:16:19 +0000302static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
303{
Mark Browne06ab3b2012-03-06 23:58:22 +0000304 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000305 mutex_lock(&w->codec->mutex);
306 else if (w->platform)
307 mutex_lock(&w->platform->mutex);
308}
309
310static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
311{
Mark Browne06ab3b2012-03-06 23:58:22 +0000312 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000313 mutex_unlock(&w->codec->mutex);
314 else if (w->platform)
315 mutex_unlock(&w->platform->mutex);
316}
317
318static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100319 unsigned short reg, unsigned int mask, unsigned int value)
320{
Mark Brown8a713da2011-12-03 12:33:55 +0000321 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100322 unsigned int old, new;
323 int ret;
324
Mark Brown8a713da2011-12-03 12:33:55 +0000325 if (w->codec && w->codec->using_regmap) {
326 ret = regmap_update_bits_check(w->codec->control_data,
327 reg, mask, value, &change);
328 if (ret != 0)
329 return ret;
330 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000331 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000332 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000333 if (ret < 0) {
334 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100335 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000336 }
Mark Brown8a713da2011-12-03 12:33:55 +0000337
338 old = ret;
339 new = (old & ~mask) | (value & mask);
340 change = old != new;
341 if (change) {
342 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000343 if (ret < 0) {
344 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000345 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000346 }
Mark Brown8a713da2011-12-03 12:33:55 +0000347 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000348 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100349 }
350
351 return change;
352}
353
Mark Brown452c5ea2009-05-17 21:41:23 +0100354/**
355 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800356 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100357 * @level: level to configure
358 *
359 * Configure the bias (power) levels for the SoC audio device.
360 *
361 * Returns 0 for success else error.
362 */
Mark Browned5a4c42011-02-18 11:12:42 -0800363static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200364 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100365{
Mark Browned5a4c42011-02-18 11:12:42 -0800366 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100367 int ret = 0;
368
Mark Brown84e90932010-11-04 00:07:02 -0400369 trace_snd_soc_bias_level_start(card, level);
370
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000371 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100372 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100373 if (ret != 0)
374 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100375
Mark Browncc4c6702011-06-06 19:03:34 +0100376 if (dapm->codec) {
377 if (dapm->codec->driver->set_bias_level)
378 ret = dapm->codec->driver->set_bias_level(dapm->codec,
379 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100380 else
381 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100382 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100383 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100384 }
Liam Girdwood41231282012-07-06 16:56:16 +0100385
Mark Brown171ec6b2011-06-06 18:15:19 +0100386 if (ret != 0)
387 goto out;
388
389 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100390 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100391out:
Mark Brown84e90932010-11-04 00:07:02 -0400392 trace_snd_soc_bias_level_done(card, level);
393
Mark Brown452c5ea2009-05-17 21:41:23 +0100394 return ret;
395}
396
Richard Purdie2b97eab2006-10-06 18:32:18 +0200397/* set up initial codec paths */
398static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
399 struct snd_soc_dapm_path *p, int i)
400{
401 switch (w->id) {
402 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000403 case snd_soc_dapm_mixer:
404 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200405 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100406 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600407 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400408 unsigned int reg = mc->reg;
409 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100410 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400411 unsigned int mask = (1 << fls(max)) - 1;
412 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200413
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100414 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200415 val = (val >> shift) & mask;
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200416 if (invert)
417 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200418
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200419 p->connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200420 }
421 break;
422 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600423 struct soc_enum *e = (struct soc_enum *)
424 w->kcontrol_news[i].private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200425 int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200426
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100427 val = soc_widget_read(w, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200428 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200429
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200430 if (item < e->max && !strcmp(p->name, e->texts[item]))
431 p->connect = 1;
432 else
433 p->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200434 }
435 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000436 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600437 struct soc_enum *e = (struct soc_enum *)
438 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000439
440 p->connect = 0;
441 /* since a virtual mux has no backing registers to
442 * decide which path to connect, it will try to match
443 * with the first enumeration. This is to ensure
444 * that the default mux choice (the first) will be
445 * correctly powered up during initialization.
446 */
447 if (!strcmp(p->name, e->texts[0]))
448 p->connect = 1;
449 }
450 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200451 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200452 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600453 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200454 int val, item;
455
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100456 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200457 val = (val >> e->shift_l) & e->mask;
458 for (item = 0; item < e->max; item++) {
459 if (val == e->values[item])
460 break;
461 }
462
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200463 if (item < e->max && !strcmp(p->name, e->texts[item]))
464 p->connect = 1;
465 else
466 p->connect = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200467 }
468 break;
Mark Brown56563102011-10-03 22:41:09 +0100469 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200470 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600471 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200472 case snd_soc_dapm_output:
473 case snd_soc_dapm_adc:
474 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000475 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200476 case snd_soc_dapm_dac:
477 case snd_soc_dapm_micbias:
478 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100479 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000480 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200481 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100482 case snd_soc_dapm_aif_in:
483 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100484 case snd_soc_dapm_dai_in:
485 case snd_soc_dapm_dai_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200486 case snd_soc_dapm_hp:
487 case snd_soc_dapm_mic:
488 case snd_soc_dapm_spk:
489 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100490 case snd_soc_dapm_dai_link:
Mark Brown56563102011-10-03 22:41:09 +0100491 p->connect = 1;
492 break;
493 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200494 case snd_soc_dapm_pre:
495 case snd_soc_dapm_post:
496 p->connect = 0;
497 break;
498 }
499}
500
Mark Brown74b8f952009-06-06 11:26:15 +0100501/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200502static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200503 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
504 struct snd_soc_dapm_path *path, const char *control_name,
505 const struct snd_kcontrol_new *kcontrol)
506{
507 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
508 int i;
509
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100510 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200511 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200512 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200513 list_add(&path->list_sink, &dest->sources);
514 list_add(&path->list_source, &src->sinks);
515 path->name = (char*)e->texts[i];
516 dapm_set_path_status(dest, path, 0);
517 return 0;
518 }
519 }
520
521 return -ENODEV;
522}
523
Mark Brown74b8f952009-06-06 11:26:15 +0100524/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200525static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200526 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
527 struct snd_soc_dapm_path *path, const char *control_name)
528{
529 int i;
530
531 /* search for mixer kcontrol */
532 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600533 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200534 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200535 list_add(&path->list_sink, &dest->sources);
536 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600537 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200538 dapm_set_path_status(dest, path, i);
539 return 0;
540 }
541 }
542 return -ENODEV;
543}
544
Stephen Warrenaf468002011-04-28 17:38:01 -0600545static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600546 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600547 const struct snd_kcontrol_new *kcontrol_new,
548 struct snd_kcontrol **kcontrol)
549{
550 struct snd_soc_dapm_widget *w;
551 int i;
552
553 *kcontrol = NULL;
554
555 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600556 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
557 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600558 for (i = 0; i < w->num_kcontrols; i++) {
559 if (&w->kcontrol_news[i] == kcontrol_new) {
560 if (w->kcontrols)
561 *kcontrol = w->kcontrols[i];
562 return 1;
563 }
564 }
565 }
566
567 return 0;
568}
569
Stephen Warren85762e72013-03-29 15:40:10 -0600570/*
571 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
572 * create it. Either way, add the widget into the control's widget list
573 */
574static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
575 int kci, struct snd_soc_dapm_path *path)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200576{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200577 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000578 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000579 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600580 size_t prefix_len;
581 int shared;
582 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600583 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600584 char *long_name;
585 const char *name;
586 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000587
588 if (dapm->codec)
589 prefix = dapm->codec->name_prefix;
590 else
591 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200592
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000593 if (prefix)
594 prefix_len = strlen(prefix) + 1;
595 else
596 prefix_len = 0;
597
Stephen Warren85762e72013-03-29 15:40:10 -0600598 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
599 &kcontrol);
600
Stephen Warren85762e72013-03-29 15:40:10 -0600601 if (!kcontrol) {
602 if (shared) {
603 wname_in_long_name = false;
604 kcname_in_long_name = true;
605 } else {
606 switch (w->id) {
607 case snd_soc_dapm_switch:
608 case snd_soc_dapm_mixer:
609 wname_in_long_name = true;
610 kcname_in_long_name = true;
611 break;
612 case snd_soc_dapm_mixer_named_ctl:
613 wname_in_long_name = false;
614 kcname_in_long_name = true;
615 break;
616 case snd_soc_dapm_mux:
617 case snd_soc_dapm_virt_mux:
618 case snd_soc_dapm_value_mux:
619 wname_in_long_name = true;
620 kcname_in_long_name = false;
621 break;
622 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600623 return -EINVAL;
624 }
625 }
626
627 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600628 /*
629 * The control will get a prefix from the control
630 * creation process but we're also using the same
631 * prefix for widgets so cut the prefix off the
632 * front of the widget name.
633 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200634 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600635 w->name + prefix_len,
636 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200637 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200638 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600639
640 name = long_name;
641 } else if (wname_in_long_name) {
642 long_name = NULL;
643 name = w->name + prefix_len;
644 } else {
645 long_name = NULL;
646 name = w->kcontrol_news[kci].name;
647 }
648
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200649 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600650 prefix);
Lars-Peter Clausen6b75bf02013-06-14 13:16:51 +0200651 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200652 kfree(long_name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200653
654 ret = dapm_kcontrol_data_alloc(w, kcontrol);
655 if (ret) {
656 snd_ctl_free_one(kcontrol);
657 return ret;
658 }
659
Stephen Warren85762e72013-03-29 15:40:10 -0600660 ret = snd_ctl_add(card, kcontrol);
661 if (ret < 0) {
662 dev_err(dapm->dev,
663 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
664 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600665 return ret;
666 }
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200667 } else {
668 ret = dapm_kcontrol_add_widget(kcontrol, w);
669 if (ret)
670 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600671 }
672
Stephen Warren85762e72013-03-29 15:40:10 -0600673 w->kcontrols[kci] = kcontrol;
674 path->kcontrol = kcontrol;
675
676 return 0;
677}
678
679/* create new dapm mixer control */
680static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
681{
682 int i, ret;
683 struct snd_soc_dapm_path *path;
684
Richard Purdie2b97eab2006-10-06 18:32:18 +0200685 /* add kcontrol */
686 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200687 /* match name */
688 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200689 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600690 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200691 continue;
692
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200693 if (w->kcontrols[i]) {
694 path->kcontrol = w->kcontrols[i];
695 continue;
696 }
697
Stephen Warren85762e72013-03-29 15:40:10 -0600698 ret = dapm_create_or_share_mixmux_kcontrol(w, i, path);
699 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200700 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200701 }
702 }
Stephen Warren85762e72013-03-29 15:40:10 -0600703
704 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200705}
706
707/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200708static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200709{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200710 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600711 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600712 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200713
Stephen Warrenaf468002011-04-28 17:38:01 -0600714 if (w->num_kcontrols != 1) {
715 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000716 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600717 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200718 return -EINVAL;
719 }
720
Stephen Warren85762e72013-03-29 15:40:10 -0600721 path = list_first_entry(&w->sources, struct snd_soc_dapm_path,
722 list_sink);
723 if (!path) {
724 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
725 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600726 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200727
Stephen Warren85762e72013-03-29 15:40:10 -0600728 ret = dapm_create_or_share_mixmux_kcontrol(w, 0, path);
729 if (ret < 0)
730 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600731
Richard Purdie2b97eab2006-10-06 18:32:18 +0200732 list_for_each_entry(path, &w->sources, list_sink)
Stephen Warren85762e72013-03-29 15:40:10 -0600733 path->kcontrol = w->kcontrols[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200734
Stephen Warrenaf468002011-04-28 17:38:01 -0600735 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200736}
737
738/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200739static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200740{
Mark Browna6c65732010-03-03 17:45:21 +0000741 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200742 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000743 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200744
Mark Browna6c65732010-03-03 17:45:21 +0000745 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200746}
747
748/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100749static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
750 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200751{
752 struct snd_soc_dapm_path *p;
753
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100754 list_for_each_entry(p, sink, list_source) {
755 if (p->walked) {
756 p->walked = 0;
757 dapm_clear_walk_output(dapm, &p->sink->sinks);
758 }
759 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200760}
761
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100762static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
763 struct list_head *source)
764{
765 struct snd_soc_dapm_path *p;
766
767 list_for_each_entry(p, source, list_sink) {
768 if (p->walked) {
769 p->walked = 0;
770 dapm_clear_walk_input(dapm, &p->source->sources);
771 }
772 }
773}
774
775
Mark Brown99497882010-05-07 20:24:05 +0100776/* We implement power down on suspend by checking the power state of
777 * the ALSA card - when we are suspending the ALSA state for the card
778 * is set to D3.
779 */
780static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
781{
Mark Brown12ea2c72011-03-02 18:17:32 +0000782 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100783
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000784 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100785 case SNDRV_CTL_POWER_D3hot:
786 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100787 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000788 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200789 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100790 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100791 default:
792 return 1;
793 }
794}
795
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100796/* add widget to list if it's not already in the list */
797static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
798 struct snd_soc_dapm_widget *w)
799{
800 struct snd_soc_dapm_widget_list *wlist;
801 int wlistsize, wlistentries, i;
802
803 if (*list == NULL)
804 return -EINVAL;
805
806 wlist = *list;
807
808 /* is this widget already in the list */
809 for (i = 0; i < wlist->num_widgets; i++) {
810 if (wlist->widgets[i] == w)
811 return 0;
812 }
813
814 /* allocate some new space */
815 wlistentries = wlist->num_widgets + 1;
816 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
817 wlistentries * sizeof(struct snd_soc_dapm_widget *);
818 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
819 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000820 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100821 w->name);
822 return -ENOMEM;
823 }
824 wlist = *list;
825
826 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000827 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100828 w->name, wlist->num_widgets);
829
830 wlist->widgets[wlist->num_widgets] = w;
831 wlist->num_widgets++;
832 return 1;
833}
834
Richard Purdie2b97eab2006-10-06 18:32:18 +0200835/*
836 * Recursively check for a completed path to an active or physically connected
837 * output widget. Returns number of complete paths.
838 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100839static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
840 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200841{
842 struct snd_soc_dapm_path *path;
843 int con = 0;
844
Mark Brown024dc072011-10-09 11:52:05 +0100845 if (widget->outputs >= 0)
846 return widget->outputs;
847
Mark Brownde02d072011-09-20 21:43:24 +0100848 DAPM_UPDATE_STAT(widget, path_checks);
849
Mark Brown62ea8742012-01-21 21:14:48 +0000850 switch (widget->id) {
851 case snd_soc_dapm_supply:
852 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200853 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100854 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000855 default:
856 break;
857 }
Mark Brown246d0a12009-04-22 18:24:55 +0100858
Mark Brown010ff262009-08-17 17:39:22 +0100859 switch (widget->id) {
860 case snd_soc_dapm_adc:
861 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100862 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100863 if (widget->active) {
864 widget->outputs = snd_soc_dapm_suspend_check(widget);
865 return widget->outputs;
866 }
Mark Brown010ff262009-08-17 17:39:22 +0100867 default:
868 break;
869 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200870
871 if (widget->connected) {
872 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100873 if (widget->id == snd_soc_dapm_output && !widget->ext) {
874 widget->outputs = snd_soc_dapm_suspend_check(widget);
875 return widget->outputs;
876 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200877
878 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100879 if (widget->id == snd_soc_dapm_hp ||
880 widget->id == snd_soc_dapm_spk ||
881 (widget->id == snd_soc_dapm_line &&
882 !list_empty(&widget->sources))) {
883 widget->outputs = snd_soc_dapm_suspend_check(widget);
884 return widget->outputs;
885 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200886 }
887
888 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100889 DAPM_UPDATE_STAT(widget, neighbour_checks);
890
Mark Brownbf3a9e12011-06-13 16:42:29 +0100891 if (path->weak)
892 continue;
893
Mark Brown8af294b2013-02-22 17:48:15 +0000894 if (path->walking)
895 return 1;
896
Richard Purdie2b97eab2006-10-06 18:32:18 +0200897 if (path->walked)
898 continue;
899
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100900 trace_snd_soc_dapm_output_path(widget, path);
901
Richard Purdie2b97eab2006-10-06 18:32:18 +0200902 if (path->sink && path->connect) {
903 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000904 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100905
906 /* do we need to add this widget to the list ? */
907 if (list) {
908 int err;
909 err = dapm_list_add_widget(list, path->sink);
910 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000911 dev_err(widget->dapm->dev,
912 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100913 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000914 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100915 return con;
916 }
917 }
918
919 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000920
921 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200922 }
923 }
924
Mark Brown024dc072011-10-09 11:52:05 +0100925 widget->outputs = con;
926
Richard Purdie2b97eab2006-10-06 18:32:18 +0200927 return con;
928}
929
930/*
931 * Recursively check for a completed path to an active or physically connected
932 * input widget. Returns number of complete paths.
933 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100934static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
935 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200936{
937 struct snd_soc_dapm_path *path;
938 int con = 0;
939
Mark Brown024dc072011-10-09 11:52:05 +0100940 if (widget->inputs >= 0)
941 return widget->inputs;
942
Mark Brownde02d072011-09-20 21:43:24 +0100943 DAPM_UPDATE_STAT(widget, path_checks);
944
Mark Brown62ea8742012-01-21 21:14:48 +0000945 switch (widget->id) {
946 case snd_soc_dapm_supply:
947 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200948 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100949 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000950 default:
951 break;
952 }
Mark Brown246d0a12009-04-22 18:24:55 +0100953
Richard Purdie2b97eab2006-10-06 18:32:18 +0200954 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100955 switch (widget->id) {
956 case snd_soc_dapm_dac:
957 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +0100958 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +0100959 if (widget->active) {
960 widget->inputs = snd_soc_dapm_suspend_check(widget);
961 return widget->inputs;
962 }
Mark Brown010ff262009-08-17 17:39:22 +0100963 default:
964 break;
965 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200966
967 if (widget->connected) {
968 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100969 if (widget->id == snd_soc_dapm_input && !widget->ext) {
970 widget->inputs = snd_soc_dapm_suspend_check(widget);
971 return widget->inputs;
972 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200973
974 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100975 if (widget->id == snd_soc_dapm_vmid) {
976 widget->inputs = snd_soc_dapm_suspend_check(widget);
977 return widget->inputs;
978 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200979
980 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300981 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100982 (widget->id == snd_soc_dapm_line &&
983 !list_empty(&widget->sinks))) {
984 widget->inputs = snd_soc_dapm_suspend_check(widget);
985 return widget->inputs;
986 }
987
Mark Brown1ab97c82011-11-27 16:21:51 +0000988 /* signal generator */
989 if (widget->id == snd_soc_dapm_siggen) {
990 widget->inputs = snd_soc_dapm_suspend_check(widget);
991 return widget->inputs;
992 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200993 }
994
995 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +0100996 DAPM_UPDATE_STAT(widget, neighbour_checks);
997
Mark Brownbf3a9e12011-06-13 16:42:29 +0100998 if (path->weak)
999 continue;
1000
Mark Brown8af294b2013-02-22 17:48:15 +00001001 if (path->walking)
1002 return 1;
1003
Richard Purdie2b97eab2006-10-06 18:32:18 +02001004 if (path->walked)
1005 continue;
1006
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001007 trace_snd_soc_dapm_input_path(widget, path);
1008
Richard Purdie2b97eab2006-10-06 18:32:18 +02001009 if (path->source && path->connect) {
1010 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001011 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001012
1013 /* do we need to add this widget to the list ? */
1014 if (list) {
1015 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001016 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001017 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001018 dev_err(widget->dapm->dev,
1019 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001020 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001021 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001022 return con;
1023 }
1024 }
1025
1026 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001027
1028 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001029 }
1030 }
1031
Mark Brown024dc072011-10-09 11:52:05 +01001032 widget->inputs = con;
1033
Richard Purdie2b97eab2006-10-06 18:32:18 +02001034 return con;
1035}
1036
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001037/**
1038 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1039 * @dai: the soc DAI.
1040 * @stream: stream direction.
1041 * @list: list of active widgets for this stream.
1042 *
1043 * Queries DAPM graph as to whether an valid audio stream path exists for
1044 * the initial stream specified by name. This takes into account
1045 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1046 *
1047 * Returns the number of valid paths or negative error.
1048 */
1049int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1050 struct snd_soc_dapm_widget_list **list)
1051{
1052 struct snd_soc_card *card = dai->card;
1053 int paths;
1054
1055 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1056 dapm_reset(card);
1057
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001058 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001059 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001060 dapm_clear_walk_output(&card->dapm,
1061 &dai->playback_widget->sinks);
1062 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001063 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001064 dapm_clear_walk_input(&card->dapm,
1065 &dai->capture_widget->sources);
1066 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001067
1068 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001069 mutex_unlock(&card->dapm_mutex);
1070
1071 return paths;
1072}
1073
Richard Purdie2b97eab2006-10-06 18:32:18 +02001074/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001075 * Handler for generic register modifier widget.
1076 */
1077int dapm_reg_event(struct snd_soc_dapm_widget *w,
1078 struct snd_kcontrol *kcontrol, int event)
1079{
1080 unsigned int val;
1081
1082 if (SND_SOC_DAPM_EVENT_ON(event))
1083 val = w->on_val;
1084 else
1085 val = w->off_val;
1086
Liam Girdwood49575fb52012-03-06 18:16:19 +00001087 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001088 w->mask << w->shift, val << w->shift);
1089
1090 return 0;
1091}
Mark Brown11589412008-07-29 11:42:23 +01001092EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001093
Mark Brown62ea8742012-01-21 21:14:48 +00001094/*
1095 * Handler for regulator supply widget.
1096 */
1097int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1098 struct snd_kcontrol *kcontrol, int event)
1099{
Mark Brownc05b84d2012-09-07 12:57:11 +08001100 int ret;
1101
1102 if (SND_SOC_DAPM_EVENT_ON(event)) {
1103 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001104 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001105 if (ret != 0)
1106 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001107 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001108 w->name, ret);
1109 }
1110
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001111 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001112 } else {
1113 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001114 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001115 if (ret != 0)
1116 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001117 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001118 w->name, ret);
1119 }
1120
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001121 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001122 }
Mark Brown62ea8742012-01-21 21:14:48 +00001123}
1124EXPORT_SYMBOL_GPL(dapm_regulator_event);
1125
Ola Liljad7e7eb92012-05-24 15:26:25 +02001126/*
1127 * Handler for clock supply widget.
1128 */
1129int dapm_clock_event(struct snd_soc_dapm_widget *w,
1130 struct snd_kcontrol *kcontrol, int event)
1131{
1132 if (!w->clk)
1133 return -EIO;
1134
Mark Brownec029952012-06-04 08:16:20 +01001135#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001136 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001137 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001138 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001139 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001140 return 0;
1141 }
Mark Brownec029952012-06-04 08:16:20 +01001142#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001143 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001144}
1145EXPORT_SYMBOL_GPL(dapm_clock_event);
1146
Mark Brownd8050022011-09-28 18:28:23 +01001147static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1148{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001149 if (w->power_checked)
1150 return w->new_power;
1151
Mark Brownd8050022011-09-28 18:28:23 +01001152 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001153 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001154 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001155 w->new_power = w->power_check(w);
1156
1157 w->power_checked = true;
1158
1159 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001160}
1161
Mark Browncd0f2d42009-04-20 16:56:59 +01001162/* Generic check to see if a widget should be powered.
1163 */
1164static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1165{
1166 int in, out;
1167
Mark Brownde02d072011-09-20 21:43:24 +01001168 DAPM_UPDATE_STAT(w, power_checks);
1169
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001170 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001171 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001172 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001173 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001174 return out != 0 && in != 0;
1175}
1176
Mark Brown6ea31b92009-04-20 17:15:41 +01001177/* Check to see if an ADC has power */
1178static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1179{
1180 int in;
1181
Mark Brownde02d072011-09-20 21:43:24 +01001182 DAPM_UPDATE_STAT(w, power_checks);
1183
Mark Brown6ea31b92009-04-20 17:15:41 +01001184 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001185 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001186 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001187 return in != 0;
1188 } else {
1189 return dapm_generic_check_power(w);
1190 }
1191}
1192
1193/* Check to see if a DAC has power */
1194static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1195{
1196 int out;
1197
Mark Brownde02d072011-09-20 21:43:24 +01001198 DAPM_UPDATE_STAT(w, power_checks);
1199
Mark Brown6ea31b92009-04-20 17:15:41 +01001200 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001201 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001202 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001203 return out != 0;
1204 } else {
1205 return dapm_generic_check_power(w);
1206 }
1207}
1208
Mark Brown246d0a12009-04-22 18:24:55 +01001209/* Check to see if a power supply is needed */
1210static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1211{
1212 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001213
Mark Brownde02d072011-09-20 21:43:24 +01001214 DAPM_UPDATE_STAT(w, power_checks);
1215
Mark Brown246d0a12009-04-22 18:24:55 +01001216 /* Check if one of our outputs is connected */
1217 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001218 DAPM_UPDATE_STAT(w, neighbour_checks);
1219
Mark Brownbf3a9e12011-06-13 16:42:29 +01001220 if (path->weak)
1221 continue;
1222
Mark Brown215edda2009-09-08 18:59:05 +01001223 if (path->connected &&
1224 !path->connected(path->source, path->sink))
1225 continue;
1226
Mark Brown30173582011-02-11 11:42:19 +00001227 if (!path->sink)
1228 continue;
1229
Mark Brownf68d7e12011-10-04 22:57:50 +01001230 if (dapm_widget_power_check(path->sink))
1231 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001232 }
1233
Mark Brownf68d7e12011-10-04 22:57:50 +01001234 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001235}
1236
Mark Brown35c64bc2011-09-28 18:23:53 +01001237static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1238{
1239 return 1;
1240}
1241
Mark Brown38357ab2009-06-06 19:03:23 +01001242static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1243 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001244 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001245{
Mark Brown828a8422011-01-15 13:14:30 +00001246 int *sort;
1247
1248 if (power_up)
1249 sort = dapm_up_seq;
1250 else
1251 sort = dapm_down_seq;
1252
Mark Brown38357ab2009-06-06 19:03:23 +01001253 if (sort[a->id] != sort[b->id])
1254 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001255 if (a->subseq != b->subseq) {
1256 if (power_up)
1257 return a->subseq - b->subseq;
1258 else
1259 return b->subseq - a->subseq;
1260 }
Mark Brownb22ead22009-06-07 12:51:26 +01001261 if (a->reg != b->reg)
1262 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001263 if (a->dapm != b->dapm)
1264 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001265
Mark Brown38357ab2009-06-06 19:03:23 +01001266 return 0;
1267}
Mark Brown42aa3412009-03-01 19:21:10 +00001268
Mark Brown38357ab2009-06-06 19:03:23 +01001269/* Insert a widget in order into a DAPM power sequence. */
1270static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1271 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001272 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001273{
1274 struct snd_soc_dapm_widget *w;
1275
1276 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001277 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001278 list_add_tail(&new_widget->power_list, &w->power_list);
1279 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001280 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001281
Mark Brown38357ab2009-06-06 19:03:23 +01001282 list_add_tail(&new_widget->power_list, list);
1283}
Mark Brown42aa3412009-03-01 19:21:10 +00001284
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001285static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001286 struct snd_soc_dapm_widget *w, int event)
1287{
Mark Brown68f89ad2010-11-03 23:51:49 -04001288 const char *ev_name;
1289 int power, ret;
1290
1291 switch (event) {
1292 case SND_SOC_DAPM_PRE_PMU:
1293 ev_name = "PRE_PMU";
1294 power = 1;
1295 break;
1296 case SND_SOC_DAPM_POST_PMU:
1297 ev_name = "POST_PMU";
1298 power = 1;
1299 break;
1300 case SND_SOC_DAPM_PRE_PMD:
1301 ev_name = "PRE_PMD";
1302 power = 0;
1303 break;
1304 case SND_SOC_DAPM_POST_PMD:
1305 ev_name = "POST_PMD";
1306 power = 0;
1307 break;
Mark Brown80114122013-02-25 15:14:19 +00001308 case SND_SOC_DAPM_WILL_PMU:
1309 ev_name = "WILL_PMU";
1310 power = 1;
1311 break;
1312 case SND_SOC_DAPM_WILL_PMD:
1313 ev_name = "WILL_PMD";
1314 power = 0;
1315 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001316 default:
1317 BUG();
1318 return;
1319 }
1320
1321 if (w->power != power)
1322 return;
1323
1324 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001325 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001326 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001327 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001328 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001329 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001330 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001331 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001332 ev_name, w->name, ret);
1333 }
1334}
1335
Mark Brownb22ead22009-06-07 12:51:26 +01001336/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001337static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001338 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001339{
Mark Brown68f89ad2010-11-03 23:51:49 -04001340 struct snd_soc_dapm_widget *w;
1341 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001342 unsigned int value = 0;
1343 unsigned int mask = 0;
1344 unsigned int cur_mask;
1345
1346 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1347 power_list)->reg;
1348
1349 list_for_each_entry(w, pending, power_list) {
1350 cur_mask = 1 << w->shift;
1351 BUG_ON(reg != w->reg);
1352
1353 if (w->invert)
1354 power = !w->power;
1355 else
1356 power = w->power;
1357
1358 mask |= cur_mask;
1359 if (power)
1360 value |= cur_mask;
1361
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001362 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001363 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1364 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001365
Mark Brown68f89ad2010-11-03 23:51:49 -04001366 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001367 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1368 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001369 }
1370
Mark Brown81628102009-06-07 13:21:24 +01001371 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001372 /* Any widget will do, they should all be updating the
1373 * same register.
1374 */
1375 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1376 power_list);
1377
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001378 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001379 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001380 value, mask, reg, card->pop_time);
1381 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001382 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001383 }
1384
1385 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001386 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1387 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001388 }
Mark Brown42aa3412009-03-01 19:21:10 +00001389}
1390
Mark Brownb22ead22009-06-07 12:51:26 +01001391/* Apply a DAPM power sequence.
1392 *
1393 * We walk over a pre-sorted list of widgets to apply power to. In
1394 * order to minimise the number of writes to the device required
1395 * multiple widgets will be updated in a single write where possible.
1396 * Currently anything that requires more than a single write is not
1397 * handled.
1398 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001399static void dapm_seq_run(struct snd_soc_card *card,
1400 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001401{
1402 struct snd_soc_dapm_widget *w, *n;
1403 LIST_HEAD(pending);
1404 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001405 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001406 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001407 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001408 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001409 int *sort;
1410
1411 if (power_up)
1412 sort = dapm_up_seq;
1413 else
1414 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001415
Mark Brownb22ead22009-06-07 12:51:26 +01001416 list_for_each_entry_safe(w, n, list, power_list) {
1417 ret = 0;
1418
1419 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001420 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001421 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001422 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001423 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001424
Mark Brown474b62d2011-01-18 16:14:44 +00001425 if (cur_dapm && cur_dapm->seq_notifier) {
1426 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1427 if (sort[i] == cur_sort)
1428 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001429 i,
1430 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001431 }
1432
Mark Brownb22ead22009-06-07 12:51:26 +01001433 INIT_LIST_HEAD(&pending);
1434 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001435 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001436 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001437 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001438 }
1439
Mark Brown163cac02009-06-07 10:12:52 +01001440 switch (w->id) {
1441 case snd_soc_dapm_pre:
1442 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001443 list_for_each_entry_safe_continue(w, n, list,
1444 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001445
Mark Brownb22ead22009-06-07 12:51:26 +01001446 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001447 ret = w->event(w,
1448 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001449 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001450 ret = w->event(w,
1451 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001452 break;
1453
1454 case snd_soc_dapm_post:
1455 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001456 list_for_each_entry_safe_continue(w, n, list,
1457 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001458
Mark Brownb22ead22009-06-07 12:51:26 +01001459 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001460 ret = w->event(w,
1461 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001462 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001463 ret = w->event(w,
1464 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001465 break;
1466
Mark Brown163cac02009-06-07 10:12:52 +01001467 default:
Mark Brown81628102009-06-07 13:21:24 +01001468 /* Queue it up for application */
1469 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001470 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001471 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001472 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001473 list_move(&w->power_list, &pending);
1474 break;
Mark Brown163cac02009-06-07 10:12:52 +01001475 }
Mark Brownb22ead22009-06-07 12:51:26 +01001476
1477 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001478 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001479 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001480 }
Mark Brownb22ead22009-06-07 12:51:26 +01001481
1482 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001483 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001484
1485 if (cur_dapm && cur_dapm->seq_notifier) {
1486 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1487 if (sort[i] == cur_sort)
1488 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001489 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001490 }
Mark Brown163cac02009-06-07 10:12:52 +01001491}
1492
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001493static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001494{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001495 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001496 struct snd_soc_dapm_widget_list *wlist;
1497 struct snd_soc_dapm_widget *w = NULL;
1498 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001499 int ret;
1500
1501 if (!update)
1502 return;
1503
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001504 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001505
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001506 for (wi = 0; wi < wlist->num_widgets; wi++) {
1507 w = wlist->widgets[wi];
1508
1509 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1510 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1511 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001512 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001513 w->name, ret);
1514 }
Mark Brown97404f22010-12-14 16:13:57 +00001515 }
1516
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001517 if (!w)
1518 return;
1519
Liam Girdwood49575fb52012-03-06 18:16:19 +00001520 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001521 update->val);
1522 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001523 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001524 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001525
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001526 for (wi = 0; wi < wlist->num_widgets; wi++) {
1527 w = wlist->widgets[wi];
1528
1529 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1530 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1531 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001532 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001533 w->name, ret);
1534 }
Mark Brown97404f22010-12-14 16:13:57 +00001535 }
1536}
1537
Mark Brown9d0624a2011-02-18 11:49:43 -08001538/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1539 * they're changing state.
1540 */
1541static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1542{
1543 struct snd_soc_dapm_context *d = data;
1544 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001545
Mark Brown56fba412011-06-04 11:25:10 +01001546 /* If we're off and we're not supposed to be go into STANDBY */
1547 if (d->bias_level == SND_SOC_BIAS_OFF &&
1548 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001549 if (d->dev)
1550 pm_runtime_get_sync(d->dev);
1551
Mark Brown9d0624a2011-02-18 11:49:43 -08001552 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1553 if (ret != 0)
1554 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001555 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001556 }
1557
Mark Brown56fba412011-06-04 11:25:10 +01001558 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1559 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001560 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1561 if (ret != 0)
1562 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001563 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001564 }
1565}
1566
1567/* Async callback run prior to DAPM sequences - brings to their final
1568 * state.
1569 */
1570static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1571{
1572 struct snd_soc_dapm_context *d = data;
1573 int ret;
1574
1575 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001576 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1577 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1578 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001579 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1580 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001581 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001582 ret);
1583 }
1584
1585 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001586 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1587 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001588 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1589 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001590 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1591 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001592
1593 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001594 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001595 }
1596
1597 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001598 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1599 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001600 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1601 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001602 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001603 ret);
1604 }
1605}
Mark Brown97404f22010-12-14 16:13:57 +00001606
Mark Brownfe4fda52011-10-03 22:36:57 +01001607static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1608 bool power, bool connect)
1609{
1610 /* If a connection is being made or broken then that update
1611 * will have marked the peer dirty, otherwise the widgets are
1612 * not connected and this update has no impact. */
1613 if (!connect)
1614 return;
1615
1616 /* If the peer is already in the state we're moving to then we
1617 * won't have an impact on it. */
1618 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001619 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001620}
1621
Mark Brown05623c42011-09-28 17:02:31 +01001622static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1623 struct list_head *up_list,
1624 struct list_head *down_list)
1625{
Mark Browndb432b42011-10-03 21:06:40 +01001626 struct snd_soc_dapm_path *path;
1627
Mark Brown05623c42011-09-28 17:02:31 +01001628 if (w->power == power)
1629 return;
1630
1631 trace_snd_soc_dapm_widget_power(w, power);
1632
Mark Browndb432b42011-10-03 21:06:40 +01001633 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001634 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001635 */
1636 list_for_each_entry(path, &w->sources, list_sink) {
1637 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001638 dapm_widget_set_peer_power(path->source, power,
1639 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001640 }
1641 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001642 switch (w->id) {
1643 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001644 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001645 case snd_soc_dapm_clock_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001646 /* Supplies can't affect their outputs, only their inputs */
1647 break;
1648 default:
1649 list_for_each_entry(path, &w->sinks, list_source) {
1650 if (path->sink) {
1651 dapm_widget_set_peer_power(path->sink, power,
1652 path->connect);
1653 }
Mark Browndb432b42011-10-03 21:06:40 +01001654 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001655 break;
Mark Browndb432b42011-10-03 21:06:40 +01001656 }
1657
Mark Brown05623c42011-09-28 17:02:31 +01001658 if (power)
1659 dapm_seq_insert(w, up_list, true);
1660 else
1661 dapm_seq_insert(w, down_list, false);
1662
1663 w->power = power;
1664}
1665
Mark Brown7c81beb2011-09-20 22:22:32 +01001666static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1667 struct list_head *up_list,
1668 struct list_head *down_list)
1669{
Mark Brown7c81beb2011-09-20 22:22:32 +01001670 int power;
1671
1672 switch (w->id) {
1673 case snd_soc_dapm_pre:
1674 dapm_seq_insert(w, down_list, false);
1675 break;
1676 case snd_soc_dapm_post:
1677 dapm_seq_insert(w, up_list, true);
1678 break;
1679
1680 default:
Mark Brownd8050022011-09-28 18:28:23 +01001681 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001682
Mark Brown05623c42011-09-28 17:02:31 +01001683 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001684 break;
1685 }
1686}
1687
Mark Brown42aa3412009-03-01 19:21:10 +00001688/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001689 * Scan each dapm widget for complete audio path.
1690 * A complete path is a route that has valid endpoints i.e.:-
1691 *
1692 * o DAC to output pin.
1693 * o Input Pin to ADC.
1694 * o Input pin to Output pin (bypass, sidetone)
1695 * o DAC to ADC (loopback).
1696 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001697static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001698{
1699 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001700 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001701 LIST_HEAD(up_list);
1702 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001703 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001704 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001705
Mark Brown84e90932010-11-04 00:07:02 -04001706 trace_snd_soc_dapm_start(card);
1707
Mark Brown56fba412011-06-04 11:25:10 +01001708 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001709 if (d->idle_bias_off)
1710 d->target_bias_level = SND_SOC_BIAS_OFF;
1711 else
1712 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001713 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001714
Liam Girdwood6c120e12012-02-15 15:15:34 +00001715 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001716
Mark Brown6d3ddc82009-05-16 17:47:29 +01001717 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001718 * lists indicating if they should be powered up or down. We
1719 * only check widgets that have been flagged as dirty but note
1720 * that new widgets may be added to the dirty list while we
1721 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001722 */
Mark Browndb432b42011-10-03 21:06:40 +01001723 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001724 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001725 }
1726
Mark Brownf9de6d72011-09-28 17:19:47 +01001727 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001728 switch (w->id) {
1729 case snd_soc_dapm_pre:
1730 case snd_soc_dapm_post:
1731 /* These widgets always need to be powered */
1732 break;
1733 default:
1734 list_del_init(&w->dirty);
1735 break;
1736 }
Mark Browndb432b42011-10-03 21:06:40 +01001737
Mark Brownf9de6d72011-09-28 17:19:47 +01001738 if (w->power) {
1739 d = w->dapm;
1740
1741 /* Supplies and micbiases only bring the
1742 * context up to STANDBY as unless something
1743 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001744 * generally don't require full power. Signal
1745 * generators are virtual pins and have no
1746 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001747 */
1748 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001749 case snd_soc_dapm_siggen:
1750 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001751 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001752 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001753 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001754 case snd_soc_dapm_micbias:
1755 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1756 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1757 break;
1758 default:
1759 d->target_bias_level = SND_SOC_BIAS_ON;
1760 break;
1761 }
1762 }
1763
1764 }
1765
Mark Brown85a843c2011-09-21 21:29:47 +01001766 /* Force all contexts in the card to the same bias state if
1767 * they're not ground referenced.
1768 */
Mark Brown56fba412011-06-04 11:25:10 +01001769 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001770 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001771 if (d->target_bias_level > bias)
1772 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001773 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001774 if (!d->idle_bias_off)
1775 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001776
Mark Brownde02d072011-09-20 21:43:24 +01001777 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001778
Mark Brown9d0624a2011-02-18 11:49:43 -08001779 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001780 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001781 async_schedule_domain(dapm_pre_sequence_async, d,
1782 &async_domain);
1783 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001784
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001785 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001786 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001787 }
1788
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001789 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001790 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001791 }
1792
Mark Brown6d3ddc82009-05-16 17:47:29 +01001793 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001794 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001795
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001796 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001797
Mark Brown6d3ddc82009-05-16 17:47:29 +01001798 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001799 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001800
Mark Brown9d0624a2011-02-18 11:49:43 -08001801 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001802 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001803 async_schedule_domain(dapm_post_sequence_async, d,
1804 &async_domain);
1805 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001806
Liam Girdwood8078d872012-02-15 15:15:35 +00001807 /* do we need to notify any clients that DAPM event is complete */
1808 list_for_each_entry(d, &card->dapm_list, list) {
1809 if (d->stream_event)
1810 d->stream_event(d, event);
1811 }
1812
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001813 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001814 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001815 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001816
Mark Brown84e90932010-11-04 00:07:02 -04001817 trace_snd_soc_dapm_done(card);
1818
Mark Brown42aa3412009-03-01 19:21:10 +00001819 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001820}
1821
Mark Brown79fb9382009-08-21 16:38:13 +01001822#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001823static ssize_t dapm_widget_power_read_file(struct file *file,
1824 char __user *user_buf,
1825 size_t count, loff_t *ppos)
1826{
1827 struct snd_soc_dapm_widget *w = file->private_data;
1828 char *buf;
1829 int in, out;
1830 ssize_t ret;
1831 struct snd_soc_dapm_path *p = NULL;
1832
1833 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1834 if (!buf)
1835 return -ENOMEM;
1836
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001837 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001838 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001839 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001840 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001841
Mark Brownf13ebad2012-03-03 18:01:01 +00001842 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1843 w->name, w->power ? "On" : "Off",
1844 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001845
Mark Brownd033c362009-12-04 15:25:56 +00001846 if (w->reg >= 0)
1847 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1848 " - R%d(0x%x) bit %d",
1849 w->reg, w->reg, w->shift);
1850
1851 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1852
Mark Brown3eef08b2009-09-14 16:49:00 +01001853 if (w->sname)
1854 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1855 w->sname,
1856 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001857
1858 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001859 if (p->connected && !p->connected(w, p->sink))
1860 continue;
1861
Mark Brown79fb9382009-08-21 16:38:13 +01001862 if (p->connect)
1863 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001864 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001865 p->name ? p->name : "static",
1866 p->source->name);
1867 }
1868 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001869 if (p->connected && !p->connected(w, p->sink))
1870 continue;
1871
Mark Brown79fb9382009-08-21 16:38:13 +01001872 if (p->connect)
1873 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001874 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001875 p->name ? p->name : "static",
1876 p->sink->name);
1877 }
1878
1879 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1880
1881 kfree(buf);
1882 return ret;
1883}
1884
1885static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001886 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001887 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001888 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001889};
1890
Mark Brownef49e4f2011-04-04 20:48:13 +09001891static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1892 size_t count, loff_t *ppos)
1893{
1894 struct snd_soc_dapm_context *dapm = file->private_data;
1895 char *level;
1896
1897 switch (dapm->bias_level) {
1898 case SND_SOC_BIAS_ON:
1899 level = "On\n";
1900 break;
1901 case SND_SOC_BIAS_PREPARE:
1902 level = "Prepare\n";
1903 break;
1904 case SND_SOC_BIAS_STANDBY:
1905 level = "Standby\n";
1906 break;
1907 case SND_SOC_BIAS_OFF:
1908 level = "Off\n";
1909 break;
1910 default:
1911 BUG();
1912 level = "Unknown\n";
1913 break;
1914 }
1915
1916 return simple_read_from_buffer(user_buf, count, ppos, level,
1917 strlen(level));
1918}
1919
1920static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001921 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001922 .read = dapm_bias_read_file,
1923 .llseek = default_llseek,
1924};
1925
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001926void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1927 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001928{
Mark Brown79fb9382009-08-21 16:38:13 +01001929 struct dentry *d;
1930
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001931 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1932
1933 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001934 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001935 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001936 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001937 }
Mark Brown79fb9382009-08-21 16:38:13 +01001938
Mark Brownef49e4f2011-04-04 20:48:13 +09001939 d = debugfs_create_file("bias_level", 0444,
1940 dapm->debugfs_dapm, dapm,
1941 &dapm_bias_fops);
1942 if (!d)
1943 dev_warn(dapm->dev,
1944 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001945}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001946
1947static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1948{
1949 struct snd_soc_dapm_context *dapm = w->dapm;
1950 struct dentry *d;
1951
1952 if (!dapm->debugfs_dapm || !w->name)
1953 return;
1954
1955 d = debugfs_create_file(w->name, 0444,
1956 dapm->debugfs_dapm, w,
1957 &dapm_widget_power_fops);
1958 if (!d)
1959 dev_warn(w->dapm->dev,
1960 "ASoC: Failed to create %s debugfs file\n",
1961 w->name);
1962}
1963
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001964static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1965{
1966 debugfs_remove_recursive(dapm->debugfs_dapm);
1967}
1968
Mark Brown79fb9382009-08-21 16:38:13 +01001969#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001970void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1971 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001972{
1973}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001974
1975static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1976{
1977}
1978
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001979static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1980{
1981}
1982
Mark Brown79fb9382009-08-21 16:38:13 +01001983#endif
1984
Richard Purdie2b97eab2006-10-06 18:32:18 +02001985/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001986static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001987 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001988{
1989 struct snd_soc_dapm_path *path;
1990 int found = 0;
1991
Richard Purdie2b97eab2006-10-06 18:32:18 +02001992 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001993 list_for_each_entry(path, &card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001994 if (path->kcontrol != kcontrol)
1995 continue;
1996
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001997 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001998 continue;
1999
2000 found = 1;
2001 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002002 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002003 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002004 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002005 } else {
2006 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002007 dapm_mark_dirty(path->source,
2008 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002009 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002010 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002011 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002012 }
2013
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002014 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002015 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002016
Liam Girdwood618dae12012-04-25 12:12:51 +01002017 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002018}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002019
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002020int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002021 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2022 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002023{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002024 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002025 int ret;
2026
Liam Girdwood3cd04342012-03-09 12:02:08 +00002027 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002028 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002029 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002030 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002031 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002032 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002033 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002034 return ret;
2035}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002036EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002037
Milan plzik1b075e32008-01-10 14:39:46 +01002038/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002039static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002040 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002041{
2042 struct snd_soc_dapm_path *path;
2043 int found = 0;
2044
Richard Purdie2b97eab2006-10-06 18:32:18 +02002045 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002046 list_for_each_entry(path, &card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002047 if (path->kcontrol != kcontrol)
2048 continue;
2049
2050 /* found, now check type */
2051 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002052 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002053 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002054 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002055 }
2056
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002057 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002058 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002059
Liam Girdwood618dae12012-04-25 12:12:51 +01002060 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002061}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002062
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002063int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002064 struct snd_kcontrol *kcontrol, int connect,
2065 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002066{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002067 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002068 int ret;
2069
Liam Girdwood3cd04342012-03-09 12:02:08 +00002070 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002071 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002072 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002073 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002074 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002075 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002076 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002077 return ret;
2078}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002079EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002080
2081/* show dapm widget status in sys fs */
2082static ssize_t dapm_widget_show(struct device *dev,
2083 struct device_attribute *attr, char *buf)
2084{
Mark Brown36ae1a92012-01-06 17:12:45 -08002085 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002086 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002087 struct snd_soc_dapm_widget *w;
2088 int count = 0;
2089 char *state = "not set";
2090
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002091 list_for_each_entry(w, &codec->card->widgets, list) {
2092 if (w->dapm != &codec->dapm)
2093 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002094
2095 /* only display widgets that burnm power */
2096 switch (w->id) {
2097 case snd_soc_dapm_hp:
2098 case snd_soc_dapm_mic:
2099 case snd_soc_dapm_spk:
2100 case snd_soc_dapm_line:
2101 case snd_soc_dapm_micbias:
2102 case snd_soc_dapm_dac:
2103 case snd_soc_dapm_adc:
2104 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002105 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002106 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002107 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002108 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002109 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002110 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002111 if (w->name)
2112 count += sprintf(buf + count, "%s: %s\n",
2113 w->name, w->power ? "On":"Off");
2114 break;
2115 default:
2116 break;
2117 }
2118 }
2119
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002120 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002121 case SND_SOC_BIAS_ON:
2122 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002123 break;
Mark Brown0be98982008-05-19 12:31:28 +02002124 case SND_SOC_BIAS_PREPARE:
2125 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002126 break;
Mark Brown0be98982008-05-19 12:31:28 +02002127 case SND_SOC_BIAS_STANDBY:
2128 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002129 break;
Mark Brown0be98982008-05-19 12:31:28 +02002130 case SND_SOC_BIAS_OFF:
2131 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002132 break;
2133 }
2134 count += sprintf(buf + count, "PM State: %s\n", state);
2135
2136 return count;
2137}
2138
2139static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2140
2141int snd_soc_dapm_sys_add(struct device *dev)
2142{
Troy Kisky12ef1932008-10-13 17:42:14 -07002143 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002144}
2145
2146static void snd_soc_dapm_sys_remove(struct device *dev)
2147{
Mark Brownaef90842009-05-16 17:53:16 +01002148 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002149}
2150
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002151static void dapm_free_path(struct snd_soc_dapm_path *path)
2152{
2153 list_del(&path->list_sink);
2154 list_del(&path->list_source);
2155 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002156 kfree(path);
2157}
2158
Richard Purdie2b97eab2006-10-06 18:32:18 +02002159/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002160static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002161{
2162 struct snd_soc_dapm_widget *w, *next_w;
2163 struct snd_soc_dapm_path *p, *next_p;
2164
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002165 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2166 if (w->dapm != dapm)
2167 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002168 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002169 /*
2170 * remove source and sink paths associated to this widget.
2171 * While removing the path, remove reference to it from both
2172 * source and sink widgets so that path is removed only once.
2173 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002174 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2175 dapm_free_path(p);
2176
2177 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2178 dapm_free_path(p);
2179
Stephen Warrenfad59882011-04-28 17:37:59 -06002180 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002181 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002182 kfree(w);
2183 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002184}
2185
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002186static struct snd_soc_dapm_widget *dapm_find_widget(
2187 struct snd_soc_dapm_context *dapm, const char *pin,
2188 bool search_other_contexts)
2189{
2190 struct snd_soc_dapm_widget *w;
2191 struct snd_soc_dapm_widget *fallback = NULL;
2192
2193 list_for_each_entry(w, &dapm->card->widgets, list) {
2194 if (!strcmp(w->name, pin)) {
2195 if (w->dapm == dapm)
2196 return w;
2197 else
2198 fallback = w;
2199 }
2200 }
2201
2202 if (search_other_contexts)
2203 return fallback;
2204
2205 return NULL;
2206}
2207
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002208static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002209 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002210{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002211 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002212
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002213 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002214 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002215 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002216 }
2217
Mark Brown1a8b2d92012-02-16 11:50:07 -08002218 if (w->connected != status)
2219 dapm_mark_dirty(w, "pin configuration");
2220
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002221 w->connected = status;
2222 if (status == 0)
2223 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002224
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002225 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002226}
2227
Richard Purdie2b97eab2006-10-06 18:32:18 +02002228/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002229 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002230 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002231 *
2232 * Walks all dapm audio paths and powers widgets according to their
2233 * stream or path usage.
2234 *
2235 * Returns 0 for success.
2236 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002237int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002238{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002239 int ret;
2240
Mark Brown4f4c0072011-10-07 14:29:19 +01002241 /*
2242 * Suppress early reports (eg, jacks syncing their state) to avoid
2243 * silly DAPM runs during card startup.
2244 */
2245 if (!dapm->card || !dapm->card->instantiated)
2246 return 0;
2247
Liam Girdwood3cd04342012-03-09 12:02:08 +00002248 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002249 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002250 mutex_unlock(&dapm->card->dapm_mutex);
2251 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002252}
Liam Girdwooda5302182008-07-07 13:35:17 +01002253EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002254
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002255static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002256 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002257{
2258 struct snd_soc_dapm_path *path;
2259 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002260 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002261 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002262 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002263 const char *source;
2264 char prefixed_sink[80];
2265 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002266 int ret = 0;
2267
Mark Brown88e8b9a2011-03-02 18:18:24 +00002268 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002269 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2270 dapm->codec->name_prefix, route->sink);
2271 sink = prefixed_sink;
2272 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2273 dapm->codec->name_prefix, route->source);
2274 source = prefixed_source;
2275 } else {
2276 sink = route->sink;
2277 source = route->source;
2278 }
2279
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002280 /*
2281 * find src and dest widgets over all widgets but favor a widget from
2282 * current DAPM context
2283 */
2284 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002285 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002286 wtsink = w;
2287 if (w->dapm == dapm)
2288 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002289 continue;
2290 }
2291 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002292 wtsource = w;
2293 if (w->dapm == dapm)
2294 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002295 }
2296 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002297 /* use widget from another DAPM context if not found from this */
2298 if (!wsink)
2299 wsink = wtsink;
2300 if (!wsource)
2301 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002302
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002303 if (wsource == NULL) {
2304 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2305 route->source);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002306 return -ENODEV;
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002307 }
2308 if (wsink == NULL) {
2309 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2310 route->sink);
2311 return -ENODEV;
2312 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002313
2314 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2315 if (!path)
2316 return -ENOMEM;
2317
2318 path->source = wsource;
2319 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002320 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002321 INIT_LIST_HEAD(&path->list);
2322 INIT_LIST_HEAD(&path->list_source);
2323 INIT_LIST_HEAD(&path->list_sink);
2324
2325 /* check for external widgets */
2326 if (wsink->id == snd_soc_dapm_input) {
2327 if (wsource->id == snd_soc_dapm_micbias ||
2328 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002329 wsource->id == snd_soc_dapm_line ||
2330 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002331 wsink->ext = 1;
2332 }
2333 if (wsource->id == snd_soc_dapm_output) {
2334 if (wsink->id == snd_soc_dapm_spk ||
2335 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002336 wsink->id == snd_soc_dapm_line ||
2337 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002338 wsource->ext = 1;
2339 }
2340
2341 /* connect static paths */
2342 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002343 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002344 list_add(&path->list_sink, &wsink->sources);
2345 list_add(&path->list_source, &wsource->sinks);
2346 path->connect = 1;
2347 return 0;
2348 }
2349
2350 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002351 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002352 case snd_soc_dapm_adc:
2353 case snd_soc_dapm_dac:
2354 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002355 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002356 case snd_soc_dapm_input:
2357 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002358 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002359 case snd_soc_dapm_micbias:
2360 case snd_soc_dapm_vmid:
2361 case snd_soc_dapm_pre:
2362 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002363 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002364 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002365 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002366 case snd_soc_dapm_aif_in:
2367 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002368 case snd_soc_dapm_dai_in:
2369 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002370 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002371 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002372 list_add(&path->list_sink, &wsink->sources);
2373 list_add(&path->list_source, &wsource->sinks);
2374 path->connect = 1;
2375 return 0;
2376 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002377 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002378 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002379 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002380 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002381 if (ret != 0)
2382 goto err;
2383 break;
2384 case snd_soc_dapm_switch:
2385 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002386 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002387 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002388 if (ret != 0)
2389 goto err;
2390 break;
2391 case snd_soc_dapm_hp:
2392 case snd_soc_dapm_mic:
2393 case snd_soc_dapm_line:
2394 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002395 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002396 list_add(&path->list_sink, &wsink->sources);
2397 list_add(&path->list_source, &wsource->sinks);
2398 path->connect = 0;
2399 return 0;
2400 }
Mark Brownfabd0382012-07-05 17:20:06 +01002401
2402 dapm_mark_dirty(wsource, "Route added");
2403 dapm_mark_dirty(wsink, "Route added");
2404
Richard Purdie2b97eab2006-10-06 18:32:18 +02002405 return 0;
2406
2407err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002408 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002409 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002410 kfree(path);
2411 return ret;
2412}
Mark Brown105f1c22008-05-13 14:52:19 +02002413
Mark Brownefcc3c62012-07-05 17:24:19 +01002414static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2415 const struct snd_soc_dapm_route *route)
2416{
2417 struct snd_soc_dapm_path *path, *p;
2418 const char *sink;
2419 const char *source;
2420 char prefixed_sink[80];
2421 char prefixed_source[80];
2422
2423 if (route->control) {
2424 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002425 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002426 return -EINVAL;
2427 }
2428
2429 if (dapm->codec && dapm->codec->name_prefix) {
2430 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2431 dapm->codec->name_prefix, route->sink);
2432 sink = prefixed_sink;
2433 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2434 dapm->codec->name_prefix, route->source);
2435 source = prefixed_source;
2436 } else {
2437 sink = route->sink;
2438 source = route->source;
2439 }
2440
2441 path = NULL;
2442 list_for_each_entry(p, &dapm->card->paths, list) {
2443 if (strcmp(p->source->name, source) != 0)
2444 continue;
2445 if (strcmp(p->sink->name, sink) != 0)
2446 continue;
2447 path = p;
2448 break;
2449 }
2450
2451 if (path) {
2452 dapm_mark_dirty(path->source, "Route removed");
2453 dapm_mark_dirty(path->sink, "Route removed");
2454
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002455 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002456 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002457 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002458 source, sink);
2459 }
2460
2461 return 0;
2462}
2463
Mark Brown105f1c22008-05-13 14:52:19 +02002464/**
Mark Brown105f1c22008-05-13 14:52:19 +02002465 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002466 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002467 * @route: audio routes
2468 * @num: number of routes
2469 *
2470 * Connects 2 dapm widgets together via a named audio path. The sink is
2471 * the widget receiving the audio signal, whilst the source is the sender
2472 * of the audio signal.
2473 *
2474 * Returns 0 for success else error. On error all resources can be freed
2475 * with a call to snd_soc_card_free().
2476 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002477int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002478 const struct snd_soc_dapm_route *route, int num)
2479{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002480 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002481
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002482 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002483 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002484 r = snd_soc_dapm_add_route(dapm, route);
2485 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002486 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2487 route->source,
2488 route->control ? route->control : "direct",
2489 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002490 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002491 }
2492 route++;
2493 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002494 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002495
Dan Carpenter60884c22012-04-13 22:25:43 +03002496 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002497}
2498EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2499
Mark Brownefcc3c62012-07-05 17:24:19 +01002500/**
2501 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2502 * @dapm: DAPM context
2503 * @route: audio routes
2504 * @num: number of routes
2505 *
2506 * Removes routes from the DAPM context.
2507 */
2508int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2509 const struct snd_soc_dapm_route *route, int num)
2510{
2511 int i, ret = 0;
2512
2513 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2514 for (i = 0; i < num; i++) {
2515 snd_soc_dapm_del_route(dapm, route);
2516 route++;
2517 }
2518 mutex_unlock(&dapm->card->dapm_mutex);
2519
2520 return ret;
2521}
2522EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2523
Mark Brownbf3a9e12011-06-13 16:42:29 +01002524static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2525 const struct snd_soc_dapm_route *route)
2526{
2527 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2528 route->source,
2529 true);
2530 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2531 route->sink,
2532 true);
2533 struct snd_soc_dapm_path *path;
2534 int count = 0;
2535
2536 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002537 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002538 route->source);
2539 return -ENODEV;
2540 }
2541
2542 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002543 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002544 route->sink);
2545 return -ENODEV;
2546 }
2547
2548 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002549 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002550 route->source, route->sink);
2551
2552 list_for_each_entry(path, &source->sinks, list_source) {
2553 if (path->sink == sink) {
2554 path->weak = 1;
2555 count++;
2556 }
2557 }
2558
2559 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002560 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002561 route->source, route->sink);
2562 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002563 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002564 count, route->source, route->sink);
2565
2566 return 0;
2567}
2568
2569/**
2570 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2571 * @dapm: DAPM context
2572 * @route: audio routes
2573 * @num: number of routes
2574 *
2575 * Mark existing routes matching those specified in the passed array
2576 * as being weak, meaning that they are ignored for the purpose of
2577 * power decisions. The main intended use case is for sidetone paths
2578 * which couple audio between other independent paths if they are both
2579 * active in order to make the combination work better at the user
2580 * level but which aren't intended to be "used".
2581 *
2582 * Note that CODEC drivers should not use this as sidetone type paths
2583 * can frequently also be used as bypass paths.
2584 */
2585int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2586 const struct snd_soc_dapm_route *route, int num)
2587{
2588 int i, err;
2589 int ret = 0;
2590
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002591 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002592 for (i = 0; i < num; i++) {
2593 err = snd_soc_dapm_weak_route(dapm, route);
2594 if (err)
2595 ret = err;
2596 route++;
2597 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002598 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002599
2600 return ret;
2601}
2602EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2603
Mark Brown105f1c22008-05-13 14:52:19 +02002604/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002605 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002606 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002607 *
2608 * Checks the codec for any new dapm widgets and creates them if found.
2609 *
2610 * Returns 0 for success.
2611 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002612int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002613{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002614 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002615 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002616 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002617
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002618 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002619
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002620 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002621 {
2622 if (w->new)
2623 continue;
2624
Stephen Warrenfad59882011-04-28 17:37:59 -06002625 if (w->num_kcontrols) {
2626 w->kcontrols = kzalloc(w->num_kcontrols *
2627 sizeof(struct snd_kcontrol *),
2628 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002629 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002630 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002631 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002632 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002633 }
2634
Richard Purdie2b97eab2006-10-06 18:32:18 +02002635 switch(w->id) {
2636 case snd_soc_dapm_switch:
2637 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002638 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002639 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002640 break;
2641 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002642 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002643 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002644 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002645 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002646 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002647 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002648 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002649 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002650 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002651 break;
2652 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002653
2654 /* Read the initial power state from the device */
2655 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002656 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002657 val &= 1 << w->shift;
2658 if (w->invert)
2659 val = !val;
2660
2661 if (val)
2662 w->power = 1;
2663 }
2664
Richard Purdie2b97eab2006-10-06 18:32:18 +02002665 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002666
Mark Brown7508b122011-10-05 12:09:12 +01002667 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002668 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002669 }
2670
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002671 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2672 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002673 return 0;
2674}
2675EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2676
2677/**
2678 * snd_soc_dapm_get_volsw - dapm mixer get callback
2679 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002680 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002681 *
2682 * Callback to get the value of a dapm mixer control.
2683 *
2684 * Returns 0 for success.
2685 */
2686int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2687 struct snd_ctl_elem_value *ucontrol)
2688{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002689 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002690 struct soc_mixer_control *mc =
2691 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002692 unsigned int reg = mc->reg;
2693 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002694 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002695 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002696 unsigned int invert = mc->invert;
2697
2698 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002699 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002700 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002701 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002702
Richard Purdie2b97eab2006-10-06 18:32:18 +02002703 ucontrol->value.integer.value[0] =
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002704 (snd_soc_read(codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002705 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002706 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002707 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002708
2709 return 0;
2710}
2711EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2712
2713/**
2714 * snd_soc_dapm_put_volsw - dapm mixer set callback
2715 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002716 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002717 *
2718 * Callback to set the value of a dapm mixer control.
2719 *
2720 * Returns 0 for success.
2721 */
2722int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2723 struct snd_ctl_elem_value *ucontrol)
2724{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002725 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002726 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002727 struct soc_mixer_control *mc =
2728 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002729 unsigned int reg = mc->reg;
2730 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002731 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002732 unsigned int mask = (1 << fls(max)) - 1;
2733 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002734 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002735 int connect, change;
2736 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002737
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002738 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002739 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002740 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002741 kcontrol->id.name);
2742
Richard Purdie2b97eab2006-10-06 18:32:18 +02002743 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002744 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002745
2746 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002747 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002748 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002749 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002750
Liam Girdwood3cd04342012-03-09 12:02:08 +00002751 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002752
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002753 change = snd_soc_test_bits(codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002754 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002755 update.kcontrol = kcontrol;
2756 update.reg = reg;
2757 update.mask = mask;
2758 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002759
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002760 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002761
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002762 soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002763
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002764 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002765 }
2766
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002767 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002768 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002769}
2770EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2771
2772/**
2773 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2774 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002775 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002776 *
2777 * Callback to get the value of a dapm enumerated double mixer control.
2778 *
2779 * Returns 0 for success.
2780 */
2781int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2782 struct snd_ctl_elem_value *ucontrol)
2783{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002784 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002785 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002786 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002787
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002788 val = snd_soc_read(codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002789 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002790 if (e->shift_l != e->shift_r)
2791 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002792 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002793
2794 return 0;
2795}
2796EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2797
2798/**
2799 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2800 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002801 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002802 *
2803 * Callback to set the value of a dapm enumerated double mixer control.
2804 *
2805 * Returns 0 for success.
2806 */
2807int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2808 struct snd_ctl_elem_value *ucontrol)
2809{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002810 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002811 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002812 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002813 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002814 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002815 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002816
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002817 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002818 return -EINVAL;
2819 mux = ucontrol->value.enumerated.item[0];
2820 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002821 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002822 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002823 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002824 return -EINVAL;
2825 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002826 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002827 }
2828
Liam Girdwood3cd04342012-03-09 12:02:08 +00002829 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002830
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002831 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002832 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002833 update.kcontrol = kcontrol;
2834 update.reg = e->reg;
2835 update.mask = mask;
2836 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002837 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002838
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002839 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002840
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002841 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002842 }
2843
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002844 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002845 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002846}
2847EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2848
2849/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002850 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2851 * @kcontrol: mixer control
2852 * @ucontrol: control element information
2853 *
2854 * Returns 0 for success.
2855 */
2856int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2857 struct snd_ctl_elem_value *ucontrol)
2858{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002859 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
Mark Brownd2b247a2009-10-06 15:21:04 +01002860 return 0;
2861}
2862EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2863
2864/**
2865 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2866 * @kcontrol: mixer control
2867 * @ucontrol: control element information
2868 *
2869 * Returns 0 for success.
2870 */
2871int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2872 struct snd_ctl_elem_value *ucontrol)
2873{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002874 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002875 struct snd_soc_card *card = codec->card;
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002876 unsigned int value;
Mark Brownd2b247a2009-10-06 15:21:04 +01002877 struct soc_enum *e =
2878 (struct soc_enum *)kcontrol->private_value;
2879 int change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002880
2881 if (ucontrol->value.enumerated.item[0] >= e->max)
2882 return -EINVAL;
2883
Liam Girdwood3cd04342012-03-09 12:02:08 +00002884 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002885
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002886 value = ucontrol->value.enumerated.item[0];
2887 change = dapm_kcontrol_set_value(kcontrol, value);
2888 if (change)
2889 soc_dapm_mux_update_power(card, kcontrol, value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002890
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002891 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002892 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002893}
2894EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2895
2896/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002897 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2898 * callback
2899 * @kcontrol: mixer control
2900 * @ucontrol: control element information
2901 *
2902 * Callback to get the value of a dapm semi enumerated double mixer control.
2903 *
2904 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2905 * used for handling bitfield coded enumeration for example.
2906 *
2907 * Returns 0 for success.
2908 */
2909int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2910 struct snd_ctl_elem_value *ucontrol)
2911{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002912 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002913 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002914 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002915
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002916 reg_val = snd_soc_read(codec, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002917 val = (reg_val >> e->shift_l) & e->mask;
2918 for (mux = 0; mux < e->max; mux++) {
2919 if (val == e->values[mux])
2920 break;
2921 }
2922 ucontrol->value.enumerated.item[0] = mux;
2923 if (e->shift_l != e->shift_r) {
2924 val = (reg_val >> e->shift_r) & e->mask;
2925 for (mux = 0; mux < e->max; mux++) {
2926 if (val == e->values[mux])
2927 break;
2928 }
2929 ucontrol->value.enumerated.item[1] = mux;
2930 }
2931
2932 return 0;
2933}
2934EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2935
2936/**
2937 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2938 * callback
2939 * @kcontrol: mixer control
2940 * @ucontrol: control element information
2941 *
2942 * Callback to set the value of a dapm semi enumerated double mixer control.
2943 *
2944 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2945 * used for handling bitfield coded enumeration for example.
2946 *
2947 * Returns 0 for success.
2948 */
2949int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2950 struct snd_ctl_elem_value *ucontrol)
2951{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002952 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002953 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002954 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002955 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002956 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002957 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002958
2959 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2960 return -EINVAL;
2961 mux = ucontrol->value.enumerated.item[0];
2962 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2963 mask = e->mask << e->shift_l;
2964 if (e->shift_l != e->shift_r) {
2965 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2966 return -EINVAL;
2967 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2968 mask |= e->mask << e->shift_r;
2969 }
2970
Liam Girdwood3cd04342012-03-09 12:02:08 +00002971 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002972
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002973 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002974 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002975 update.kcontrol = kcontrol;
2976 update.reg = e->reg;
2977 update.mask = mask;
2978 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002979 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002980
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002981 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002982
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002983 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002984 }
2985
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002986 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002987 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002988}
2989EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2990
2991/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002992 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2993 *
2994 * @kcontrol: mixer control
2995 * @uinfo: control element information
2996 *
2997 * Callback to provide information about a pin switch control.
2998 */
2999int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3000 struct snd_ctl_elem_info *uinfo)
3001{
3002 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3003 uinfo->count = 1;
3004 uinfo->value.integer.min = 0;
3005 uinfo->value.integer.max = 1;
3006
3007 return 0;
3008}
3009EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3010
3011/**
3012 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3013 *
3014 * @kcontrol: mixer control
3015 * @ucontrol: Value
3016 */
3017int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3018 struct snd_ctl_elem_value *ucontrol)
3019{
Mark Brown48a8c392012-02-14 17:11:15 -08003020 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003021 const char *pin = (const char *)kcontrol->private_value;
3022
Liam Girdwood3cd04342012-03-09 12:02:08 +00003023 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003024
3025 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003026 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003027
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003028 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003029
3030 return 0;
3031}
3032EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3033
3034/**
3035 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3036 *
3037 * @kcontrol: mixer control
3038 * @ucontrol: Value
3039 */
3040int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3041 struct snd_ctl_elem_value *ucontrol)
3042{
Mark Brown48a8c392012-02-14 17:11:15 -08003043 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003044 const char *pin = (const char *)kcontrol->private_value;
3045
Liam Girdwood3cd04342012-03-09 12:02:08 +00003046 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003047
3048 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003049 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003050 else
Mark Brown48a8c392012-02-14 17:11:15 -08003051 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003052
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003053 mutex_unlock(&card->dapm_mutex);
3054
Mark Brown48a8c392012-02-14 17:11:15 -08003055 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003056 return 0;
3057}
3058EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3059
Mark Brown5ba06fc2012-02-16 11:07:13 -08003060static struct snd_soc_dapm_widget *
3061snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3062 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003063{
3064 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003065 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003066
3067 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003068 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003069
Mark Brown62ea8742012-01-21 21:14:48 +00003070 switch (w->id) {
3071 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003072 w->regulator = devm_regulator_get(dapm->dev, w->name);
3073 if (IS_ERR(w->regulator)) {
3074 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003075 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003076 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003077 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003078 }
Mark Brown8784c772013-01-10 19:33:47 +00003079
3080 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
3081 ret = regulator_allow_bypass(w->regulator, true);
3082 if (ret != 0)
3083 dev_warn(w->dapm->dev,
3084 "ASoC: Failed to unbypass %s: %d\n",
3085 w->name, ret);
3086 }
Mark Brown62ea8742012-01-21 21:14:48 +00003087 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003088 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003089#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003090 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003091 if (IS_ERR(w->clk)) {
3092 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003093 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003094 w->name, ret);
3095 return NULL;
3096 }
Mark Brownec029952012-06-04 08:16:20 +01003097#else
3098 return NULL;
3099#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003100 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003101 default:
3102 break;
3103 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003104
Mark Brown88e8b9a2011-03-02 18:18:24 +00003105 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003106 w->name = kasprintf(GFP_KERNEL, "%s %s",
3107 dapm->codec->name_prefix, widget->name);
3108 else
3109 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3110
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003111 if (w->name == NULL) {
3112 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003113 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003114 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003115
Mark Brown7ca3a182011-10-08 14:04:50 +01003116 switch (w->id) {
3117 case snd_soc_dapm_switch:
3118 case snd_soc_dapm_mixer:
3119 case snd_soc_dapm_mixer_named_ctl:
3120 w->power_check = dapm_generic_check_power;
3121 break;
3122 case snd_soc_dapm_mux:
3123 case snd_soc_dapm_virt_mux:
3124 case snd_soc_dapm_value_mux:
3125 w->power_check = dapm_generic_check_power;
3126 break;
Mark Brown46162742013-06-05 19:36:11 +01003127 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003128 w->power_check = dapm_adc_check_power;
3129 break;
Mark Brown46162742013-06-05 19:36:11 +01003130 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003131 w->power_check = dapm_dac_check_power;
3132 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003133 case snd_soc_dapm_adc:
3134 case snd_soc_dapm_aif_out:
3135 case snd_soc_dapm_dac:
3136 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003137 case snd_soc_dapm_pga:
3138 case snd_soc_dapm_out_drv:
3139 case snd_soc_dapm_input:
3140 case snd_soc_dapm_output:
3141 case snd_soc_dapm_micbias:
3142 case snd_soc_dapm_spk:
3143 case snd_soc_dapm_hp:
3144 case snd_soc_dapm_mic:
3145 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003146 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003147 w->power_check = dapm_generic_check_power;
3148 break;
3149 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003150 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003151 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003152 w->power_check = dapm_supply_check_power;
3153 break;
3154 default:
3155 w->power_check = dapm_always_on_check_power;
3156 break;
3157 }
3158
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003159 w->dapm = dapm;
3160 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003161 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003162 INIT_LIST_HEAD(&w->sources);
3163 INIT_LIST_HEAD(&w->sinks);
3164 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003165 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003166 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003167
3168 /* machine layer set ups unconnected pins and insertions */
3169 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003170 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003171}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003172
3173/**
Mark Brown4ba13272008-05-13 14:51:19 +02003174 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003175 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003176 * @widget: widget array
3177 * @num: number of widgets
3178 *
3179 * Creates new DAPM controls based upon the templates.
3180 *
3181 * Returns 0 for success else error.
3182 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003183int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003184 const struct snd_soc_dapm_widget *widget,
3185 int num)
3186{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003187 struct snd_soc_dapm_widget *w;
3188 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003189 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003190
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003191 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003192 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003193 w = snd_soc_dapm_new_control(dapm, widget);
3194 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003195 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003196 "ASoC: Failed to create DAPM control %s\n",
3197 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003198 ret = -ENOMEM;
3199 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003200 }
Mark Brown4ba13272008-05-13 14:51:19 +02003201 widget++;
3202 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003203 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003204 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003205}
3206EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3207
Mark Brownc74184e2012-04-04 22:12:09 +01003208static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3209 struct snd_kcontrol *kcontrol, int event)
3210{
3211 struct snd_soc_dapm_path *source_p, *sink_p;
3212 struct snd_soc_dai *source, *sink;
3213 const struct snd_soc_pcm_stream *config = w->params;
3214 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003215 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003216 u64 fmt;
3217 int ret;
3218
3219 BUG_ON(!config);
3220 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3221
3222 /* We only support a single source and sink, pick the first */
3223 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3224 list_sink);
3225 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3226 list_source);
3227
3228 BUG_ON(!source_p || !sink_p);
3229 BUG_ON(!sink_p->source || !source_p->sink);
3230 BUG_ON(!source_p->source || !sink_p->sink);
3231
3232 source = source_p->source->priv;
3233 sink = sink_p->sink->priv;
3234
3235 /* Be a little careful as we don't want to overflow the mask array */
3236 if (config->formats) {
3237 fmt = ffs(config->formats) - 1;
3238 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003239 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003240 config->formats);
3241 fmt = 0;
3242 }
3243
3244 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003245 params = kzalloc(sizeof(*params), GFP_KERNEL);
3246 if (!params) {
3247 ret = -ENOMEM;
3248 goto out;
3249 }
3250 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003251
Mark Brown9747cec2012-04-26 19:12:21 +01003252 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003253 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003254 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003255 config->rate_max;
3256
Mark Brown9747cec2012-04-26 19:12:21 +01003257 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003258 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003259 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003260 = config->channels_max;
3261
3262 memset(&substream, 0, sizeof(substream));
3263
3264 switch (event) {
3265 case SND_SOC_DAPM_PRE_PMU:
3266 if (source->driver->ops && source->driver->ops->hw_params) {
3267 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3268 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003269 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003270 if (ret != 0) {
3271 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003272 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003273 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003274 }
3275 }
3276
3277 if (sink->driver->ops && sink->driver->ops->hw_params) {
3278 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003279 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003280 sink);
3281 if (ret != 0) {
3282 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003283 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003284 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003285 }
3286 }
3287 break;
3288
3289 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003290 ret = snd_soc_dai_digital_mute(sink, 0,
3291 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003292 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003293 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003294 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003295 break;
3296
3297 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003298 ret = snd_soc_dai_digital_mute(sink, 1,
3299 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003300 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003301 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003302 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003303 break;
3304
3305 default:
3306 BUG();
3307 return -EINVAL;
3308 }
3309
Mark Brown9747cec2012-04-26 19:12:21 +01003310out:
3311 kfree(params);
3312 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003313}
3314
3315int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3316 const struct snd_soc_pcm_stream *params,
3317 struct snd_soc_dapm_widget *source,
3318 struct snd_soc_dapm_widget *sink)
3319{
3320 struct snd_soc_dapm_route routes[2];
3321 struct snd_soc_dapm_widget template;
3322 struct snd_soc_dapm_widget *w;
3323 size_t len;
3324 char *link_name;
3325
3326 len = strlen(source->name) + strlen(sink->name) + 2;
3327 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3328 if (!link_name)
3329 return -ENOMEM;
3330 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3331
3332 memset(&template, 0, sizeof(template));
3333 template.reg = SND_SOC_NOPM;
3334 template.id = snd_soc_dapm_dai_link;
3335 template.name = link_name;
3336 template.event = snd_soc_dai_link_event;
3337 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3338 SND_SOC_DAPM_PRE_PMD;
3339
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003340 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003341
3342 w = snd_soc_dapm_new_control(&card->dapm, &template);
3343 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003344 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003345 link_name);
3346 return -ENOMEM;
3347 }
3348
3349 w->params = params;
3350
3351 memset(&routes, 0, sizeof(routes));
3352
3353 routes[0].source = source->name;
3354 routes[0].sink = link_name;
3355 routes[1].source = link_name;
3356 routes[1].sink = sink->name;
3357
3358 return snd_soc_dapm_add_routes(&card->dapm, routes,
3359 ARRAY_SIZE(routes));
3360}
3361
Mark Brown888df392012-02-16 19:37:51 -08003362int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3363 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003364{
Mark Brown888df392012-02-16 19:37:51 -08003365 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003366 struct snd_soc_dapm_widget *w;
3367
Mark Brown888df392012-02-16 19:37:51 -08003368 WARN_ON(dapm->dev != dai->dev);
3369
3370 memset(&template, 0, sizeof(template));
3371 template.reg = SND_SOC_NOPM;
3372
3373 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003374 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003375 template.name = dai->driver->playback.stream_name;
3376 template.sname = dai->driver->playback.stream_name;
3377
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003378 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003379 template.name);
3380
3381 w = snd_soc_dapm_new_control(dapm, &template);
3382 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003383 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003384 dai->driver->playback.stream_name);
3385 }
3386
3387 w->priv = dai;
3388 dai->playback_widget = w;
3389 }
3390
3391 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003392 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003393 template.name = dai->driver->capture.stream_name;
3394 template.sname = dai->driver->capture.stream_name;
3395
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003396 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003397 template.name);
3398
3399 w = snd_soc_dapm_new_control(dapm, &template);
3400 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003401 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003402 dai->driver->capture.stream_name);
3403 }
3404
3405 w->priv = dai;
3406 dai->capture_widget = w;
3407 }
3408
3409 return 0;
3410}
3411
3412int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3413{
3414 struct snd_soc_dapm_widget *dai_w, *w;
3415 struct snd_soc_dai *dai;
3416 struct snd_soc_dapm_route r;
3417
3418 memset(&r, 0, sizeof(r));
3419
3420 /* For each DAI widget... */
3421 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003422 switch (dai_w->id) {
3423 case snd_soc_dapm_dai_in:
3424 case snd_soc_dapm_dai_out:
3425 break;
3426 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003427 continue;
Mark Brown46162742013-06-05 19:36:11 +01003428 }
Mark Brown888df392012-02-16 19:37:51 -08003429
3430 dai = dai_w->priv;
3431
3432 /* ...find all widgets with the same stream and link them */
3433 list_for_each_entry(w, &card->widgets, list) {
3434 if (w->dapm != dai_w->dapm)
3435 continue;
3436
Mark Brown46162742013-06-05 19:36:11 +01003437 switch (w->id) {
3438 case snd_soc_dapm_dai_in:
3439 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003440 continue;
Mark Brown46162742013-06-05 19:36:11 +01003441 default:
3442 break;
3443 }
Mark Brown888df392012-02-16 19:37:51 -08003444
3445 if (!w->sname)
3446 continue;
3447
3448 if (dai->driver->playback.stream_name &&
3449 strstr(w->sname,
3450 dai->driver->playback.stream_name)) {
3451 r.source = dai->playback_widget->name;
3452 r.sink = w->name;
3453 dev_dbg(dai->dev, "%s -> %s\n",
3454 r.source, r.sink);
3455
3456 snd_soc_dapm_add_route(w->dapm, &r);
3457 }
3458
3459 if (dai->driver->capture.stream_name &&
3460 strstr(w->sname,
3461 dai->driver->capture.stream_name)) {
3462 r.source = w->name;
3463 r.sink = dai->capture_widget->name;
3464 dev_dbg(dai->dev, "%s -> %s\n",
3465 r.source, r.sink);
3466
3467 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003468 }
3469 }
3470 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003471
Mark Brown888df392012-02-16 19:37:51 -08003472 return 0;
3473}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003474
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003475static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3476 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003477{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003478
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003479 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3480 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3481 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003482
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003483 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3484 w_cpu = cpu_dai->playback_widget;
3485 w_codec = codec_dai->playback_widget;
3486 } else {
3487 w_cpu = cpu_dai->capture_widget;
3488 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003489 }
3490
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003491 if (w_cpu) {
3492
3493 dapm_mark_dirty(w_cpu, "stream event");
3494
3495 switch (event) {
3496 case SND_SOC_DAPM_STREAM_START:
3497 w_cpu->active = 1;
3498 break;
3499 case SND_SOC_DAPM_STREAM_STOP:
3500 w_cpu->active = 0;
3501 break;
3502 case SND_SOC_DAPM_STREAM_SUSPEND:
3503 case SND_SOC_DAPM_STREAM_RESUME:
3504 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3505 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3506 break;
3507 }
3508 }
3509
3510 if (w_codec) {
3511
3512 dapm_mark_dirty(w_codec, "stream event");
3513
3514 switch (event) {
3515 case SND_SOC_DAPM_STREAM_START:
3516 w_codec->active = 1;
3517 break;
3518 case SND_SOC_DAPM_STREAM_STOP:
3519 w_codec->active = 0;
3520 break;
3521 case SND_SOC_DAPM_STREAM_SUSPEND:
3522 case SND_SOC_DAPM_STREAM_RESUME:
3523 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3524 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3525 break;
3526 }
3527 }
3528
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003529 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003530}
3531
3532/**
3533 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3534 * @rtd: PCM runtime data
3535 * @stream: stream name
3536 * @event: stream event
3537 *
3538 * Sends a stream event to the dapm core. The core then makes any
3539 * necessary widget power changes.
3540 *
3541 * Returns 0 for success else error.
3542 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003543void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3544 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003545{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003546 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003547
Liam Girdwood3cd04342012-03-09 12:02:08 +00003548 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003549 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003550 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003551}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003552
3553/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003554 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003555 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003556 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003557 *
Mark Brown74b8f952009-06-06 11:26:15 +01003558 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003559 * a valid audio route and active audio stream.
3560 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3561 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003562 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003563int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003564{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003565 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003566}
Liam Girdwooda5302182008-07-07 13:35:17 +01003567EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003568
3569/**
Mark Brownda341832010-03-15 19:23:37 +00003570 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003571 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003572 * @pin: pin name
3573 *
3574 * Enables input/output pin regardless of any other state. This is
3575 * intended for use with microphone bias supplies used in microphone
3576 * jack detection.
3577 *
3578 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3579 * do any widget power switching.
3580 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003581int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3582 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003583{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003584 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003585
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003586 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003587 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003588 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003589 }
3590
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003591 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003592 w->connected = 1;
3593 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003594 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003595
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003596 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003597}
3598EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3599
3600/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003601 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003602 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003603 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003604 *
Mark Brown74b8f952009-06-06 11:26:15 +01003605 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003606 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3607 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003608 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003609int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3610 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003611{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003612 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003613}
3614EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3615
3616/**
Mark Brown5817b522008-09-24 11:23:11 +01003617 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003618 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003619 * @pin: pin name
3620 *
3621 * Marks the specified pin as being not connected, disabling it along
3622 * any parent or child widgets. At present this is identical to
3623 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3624 * additional things such as disabling controls which only affect
3625 * paths through the pin.
3626 *
3627 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3628 * do any widget power switching.
3629 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003630int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003631{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003632 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003633}
3634EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3635
3636/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003637 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003638 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003639 * @pin: audio signal pin endpoint (or start point)
3640 *
3641 * Get audio pin status - connected or disconnected.
3642 *
3643 * Returns 1 for connected otherwise 0.
3644 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003645int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3646 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003647{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003648 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003649
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003650 if (w)
3651 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003652
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003653 return 0;
3654}
Liam Girdwooda5302182008-07-07 13:35:17 +01003655EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003656
3657/**
Mark Brown1547aba2010-05-07 21:11:40 +01003658 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003659 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003660 * @pin: audio signal pin endpoint (or start point)
3661 *
3662 * Mark the given endpoint or pin as ignoring suspend. When the
3663 * system is disabled a path between two endpoints flagged as ignoring
3664 * suspend will not be disabled. The path must already be enabled via
3665 * normal means at suspend time, it will not be turned on if it was not
3666 * already enabled.
3667 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003668int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3669 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003670{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003671 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003672
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003673 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003674 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003675 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003676 }
3677
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003678 w->ignore_suspend = 1;
3679
3680 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003681}
3682EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3683
Stephen Warren16332812011-11-23 12:42:04 -07003684static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3685 struct snd_soc_dapm_widget *w)
3686{
3687 struct snd_soc_dapm_path *p;
3688
3689 list_for_each_entry(p, &card->paths, list) {
3690 if ((p->source == w) || (p->sink == w)) {
3691 dev_dbg(card->dev,
3692 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3693 p->source->name, p->source->id, p->source->dapm,
3694 p->sink->name, p->sink->id, p->sink->dapm);
3695
3696 /* Connected to something other than the codec */
3697 if (p->source->dapm != p->sink->dapm)
3698 return true;
3699 /*
3700 * Loopback connection from codec external pin to
3701 * codec external pin
3702 */
3703 if (p->sink->id == snd_soc_dapm_input) {
3704 switch (p->source->id) {
3705 case snd_soc_dapm_output:
3706 case snd_soc_dapm_micbias:
3707 return true;
3708 default:
3709 break;
3710 }
3711 }
3712 }
3713 }
3714
3715 return false;
3716}
3717
3718/**
3719 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3720 * @codec: The codec whose pins should be processed
3721 *
3722 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3723 * which are unused. Pins are used if they are connected externally to the
3724 * codec, whether that be to some other device, or a loop-back connection to
3725 * the codec itself.
3726 */
3727void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3728{
3729 struct snd_soc_card *card = codec->card;
3730 struct snd_soc_dapm_context *dapm = &codec->dapm;
3731 struct snd_soc_dapm_widget *w;
3732
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003733 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003734 &card->dapm, &codec->dapm);
3735
3736 list_for_each_entry(w, &card->widgets, list) {
3737 if (w->dapm != dapm)
3738 continue;
3739 switch (w->id) {
3740 case snd_soc_dapm_input:
3741 case snd_soc_dapm_output:
3742 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003743 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003744 w->name);
3745 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003746 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003747 "... Not in map; disabling\n");
3748 snd_soc_dapm_nc_pin(dapm, w->name);
3749 }
3750 break;
3751 default:
3752 break;
3753 }
3754 }
3755}
3756
Mark Brown1547aba2010-05-07 21:11:40 +01003757/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003758 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003759 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003760 *
3761 * Free all dapm widgets and resources.
3762 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003763void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003764{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003765 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003766 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003767 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003768 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003769}
3770EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3771
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003772static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003773{
Liam Girdwood01005a72012-07-06 16:57:05 +01003774 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003775 struct snd_soc_dapm_widget *w;
3776 LIST_HEAD(down_list);
3777 int powerdown = 0;
3778
Liam Girdwood01005a72012-07-06 16:57:05 +01003779 mutex_lock(&card->dapm_mutex);
3780
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003781 list_for_each_entry(w, &dapm->card->widgets, list) {
3782 if (w->dapm != dapm)
3783 continue;
Mark Brown51737472009-06-22 13:16:51 +01003784 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003785 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003786 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003787 powerdown = 1;
3788 }
3789 }
3790
3791 /* If there were no widgets to power down we're already in
3792 * standby.
3793 */
3794 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003795 if (dapm->bias_level == SND_SOC_BIAS_ON)
3796 snd_soc_dapm_set_bias_level(dapm,
3797 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003798 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003799 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3800 snd_soc_dapm_set_bias_level(dapm,
3801 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003802 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003803
3804 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003805}
Mark Brown51737472009-06-22 13:16:51 +01003806
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003807/*
3808 * snd_soc_dapm_shutdown - callback for system shutdown
3809 */
3810void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3811{
3812 struct snd_soc_codec *codec;
3813
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003814 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003815 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003816 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3817 snd_soc_dapm_set_bias_level(&codec->dapm,
3818 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003819 }
Mark Brown51737472009-06-22 13:16:51 +01003820}
3821
Richard Purdie2b97eab2006-10-06 18:32:18 +02003822/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003823MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003824MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3825MODULE_LICENSE("GPL");