blob: 59bcc66358cab3a2b19d94afe249780d1d77b41a [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 Brown15e4c72f2008-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 Brown15e4c72f2008-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 Brown15e4c72f2008-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 Brown15e4c72f2008-07-02 11:51:20 +0100128 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200129
130 kfree(buf);
Mark Brown15e4c72f2008-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 Clausen5106b922013-07-29 17:14:00 +0200179 struct list_head paths;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200180 struct snd_soc_dapm_widget_list wlist;
181};
182
183static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
184 struct snd_kcontrol *kcontrol)
185{
186 struct dapm_kcontrol_data *data;
187
188 data = kzalloc(sizeof(*data) + sizeof(widget), GFP_KERNEL);
189 if (!data) {
190 dev_err(widget->dapm->dev,
191 "ASoC: can't allocate kcontrol data for %s\n",
192 widget->name);
193 return -ENOMEM;
194 }
195
196 data->wlist.widgets[0] = widget;
197 data->wlist.num_widgets = 1;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200198 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200199
200 kcontrol->private_data = data;
201
202 return 0;
203}
204
205static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
206{
207 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
208 kfree(data);
209}
210
211static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
212 const struct snd_kcontrol *kcontrol)
213{
214 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
215
216 return &data->wlist;
217}
218
219static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
220 struct snd_soc_dapm_widget *widget)
221{
222 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
223 struct dapm_kcontrol_data *new_data;
224 unsigned int n = data->wlist.num_widgets + 1;
225
226 new_data = krealloc(data, sizeof(*data) + sizeof(widget) * n,
227 GFP_KERNEL);
228 if (!data)
229 return -ENOMEM;
230
231 data->wlist.widgets[n - 1] = widget;
232 data->wlist.num_widgets = n;
233
234 kcontrol->private_data = data;
235
236 return 0;
237}
238
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200239static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
240 struct snd_soc_dapm_path *path)
241{
242 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
243
244 list_add_tail(&path->list_kcontrol, &data->paths);
245}
246
247static struct list_head *dapm_kcontrol_get_path_list(
248 const struct snd_kcontrol *kcontrol)
249{
250 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
251
252 return &data->paths;
253}
254
255#define dapm_kcontrol_for_each_path(path, kcontrol) \
256 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
257 list_kcontrol)
258
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200259static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
260{
261 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
262
263 return data->value;
264}
265
266static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
267 unsigned int value)
268{
269 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
270
271 if (data->value == value)
272 return false;
273
274 data->value = value;
275
276 return true;
277}
278
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200279/**
280 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
281 * @kcontrol: The kcontrol
282 */
283struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
284{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200285 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200286}
287EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
288
Liam Girdwood6c120e12012-02-15 15:15:34 +0000289static void dapm_reset(struct snd_soc_card *card)
290{
291 struct snd_soc_dapm_widget *w;
292
293 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
294
295 list_for_each_entry(w, &card->widgets, list) {
296 w->power_checked = false;
297 w->inputs = -1;
298 w->outputs = -1;
299 }
300}
301
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100302static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
303{
304 if (w->codec)
305 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100306 else if (w->platform)
307 return snd_soc_platform_read(w->platform, reg);
308
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000309 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100310 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100311}
312
313static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
314{
315 if (w->codec)
316 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100317 else if (w->platform)
318 return snd_soc_platform_write(w->platform, reg, val);
319
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000320 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100321 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100322}
323
Liam Girdwood49575fb52012-03-06 18:16:19 +0000324static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
325{
Mark Browne06ab3b2012-03-06 23:58:22 +0000326 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000327 mutex_lock(&w->codec->mutex);
328 else if (w->platform)
329 mutex_lock(&w->platform->mutex);
330}
331
332static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
333{
Mark Browne06ab3b2012-03-06 23:58:22 +0000334 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000335 mutex_unlock(&w->codec->mutex);
336 else if (w->platform)
337 mutex_unlock(&w->platform->mutex);
338}
339
340static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100341 unsigned short reg, unsigned int mask, unsigned int value)
342{
Mark Brown8a713da2011-12-03 12:33:55 +0000343 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100344 unsigned int old, new;
345 int ret;
346
Mark Brown8a713da2011-12-03 12:33:55 +0000347 if (w->codec && w->codec->using_regmap) {
348 ret = regmap_update_bits_check(w->codec->control_data,
349 reg, mask, value, &change);
350 if (ret != 0)
351 return ret;
352 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000353 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000354 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000355 if (ret < 0) {
356 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100357 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000358 }
Mark Brown8a713da2011-12-03 12:33:55 +0000359
360 old = ret;
361 new = (old & ~mask) | (value & mask);
362 change = old != new;
363 if (change) {
364 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000365 if (ret < 0) {
366 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000367 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000368 }
Mark Brown8a713da2011-12-03 12:33:55 +0000369 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000370 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100371 }
372
373 return change;
374}
375
Mark Brown452c5ea2009-05-17 21:41:23 +0100376/**
377 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800378 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100379 * @level: level to configure
380 *
381 * Configure the bias (power) levels for the SoC audio device.
382 *
383 * Returns 0 for success else error.
384 */
Mark Browned5a4c42011-02-18 11:12:42 -0800385static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200386 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100387{
Mark Browned5a4c42011-02-18 11:12:42 -0800388 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100389 int ret = 0;
390
Mark Brown84e90932010-11-04 00:07:02 -0400391 trace_snd_soc_bias_level_start(card, level);
392
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000393 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100394 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100395 if (ret != 0)
396 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100397
Mark Browncc4c6702011-06-06 19:03:34 +0100398 if (dapm->codec) {
399 if (dapm->codec->driver->set_bias_level)
400 ret = dapm->codec->driver->set_bias_level(dapm->codec,
401 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100402 else
403 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100404 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100405 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100406 }
Liam Girdwood41231282012-07-06 16:56:16 +0100407
Mark Brown171ec6b2011-06-06 18:15:19 +0100408 if (ret != 0)
409 goto out;
410
411 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100412 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100413out:
Mark Brown84e90932010-11-04 00:07:02 -0400414 trace_snd_soc_bias_level_done(card, level);
415
Mark Brown452c5ea2009-05-17 21:41:23 +0100416 return ret;
417}
418
Richard Purdie2b97eab2006-10-06 18:32:18 +0200419/* set up initial codec paths */
420static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
421 struct snd_soc_dapm_path *p, int i)
422{
423 switch (w->id) {
424 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000425 case snd_soc_dapm_mixer:
426 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200427 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100428 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600429 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400430 unsigned int reg = mc->reg;
431 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100432 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400433 unsigned int mask = (1 << fls(max)) - 1;
434 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200435
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100436 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200437 val = (val >> shift) & mask;
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200438 if (invert)
439 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200440
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200441 p->connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200442 }
443 break;
444 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600445 struct soc_enum *e = (struct soc_enum *)
446 w->kcontrol_news[i].private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200447 int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200448
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100449 val = soc_widget_read(w, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200450 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200451
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200452 if (item < e->max && !strcmp(p->name, e->texts[item]))
453 p->connect = 1;
454 else
455 p->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200456 }
457 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000458 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600459 struct soc_enum *e = (struct soc_enum *)
460 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000461
462 p->connect = 0;
463 /* since a virtual mux has no backing registers to
464 * decide which path to connect, it will try to match
465 * with the first enumeration. This is to ensure
466 * that the default mux choice (the first) will be
467 * correctly powered up during initialization.
468 */
469 if (!strcmp(p->name, e->texts[0]))
470 p->connect = 1;
471 }
472 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200473 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200474 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600475 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200476 int val, item;
477
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100478 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200479 val = (val >> e->shift_l) & e->mask;
480 for (item = 0; item < e->max; item++) {
481 if (val == e->values[item])
482 break;
483 }
484
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200485 if (item < e->max && !strcmp(p->name, e->texts[item]))
486 p->connect = 1;
487 else
488 p->connect = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200489 }
490 break;
Mark Brown56563102011-10-03 22:41:09 +0100491 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200492 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600493 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200494 case snd_soc_dapm_output:
495 case snd_soc_dapm_adc:
496 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000497 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200498 case snd_soc_dapm_dac:
499 case snd_soc_dapm_micbias:
500 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100501 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000502 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200503 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100504 case snd_soc_dapm_aif_in:
505 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100506 case snd_soc_dapm_dai_in:
507 case snd_soc_dapm_dai_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200508 case snd_soc_dapm_hp:
509 case snd_soc_dapm_mic:
510 case snd_soc_dapm_spk:
511 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100512 case snd_soc_dapm_dai_link:
Mark Brown56563102011-10-03 22:41:09 +0100513 p->connect = 1;
514 break;
515 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200516 case snd_soc_dapm_pre:
517 case snd_soc_dapm_post:
518 p->connect = 0;
519 break;
520 }
521}
522
Mark Brown74b8f952009-06-06 11:26:15 +0100523/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200524static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200525 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
526 struct snd_soc_dapm_path *path, const char *control_name,
527 const struct snd_kcontrol_new *kcontrol)
528{
529 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
530 int i;
531
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100532 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200533 if (!(strcmp(control_name, e->texts[i]))) {
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);
537 path->name = (char*)e->texts[i];
538 dapm_set_path_status(dest, path, 0);
539 return 0;
540 }
541 }
542
543 return -ENODEV;
544}
545
Mark Brown74b8f952009-06-06 11:26:15 +0100546/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200547static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200548 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
549 struct snd_soc_dapm_path *path, const char *control_name)
550{
551 int i;
552
553 /* search for mixer kcontrol */
554 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600555 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200556 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200557 list_add(&path->list_sink, &dest->sources);
558 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600559 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200560 dapm_set_path_status(dest, path, i);
561 return 0;
562 }
563 }
564 return -ENODEV;
565}
566
Stephen Warrenaf468002011-04-28 17:38:01 -0600567static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600568 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600569 const struct snd_kcontrol_new *kcontrol_new,
570 struct snd_kcontrol **kcontrol)
571{
572 struct snd_soc_dapm_widget *w;
573 int i;
574
575 *kcontrol = NULL;
576
577 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600578 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
579 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600580 for (i = 0; i < w->num_kcontrols; i++) {
581 if (&w->kcontrol_news[i] == kcontrol_new) {
582 if (w->kcontrols)
583 *kcontrol = w->kcontrols[i];
584 return 1;
585 }
586 }
587 }
588
589 return 0;
590}
591
Stephen Warren85762e72013-03-29 15:40:10 -0600592/*
593 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
594 * create it. Either way, add the widget into the control's widget list
595 */
596static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
597 int kci, struct snd_soc_dapm_path *path)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200598{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200599 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000600 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000601 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600602 size_t prefix_len;
603 int shared;
604 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600605 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600606 char *long_name;
607 const char *name;
608 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000609
610 if (dapm->codec)
611 prefix = dapm->codec->name_prefix;
612 else
613 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200614
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000615 if (prefix)
616 prefix_len = strlen(prefix) + 1;
617 else
618 prefix_len = 0;
619
Stephen Warren85762e72013-03-29 15:40:10 -0600620 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
621 &kcontrol);
622
Stephen Warren85762e72013-03-29 15:40:10 -0600623 if (!kcontrol) {
624 if (shared) {
625 wname_in_long_name = false;
626 kcname_in_long_name = true;
627 } else {
628 switch (w->id) {
629 case snd_soc_dapm_switch:
630 case snd_soc_dapm_mixer:
631 wname_in_long_name = true;
632 kcname_in_long_name = true;
633 break;
634 case snd_soc_dapm_mixer_named_ctl:
635 wname_in_long_name = false;
636 kcname_in_long_name = true;
637 break;
638 case snd_soc_dapm_mux:
639 case snd_soc_dapm_virt_mux:
640 case snd_soc_dapm_value_mux:
641 wname_in_long_name = true;
642 kcname_in_long_name = false;
643 break;
644 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600645 return -EINVAL;
646 }
647 }
648
649 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600650 /*
651 * The control will get a prefix from the control
652 * creation process but we're also using the same
653 * prefix for widgets so cut the prefix off the
654 * front of the widget name.
655 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200656 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600657 w->name + prefix_len,
658 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200659 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200660 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600661
662 name = long_name;
663 } else if (wname_in_long_name) {
664 long_name = NULL;
665 name = w->name + prefix_len;
666 } else {
667 long_name = NULL;
668 name = w->kcontrol_news[kci].name;
669 }
670
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200671 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600672 prefix);
Lars-Peter Clausen6b75bf02013-06-14 13:16:51 +0200673 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200674 kfree(long_name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200675
676 ret = dapm_kcontrol_data_alloc(w, kcontrol);
677 if (ret) {
678 snd_ctl_free_one(kcontrol);
679 return ret;
680 }
681
Stephen Warren85762e72013-03-29 15:40:10 -0600682 ret = snd_ctl_add(card, kcontrol);
683 if (ret < 0) {
684 dev_err(dapm->dev,
685 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
686 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600687 return ret;
688 }
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200689 } else {
690 ret = dapm_kcontrol_add_widget(kcontrol, w);
691 if (ret)
692 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600693 }
694
Stephen Warren85762e72013-03-29 15:40:10 -0600695 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200696 dapm_kcontrol_add_path(kcontrol, path);
Stephen Warren85762e72013-03-29 15:40:10 -0600697
698 return 0;
699}
700
701/* create new dapm mixer control */
702static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
703{
704 int i, ret;
705 struct snd_soc_dapm_path *path;
706
Richard Purdie2b97eab2006-10-06 18:32:18 +0200707 /* add kcontrol */
708 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200709 /* match name */
710 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200711 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600712 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200713 continue;
714
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200715 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200716 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200717 continue;
718 }
719
Stephen Warren85762e72013-03-29 15:40:10 -0600720 ret = dapm_create_or_share_mixmux_kcontrol(w, i, path);
721 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200722 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200723 }
724 }
Stephen Warren85762e72013-03-29 15:40:10 -0600725
726 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200727}
728
729/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200730static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200731{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200732 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600733 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600734 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200735
Stephen Warrenaf468002011-04-28 17:38:01 -0600736 if (w->num_kcontrols != 1) {
737 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000738 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600739 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200740 return -EINVAL;
741 }
742
Stephen Warren85762e72013-03-29 15:40:10 -0600743 path = list_first_entry(&w->sources, struct snd_soc_dapm_path,
744 list_sink);
745 if (!path) {
746 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
747 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600748 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200749
Stephen Warren85762e72013-03-29 15:40:10 -0600750 ret = dapm_create_or_share_mixmux_kcontrol(w, 0, path);
751 if (ret < 0)
752 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600753
Richard Purdie2b97eab2006-10-06 18:32:18 +0200754 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200755 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200756
Stephen Warrenaf468002011-04-28 17:38:01 -0600757 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200758}
759
760/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200761static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200762{
Mark Browna6c65732010-03-03 17:45:21 +0000763 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200764 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000765 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200766
Mark Browna6c65732010-03-03 17:45:21 +0000767 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200768}
769
770/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100771static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
772 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200773{
774 struct snd_soc_dapm_path *p;
775
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100776 list_for_each_entry(p, sink, list_source) {
777 if (p->walked) {
778 p->walked = 0;
779 dapm_clear_walk_output(dapm, &p->sink->sinks);
780 }
781 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200782}
783
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100784static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
785 struct list_head *source)
786{
787 struct snd_soc_dapm_path *p;
788
789 list_for_each_entry(p, source, list_sink) {
790 if (p->walked) {
791 p->walked = 0;
792 dapm_clear_walk_input(dapm, &p->source->sources);
793 }
794 }
795}
796
797
Mark Brown9949788b2010-05-07 20:24:05 +0100798/* We implement power down on suspend by checking the power state of
799 * the ALSA card - when we are suspending the ALSA state for the card
800 * is set to D3.
801 */
802static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
803{
Mark Brown12ea2c72011-03-02 18:17:32 +0000804 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100805
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000806 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100807 case SNDRV_CTL_POWER_D3hot:
808 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100809 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000810 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200811 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100812 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100813 default:
814 return 1;
815 }
816}
817
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100818/* add widget to list if it's not already in the list */
819static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
820 struct snd_soc_dapm_widget *w)
821{
822 struct snd_soc_dapm_widget_list *wlist;
823 int wlistsize, wlistentries, i;
824
825 if (*list == NULL)
826 return -EINVAL;
827
828 wlist = *list;
829
830 /* is this widget already in the list */
831 for (i = 0; i < wlist->num_widgets; i++) {
832 if (wlist->widgets[i] == w)
833 return 0;
834 }
835
836 /* allocate some new space */
837 wlistentries = wlist->num_widgets + 1;
838 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
839 wlistentries * sizeof(struct snd_soc_dapm_widget *);
840 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
841 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000842 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100843 w->name);
844 return -ENOMEM;
845 }
846 wlist = *list;
847
848 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000849 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100850 w->name, wlist->num_widgets);
851
852 wlist->widgets[wlist->num_widgets] = w;
853 wlist->num_widgets++;
854 return 1;
855}
856
Richard Purdie2b97eab2006-10-06 18:32:18 +0200857/*
858 * Recursively check for a completed path to an active or physically connected
859 * output widget. Returns number of complete paths.
860 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100861static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
862 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200863{
864 struct snd_soc_dapm_path *path;
865 int con = 0;
866
Mark Brown024dc072011-10-09 11:52:05 +0100867 if (widget->outputs >= 0)
868 return widget->outputs;
869
Mark Brownde02d072011-09-20 21:43:24 +0100870 DAPM_UPDATE_STAT(widget, path_checks);
871
Mark Brown62ea8742012-01-21 21:14:48 +0000872 switch (widget->id) {
873 case snd_soc_dapm_supply:
874 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200875 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100876 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000877 default:
878 break;
879 }
Mark Brown246d0a12009-04-22 18:24:55 +0100880
Mark Brown010ff262009-08-17 17:39:22 +0100881 switch (widget->id) {
882 case snd_soc_dapm_adc:
883 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100884 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100885 if (widget->active) {
886 widget->outputs = snd_soc_dapm_suspend_check(widget);
887 return widget->outputs;
888 }
Mark Brown010ff262009-08-17 17:39:22 +0100889 default:
890 break;
891 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200892
893 if (widget->connected) {
894 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100895 if (widget->id == snd_soc_dapm_output && !widget->ext) {
896 widget->outputs = snd_soc_dapm_suspend_check(widget);
897 return widget->outputs;
898 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200899
900 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100901 if (widget->id == snd_soc_dapm_hp ||
902 widget->id == snd_soc_dapm_spk ||
903 (widget->id == snd_soc_dapm_line &&
904 !list_empty(&widget->sources))) {
905 widget->outputs = snd_soc_dapm_suspend_check(widget);
906 return widget->outputs;
907 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200908 }
909
910 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100911 DAPM_UPDATE_STAT(widget, neighbour_checks);
912
Mark Brownbf3a9e12011-06-13 16:42:29 +0100913 if (path->weak)
914 continue;
915
Mark Brown8af294b2013-02-22 17:48:15 +0000916 if (path->walking)
917 return 1;
918
Richard Purdie2b97eab2006-10-06 18:32:18 +0200919 if (path->walked)
920 continue;
921
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100922 trace_snd_soc_dapm_output_path(widget, path);
923
Richard Purdie2b97eab2006-10-06 18:32:18 +0200924 if (path->sink && path->connect) {
925 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000926 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100927
928 /* do we need to add this widget to the list ? */
929 if (list) {
930 int err;
931 err = dapm_list_add_widget(list, path->sink);
932 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000933 dev_err(widget->dapm->dev,
934 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100935 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000936 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100937 return con;
938 }
939 }
940
941 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000942
943 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200944 }
945 }
946
Mark Brown024dc072011-10-09 11:52:05 +0100947 widget->outputs = con;
948
Richard Purdie2b97eab2006-10-06 18:32:18 +0200949 return con;
950}
951
952/*
953 * Recursively check for a completed path to an active or physically connected
954 * input widget. Returns number of complete paths.
955 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100956static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
957 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200958{
959 struct snd_soc_dapm_path *path;
960 int con = 0;
961
Mark Brown024dc072011-10-09 11:52:05 +0100962 if (widget->inputs >= 0)
963 return widget->inputs;
964
Mark Brownde02d072011-09-20 21:43:24 +0100965 DAPM_UPDATE_STAT(widget, path_checks);
966
Mark Brown62ea8742012-01-21 21:14:48 +0000967 switch (widget->id) {
968 case snd_soc_dapm_supply:
969 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200970 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100971 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000972 default:
973 break;
974 }
Mark Brown246d0a12009-04-22 18:24:55 +0100975
Richard Purdie2b97eab2006-10-06 18:32:18 +0200976 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100977 switch (widget->id) {
978 case snd_soc_dapm_dac:
979 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +0100980 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +0100981 if (widget->active) {
982 widget->inputs = snd_soc_dapm_suspend_check(widget);
983 return widget->inputs;
984 }
Mark Brown010ff262009-08-17 17:39:22 +0100985 default:
986 break;
987 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200988
989 if (widget->connected) {
990 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100991 if (widget->id == snd_soc_dapm_input && !widget->ext) {
992 widget->inputs = snd_soc_dapm_suspend_check(widget);
993 return widget->inputs;
994 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200995
996 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100997 if (widget->id == snd_soc_dapm_vmid) {
998 widget->inputs = snd_soc_dapm_suspend_check(widget);
999 return widget->inputs;
1000 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001001
1002 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +03001003 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +01001004 (widget->id == snd_soc_dapm_line &&
1005 !list_empty(&widget->sinks))) {
1006 widget->inputs = snd_soc_dapm_suspend_check(widget);
1007 return widget->inputs;
1008 }
1009
Mark Brown1ab97c82011-11-27 16:21:51 +00001010 /* signal generator */
1011 if (widget->id == snd_soc_dapm_siggen) {
1012 widget->inputs = snd_soc_dapm_suspend_check(widget);
1013 return widget->inputs;
1014 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001015 }
1016
1017 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +01001018 DAPM_UPDATE_STAT(widget, neighbour_checks);
1019
Mark Brownbf3a9e12011-06-13 16:42:29 +01001020 if (path->weak)
1021 continue;
1022
Mark Brown8af294b2013-02-22 17:48:15 +00001023 if (path->walking)
1024 return 1;
1025
Richard Purdie2b97eab2006-10-06 18:32:18 +02001026 if (path->walked)
1027 continue;
1028
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001029 trace_snd_soc_dapm_input_path(widget, path);
1030
Richard Purdie2b97eab2006-10-06 18:32:18 +02001031 if (path->source && path->connect) {
1032 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001033 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001034
1035 /* do we need to add this widget to the list ? */
1036 if (list) {
1037 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001038 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001039 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001040 dev_err(widget->dapm->dev,
1041 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001042 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001043 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001044 return con;
1045 }
1046 }
1047
1048 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001049
1050 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001051 }
1052 }
1053
Mark Brown024dc072011-10-09 11:52:05 +01001054 widget->inputs = con;
1055
Richard Purdie2b97eab2006-10-06 18:32:18 +02001056 return con;
1057}
1058
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001059/**
1060 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1061 * @dai: the soc DAI.
1062 * @stream: stream direction.
1063 * @list: list of active widgets for this stream.
1064 *
1065 * Queries DAPM graph as to whether an valid audio stream path exists for
1066 * the initial stream specified by name. This takes into account
1067 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1068 *
1069 * Returns the number of valid paths or negative error.
1070 */
1071int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1072 struct snd_soc_dapm_widget_list **list)
1073{
1074 struct snd_soc_card *card = dai->card;
1075 int paths;
1076
1077 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1078 dapm_reset(card);
1079
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001080 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001081 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001082 dapm_clear_walk_output(&card->dapm,
1083 &dai->playback_widget->sinks);
1084 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001085 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001086 dapm_clear_walk_input(&card->dapm,
1087 &dai->capture_widget->sources);
1088 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001089
1090 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001091 mutex_unlock(&card->dapm_mutex);
1092
1093 return paths;
1094}
1095
Richard Purdie2b97eab2006-10-06 18:32:18 +02001096/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001097 * Handler for generic register modifier widget.
1098 */
1099int dapm_reg_event(struct snd_soc_dapm_widget *w,
1100 struct snd_kcontrol *kcontrol, int event)
1101{
1102 unsigned int val;
1103
1104 if (SND_SOC_DAPM_EVENT_ON(event))
1105 val = w->on_val;
1106 else
1107 val = w->off_val;
1108
Liam Girdwood49575fb52012-03-06 18:16:19 +00001109 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001110 w->mask << w->shift, val << w->shift);
1111
1112 return 0;
1113}
Mark Brown11589412008-07-29 11:42:23 +01001114EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001115
Mark Brown62ea8742012-01-21 21:14:48 +00001116/*
1117 * Handler for regulator supply widget.
1118 */
1119int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1120 struct snd_kcontrol *kcontrol, int event)
1121{
Mark Brownc05b84d2012-09-07 12:57:11 +08001122 int ret;
1123
1124 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001125 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001126 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001127 if (ret != 0)
1128 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001129 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001130 w->name, ret);
1131 }
1132
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001133 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001134 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001135 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001136 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001137 if (ret != 0)
1138 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001139 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001140 w->name, ret);
1141 }
1142
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001143 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001144 }
Mark Brown62ea8742012-01-21 21:14:48 +00001145}
1146EXPORT_SYMBOL_GPL(dapm_regulator_event);
1147
Ola Liljad7e7eb92012-05-24 15:26:25 +02001148/*
1149 * Handler for clock supply widget.
1150 */
1151int dapm_clock_event(struct snd_soc_dapm_widget *w,
1152 struct snd_kcontrol *kcontrol, int event)
1153{
1154 if (!w->clk)
1155 return -EIO;
1156
Mark Brownec029952012-06-04 08:16:20 +01001157#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001158 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001159 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001160 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001161 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001162 return 0;
1163 }
Mark Brownec029952012-06-04 08:16:20 +01001164#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001165 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001166}
1167EXPORT_SYMBOL_GPL(dapm_clock_event);
1168
Mark Brownd805002b2011-09-28 18:28:23 +01001169static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1170{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001171 if (w->power_checked)
1172 return w->new_power;
1173
Mark Brownd805002b2011-09-28 18:28:23 +01001174 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001175 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +01001176 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001177 w->new_power = w->power_check(w);
1178
1179 w->power_checked = true;
1180
1181 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +01001182}
1183
Mark Browncd0f2d42009-04-20 16:56:59 +01001184/* Generic check to see if a widget should be powered.
1185 */
1186static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1187{
1188 int in, out;
1189
Mark Brownde02d072011-09-20 21:43:24 +01001190 DAPM_UPDATE_STAT(w, power_checks);
1191
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001192 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001193 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001194 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001195 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001196 return out != 0 && in != 0;
1197}
1198
Mark Brown6ea31b92009-04-20 17:15:41 +01001199/* Check to see if an ADC has power */
1200static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1201{
1202 int in;
1203
Mark Brownde02d072011-09-20 21:43:24 +01001204 DAPM_UPDATE_STAT(w, power_checks);
1205
Mark Brown6ea31b92009-04-20 17:15:41 +01001206 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001207 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001208 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001209 return in != 0;
1210 } else {
1211 return dapm_generic_check_power(w);
1212 }
1213}
1214
1215/* Check to see if a DAC has power */
1216static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1217{
1218 int out;
1219
Mark Brownde02d072011-09-20 21:43:24 +01001220 DAPM_UPDATE_STAT(w, power_checks);
1221
Mark Brown6ea31b92009-04-20 17:15:41 +01001222 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001223 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001224 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001225 return out != 0;
1226 } else {
1227 return dapm_generic_check_power(w);
1228 }
1229}
1230
Mark Brown246d0a12009-04-22 18:24:55 +01001231/* Check to see if a power supply is needed */
1232static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1233{
1234 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001235
Mark Brownde02d072011-09-20 21:43:24 +01001236 DAPM_UPDATE_STAT(w, power_checks);
1237
Mark Brown246d0a12009-04-22 18:24:55 +01001238 /* Check if one of our outputs is connected */
1239 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001240 DAPM_UPDATE_STAT(w, neighbour_checks);
1241
Mark Brownbf3a9e12011-06-13 16:42:29 +01001242 if (path->weak)
1243 continue;
1244
Mark Brown215edda2009-09-08 18:59:05 +01001245 if (path->connected &&
1246 !path->connected(path->source, path->sink))
1247 continue;
1248
Mark Brown30173582011-02-11 11:42:19 +00001249 if (!path->sink)
1250 continue;
1251
Mark Brownf68d7e12011-10-04 22:57:50 +01001252 if (dapm_widget_power_check(path->sink))
1253 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001254 }
1255
Mark Brownf68d7e12011-10-04 22:57:50 +01001256 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001257}
1258
Mark Brown35c64bc2011-09-28 18:23:53 +01001259static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1260{
1261 return 1;
1262}
1263
Mark Brown38357ab2009-06-06 19:03:23 +01001264static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1265 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001266 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001267{
Mark Brown828a8422011-01-15 13:14:30 +00001268 int *sort;
1269
1270 if (power_up)
1271 sort = dapm_up_seq;
1272 else
1273 sort = dapm_down_seq;
1274
Mark Brown38357ab2009-06-06 19:03:23 +01001275 if (sort[a->id] != sort[b->id])
1276 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001277 if (a->subseq != b->subseq) {
1278 if (power_up)
1279 return a->subseq - b->subseq;
1280 else
1281 return b->subseq - a->subseq;
1282 }
Mark Brownb22ead22009-06-07 12:51:26 +01001283 if (a->reg != b->reg)
1284 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001285 if (a->dapm != b->dapm)
1286 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001287
Mark Brown38357ab2009-06-06 19:03:23 +01001288 return 0;
1289}
Mark Brown42aa3412009-03-01 19:21:10 +00001290
Mark Brown38357ab2009-06-06 19:03:23 +01001291/* Insert a widget in order into a DAPM power sequence. */
1292static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1293 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001294 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001295{
1296 struct snd_soc_dapm_widget *w;
1297
1298 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001299 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001300 list_add_tail(&new_widget->power_list, &w->power_list);
1301 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001302 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001303
Mark Brown38357ab2009-06-06 19:03:23 +01001304 list_add_tail(&new_widget->power_list, list);
1305}
Mark Brown42aa3412009-03-01 19:21:10 +00001306
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001307static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001308 struct snd_soc_dapm_widget *w, int event)
1309{
Mark Brown68f89ad2010-11-03 23:51:49 -04001310 const char *ev_name;
1311 int power, ret;
1312
1313 switch (event) {
1314 case SND_SOC_DAPM_PRE_PMU:
1315 ev_name = "PRE_PMU";
1316 power = 1;
1317 break;
1318 case SND_SOC_DAPM_POST_PMU:
1319 ev_name = "POST_PMU";
1320 power = 1;
1321 break;
1322 case SND_SOC_DAPM_PRE_PMD:
1323 ev_name = "PRE_PMD";
1324 power = 0;
1325 break;
1326 case SND_SOC_DAPM_POST_PMD:
1327 ev_name = "POST_PMD";
1328 power = 0;
1329 break;
Mark Brown80114122013-02-25 15:14:19 +00001330 case SND_SOC_DAPM_WILL_PMU:
1331 ev_name = "WILL_PMU";
1332 power = 1;
1333 break;
1334 case SND_SOC_DAPM_WILL_PMD:
1335 ev_name = "WILL_PMD";
1336 power = 0;
1337 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001338 default:
1339 BUG();
1340 return;
1341 }
1342
1343 if (w->power != power)
1344 return;
1345
1346 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001347 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001348 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001349 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001350 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001351 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001352 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001353 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001354 ev_name, w->name, ret);
1355 }
1356}
1357
Mark Brownb22ead22009-06-07 12:51:26 +01001358/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001359static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001360 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001361{
Mark Brown68f89ad2010-11-03 23:51:49 -04001362 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001363 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001364 unsigned int value = 0;
1365 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001366
1367 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1368 power_list)->reg;
1369
1370 list_for_each_entry(w, pending, power_list) {
Mark Brownb22ead22009-06-07 12:51:26 +01001371 BUG_ON(reg != w->reg);
1372
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001373 mask |= w->mask << w->shift;
1374 if (w->power)
1375 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001376 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001377 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001378
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001379 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001380 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1381 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001382
Mark Brown68f89ad2010-11-03 23:51:49 -04001383 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001384 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1385 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001386 }
1387
Mark Brown81628102009-06-07 13:21:24 +01001388 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001389 /* Any widget will do, they should all be updating the
1390 * same register.
1391 */
1392 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1393 power_list);
1394
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001395 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001396 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001397 value, mask, reg, card->pop_time);
1398 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001399 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001400 }
1401
1402 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001403 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1404 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001405 }
Mark Brown42aa3412009-03-01 19:21:10 +00001406}
1407
Mark Brownb22ead22009-06-07 12:51:26 +01001408/* Apply a DAPM power sequence.
1409 *
1410 * We walk over a pre-sorted list of widgets to apply power to. In
1411 * order to minimise the number of writes to the device required
1412 * multiple widgets will be updated in a single write where possible.
1413 * Currently anything that requires more than a single write is not
1414 * handled.
1415 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001416static void dapm_seq_run(struct snd_soc_card *card,
1417 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001418{
1419 struct snd_soc_dapm_widget *w, *n;
1420 LIST_HEAD(pending);
1421 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001422 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001423 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001424 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001425 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001426 int *sort;
1427
1428 if (power_up)
1429 sort = dapm_up_seq;
1430 else
1431 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001432
Mark Brownb22ead22009-06-07 12:51:26 +01001433 list_for_each_entry_safe(w, n, list, power_list) {
1434 ret = 0;
1435
1436 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001437 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001438 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001439 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001440 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001441
Mark Brown474b62d2011-01-18 16:14:44 +00001442 if (cur_dapm && cur_dapm->seq_notifier) {
1443 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1444 if (sort[i] == cur_sort)
1445 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001446 i,
1447 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001448 }
1449
Mark Brownb22ead22009-06-07 12:51:26 +01001450 INIT_LIST_HEAD(&pending);
1451 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001452 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001453 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001454 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001455 }
1456
Mark Brown163cac02009-06-07 10:12:52 +01001457 switch (w->id) {
1458 case snd_soc_dapm_pre:
1459 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001460 list_for_each_entry_safe_continue(w, n, list,
1461 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001462
Mark Brownb22ead22009-06-07 12:51:26 +01001463 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001464 ret = w->event(w,
1465 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001466 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001467 ret = w->event(w,
1468 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001469 break;
1470
1471 case snd_soc_dapm_post:
1472 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001473 list_for_each_entry_safe_continue(w, n, list,
1474 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001475
Mark Brownb22ead22009-06-07 12:51:26 +01001476 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001477 ret = w->event(w,
1478 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001479 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001480 ret = w->event(w,
1481 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001482 break;
1483
Mark Brown163cac02009-06-07 10:12:52 +01001484 default:
Mark Brown81628102009-06-07 13:21:24 +01001485 /* Queue it up for application */
1486 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001487 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001488 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001489 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001490 list_move(&w->power_list, &pending);
1491 break;
Mark Brown163cac02009-06-07 10:12:52 +01001492 }
Mark Brownb22ead22009-06-07 12:51:26 +01001493
1494 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001495 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001496 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001497 }
Mark Brownb22ead22009-06-07 12:51:26 +01001498
1499 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001500 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001501
1502 if (cur_dapm && cur_dapm->seq_notifier) {
1503 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1504 if (sort[i] == cur_sort)
1505 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001506 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001507 }
Mark Brown163cac02009-06-07 10:12:52 +01001508}
1509
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001510static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001511{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001512 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001513 struct snd_soc_dapm_widget_list *wlist;
1514 struct snd_soc_dapm_widget *w = NULL;
1515 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001516 int ret;
1517
1518 if (!update)
1519 return;
1520
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001521 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001522
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001523 for (wi = 0; wi < wlist->num_widgets; wi++) {
1524 w = wlist->widgets[wi];
1525
1526 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1527 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1528 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001529 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001530 w->name, ret);
1531 }
Mark Brown97404f22010-12-14 16:13:57 +00001532 }
1533
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001534 if (!w)
1535 return;
1536
Liam Girdwood49575fb52012-03-06 18:16:19 +00001537 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001538 update->val);
1539 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001540 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001541 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001542
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001543 for (wi = 0; wi < wlist->num_widgets; wi++) {
1544 w = wlist->widgets[wi];
1545
1546 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1547 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1548 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001549 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001550 w->name, ret);
1551 }
Mark Brown97404f22010-12-14 16:13:57 +00001552 }
1553}
1554
Mark Brown9d0624a2011-02-18 11:49:43 -08001555/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1556 * they're changing state.
1557 */
1558static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1559{
1560 struct snd_soc_dapm_context *d = data;
1561 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001562
Mark Brown56fba412011-06-04 11:25:10 +01001563 /* If we're off and we're not supposed to be go into STANDBY */
1564 if (d->bias_level == SND_SOC_BIAS_OFF &&
1565 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001566 if (d->dev)
1567 pm_runtime_get_sync(d->dev);
1568
Mark Brown9d0624a2011-02-18 11:49:43 -08001569 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1570 if (ret != 0)
1571 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001572 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001573 }
1574
Mark Brown56fba412011-06-04 11:25:10 +01001575 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1576 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001577 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1578 if (ret != 0)
1579 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001580 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001581 }
1582}
1583
1584/* Async callback run prior to DAPM sequences - brings to their final
1585 * state.
1586 */
1587static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1588{
1589 struct snd_soc_dapm_context *d = data;
1590 int ret;
1591
1592 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001593 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1594 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1595 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001596 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1597 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001598 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001599 ret);
1600 }
1601
1602 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001603 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1604 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001605 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1606 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001607 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1608 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001609
1610 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001611 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001612 }
1613
1614 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001615 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1616 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001617 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1618 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001619 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001620 ret);
1621 }
1622}
Mark Brown97404f22010-12-14 16:13:57 +00001623
Mark Brownfe4fda52011-10-03 22:36:57 +01001624static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1625 bool power, bool connect)
1626{
1627 /* If a connection is being made or broken then that update
1628 * will have marked the peer dirty, otherwise the widgets are
1629 * not connected and this update has no impact. */
1630 if (!connect)
1631 return;
1632
1633 /* If the peer is already in the state we're moving to then we
1634 * won't have an impact on it. */
1635 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001636 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001637}
1638
Mark Brown05623c42011-09-28 17:02:31 +01001639static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1640 struct list_head *up_list,
1641 struct list_head *down_list)
1642{
Mark Browndb432b42011-10-03 21:06:40 +01001643 struct snd_soc_dapm_path *path;
1644
Mark Brown05623c42011-09-28 17:02:31 +01001645 if (w->power == power)
1646 return;
1647
1648 trace_snd_soc_dapm_widget_power(w, power);
1649
Mark Browndb432b42011-10-03 21:06:40 +01001650 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001651 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001652 */
1653 list_for_each_entry(path, &w->sources, list_sink) {
1654 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001655 dapm_widget_set_peer_power(path->source, power,
1656 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001657 }
1658 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001659 switch (w->id) {
1660 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001661 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001662 case snd_soc_dapm_clock_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001663 /* Supplies can't affect their outputs, only their inputs */
1664 break;
1665 default:
1666 list_for_each_entry(path, &w->sinks, list_source) {
1667 if (path->sink) {
1668 dapm_widget_set_peer_power(path->sink, power,
1669 path->connect);
1670 }
Mark Browndb432b42011-10-03 21:06:40 +01001671 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001672 break;
Mark Browndb432b42011-10-03 21:06:40 +01001673 }
1674
Mark Brown05623c42011-09-28 17:02:31 +01001675 if (power)
1676 dapm_seq_insert(w, up_list, true);
1677 else
1678 dapm_seq_insert(w, down_list, false);
1679
1680 w->power = power;
1681}
1682
Mark Brown7c81beb2011-09-20 22:22:32 +01001683static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1684 struct list_head *up_list,
1685 struct list_head *down_list)
1686{
Mark Brown7c81beb2011-09-20 22:22:32 +01001687 int power;
1688
1689 switch (w->id) {
1690 case snd_soc_dapm_pre:
1691 dapm_seq_insert(w, down_list, false);
1692 break;
1693 case snd_soc_dapm_post:
1694 dapm_seq_insert(w, up_list, true);
1695 break;
1696
1697 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001698 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001699
Mark Brown05623c42011-09-28 17:02:31 +01001700 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001701 break;
1702 }
1703}
1704
Mark Brown42aa3412009-03-01 19:21:10 +00001705/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001706 * Scan each dapm widget for complete audio path.
1707 * A complete path is a route that has valid endpoints i.e.:-
1708 *
1709 * o DAC to output pin.
1710 * o Input Pin to ADC.
1711 * o Input pin to Output pin (bypass, sidetone)
1712 * o DAC to ADC (loopback).
1713 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001714static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001715{
1716 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001717 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001718 LIST_HEAD(up_list);
1719 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001720 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001721 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001722
Mark Brown84e90932010-11-04 00:07:02 -04001723 trace_snd_soc_dapm_start(card);
1724
Mark Brown56fba412011-06-04 11:25:10 +01001725 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001726 if (d->idle_bias_off)
1727 d->target_bias_level = SND_SOC_BIAS_OFF;
1728 else
1729 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001730 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001731
Liam Girdwood6c120e12012-02-15 15:15:34 +00001732 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001733
Mark Brown6d3ddc82009-05-16 17:47:29 +01001734 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001735 * lists indicating if they should be powered up or down. We
1736 * only check widgets that have been flagged as dirty but note
1737 * that new widgets may be added to the dirty list while we
1738 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001739 */
Mark Browndb432b42011-10-03 21:06:40 +01001740 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001741 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001742 }
1743
Mark Brownf9de6d72011-09-28 17:19:47 +01001744 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001745 switch (w->id) {
1746 case snd_soc_dapm_pre:
1747 case snd_soc_dapm_post:
1748 /* These widgets always need to be powered */
1749 break;
1750 default:
1751 list_del_init(&w->dirty);
1752 break;
1753 }
Mark Browndb432b42011-10-03 21:06:40 +01001754
Mark Brownf9de6d72011-09-28 17:19:47 +01001755 if (w->power) {
1756 d = w->dapm;
1757
1758 /* Supplies and micbiases only bring the
1759 * context up to STANDBY as unless something
1760 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001761 * generally don't require full power. Signal
1762 * generators are virtual pins and have no
1763 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001764 */
1765 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001766 case snd_soc_dapm_siggen:
1767 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001768 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001769 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001770 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001771 case snd_soc_dapm_micbias:
1772 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1773 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1774 break;
1775 default:
1776 d->target_bias_level = SND_SOC_BIAS_ON;
1777 break;
1778 }
1779 }
1780
1781 }
1782
Mark Brown85a843c2011-09-21 21:29:47 +01001783 /* Force all contexts in the card to the same bias state if
1784 * they're not ground referenced.
1785 */
Mark Brown56fba412011-06-04 11:25:10 +01001786 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001787 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001788 if (d->target_bias_level > bias)
1789 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001790 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001791 if (!d->idle_bias_off)
1792 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001793
Mark Brownde02d072011-09-20 21:43:24 +01001794 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001795
Mark Brown9d0624a2011-02-18 11:49:43 -08001796 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001797 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001798 async_schedule_domain(dapm_pre_sequence_async, d,
1799 &async_domain);
1800 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001801
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001802 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001803 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001804 }
1805
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001806 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001807 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001808 }
1809
Mark Brown6d3ddc82009-05-16 17:47:29 +01001810 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001811 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001812
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001813 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001814
Mark Brown6d3ddc82009-05-16 17:47:29 +01001815 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001816 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001817
Mark Brown9d0624a2011-02-18 11:49:43 -08001818 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001819 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001820 async_schedule_domain(dapm_post_sequence_async, d,
1821 &async_domain);
1822 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001823
Liam Girdwood8078d872012-02-15 15:15:35 +00001824 /* do we need to notify any clients that DAPM event is complete */
1825 list_for_each_entry(d, &card->dapm_list, list) {
1826 if (d->stream_event)
1827 d->stream_event(d, event);
1828 }
1829
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001830 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001831 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001832 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001833
Mark Brown84e90932010-11-04 00:07:02 -04001834 trace_snd_soc_dapm_done(card);
1835
Mark Brown42aa3412009-03-01 19:21:10 +00001836 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001837}
1838
Mark Brown79fb9382009-08-21 16:38:13 +01001839#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001840static ssize_t dapm_widget_power_read_file(struct file *file,
1841 char __user *user_buf,
1842 size_t count, loff_t *ppos)
1843{
1844 struct snd_soc_dapm_widget *w = file->private_data;
1845 char *buf;
1846 int in, out;
1847 ssize_t ret;
1848 struct snd_soc_dapm_path *p = NULL;
1849
1850 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1851 if (!buf)
1852 return -ENOMEM;
1853
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001854 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001855 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001856 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001857 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001858
Mark Brownf13ebad2012-03-03 18:01:01 +00001859 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1860 w->name, w->power ? "On" : "Off",
1861 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001862
Mark Brownd033c362009-12-04 15:25:56 +00001863 if (w->reg >= 0)
1864 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001865 " - R%d(0x%x) mask 0x%x",
1866 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001867
1868 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1869
Mark Brown3eef08b2009-09-14 16:49:00 +01001870 if (w->sname)
1871 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1872 w->sname,
1873 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001874
1875 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001876 if (p->connected && !p->connected(w, p->sink))
1877 continue;
1878
Mark Brown79fb9382009-08-21 16:38:13 +01001879 if (p->connect)
1880 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001881 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001882 p->name ? p->name : "static",
1883 p->source->name);
1884 }
1885 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001886 if (p->connected && !p->connected(w, p->sink))
1887 continue;
1888
Mark Brown79fb9382009-08-21 16:38:13 +01001889 if (p->connect)
1890 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001891 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001892 p->name ? p->name : "static",
1893 p->sink->name);
1894 }
1895
1896 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1897
1898 kfree(buf);
1899 return ret;
1900}
1901
1902static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001903 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001904 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001905 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001906};
1907
Mark Brownef49e4f2011-04-04 20:48:13 +09001908static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1909 size_t count, loff_t *ppos)
1910{
1911 struct snd_soc_dapm_context *dapm = file->private_data;
1912 char *level;
1913
1914 switch (dapm->bias_level) {
1915 case SND_SOC_BIAS_ON:
1916 level = "On\n";
1917 break;
1918 case SND_SOC_BIAS_PREPARE:
1919 level = "Prepare\n";
1920 break;
1921 case SND_SOC_BIAS_STANDBY:
1922 level = "Standby\n";
1923 break;
1924 case SND_SOC_BIAS_OFF:
1925 level = "Off\n";
1926 break;
1927 default:
1928 BUG();
1929 level = "Unknown\n";
1930 break;
1931 }
1932
1933 return simple_read_from_buffer(user_buf, count, ppos, level,
1934 strlen(level));
1935}
1936
1937static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001938 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001939 .read = dapm_bias_read_file,
1940 .llseek = default_llseek,
1941};
1942
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001943void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1944 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001945{
Mark Brown79fb9382009-08-21 16:38:13 +01001946 struct dentry *d;
1947
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001948 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1949
1950 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001951 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001952 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001953 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001954 }
Mark Brown79fb9382009-08-21 16:38:13 +01001955
Mark Brownef49e4f2011-04-04 20:48:13 +09001956 d = debugfs_create_file("bias_level", 0444,
1957 dapm->debugfs_dapm, dapm,
1958 &dapm_bias_fops);
1959 if (!d)
1960 dev_warn(dapm->dev,
1961 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001962}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001963
1964static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1965{
1966 struct snd_soc_dapm_context *dapm = w->dapm;
1967 struct dentry *d;
1968
1969 if (!dapm->debugfs_dapm || !w->name)
1970 return;
1971
1972 d = debugfs_create_file(w->name, 0444,
1973 dapm->debugfs_dapm, w,
1974 &dapm_widget_power_fops);
1975 if (!d)
1976 dev_warn(w->dapm->dev,
1977 "ASoC: Failed to create %s debugfs file\n",
1978 w->name);
1979}
1980
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001981static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1982{
1983 debugfs_remove_recursive(dapm->debugfs_dapm);
1984}
1985
Mark Brown79fb9382009-08-21 16:38:13 +01001986#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001987void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1988 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001989{
1990}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001991
1992static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1993{
1994}
1995
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001996static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1997{
1998}
1999
Mark Brown79fb9382009-08-21 16:38:13 +01002000#endif
2001
Richard Purdie2b97eab2006-10-06 18:32:18 +02002002/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002003static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002004 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002005{
2006 struct snd_soc_dapm_path *path;
2007 int found = 0;
2008
Richard Purdie2b97eab2006-10-06 18:32:18 +02002009 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002010 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002011 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002012 continue;
2013
2014 found = 1;
2015 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002016 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002017 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002018 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002019 } else {
2020 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002021 dapm_mark_dirty(path->source,
2022 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002023 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002024 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002025 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002026 }
2027
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002028 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002029 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002030
Liam Girdwood618dae12012-04-25 12:12:51 +01002031 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002032}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002033
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002034int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002035 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2036 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002037{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002038 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002039 int ret;
2040
Liam Girdwood3cd04342012-03-09 12:02:08 +00002041 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002042 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002043 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002044 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002045 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002046 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002047 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002048 return ret;
2049}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002050EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002051
Milan plzik1b075e32008-01-10 14:39:46 +01002052/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002053static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002054 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002055{
2056 struct snd_soc_dapm_path *path;
2057 int found = 0;
2058
Richard Purdie2b97eab2006-10-06 18:32:18 +02002059 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002060 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002061 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002062 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002063 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002064 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002065 }
2066
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002067 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002068 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002069
Liam Girdwood618dae12012-04-25 12:12:51 +01002070 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002071}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002072
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002073int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002074 struct snd_kcontrol *kcontrol, int connect,
2075 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002076{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002077 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002078 int ret;
2079
Liam Girdwood3cd04342012-03-09 12:02:08 +00002080 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002081 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002082 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002083 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002084 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002085 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002086 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002087 return ret;
2088}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002089EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002090
2091/* show dapm widget status in sys fs */
2092static ssize_t dapm_widget_show(struct device *dev,
2093 struct device_attribute *attr, char *buf)
2094{
Mark Brown36ae1a92012-01-06 17:12:45 -08002095 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002096 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002097 struct snd_soc_dapm_widget *w;
2098 int count = 0;
2099 char *state = "not set";
2100
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002101 list_for_each_entry(w, &codec->card->widgets, list) {
2102 if (w->dapm != &codec->dapm)
2103 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002104
2105 /* only display widgets that burnm power */
2106 switch (w->id) {
2107 case snd_soc_dapm_hp:
2108 case snd_soc_dapm_mic:
2109 case snd_soc_dapm_spk:
2110 case snd_soc_dapm_line:
2111 case snd_soc_dapm_micbias:
2112 case snd_soc_dapm_dac:
2113 case snd_soc_dapm_adc:
2114 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002115 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002116 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002117 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002118 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002119 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002120 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002121 if (w->name)
2122 count += sprintf(buf + count, "%s: %s\n",
2123 w->name, w->power ? "On":"Off");
2124 break;
2125 default:
2126 break;
2127 }
2128 }
2129
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002130 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002131 case SND_SOC_BIAS_ON:
2132 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002133 break;
Mark Brown0be98982008-05-19 12:31:28 +02002134 case SND_SOC_BIAS_PREPARE:
2135 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002136 break;
Mark Brown0be98982008-05-19 12:31:28 +02002137 case SND_SOC_BIAS_STANDBY:
2138 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002139 break;
Mark Brown0be98982008-05-19 12:31:28 +02002140 case SND_SOC_BIAS_OFF:
2141 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002142 break;
2143 }
2144 count += sprintf(buf + count, "PM State: %s\n", state);
2145
2146 return count;
2147}
2148
2149static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2150
2151int snd_soc_dapm_sys_add(struct device *dev)
2152{
Troy Kisky12ef1932008-10-13 17:42:14 -07002153 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002154}
2155
2156static void snd_soc_dapm_sys_remove(struct device *dev)
2157{
Mark Brownaef90842009-05-16 17:53:16 +01002158 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002159}
2160
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002161static void dapm_free_path(struct snd_soc_dapm_path *path)
2162{
2163 list_del(&path->list_sink);
2164 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002165 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002166 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002167 kfree(path);
2168}
2169
Richard Purdie2b97eab2006-10-06 18:32:18 +02002170/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002171static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002172{
2173 struct snd_soc_dapm_widget *w, *next_w;
2174 struct snd_soc_dapm_path *p, *next_p;
2175
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002176 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2177 if (w->dapm != dapm)
2178 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002179 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002180 /*
2181 * remove source and sink paths associated to this widget.
2182 * While removing the path, remove reference to it from both
2183 * source and sink widgets so that path is removed only once.
2184 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002185 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2186 dapm_free_path(p);
2187
2188 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2189 dapm_free_path(p);
2190
Stephen Warrenfad59882011-04-28 17:37:59 -06002191 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002192 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002193 kfree(w);
2194 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002195}
2196
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002197static struct snd_soc_dapm_widget *dapm_find_widget(
2198 struct snd_soc_dapm_context *dapm, const char *pin,
2199 bool search_other_contexts)
2200{
2201 struct snd_soc_dapm_widget *w;
2202 struct snd_soc_dapm_widget *fallback = NULL;
2203
2204 list_for_each_entry(w, &dapm->card->widgets, list) {
2205 if (!strcmp(w->name, pin)) {
2206 if (w->dapm == dapm)
2207 return w;
2208 else
2209 fallback = w;
2210 }
2211 }
2212
2213 if (search_other_contexts)
2214 return fallback;
2215
2216 return NULL;
2217}
2218
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002219static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002220 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002221{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002222 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002223
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002224 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002225 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002226 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002227 }
2228
Mark Brown1a8b2d92012-02-16 11:50:07 -08002229 if (w->connected != status)
2230 dapm_mark_dirty(w, "pin configuration");
2231
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002232 w->connected = status;
2233 if (status == 0)
2234 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002235
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002236 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002237}
2238
Richard Purdie2b97eab2006-10-06 18:32:18 +02002239/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002240 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002241 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002242 *
2243 * Walks all dapm audio paths and powers widgets according to their
2244 * stream or path usage.
2245 *
2246 * Returns 0 for success.
2247 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002248int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002249{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002250 int ret;
2251
Mark Brown4f4c0072011-10-07 14:29:19 +01002252 /*
2253 * Suppress early reports (eg, jacks syncing their state) to avoid
2254 * silly DAPM runs during card startup.
2255 */
2256 if (!dapm->card || !dapm->card->instantiated)
2257 return 0;
2258
Liam Girdwood3cd04342012-03-09 12:02:08 +00002259 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002260 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002261 mutex_unlock(&dapm->card->dapm_mutex);
2262 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002263}
Liam Girdwooda5302182008-07-07 13:35:17 +01002264EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002265
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002266static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002267 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002268{
2269 struct snd_soc_dapm_path *path;
2270 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002271 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002272 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002273 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002274 const char *source;
2275 char prefixed_sink[80];
2276 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002277 int ret = 0;
2278
Mark Brown88e8b9a2011-03-02 18:18:24 +00002279 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002280 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2281 dapm->codec->name_prefix, route->sink);
2282 sink = prefixed_sink;
2283 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2284 dapm->codec->name_prefix, route->source);
2285 source = prefixed_source;
2286 } else {
2287 sink = route->sink;
2288 source = route->source;
2289 }
2290
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002291 /*
2292 * find src and dest widgets over all widgets but favor a widget from
2293 * current DAPM context
2294 */
2295 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002296 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002297 wtsink = w;
2298 if (w->dapm == dapm)
2299 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002300 continue;
2301 }
2302 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002303 wtsource = w;
2304 if (w->dapm == dapm)
2305 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002306 }
2307 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002308 /* use widget from another DAPM context if not found from this */
2309 if (!wsink)
2310 wsink = wtsink;
2311 if (!wsource)
2312 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002313
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002314 if (wsource == NULL) {
2315 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2316 route->source);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002317 return -ENODEV;
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002318 }
2319 if (wsink == NULL) {
2320 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2321 route->sink);
2322 return -ENODEV;
2323 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002324
2325 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2326 if (!path)
2327 return -ENOMEM;
2328
2329 path->source = wsource;
2330 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002331 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002332 INIT_LIST_HEAD(&path->list);
2333 INIT_LIST_HEAD(&path->list_source);
2334 INIT_LIST_HEAD(&path->list_sink);
2335
2336 /* check for external widgets */
2337 if (wsink->id == snd_soc_dapm_input) {
2338 if (wsource->id == snd_soc_dapm_micbias ||
2339 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002340 wsource->id == snd_soc_dapm_line ||
2341 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002342 wsink->ext = 1;
2343 }
2344 if (wsource->id == snd_soc_dapm_output) {
2345 if (wsink->id == snd_soc_dapm_spk ||
2346 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002347 wsink->id == snd_soc_dapm_line ||
2348 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002349 wsource->ext = 1;
2350 }
2351
2352 /* connect static paths */
2353 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002354 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002355 list_add(&path->list_sink, &wsink->sources);
2356 list_add(&path->list_source, &wsource->sinks);
2357 path->connect = 1;
2358 return 0;
2359 }
2360
2361 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002362 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002363 case snd_soc_dapm_adc:
2364 case snd_soc_dapm_dac:
2365 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002366 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002367 case snd_soc_dapm_input:
2368 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002369 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002370 case snd_soc_dapm_micbias:
2371 case snd_soc_dapm_vmid:
2372 case snd_soc_dapm_pre:
2373 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002374 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002375 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002376 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002377 case snd_soc_dapm_aif_in:
2378 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002379 case snd_soc_dapm_dai_in:
2380 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002381 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002382 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002383 list_add(&path->list_sink, &wsink->sources);
2384 list_add(&path->list_source, &wsource->sinks);
2385 path->connect = 1;
2386 return 0;
2387 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002388 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002389 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002390 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002391 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002392 if (ret != 0)
2393 goto err;
2394 break;
2395 case snd_soc_dapm_switch:
2396 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002397 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002398 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002399 if (ret != 0)
2400 goto err;
2401 break;
2402 case snd_soc_dapm_hp:
2403 case snd_soc_dapm_mic:
2404 case snd_soc_dapm_line:
2405 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002406 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002407 list_add(&path->list_sink, &wsink->sources);
2408 list_add(&path->list_source, &wsource->sinks);
2409 path->connect = 0;
2410 return 0;
2411 }
Mark Brownfabd0382012-07-05 17:20:06 +01002412
2413 dapm_mark_dirty(wsource, "Route added");
2414 dapm_mark_dirty(wsink, "Route added");
2415
Richard Purdie2b97eab2006-10-06 18:32:18 +02002416 return 0;
2417
2418err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002419 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002420 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002421 kfree(path);
2422 return ret;
2423}
Mark Brown105f1c22008-05-13 14:52:19 +02002424
Mark Brownefcc3c62012-07-05 17:24:19 +01002425static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2426 const struct snd_soc_dapm_route *route)
2427{
2428 struct snd_soc_dapm_path *path, *p;
2429 const char *sink;
2430 const char *source;
2431 char prefixed_sink[80];
2432 char prefixed_source[80];
2433
2434 if (route->control) {
2435 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002436 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002437 return -EINVAL;
2438 }
2439
2440 if (dapm->codec && dapm->codec->name_prefix) {
2441 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2442 dapm->codec->name_prefix, route->sink);
2443 sink = prefixed_sink;
2444 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2445 dapm->codec->name_prefix, route->source);
2446 source = prefixed_source;
2447 } else {
2448 sink = route->sink;
2449 source = route->source;
2450 }
2451
2452 path = NULL;
2453 list_for_each_entry(p, &dapm->card->paths, list) {
2454 if (strcmp(p->source->name, source) != 0)
2455 continue;
2456 if (strcmp(p->sink->name, sink) != 0)
2457 continue;
2458 path = p;
2459 break;
2460 }
2461
2462 if (path) {
2463 dapm_mark_dirty(path->source, "Route removed");
2464 dapm_mark_dirty(path->sink, "Route removed");
2465
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002466 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002467 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002468 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002469 source, sink);
2470 }
2471
2472 return 0;
2473}
2474
Mark Brown105f1c22008-05-13 14:52:19 +02002475/**
Mark Brown105f1c22008-05-13 14:52:19 +02002476 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002477 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002478 * @route: audio routes
2479 * @num: number of routes
2480 *
2481 * Connects 2 dapm widgets together via a named audio path. The sink is
2482 * the widget receiving the audio signal, whilst the source is the sender
2483 * of the audio signal.
2484 *
2485 * Returns 0 for success else error. On error all resources can be freed
2486 * with a call to snd_soc_card_free().
2487 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002488int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002489 const struct snd_soc_dapm_route *route, int num)
2490{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002491 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002492
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002493 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002494 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002495 r = snd_soc_dapm_add_route(dapm, route);
2496 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002497 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2498 route->source,
2499 route->control ? route->control : "direct",
2500 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002501 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002502 }
2503 route++;
2504 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002505 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002506
Dan Carpenter60884c22012-04-13 22:25:43 +03002507 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002508}
2509EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2510
Mark Brownefcc3c62012-07-05 17:24:19 +01002511/**
2512 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2513 * @dapm: DAPM context
2514 * @route: audio routes
2515 * @num: number of routes
2516 *
2517 * Removes routes from the DAPM context.
2518 */
2519int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2520 const struct snd_soc_dapm_route *route, int num)
2521{
2522 int i, ret = 0;
2523
2524 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2525 for (i = 0; i < num; i++) {
2526 snd_soc_dapm_del_route(dapm, route);
2527 route++;
2528 }
2529 mutex_unlock(&dapm->card->dapm_mutex);
2530
2531 return ret;
2532}
2533EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2534
Mark Brownbf3a9e12011-06-13 16:42:29 +01002535static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2536 const struct snd_soc_dapm_route *route)
2537{
2538 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2539 route->source,
2540 true);
2541 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2542 route->sink,
2543 true);
2544 struct snd_soc_dapm_path *path;
2545 int count = 0;
2546
2547 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002548 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002549 route->source);
2550 return -ENODEV;
2551 }
2552
2553 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002554 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002555 route->sink);
2556 return -ENODEV;
2557 }
2558
2559 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002560 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002561 route->source, route->sink);
2562
2563 list_for_each_entry(path, &source->sinks, list_source) {
2564 if (path->sink == sink) {
2565 path->weak = 1;
2566 count++;
2567 }
2568 }
2569
2570 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002571 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002572 route->source, route->sink);
2573 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002574 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002575 count, route->source, route->sink);
2576
2577 return 0;
2578}
2579
2580/**
2581 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2582 * @dapm: DAPM context
2583 * @route: audio routes
2584 * @num: number of routes
2585 *
2586 * Mark existing routes matching those specified in the passed array
2587 * as being weak, meaning that they are ignored for the purpose of
2588 * power decisions. The main intended use case is for sidetone paths
2589 * which couple audio between other independent paths if they are both
2590 * active in order to make the combination work better at the user
2591 * level but which aren't intended to be "used".
2592 *
2593 * Note that CODEC drivers should not use this as sidetone type paths
2594 * can frequently also be used as bypass paths.
2595 */
2596int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2597 const struct snd_soc_dapm_route *route, int num)
2598{
2599 int i, err;
2600 int ret = 0;
2601
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002602 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002603 for (i = 0; i < num; i++) {
2604 err = snd_soc_dapm_weak_route(dapm, route);
2605 if (err)
2606 ret = err;
2607 route++;
2608 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002609 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002610
2611 return ret;
2612}
2613EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2614
Mark Brown105f1c22008-05-13 14:52:19 +02002615/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002616 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002617 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002618 *
2619 * Checks the codec for any new dapm widgets and creates them if found.
2620 *
2621 * Returns 0 for success.
2622 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002623int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002624{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002625 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002626 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002627 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002628
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002629 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002630
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002631 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002632 {
2633 if (w->new)
2634 continue;
2635
Stephen Warrenfad59882011-04-28 17:37:59 -06002636 if (w->num_kcontrols) {
2637 w->kcontrols = kzalloc(w->num_kcontrols *
2638 sizeof(struct snd_kcontrol *),
2639 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002640 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002641 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002642 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002643 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002644 }
2645
Richard Purdie2b97eab2006-10-06 18:32:18 +02002646 switch(w->id) {
2647 case snd_soc_dapm_switch:
2648 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002649 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002650 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002651 break;
2652 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002653 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002654 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002655 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002656 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002657 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002658 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002659 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002660 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002661 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002662 break;
2663 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002664
2665 /* Read the initial power state from the device */
2666 if (w->reg >= 0) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002667 val = soc_widget_read(w, w->reg) >> w->shift;
2668 val &= w->mask;
2669 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002670 w->power = 1;
2671 }
2672
Richard Purdie2b97eab2006-10-06 18:32:18 +02002673 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002674
Mark Brown7508b122011-10-05 12:09:12 +01002675 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002676 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002677 }
2678
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002679 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2680 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002681 return 0;
2682}
2683EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2684
2685/**
2686 * snd_soc_dapm_get_volsw - dapm mixer get callback
2687 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002688 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002689 *
2690 * Callback to get the value of a dapm mixer control.
2691 *
2692 * Returns 0 for success.
2693 */
2694int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2695 struct snd_ctl_elem_value *ucontrol)
2696{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002697 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002698 struct soc_mixer_control *mc =
2699 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002700 unsigned int reg = mc->reg;
2701 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002702 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002703 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002704 unsigned int invert = mc->invert;
2705
2706 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002707 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002708 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002709 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002710
Richard Purdie2b97eab2006-10-06 18:32:18 +02002711 ucontrol->value.integer.value[0] =
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002712 (snd_soc_read(codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002713 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002714 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002715 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002716
2717 return 0;
2718}
2719EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2720
2721/**
2722 * snd_soc_dapm_put_volsw - dapm mixer set callback
2723 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002724 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002725 *
2726 * Callback to set the value of a dapm mixer control.
2727 *
2728 * Returns 0 for success.
2729 */
2730int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2731 struct snd_ctl_elem_value *ucontrol)
2732{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002733 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002734 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002735 struct soc_mixer_control *mc =
2736 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002737 unsigned int reg = mc->reg;
2738 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002739 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002740 unsigned int mask = (1 << fls(max)) - 1;
2741 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002742 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002743 int connect, change;
2744 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002745
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002746 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002747 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002748 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002749 kcontrol->id.name);
2750
Richard Purdie2b97eab2006-10-06 18:32:18 +02002751 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002752 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002753
2754 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002755 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002756 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002757 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002758
Liam Girdwood3cd04342012-03-09 12:02:08 +00002759 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002760
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002761 change = snd_soc_test_bits(codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002762 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002763 update.kcontrol = kcontrol;
2764 update.reg = reg;
2765 update.mask = mask;
2766 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002767
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002768 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002769
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002770 soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002771
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002772 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002773 }
2774
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002775 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002776 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002777}
2778EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2779
2780/**
2781 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2782 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002783 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002784 *
2785 * Callback to get the value of a dapm enumerated double mixer control.
2786 *
2787 * Returns 0 for success.
2788 */
2789int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2790 struct snd_ctl_elem_value *ucontrol)
2791{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002792 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002793 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002794 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002795
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002796 val = snd_soc_read(codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002797 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002798 if (e->shift_l != e->shift_r)
2799 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002800 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002801
2802 return 0;
2803}
2804EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2805
2806/**
2807 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2808 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002809 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002810 *
2811 * Callback to set the value of a dapm enumerated double mixer control.
2812 *
2813 * Returns 0 for success.
2814 */
2815int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2816 struct snd_ctl_elem_value *ucontrol)
2817{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002818 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002819 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002820 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002821 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002822 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002823 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002824
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002825 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002826 return -EINVAL;
2827 mux = ucontrol->value.enumerated.item[0];
2828 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002829 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002830 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002831 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002832 return -EINVAL;
2833 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002834 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002835 }
2836
Liam Girdwood3cd04342012-03-09 12:02:08 +00002837 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002838
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002839 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002840 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002841 update.kcontrol = kcontrol;
2842 update.reg = e->reg;
2843 update.mask = mask;
2844 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002845 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002846
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002847 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002848
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002849 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002850 }
2851
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002852 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002853 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002854}
2855EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2856
2857/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002858 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2859 * @kcontrol: mixer control
2860 * @ucontrol: control element information
2861 *
2862 * Returns 0 for success.
2863 */
2864int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2865 struct snd_ctl_elem_value *ucontrol)
2866{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002867 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
Mark Brownd2b247a2009-10-06 15:21:04 +01002868 return 0;
2869}
2870EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2871
2872/**
2873 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2874 * @kcontrol: mixer control
2875 * @ucontrol: control element information
2876 *
2877 * Returns 0 for success.
2878 */
2879int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2880 struct snd_ctl_elem_value *ucontrol)
2881{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002882 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002883 struct snd_soc_card *card = codec->card;
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002884 unsigned int value;
Mark Brownd2b247a2009-10-06 15:21:04 +01002885 struct soc_enum *e =
2886 (struct soc_enum *)kcontrol->private_value;
2887 int change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002888
2889 if (ucontrol->value.enumerated.item[0] >= e->max)
2890 return -EINVAL;
2891
Liam Girdwood3cd04342012-03-09 12:02:08 +00002892 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002893
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002894 value = ucontrol->value.enumerated.item[0];
2895 change = dapm_kcontrol_set_value(kcontrol, value);
2896 if (change)
2897 soc_dapm_mux_update_power(card, kcontrol, value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002898
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002899 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002900 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002901}
2902EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2903
2904/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002905 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2906 * callback
2907 * @kcontrol: mixer control
2908 * @ucontrol: control element information
2909 *
2910 * Callback to get the value of a dapm semi enumerated double mixer control.
2911 *
2912 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2913 * used for handling bitfield coded enumeration for example.
2914 *
2915 * Returns 0 for success.
2916 */
2917int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2918 struct snd_ctl_elem_value *ucontrol)
2919{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002920 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002921 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002922 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002923
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002924 reg_val = snd_soc_read(codec, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002925 val = (reg_val >> e->shift_l) & e->mask;
2926 for (mux = 0; mux < e->max; mux++) {
2927 if (val == e->values[mux])
2928 break;
2929 }
2930 ucontrol->value.enumerated.item[0] = mux;
2931 if (e->shift_l != e->shift_r) {
2932 val = (reg_val >> e->shift_r) & e->mask;
2933 for (mux = 0; mux < e->max; mux++) {
2934 if (val == e->values[mux])
2935 break;
2936 }
2937 ucontrol->value.enumerated.item[1] = mux;
2938 }
2939
2940 return 0;
2941}
2942EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2943
2944/**
2945 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2946 * callback
2947 * @kcontrol: mixer control
2948 * @ucontrol: control element information
2949 *
2950 * Callback to set the value of a dapm semi enumerated double mixer control.
2951 *
2952 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2953 * used for handling bitfield coded enumeration for example.
2954 *
2955 * Returns 0 for success.
2956 */
2957int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2958 struct snd_ctl_elem_value *ucontrol)
2959{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002960 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002961 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002962 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002963 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002964 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002965 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002966
2967 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2968 return -EINVAL;
2969 mux = ucontrol->value.enumerated.item[0];
2970 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2971 mask = e->mask << e->shift_l;
2972 if (e->shift_l != e->shift_r) {
2973 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2974 return -EINVAL;
2975 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2976 mask |= e->mask << e->shift_r;
2977 }
2978
Liam Girdwood3cd04342012-03-09 12:02:08 +00002979 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002980
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002981 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002982 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002983 update.kcontrol = kcontrol;
2984 update.reg = e->reg;
2985 update.mask = mask;
2986 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002987 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002988
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002989 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002990
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002991 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002992 }
2993
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002994 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002995 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002996}
2997EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2998
2999/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003000 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3001 *
3002 * @kcontrol: mixer control
3003 * @uinfo: control element information
3004 *
3005 * Callback to provide information about a pin switch control.
3006 */
3007int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3008 struct snd_ctl_elem_info *uinfo)
3009{
3010 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3011 uinfo->count = 1;
3012 uinfo->value.integer.min = 0;
3013 uinfo->value.integer.max = 1;
3014
3015 return 0;
3016}
3017EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3018
3019/**
3020 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3021 *
3022 * @kcontrol: mixer control
3023 * @ucontrol: Value
3024 */
3025int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3026 struct snd_ctl_elem_value *ucontrol)
3027{
Mark Brown48a8c392012-02-14 17:11:15 -08003028 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003029 const char *pin = (const char *)kcontrol->private_value;
3030
Liam Girdwood3cd04342012-03-09 12:02:08 +00003031 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003032
3033 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003034 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003035
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003036 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003037
3038 return 0;
3039}
3040EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3041
3042/**
3043 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3044 *
3045 * @kcontrol: mixer control
3046 * @ucontrol: Value
3047 */
3048int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3049 struct snd_ctl_elem_value *ucontrol)
3050{
Mark Brown48a8c392012-02-14 17:11:15 -08003051 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003052 const char *pin = (const char *)kcontrol->private_value;
3053
Liam Girdwood3cd04342012-03-09 12:02:08 +00003054 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003055
3056 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003057 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003058 else
Mark Brown48a8c392012-02-14 17:11:15 -08003059 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003060
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003061 mutex_unlock(&card->dapm_mutex);
3062
Mark Brown48a8c392012-02-14 17:11:15 -08003063 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003064 return 0;
3065}
3066EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3067
Mark Brown5ba06fc2012-02-16 11:07:13 -08003068static struct snd_soc_dapm_widget *
3069snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3070 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003071{
3072 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003073 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003074
3075 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003076 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003077
Mark Brown62ea8742012-01-21 21:14:48 +00003078 switch (w->id) {
3079 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003080 w->regulator = devm_regulator_get(dapm->dev, w->name);
3081 if (IS_ERR(w->regulator)) {
3082 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003083 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003084 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003085 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003086 }
Mark Brown8784c772013-01-10 19:33:47 +00003087
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003088 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003089 ret = regulator_allow_bypass(w->regulator, true);
3090 if (ret != 0)
3091 dev_warn(w->dapm->dev,
3092 "ASoC: Failed to unbypass %s: %d\n",
3093 w->name, ret);
3094 }
Mark Brown62ea8742012-01-21 21:14:48 +00003095 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003096 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003097#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003098 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003099 if (IS_ERR(w->clk)) {
3100 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003101 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003102 w->name, ret);
3103 return NULL;
3104 }
Mark Brownec029952012-06-04 08:16:20 +01003105#else
3106 return NULL;
3107#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003108 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003109 default:
3110 break;
3111 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003112
Mark Brown88e8b9a2011-03-02 18:18:24 +00003113 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003114 w->name = kasprintf(GFP_KERNEL, "%s %s",
3115 dapm->codec->name_prefix, widget->name);
3116 else
3117 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3118
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003119 if (w->name == NULL) {
3120 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003121 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003122 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003123
Mark Brown7ca3a182011-10-08 14:04:50 +01003124 switch (w->id) {
3125 case snd_soc_dapm_switch:
3126 case snd_soc_dapm_mixer:
3127 case snd_soc_dapm_mixer_named_ctl:
3128 w->power_check = dapm_generic_check_power;
3129 break;
3130 case snd_soc_dapm_mux:
3131 case snd_soc_dapm_virt_mux:
3132 case snd_soc_dapm_value_mux:
3133 w->power_check = dapm_generic_check_power;
3134 break;
Mark Brown46162742013-06-05 19:36:11 +01003135 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003136 w->power_check = dapm_adc_check_power;
3137 break;
Mark Brown46162742013-06-05 19:36:11 +01003138 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003139 w->power_check = dapm_dac_check_power;
3140 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003141 case snd_soc_dapm_adc:
3142 case snd_soc_dapm_aif_out:
3143 case snd_soc_dapm_dac:
3144 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003145 case snd_soc_dapm_pga:
3146 case snd_soc_dapm_out_drv:
3147 case snd_soc_dapm_input:
3148 case snd_soc_dapm_output:
3149 case snd_soc_dapm_micbias:
3150 case snd_soc_dapm_spk:
3151 case snd_soc_dapm_hp:
3152 case snd_soc_dapm_mic:
3153 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003154 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003155 w->power_check = dapm_generic_check_power;
3156 break;
3157 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003158 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003159 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003160 w->power_check = dapm_supply_check_power;
3161 break;
3162 default:
3163 w->power_check = dapm_always_on_check_power;
3164 break;
3165 }
3166
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003167 w->dapm = dapm;
3168 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003169 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003170 INIT_LIST_HEAD(&w->sources);
3171 INIT_LIST_HEAD(&w->sinks);
3172 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003173 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003174 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003175
3176 /* machine layer set ups unconnected pins and insertions */
3177 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003178 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003179}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003180
3181/**
Mark Brown4ba13272008-05-13 14:51:19 +02003182 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003183 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003184 * @widget: widget array
3185 * @num: number of widgets
3186 *
3187 * Creates new DAPM controls based upon the templates.
3188 *
3189 * Returns 0 for success else error.
3190 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003191int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003192 const struct snd_soc_dapm_widget *widget,
3193 int num)
3194{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003195 struct snd_soc_dapm_widget *w;
3196 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003197 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003198
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003199 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003200 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003201 w = snd_soc_dapm_new_control(dapm, widget);
3202 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003203 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003204 "ASoC: Failed to create DAPM control %s\n",
3205 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003206 ret = -ENOMEM;
3207 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003208 }
Mark Brown4ba13272008-05-13 14:51:19 +02003209 widget++;
3210 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003211 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003212 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003213}
3214EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3215
Mark Brownc74184e2012-04-04 22:12:09 +01003216static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3217 struct snd_kcontrol *kcontrol, int event)
3218{
3219 struct snd_soc_dapm_path *source_p, *sink_p;
3220 struct snd_soc_dai *source, *sink;
3221 const struct snd_soc_pcm_stream *config = w->params;
3222 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003223 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003224 u64 fmt;
3225 int ret;
3226
3227 BUG_ON(!config);
3228 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3229
3230 /* We only support a single source and sink, pick the first */
3231 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3232 list_sink);
3233 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3234 list_source);
3235
3236 BUG_ON(!source_p || !sink_p);
3237 BUG_ON(!sink_p->source || !source_p->sink);
3238 BUG_ON(!source_p->source || !sink_p->sink);
3239
3240 source = source_p->source->priv;
3241 sink = sink_p->sink->priv;
3242
3243 /* Be a little careful as we don't want to overflow the mask array */
3244 if (config->formats) {
3245 fmt = ffs(config->formats) - 1;
3246 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003247 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003248 config->formats);
3249 fmt = 0;
3250 }
3251
3252 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003253 params = kzalloc(sizeof(*params), GFP_KERNEL);
3254 if (!params) {
3255 ret = -ENOMEM;
3256 goto out;
3257 }
3258 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003259
Mark Brown9747cec2012-04-26 19:12:21 +01003260 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003261 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003262 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003263 config->rate_max;
3264
Mark Brown9747cec2012-04-26 19:12:21 +01003265 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003266 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003267 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003268 = config->channels_max;
3269
3270 memset(&substream, 0, sizeof(substream));
3271
3272 switch (event) {
3273 case SND_SOC_DAPM_PRE_PMU:
3274 if (source->driver->ops && source->driver->ops->hw_params) {
3275 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3276 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003277 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003278 if (ret != 0) {
3279 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003280 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003281 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003282 }
3283 }
3284
3285 if (sink->driver->ops && sink->driver->ops->hw_params) {
3286 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003287 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003288 sink);
3289 if (ret != 0) {
3290 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003291 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003292 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003293 }
3294 }
3295 break;
3296
3297 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003298 ret = snd_soc_dai_digital_mute(sink, 0,
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 unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003302 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003303 break;
3304
3305 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003306 ret = snd_soc_dai_digital_mute(sink, 1,
3307 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003308 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003309 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003310 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003311 break;
3312
3313 default:
3314 BUG();
3315 return -EINVAL;
3316 }
3317
Mark Brown9747cec2012-04-26 19:12:21 +01003318out:
3319 kfree(params);
3320 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003321}
3322
3323int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3324 const struct snd_soc_pcm_stream *params,
3325 struct snd_soc_dapm_widget *source,
3326 struct snd_soc_dapm_widget *sink)
3327{
3328 struct snd_soc_dapm_route routes[2];
3329 struct snd_soc_dapm_widget template;
3330 struct snd_soc_dapm_widget *w;
3331 size_t len;
3332 char *link_name;
3333
3334 len = strlen(source->name) + strlen(sink->name) + 2;
3335 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3336 if (!link_name)
3337 return -ENOMEM;
3338 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3339
3340 memset(&template, 0, sizeof(template));
3341 template.reg = SND_SOC_NOPM;
3342 template.id = snd_soc_dapm_dai_link;
3343 template.name = link_name;
3344 template.event = snd_soc_dai_link_event;
3345 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3346 SND_SOC_DAPM_PRE_PMD;
3347
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003348 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003349
3350 w = snd_soc_dapm_new_control(&card->dapm, &template);
3351 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003352 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003353 link_name);
3354 return -ENOMEM;
3355 }
3356
3357 w->params = params;
3358
3359 memset(&routes, 0, sizeof(routes));
3360
3361 routes[0].source = source->name;
3362 routes[0].sink = link_name;
3363 routes[1].source = link_name;
3364 routes[1].sink = sink->name;
3365
3366 return snd_soc_dapm_add_routes(&card->dapm, routes,
3367 ARRAY_SIZE(routes));
3368}
3369
Mark Brown888df392012-02-16 19:37:51 -08003370int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3371 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003372{
Mark Brown888df392012-02-16 19:37:51 -08003373 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003374 struct snd_soc_dapm_widget *w;
3375
Mark Brown888df392012-02-16 19:37:51 -08003376 WARN_ON(dapm->dev != dai->dev);
3377
3378 memset(&template, 0, sizeof(template));
3379 template.reg = SND_SOC_NOPM;
3380
3381 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003382 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003383 template.name = dai->driver->playback.stream_name;
3384 template.sname = dai->driver->playback.stream_name;
3385
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003386 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003387 template.name);
3388
3389 w = snd_soc_dapm_new_control(dapm, &template);
3390 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003391 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003392 dai->driver->playback.stream_name);
3393 }
3394
3395 w->priv = dai;
3396 dai->playback_widget = w;
3397 }
3398
3399 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003400 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003401 template.name = dai->driver->capture.stream_name;
3402 template.sname = dai->driver->capture.stream_name;
3403
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003404 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003405 template.name);
3406
3407 w = snd_soc_dapm_new_control(dapm, &template);
3408 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003409 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003410 dai->driver->capture.stream_name);
3411 }
3412
3413 w->priv = dai;
3414 dai->capture_widget = w;
3415 }
3416
3417 return 0;
3418}
3419
3420int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3421{
3422 struct snd_soc_dapm_widget *dai_w, *w;
3423 struct snd_soc_dai *dai;
3424 struct snd_soc_dapm_route r;
3425
3426 memset(&r, 0, sizeof(r));
3427
3428 /* For each DAI widget... */
3429 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003430 switch (dai_w->id) {
3431 case snd_soc_dapm_dai_in:
3432 case snd_soc_dapm_dai_out:
3433 break;
3434 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003435 continue;
Mark Brown46162742013-06-05 19:36:11 +01003436 }
Mark Brown888df392012-02-16 19:37:51 -08003437
3438 dai = dai_w->priv;
3439
3440 /* ...find all widgets with the same stream and link them */
3441 list_for_each_entry(w, &card->widgets, list) {
3442 if (w->dapm != dai_w->dapm)
3443 continue;
3444
Mark Brown46162742013-06-05 19:36:11 +01003445 switch (w->id) {
3446 case snd_soc_dapm_dai_in:
3447 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003448 continue;
Mark Brown46162742013-06-05 19:36:11 +01003449 default:
3450 break;
3451 }
Mark Brown888df392012-02-16 19:37:51 -08003452
3453 if (!w->sname)
3454 continue;
3455
3456 if (dai->driver->playback.stream_name &&
3457 strstr(w->sname,
3458 dai->driver->playback.stream_name)) {
3459 r.source = dai->playback_widget->name;
3460 r.sink = w->name;
3461 dev_dbg(dai->dev, "%s -> %s\n",
3462 r.source, r.sink);
3463
3464 snd_soc_dapm_add_route(w->dapm, &r);
3465 }
3466
3467 if (dai->driver->capture.stream_name &&
3468 strstr(w->sname,
3469 dai->driver->capture.stream_name)) {
3470 r.source = w->name;
3471 r.sink = dai->capture_widget->name;
3472 dev_dbg(dai->dev, "%s -> %s\n",
3473 r.source, r.sink);
3474
3475 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003476 }
3477 }
3478 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003479
Mark Brown888df392012-02-16 19:37:51 -08003480 return 0;
3481}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003482
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003483static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3484 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003485{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003486
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003487 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3488 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3489 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003490
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003491 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3492 w_cpu = cpu_dai->playback_widget;
3493 w_codec = codec_dai->playback_widget;
3494 } else {
3495 w_cpu = cpu_dai->capture_widget;
3496 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003497 }
3498
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003499 if (w_cpu) {
3500
3501 dapm_mark_dirty(w_cpu, "stream event");
3502
3503 switch (event) {
3504 case SND_SOC_DAPM_STREAM_START:
3505 w_cpu->active = 1;
3506 break;
3507 case SND_SOC_DAPM_STREAM_STOP:
3508 w_cpu->active = 0;
3509 break;
3510 case SND_SOC_DAPM_STREAM_SUSPEND:
3511 case SND_SOC_DAPM_STREAM_RESUME:
3512 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3513 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3514 break;
3515 }
3516 }
3517
3518 if (w_codec) {
3519
3520 dapm_mark_dirty(w_codec, "stream event");
3521
3522 switch (event) {
3523 case SND_SOC_DAPM_STREAM_START:
3524 w_codec->active = 1;
3525 break;
3526 case SND_SOC_DAPM_STREAM_STOP:
3527 w_codec->active = 0;
3528 break;
3529 case SND_SOC_DAPM_STREAM_SUSPEND:
3530 case SND_SOC_DAPM_STREAM_RESUME:
3531 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3532 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3533 break;
3534 }
3535 }
3536
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003537 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003538}
3539
3540/**
3541 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3542 * @rtd: PCM runtime data
3543 * @stream: stream name
3544 * @event: stream event
3545 *
3546 * Sends a stream event to the dapm core. The core then makes any
3547 * necessary widget power changes.
3548 *
3549 * Returns 0 for success else error.
3550 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003551void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3552 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003553{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003554 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003555
Liam Girdwood3cd04342012-03-09 12:02:08 +00003556 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003557 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003558 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003559}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003560
3561/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003562 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003563 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003564 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003565 *
Mark Brown74b8f952009-06-06 11:26:15 +01003566 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003567 * a valid audio route and active audio stream.
3568 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3569 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003570 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003571int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003572{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003573 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003574}
Liam Girdwooda5302182008-07-07 13:35:17 +01003575EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003576
3577/**
Mark Brownda341832010-03-15 19:23:37 +00003578 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003579 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003580 * @pin: pin name
3581 *
3582 * Enables input/output pin regardless of any other state. This is
3583 * intended for use with microphone bias supplies used in microphone
3584 * jack detection.
3585 *
3586 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3587 * do any widget power switching.
3588 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003589int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3590 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003591{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003592 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003593
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003594 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003595 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003596 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003597 }
3598
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003599 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003600 w->connected = 1;
3601 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003602 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003603
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003604 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003605}
3606EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3607
3608/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003609 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003610 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003611 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003612 *
Mark Brown74b8f952009-06-06 11:26:15 +01003613 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003614 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3615 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003616 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003617int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3618 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003619{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003620 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003621}
3622EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3623
3624/**
Mark Brown5817b522008-09-24 11:23:11 +01003625 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003626 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003627 * @pin: pin name
3628 *
3629 * Marks the specified pin as being not connected, disabling it along
3630 * any parent or child widgets. At present this is identical to
3631 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3632 * additional things such as disabling controls which only affect
3633 * paths through the pin.
3634 *
3635 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3636 * do any widget power switching.
3637 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003638int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003639{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003640 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003641}
3642EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3643
3644/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003645 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003646 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003647 * @pin: audio signal pin endpoint (or start point)
3648 *
3649 * Get audio pin status - connected or disconnected.
3650 *
3651 * Returns 1 for connected otherwise 0.
3652 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003653int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3654 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003655{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003656 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003657
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003658 if (w)
3659 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003660
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003661 return 0;
3662}
Liam Girdwooda5302182008-07-07 13:35:17 +01003663EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003664
3665/**
Mark Brown1547aba2010-05-07 21:11:40 +01003666 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003667 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003668 * @pin: audio signal pin endpoint (or start point)
3669 *
3670 * Mark the given endpoint or pin as ignoring suspend. When the
3671 * system is disabled a path between two endpoints flagged as ignoring
3672 * suspend will not be disabled. The path must already be enabled via
3673 * normal means at suspend time, it will not be turned on if it was not
3674 * already enabled.
3675 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003676int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3677 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003678{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003679 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003680
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003681 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003682 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003683 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003684 }
3685
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003686 w->ignore_suspend = 1;
3687
3688 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003689}
3690EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3691
Stephen Warren16332812011-11-23 12:42:04 -07003692static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3693 struct snd_soc_dapm_widget *w)
3694{
3695 struct snd_soc_dapm_path *p;
3696
3697 list_for_each_entry(p, &card->paths, list) {
3698 if ((p->source == w) || (p->sink == w)) {
3699 dev_dbg(card->dev,
3700 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3701 p->source->name, p->source->id, p->source->dapm,
3702 p->sink->name, p->sink->id, p->sink->dapm);
3703
3704 /* Connected to something other than the codec */
3705 if (p->source->dapm != p->sink->dapm)
3706 return true;
3707 /*
3708 * Loopback connection from codec external pin to
3709 * codec external pin
3710 */
3711 if (p->sink->id == snd_soc_dapm_input) {
3712 switch (p->source->id) {
3713 case snd_soc_dapm_output:
3714 case snd_soc_dapm_micbias:
3715 return true;
3716 default:
3717 break;
3718 }
3719 }
3720 }
3721 }
3722
3723 return false;
3724}
3725
3726/**
3727 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3728 * @codec: The codec whose pins should be processed
3729 *
3730 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3731 * which are unused. Pins are used if they are connected externally to the
3732 * codec, whether that be to some other device, or a loop-back connection to
3733 * the codec itself.
3734 */
3735void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3736{
3737 struct snd_soc_card *card = codec->card;
3738 struct snd_soc_dapm_context *dapm = &codec->dapm;
3739 struct snd_soc_dapm_widget *w;
3740
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003741 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003742 &card->dapm, &codec->dapm);
3743
3744 list_for_each_entry(w, &card->widgets, list) {
3745 if (w->dapm != dapm)
3746 continue;
3747 switch (w->id) {
3748 case snd_soc_dapm_input:
3749 case snd_soc_dapm_output:
3750 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003751 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003752 w->name);
3753 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003754 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003755 "... Not in map; disabling\n");
3756 snd_soc_dapm_nc_pin(dapm, w->name);
3757 }
3758 break;
3759 default:
3760 break;
3761 }
3762 }
3763}
3764
Mark Brown1547aba2010-05-07 21:11:40 +01003765/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003766 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003767 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003768 *
3769 * Free all dapm widgets and resources.
3770 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003771void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003772{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003773 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003774 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003775 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003776 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003777}
3778EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3779
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003780static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003781{
Liam Girdwood01005a72012-07-06 16:57:05 +01003782 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003783 struct snd_soc_dapm_widget *w;
3784 LIST_HEAD(down_list);
3785 int powerdown = 0;
3786
Liam Girdwood01005a72012-07-06 16:57:05 +01003787 mutex_lock(&card->dapm_mutex);
3788
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003789 list_for_each_entry(w, &dapm->card->widgets, list) {
3790 if (w->dapm != dapm)
3791 continue;
Mark Brown51737472009-06-22 13:16:51 +01003792 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003793 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003794 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003795 powerdown = 1;
3796 }
3797 }
3798
3799 /* If there were no widgets to power down we're already in
3800 * standby.
3801 */
3802 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003803 if (dapm->bias_level == SND_SOC_BIAS_ON)
3804 snd_soc_dapm_set_bias_level(dapm,
3805 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003806 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003807 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3808 snd_soc_dapm_set_bias_level(dapm,
3809 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003810 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003811
3812 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003813}
Mark Brown51737472009-06-22 13:16:51 +01003814
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003815/*
3816 * snd_soc_dapm_shutdown - callback for system shutdown
3817 */
3818void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3819{
3820 struct snd_soc_codec *codec;
3821
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003822 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003823 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003824 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3825 snd_soc_dapm_set_bias_level(&codec->dapm,
3826 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003827 }
Mark Brown51737472009-06-22 13:16:51 +01003828}
3829
Richard Purdie2b97eab2006-10-06 18:32:18 +02003830/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003831MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003832MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3833MODULE_LICENSE("GPL");