blob: b779d36d5b3acfb456d06c0475850075442f6abf [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
Liam Girdwood612a3fe2012-02-06 16:05:29 +000017 * mic/headphone insertion events.
Richard Purdie2b97eab2006-10-06 18:32:18 +020018 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Liam Girdwood612a3fe2012-02-06 16:05:29 +000021 * o Delayed power down of audio subsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020024 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080029#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020030#include <linux/delay.h>
31#include <linux/pm.h>
32#include <linux/bitops.h>
33#include <linux/platform_device.h>
34#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020035#include <linux/debugfs.h>
Mark Brownf1aac482011-12-05 15:17:06 +000036#include <linux/pm_runtime.h>
Mark Brown62ea8742012-01-21 21:14:48 +000037#include <linux/regulator/consumer.h>
Ola Liljad7e7eb92012-05-24 15:26:25 +020038#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020040#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020043#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020044#include <sound/initval.h>
45
Mark Brown84e90932010-11-04 00:07:02 -040046#include <trace/events/asoc.h>
47
Mark Brownde02d072011-09-20 21:43:24 +010048#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49
Richard Purdie2b97eab2006-10-06 18:32:18 +020050/* dapm power sequences - make this per codec in the future */
51static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010052 [snd_soc_dapm_pre] = 0,
53 [snd_soc_dapm_supply] = 1,
Mark Brown62ea8742012-01-21 21:14:48 +000054 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020055 [snd_soc_dapm_clock_supply] = 1,
Mark Brown38357ab2009-06-06 19:03:23 +010056 [snd_soc_dapm_micbias] = 2,
Mark Brownc74184e2012-04-04 22:12:09 +010057 [snd_soc_dapm_dai_link] = 2,
Mark Brown46162742013-06-05 19:36:11 +010058 [snd_soc_dapm_dai_in] = 3,
59 [snd_soc_dapm_dai_out] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010060 [snd_soc_dapm_aif_in] = 3,
61 [snd_soc_dapm_aif_out] = 3,
62 [snd_soc_dapm_mic] = 4,
63 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000064 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010065 [snd_soc_dapm_value_mux] = 5,
66 [snd_soc_dapm_dac] = 6,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020067 [snd_soc_dapm_switch] = 7,
Mark Brown010ff262009-08-17 17:39:22 +010068 [snd_soc_dapm_mixer] = 7,
69 [snd_soc_dapm_mixer_named_ctl] = 7,
70 [snd_soc_dapm_pga] = 8,
71 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060072 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010073 [snd_soc_dapm_hp] = 10,
74 [snd_soc_dapm_spk] = 10,
Mark Brown7e1f7c82012-04-12 17:29:36 +010075 [snd_soc_dapm_line] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010076 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020077};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000078
Richard Purdie2b97eab2006-10-06 18:32:18 +020079static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010080 [snd_soc_dapm_pre] = 0,
81 [snd_soc_dapm_adc] = 1,
82 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010083 [snd_soc_dapm_spk] = 2,
Mark Brown7e1f7c82012-04-12 17:29:36 +010084 [snd_soc_dapm_line] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060085 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010086 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020087 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +010088 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010089 [snd_soc_dapm_mixer] = 5,
90 [snd_soc_dapm_dac] = 6,
91 [snd_soc_dapm_mic] = 7,
92 [snd_soc_dapm_micbias] = 8,
93 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000094 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010095 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010096 [snd_soc_dapm_aif_in] = 10,
97 [snd_soc_dapm_aif_out] = 10,
Mark Brown46162742013-06-05 19:36:11 +010098 [snd_soc_dapm_dai_in] = 10,
99 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100100 [snd_soc_dapm_dai_link] = 11,
Ola Liljad7e7eb92012-05-24 15:26:25 +0200101 [snd_soc_dapm_clock_supply] = 12,
Mark Brownc74184e2012-04-04 22:12:09 +0100102 [snd_soc_dapm_regulator_supply] = 12,
103 [snd_soc_dapm_supply] = 12,
104 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200105};
106
Troy Kisky12ef1932008-10-13 17:42:14 -0700107static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100108{
109 if (pop_time)
110 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
111}
112
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200113static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100114{
115 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200116 char *buf;
117
118 if (!pop_time)
119 return;
120
121 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
122 if (buf == NULL)
123 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100124
125 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200126 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100127 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100128 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200129
130 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100131}
132
Mark Browndb432b42011-10-03 21:06:40 +0100133static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
134{
135 return !list_empty(&w->dirty);
136}
137
Mark Brown25c77c52011-10-08 13:36:03 +0100138void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100139{
Mark Brown75c1f892011-10-04 22:28:08 +0100140 if (!dapm_dirty_widget(w)) {
141 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
142 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100143 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100144 }
Mark Browndb432b42011-10-03 21:06:40 +0100145}
Mark Brown25c77c52011-10-08 13:36:03 +0100146EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100147
Mark Browne2d32ff2012-08-31 17:38:32 -0700148void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
149{
150 struct snd_soc_card *card = dapm->card;
151 struct snd_soc_dapm_widget *w;
152
153 mutex_lock(&card->dapm_mutex);
154
155 list_for_each_entry(w, &card->widgets, list) {
156 switch (w->id) {
157 case snd_soc_dapm_input:
158 case snd_soc_dapm_output:
159 dapm_mark_dirty(w, "Rechecking inputs and outputs");
160 break;
161 default:
162 break;
163 }
164 }
165
166 mutex_unlock(&card->dapm_mutex);
167}
168EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
169
Richard Purdie2b97eab2006-10-06 18:32:18 +0200170/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100171static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200172 const struct snd_soc_dapm_widget *_widget)
173{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100174 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200175}
176
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200177struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200178 unsigned int value;
Lars-Peter 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 Brown99497882010-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 Brown99497882010-05-07 20:24:05 +0100805
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000806 switch (level) {
Mark Brown99497882010-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 Brown99497882010-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)) {
1125 if (w->invert & 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 {
1135 if (w->invert & 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 Brownd8050022011-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 Brownd8050022011-09-28 18:28:23 +01001174 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001175 w->new_power = 1;
Mark Brownd8050022011-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 Brownd8050022011-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;
1363 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001364 unsigned int value = 0;
1365 unsigned int mask = 0;
1366 unsigned int cur_mask;
1367
1368 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1369 power_list)->reg;
1370
1371 list_for_each_entry(w, pending, power_list) {
1372 cur_mask = 1 << w->shift;
1373 BUG_ON(reg != w->reg);
1374
1375 if (w->invert)
1376 power = !w->power;
1377 else
1378 power = w->power;
1379
1380 mask |= cur_mask;
1381 if (power)
1382 value |= cur_mask;
1383
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001384 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001385 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1386 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001387
Mark Brown68f89ad2010-11-03 23:51:49 -04001388 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001389 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1390 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001391 }
1392
Mark Brown81628102009-06-07 13:21:24 +01001393 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001394 /* Any widget will do, they should all be updating the
1395 * same register.
1396 */
1397 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1398 power_list);
1399
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001400 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001401 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001402 value, mask, reg, card->pop_time);
1403 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001404 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001405 }
1406
1407 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001408 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1409 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001410 }
Mark Brown42aa3412009-03-01 19:21:10 +00001411}
1412
Mark Brownb22ead22009-06-07 12:51:26 +01001413/* Apply a DAPM power sequence.
1414 *
1415 * We walk over a pre-sorted list of widgets to apply power to. In
1416 * order to minimise the number of writes to the device required
1417 * multiple widgets will be updated in a single write where possible.
1418 * Currently anything that requires more than a single write is not
1419 * handled.
1420 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001421static void dapm_seq_run(struct snd_soc_card *card,
1422 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001423{
1424 struct snd_soc_dapm_widget *w, *n;
1425 LIST_HEAD(pending);
1426 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001427 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001428 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001429 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001430 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001431 int *sort;
1432
1433 if (power_up)
1434 sort = dapm_up_seq;
1435 else
1436 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001437
Mark Brownb22ead22009-06-07 12:51:26 +01001438 list_for_each_entry_safe(w, n, list, power_list) {
1439 ret = 0;
1440
1441 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001442 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001443 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001444 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001445 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001446
Mark Brown474b62d2011-01-18 16:14:44 +00001447 if (cur_dapm && cur_dapm->seq_notifier) {
1448 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1449 if (sort[i] == cur_sort)
1450 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001451 i,
1452 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001453 }
1454
Mark Brownb22ead22009-06-07 12:51:26 +01001455 INIT_LIST_HEAD(&pending);
1456 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001457 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001458 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001459 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001460 }
1461
Mark Brown163cac02009-06-07 10:12:52 +01001462 switch (w->id) {
1463 case snd_soc_dapm_pre:
1464 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001465 list_for_each_entry_safe_continue(w, n, list,
1466 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001467
Mark Brownb22ead22009-06-07 12:51:26 +01001468 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001469 ret = w->event(w,
1470 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001471 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001472 ret = w->event(w,
1473 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001474 break;
1475
1476 case snd_soc_dapm_post:
1477 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001478 list_for_each_entry_safe_continue(w, n, list,
1479 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001480
Mark Brownb22ead22009-06-07 12:51:26 +01001481 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001482 ret = w->event(w,
1483 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001484 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001485 ret = w->event(w,
1486 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001487 break;
1488
Mark Brown163cac02009-06-07 10:12:52 +01001489 default:
Mark Brown81628102009-06-07 13:21:24 +01001490 /* Queue it up for application */
1491 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001492 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001493 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001494 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001495 list_move(&w->power_list, &pending);
1496 break;
Mark Brown163cac02009-06-07 10:12:52 +01001497 }
Mark Brownb22ead22009-06-07 12:51:26 +01001498
1499 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001500 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001501 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001502 }
Mark Brownb22ead22009-06-07 12:51:26 +01001503
1504 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001505 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001506
1507 if (cur_dapm && cur_dapm->seq_notifier) {
1508 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1509 if (sort[i] == cur_sort)
1510 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001511 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001512 }
Mark Brown163cac02009-06-07 10:12:52 +01001513}
1514
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001515static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001516{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001517 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001518 struct snd_soc_dapm_widget_list *wlist;
1519 struct snd_soc_dapm_widget *w = NULL;
1520 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001521 int ret;
1522
1523 if (!update)
1524 return;
1525
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001526 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001527
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001528 for (wi = 0; wi < wlist->num_widgets; wi++) {
1529 w = wlist->widgets[wi];
1530
1531 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1532 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1533 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001534 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001535 w->name, ret);
1536 }
Mark Brown97404f22010-12-14 16:13:57 +00001537 }
1538
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001539 if (!w)
1540 return;
1541
Liam Girdwood49575fb52012-03-06 18:16:19 +00001542 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001543 update->val);
1544 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001545 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001546 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001547
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001548 for (wi = 0; wi < wlist->num_widgets; wi++) {
1549 w = wlist->widgets[wi];
1550
1551 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1552 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1553 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001554 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001555 w->name, ret);
1556 }
Mark Brown97404f22010-12-14 16:13:57 +00001557 }
1558}
1559
Mark Brown9d0624a2011-02-18 11:49:43 -08001560/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1561 * they're changing state.
1562 */
1563static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1564{
1565 struct snd_soc_dapm_context *d = data;
1566 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001567
Mark Brown56fba412011-06-04 11:25:10 +01001568 /* If we're off and we're not supposed to be go into STANDBY */
1569 if (d->bias_level == SND_SOC_BIAS_OFF &&
1570 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001571 if (d->dev)
1572 pm_runtime_get_sync(d->dev);
1573
Mark Brown9d0624a2011-02-18 11:49:43 -08001574 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1575 if (ret != 0)
1576 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001577 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001578 }
1579
Mark Brown56fba412011-06-04 11:25:10 +01001580 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1581 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001582 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1583 if (ret != 0)
1584 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001585 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001586 }
1587}
1588
1589/* Async callback run prior to DAPM sequences - brings to their final
1590 * state.
1591 */
1592static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1593{
1594 struct snd_soc_dapm_context *d = data;
1595 int ret;
1596
1597 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001598 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1599 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1600 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001601 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1602 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001603 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001604 ret);
1605 }
1606
1607 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001608 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1609 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001610 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1611 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001612 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1613 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001614
1615 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001616 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001617 }
1618
1619 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001620 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1621 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001622 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1623 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001624 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001625 ret);
1626 }
1627}
Mark Brown97404f22010-12-14 16:13:57 +00001628
Mark Brownfe4fda52011-10-03 22:36:57 +01001629static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1630 bool power, bool connect)
1631{
1632 /* If a connection is being made or broken then that update
1633 * will have marked the peer dirty, otherwise the widgets are
1634 * not connected and this update has no impact. */
1635 if (!connect)
1636 return;
1637
1638 /* If the peer is already in the state we're moving to then we
1639 * won't have an impact on it. */
1640 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001641 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001642}
1643
Mark Brown05623c42011-09-28 17:02:31 +01001644static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1645 struct list_head *up_list,
1646 struct list_head *down_list)
1647{
Mark Browndb432b42011-10-03 21:06:40 +01001648 struct snd_soc_dapm_path *path;
1649
Mark Brown05623c42011-09-28 17:02:31 +01001650 if (w->power == power)
1651 return;
1652
1653 trace_snd_soc_dapm_widget_power(w, power);
1654
Mark Browndb432b42011-10-03 21:06:40 +01001655 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001656 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001657 */
1658 list_for_each_entry(path, &w->sources, list_sink) {
1659 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001660 dapm_widget_set_peer_power(path->source, power,
1661 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001662 }
1663 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001664 switch (w->id) {
1665 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001666 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001667 case snd_soc_dapm_clock_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001668 /* Supplies can't affect their outputs, only their inputs */
1669 break;
1670 default:
1671 list_for_each_entry(path, &w->sinks, list_source) {
1672 if (path->sink) {
1673 dapm_widget_set_peer_power(path->sink, power,
1674 path->connect);
1675 }
Mark Browndb432b42011-10-03 21:06:40 +01001676 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001677 break;
Mark Browndb432b42011-10-03 21:06:40 +01001678 }
1679
Mark Brown05623c42011-09-28 17:02:31 +01001680 if (power)
1681 dapm_seq_insert(w, up_list, true);
1682 else
1683 dapm_seq_insert(w, down_list, false);
1684
1685 w->power = power;
1686}
1687
Mark Brown7c81beb2011-09-20 22:22:32 +01001688static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1689 struct list_head *up_list,
1690 struct list_head *down_list)
1691{
Mark Brown7c81beb2011-09-20 22:22:32 +01001692 int power;
1693
1694 switch (w->id) {
1695 case snd_soc_dapm_pre:
1696 dapm_seq_insert(w, down_list, false);
1697 break;
1698 case snd_soc_dapm_post:
1699 dapm_seq_insert(w, up_list, true);
1700 break;
1701
1702 default:
Mark Brownd8050022011-09-28 18:28:23 +01001703 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001704
Mark Brown05623c42011-09-28 17:02:31 +01001705 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001706 break;
1707 }
1708}
1709
Mark Brown42aa3412009-03-01 19:21:10 +00001710/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001711 * Scan each dapm widget for complete audio path.
1712 * A complete path is a route that has valid endpoints i.e.:-
1713 *
1714 * o DAC to output pin.
1715 * o Input Pin to ADC.
1716 * o Input pin to Output pin (bypass, sidetone)
1717 * o DAC to ADC (loopback).
1718 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001719static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001720{
1721 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001722 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001723 LIST_HEAD(up_list);
1724 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001725 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001726 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001727
Mark Brown84e90932010-11-04 00:07:02 -04001728 trace_snd_soc_dapm_start(card);
1729
Mark Brown56fba412011-06-04 11:25:10 +01001730 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001731 if (d->idle_bias_off)
1732 d->target_bias_level = SND_SOC_BIAS_OFF;
1733 else
1734 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001735 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001736
Liam Girdwood6c120e12012-02-15 15:15:34 +00001737 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001738
Mark Brown6d3ddc82009-05-16 17:47:29 +01001739 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001740 * lists indicating if they should be powered up or down. We
1741 * only check widgets that have been flagged as dirty but note
1742 * that new widgets may be added to the dirty list while we
1743 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001744 */
Mark Browndb432b42011-10-03 21:06:40 +01001745 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001746 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001747 }
1748
Mark Brownf9de6d72011-09-28 17:19:47 +01001749 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001750 switch (w->id) {
1751 case snd_soc_dapm_pre:
1752 case snd_soc_dapm_post:
1753 /* These widgets always need to be powered */
1754 break;
1755 default:
1756 list_del_init(&w->dirty);
1757 break;
1758 }
Mark Browndb432b42011-10-03 21:06:40 +01001759
Mark Brownf9de6d72011-09-28 17:19:47 +01001760 if (w->power) {
1761 d = w->dapm;
1762
1763 /* Supplies and micbiases only bring the
1764 * context up to STANDBY as unless something
1765 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001766 * generally don't require full power. Signal
1767 * generators are virtual pins and have no
1768 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001769 */
1770 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001771 case snd_soc_dapm_siggen:
1772 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001773 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001774 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001775 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001776 case snd_soc_dapm_micbias:
1777 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1778 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1779 break;
1780 default:
1781 d->target_bias_level = SND_SOC_BIAS_ON;
1782 break;
1783 }
1784 }
1785
1786 }
1787
Mark Brown85a843c2011-09-21 21:29:47 +01001788 /* Force all contexts in the card to the same bias state if
1789 * they're not ground referenced.
1790 */
Mark Brown56fba412011-06-04 11:25:10 +01001791 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001792 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001793 if (d->target_bias_level > bias)
1794 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001795 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001796 if (!d->idle_bias_off)
1797 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001798
Mark Brownde02d072011-09-20 21:43:24 +01001799 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001800
Mark Brown9d0624a2011-02-18 11:49:43 -08001801 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001802 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001803 async_schedule_domain(dapm_pre_sequence_async, d,
1804 &async_domain);
1805 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001806
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001807 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001808 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001809 }
1810
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001811 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001812 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001813 }
1814
Mark Brown6d3ddc82009-05-16 17:47:29 +01001815 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001816 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001817
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001818 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001819
Mark Brown6d3ddc82009-05-16 17:47:29 +01001820 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001821 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001822
Mark Brown9d0624a2011-02-18 11:49:43 -08001823 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001824 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001825 async_schedule_domain(dapm_post_sequence_async, d,
1826 &async_domain);
1827 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001828
Liam Girdwood8078d872012-02-15 15:15:35 +00001829 /* do we need to notify any clients that DAPM event is complete */
1830 list_for_each_entry(d, &card->dapm_list, list) {
1831 if (d->stream_event)
1832 d->stream_event(d, event);
1833 }
1834
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001835 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001836 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001837 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001838
Mark Brown84e90932010-11-04 00:07:02 -04001839 trace_snd_soc_dapm_done(card);
1840
Mark Brown42aa3412009-03-01 19:21:10 +00001841 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001842}
1843
Mark Brown79fb9382009-08-21 16:38:13 +01001844#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001845static ssize_t dapm_widget_power_read_file(struct file *file,
1846 char __user *user_buf,
1847 size_t count, loff_t *ppos)
1848{
1849 struct snd_soc_dapm_widget *w = file->private_data;
1850 char *buf;
1851 int in, out;
1852 ssize_t ret;
1853 struct snd_soc_dapm_path *p = NULL;
1854
1855 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1856 if (!buf)
1857 return -ENOMEM;
1858
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001859 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001860 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001861 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001862 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001863
Mark Brownf13ebad2012-03-03 18:01:01 +00001864 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1865 w->name, w->power ? "On" : "Off",
1866 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001867
Mark Brownd033c362009-12-04 15:25:56 +00001868 if (w->reg >= 0)
1869 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1870 " - R%d(0x%x) bit %d",
1871 w->reg, w->reg, w->shift);
1872
1873 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1874
Mark Brown3eef08b2009-09-14 16:49:00 +01001875 if (w->sname)
1876 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1877 w->sname,
1878 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001879
1880 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001881 if (p->connected && !p->connected(w, p->sink))
1882 continue;
1883
Mark Brown79fb9382009-08-21 16:38:13 +01001884 if (p->connect)
1885 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001886 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001887 p->name ? p->name : "static",
1888 p->source->name);
1889 }
1890 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001891 if (p->connected && !p->connected(w, p->sink))
1892 continue;
1893
Mark Brown79fb9382009-08-21 16:38:13 +01001894 if (p->connect)
1895 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001896 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001897 p->name ? p->name : "static",
1898 p->sink->name);
1899 }
1900
1901 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1902
1903 kfree(buf);
1904 return ret;
1905}
1906
1907static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001908 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001909 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001910 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001911};
1912
Mark Brownef49e4f2011-04-04 20:48:13 +09001913static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1914 size_t count, loff_t *ppos)
1915{
1916 struct snd_soc_dapm_context *dapm = file->private_data;
1917 char *level;
1918
1919 switch (dapm->bias_level) {
1920 case SND_SOC_BIAS_ON:
1921 level = "On\n";
1922 break;
1923 case SND_SOC_BIAS_PREPARE:
1924 level = "Prepare\n";
1925 break;
1926 case SND_SOC_BIAS_STANDBY:
1927 level = "Standby\n";
1928 break;
1929 case SND_SOC_BIAS_OFF:
1930 level = "Off\n";
1931 break;
1932 default:
1933 BUG();
1934 level = "Unknown\n";
1935 break;
1936 }
1937
1938 return simple_read_from_buffer(user_buf, count, ppos, level,
1939 strlen(level));
1940}
1941
1942static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001943 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001944 .read = dapm_bias_read_file,
1945 .llseek = default_llseek,
1946};
1947
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001948void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1949 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001950{
Mark Brown79fb9382009-08-21 16:38:13 +01001951 struct dentry *d;
1952
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001953 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1954
1955 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001956 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001957 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001958 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001959 }
Mark Brown79fb9382009-08-21 16:38:13 +01001960
Mark Brownef49e4f2011-04-04 20:48:13 +09001961 d = debugfs_create_file("bias_level", 0444,
1962 dapm->debugfs_dapm, dapm,
1963 &dapm_bias_fops);
1964 if (!d)
1965 dev_warn(dapm->dev,
1966 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001967}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001968
1969static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1970{
1971 struct snd_soc_dapm_context *dapm = w->dapm;
1972 struct dentry *d;
1973
1974 if (!dapm->debugfs_dapm || !w->name)
1975 return;
1976
1977 d = debugfs_create_file(w->name, 0444,
1978 dapm->debugfs_dapm, w,
1979 &dapm_widget_power_fops);
1980 if (!d)
1981 dev_warn(w->dapm->dev,
1982 "ASoC: Failed to create %s debugfs file\n",
1983 w->name);
1984}
1985
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001986static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1987{
1988 debugfs_remove_recursive(dapm->debugfs_dapm);
1989}
1990
Mark Brown79fb9382009-08-21 16:38:13 +01001991#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001992void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1993 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001994{
1995}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001996
1997static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1998{
1999}
2000
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002001static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2002{
2003}
2004
Mark Brown79fb9382009-08-21 16:38:13 +01002005#endif
2006
Richard Purdie2b97eab2006-10-06 18:32:18 +02002007/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002008static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002009 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002010{
2011 struct snd_soc_dapm_path *path;
2012 int found = 0;
2013
Richard Purdie2b97eab2006-10-06 18:32:18 +02002014 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002015 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002016 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002017 continue;
2018
2019 found = 1;
2020 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002021 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002022 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002023 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002024 } else {
2025 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002026 dapm_mark_dirty(path->source,
2027 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002028 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002029 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002030 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002031 }
2032
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002033 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002034 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002035
Liam Girdwood618dae12012-04-25 12:12:51 +01002036 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002037}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002038
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002039int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002040 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2041 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002042{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002043 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002044 int ret;
2045
Liam Girdwood3cd04342012-03-09 12:02:08 +00002046 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002047 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002048 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002049 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002050 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002051 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002052 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002053 return ret;
2054}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002055EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002056
Milan plzik1b075e32008-01-10 14:39:46 +01002057/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002058static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002059 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002060{
2061 struct snd_soc_dapm_path *path;
2062 int found = 0;
2063
Richard Purdie2b97eab2006-10-06 18:32:18 +02002064 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002065 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002066 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002067 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002068 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002069 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002070 }
2071
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002072 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002073 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002074
Liam Girdwood618dae12012-04-25 12:12:51 +01002075 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002076}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002077
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002078int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002079 struct snd_kcontrol *kcontrol, int connect,
2080 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002081{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002082 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002083 int ret;
2084
Liam Girdwood3cd04342012-03-09 12:02:08 +00002085 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002086 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002087 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002088 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002089 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002090 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002091 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002092 return ret;
2093}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002094EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002095
2096/* show dapm widget status in sys fs */
2097static ssize_t dapm_widget_show(struct device *dev,
2098 struct device_attribute *attr, char *buf)
2099{
Mark Brown36ae1a92012-01-06 17:12:45 -08002100 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002101 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002102 struct snd_soc_dapm_widget *w;
2103 int count = 0;
2104 char *state = "not set";
2105
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002106 list_for_each_entry(w, &codec->card->widgets, list) {
2107 if (w->dapm != &codec->dapm)
2108 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002109
2110 /* only display widgets that burnm power */
2111 switch (w->id) {
2112 case snd_soc_dapm_hp:
2113 case snd_soc_dapm_mic:
2114 case snd_soc_dapm_spk:
2115 case snd_soc_dapm_line:
2116 case snd_soc_dapm_micbias:
2117 case snd_soc_dapm_dac:
2118 case snd_soc_dapm_adc:
2119 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002120 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002121 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002122 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002123 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002124 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002125 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002126 if (w->name)
2127 count += sprintf(buf + count, "%s: %s\n",
2128 w->name, w->power ? "On":"Off");
2129 break;
2130 default:
2131 break;
2132 }
2133 }
2134
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002135 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002136 case SND_SOC_BIAS_ON:
2137 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002138 break;
Mark Brown0be98982008-05-19 12:31:28 +02002139 case SND_SOC_BIAS_PREPARE:
2140 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002141 break;
Mark Brown0be98982008-05-19 12:31:28 +02002142 case SND_SOC_BIAS_STANDBY:
2143 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002144 break;
Mark Brown0be98982008-05-19 12:31:28 +02002145 case SND_SOC_BIAS_OFF:
2146 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002147 break;
2148 }
2149 count += sprintf(buf + count, "PM State: %s\n", state);
2150
2151 return count;
2152}
2153
2154static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2155
2156int snd_soc_dapm_sys_add(struct device *dev)
2157{
Troy Kisky12ef1932008-10-13 17:42:14 -07002158 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002159}
2160
2161static void snd_soc_dapm_sys_remove(struct device *dev)
2162{
Mark Brownaef90842009-05-16 17:53:16 +01002163 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002164}
2165
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002166static void dapm_free_path(struct snd_soc_dapm_path *path)
2167{
2168 list_del(&path->list_sink);
2169 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002170 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002171 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002172 kfree(path);
2173}
2174
Richard Purdie2b97eab2006-10-06 18:32:18 +02002175/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002176static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002177{
2178 struct snd_soc_dapm_widget *w, *next_w;
2179 struct snd_soc_dapm_path *p, *next_p;
2180
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002181 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2182 if (w->dapm != dapm)
2183 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002184 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002185 /*
2186 * remove source and sink paths associated to this widget.
2187 * While removing the path, remove reference to it from both
2188 * source and sink widgets so that path is removed only once.
2189 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002190 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2191 dapm_free_path(p);
2192
2193 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2194 dapm_free_path(p);
2195
Stephen Warrenfad59882011-04-28 17:37:59 -06002196 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002197 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002198 kfree(w);
2199 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002200}
2201
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002202static struct snd_soc_dapm_widget *dapm_find_widget(
2203 struct snd_soc_dapm_context *dapm, const char *pin,
2204 bool search_other_contexts)
2205{
2206 struct snd_soc_dapm_widget *w;
2207 struct snd_soc_dapm_widget *fallback = NULL;
2208
2209 list_for_each_entry(w, &dapm->card->widgets, list) {
2210 if (!strcmp(w->name, pin)) {
2211 if (w->dapm == dapm)
2212 return w;
2213 else
2214 fallback = w;
2215 }
2216 }
2217
2218 if (search_other_contexts)
2219 return fallback;
2220
2221 return NULL;
2222}
2223
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002224static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002225 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002226{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002227 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002228
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002229 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002230 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002231 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002232 }
2233
Mark Brown1a8b2d92012-02-16 11:50:07 -08002234 if (w->connected != status)
2235 dapm_mark_dirty(w, "pin configuration");
2236
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002237 w->connected = status;
2238 if (status == 0)
2239 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002240
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002241 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002242}
2243
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002245 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002246 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002247 *
2248 * Walks all dapm audio paths and powers widgets according to their
2249 * stream or path usage.
2250 *
2251 * Returns 0 for success.
2252 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002253int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002254{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002255 int ret;
2256
Mark Brown4f4c0072011-10-07 14:29:19 +01002257 /*
2258 * Suppress early reports (eg, jacks syncing their state) to avoid
2259 * silly DAPM runs during card startup.
2260 */
2261 if (!dapm->card || !dapm->card->instantiated)
2262 return 0;
2263
Liam Girdwood3cd04342012-03-09 12:02:08 +00002264 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002265 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002266 mutex_unlock(&dapm->card->dapm_mutex);
2267 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002268}
Liam Girdwooda5302182008-07-07 13:35:17 +01002269EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002270
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002271static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002272 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002273{
2274 struct snd_soc_dapm_path *path;
2275 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002276 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002277 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002278 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002279 const char *source;
2280 char prefixed_sink[80];
2281 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002282 int ret = 0;
2283
Mark Brown88e8b9a2011-03-02 18:18:24 +00002284 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002285 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2286 dapm->codec->name_prefix, route->sink);
2287 sink = prefixed_sink;
2288 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2289 dapm->codec->name_prefix, route->source);
2290 source = prefixed_source;
2291 } else {
2292 sink = route->sink;
2293 source = route->source;
2294 }
2295
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002296 /*
2297 * find src and dest widgets over all widgets but favor a widget from
2298 * current DAPM context
2299 */
2300 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002301 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002302 wtsink = w;
2303 if (w->dapm == dapm)
2304 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002305 continue;
2306 }
2307 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002308 wtsource = w;
2309 if (w->dapm == dapm)
2310 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002311 }
2312 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002313 /* use widget from another DAPM context if not found from this */
2314 if (!wsink)
2315 wsink = wtsink;
2316 if (!wsource)
2317 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002318
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002319 if (wsource == NULL) {
2320 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2321 route->source);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002322 return -ENODEV;
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002323 }
2324 if (wsink == NULL) {
2325 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2326 route->sink);
2327 return -ENODEV;
2328 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002329
2330 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2331 if (!path)
2332 return -ENOMEM;
2333
2334 path->source = wsource;
2335 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002336 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002337 INIT_LIST_HEAD(&path->list);
2338 INIT_LIST_HEAD(&path->list_source);
2339 INIT_LIST_HEAD(&path->list_sink);
2340
2341 /* check for external widgets */
2342 if (wsink->id == snd_soc_dapm_input) {
2343 if (wsource->id == snd_soc_dapm_micbias ||
2344 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002345 wsource->id == snd_soc_dapm_line ||
2346 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002347 wsink->ext = 1;
2348 }
2349 if (wsource->id == snd_soc_dapm_output) {
2350 if (wsink->id == snd_soc_dapm_spk ||
2351 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002352 wsink->id == snd_soc_dapm_line ||
2353 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002354 wsource->ext = 1;
2355 }
2356
2357 /* connect static paths */
2358 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002359 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002360 list_add(&path->list_sink, &wsink->sources);
2361 list_add(&path->list_source, &wsource->sinks);
2362 path->connect = 1;
2363 return 0;
2364 }
2365
2366 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002367 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002368 case snd_soc_dapm_adc:
2369 case snd_soc_dapm_dac:
2370 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002371 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002372 case snd_soc_dapm_input:
2373 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002374 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002375 case snd_soc_dapm_micbias:
2376 case snd_soc_dapm_vmid:
2377 case snd_soc_dapm_pre:
2378 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002379 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002380 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002381 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002382 case snd_soc_dapm_aif_in:
2383 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002384 case snd_soc_dapm_dai_in:
2385 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002386 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002387 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002388 list_add(&path->list_sink, &wsink->sources);
2389 list_add(&path->list_source, &wsource->sinks);
2390 path->connect = 1;
2391 return 0;
2392 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002393 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002394 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002395 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002396 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002397 if (ret != 0)
2398 goto err;
2399 break;
2400 case snd_soc_dapm_switch:
2401 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002402 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002403 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002404 if (ret != 0)
2405 goto err;
2406 break;
2407 case snd_soc_dapm_hp:
2408 case snd_soc_dapm_mic:
2409 case snd_soc_dapm_line:
2410 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002411 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002412 list_add(&path->list_sink, &wsink->sources);
2413 list_add(&path->list_source, &wsource->sinks);
2414 path->connect = 0;
2415 return 0;
2416 }
Mark Brownfabd0382012-07-05 17:20:06 +01002417
2418 dapm_mark_dirty(wsource, "Route added");
2419 dapm_mark_dirty(wsink, "Route added");
2420
Richard Purdie2b97eab2006-10-06 18:32:18 +02002421 return 0;
2422
2423err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002424 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002425 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002426 kfree(path);
2427 return ret;
2428}
Mark Brown105f1c22008-05-13 14:52:19 +02002429
Mark Brownefcc3c62012-07-05 17:24:19 +01002430static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2431 const struct snd_soc_dapm_route *route)
2432{
2433 struct snd_soc_dapm_path *path, *p;
2434 const char *sink;
2435 const char *source;
2436 char prefixed_sink[80];
2437 char prefixed_source[80];
2438
2439 if (route->control) {
2440 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002441 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002442 return -EINVAL;
2443 }
2444
2445 if (dapm->codec && dapm->codec->name_prefix) {
2446 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2447 dapm->codec->name_prefix, route->sink);
2448 sink = prefixed_sink;
2449 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2450 dapm->codec->name_prefix, route->source);
2451 source = prefixed_source;
2452 } else {
2453 sink = route->sink;
2454 source = route->source;
2455 }
2456
2457 path = NULL;
2458 list_for_each_entry(p, &dapm->card->paths, list) {
2459 if (strcmp(p->source->name, source) != 0)
2460 continue;
2461 if (strcmp(p->sink->name, sink) != 0)
2462 continue;
2463 path = p;
2464 break;
2465 }
2466
2467 if (path) {
2468 dapm_mark_dirty(path->source, "Route removed");
2469 dapm_mark_dirty(path->sink, "Route removed");
2470
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002471 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002472 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002473 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002474 source, sink);
2475 }
2476
2477 return 0;
2478}
2479
Mark Brown105f1c22008-05-13 14:52:19 +02002480/**
Mark Brown105f1c22008-05-13 14:52:19 +02002481 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002482 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002483 * @route: audio routes
2484 * @num: number of routes
2485 *
2486 * Connects 2 dapm widgets together via a named audio path. The sink is
2487 * the widget receiving the audio signal, whilst the source is the sender
2488 * of the audio signal.
2489 *
2490 * Returns 0 for success else error. On error all resources can be freed
2491 * with a call to snd_soc_card_free().
2492 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002493int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002494 const struct snd_soc_dapm_route *route, int num)
2495{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002496 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002497
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002498 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002499 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002500 r = snd_soc_dapm_add_route(dapm, route);
2501 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002502 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2503 route->source,
2504 route->control ? route->control : "direct",
2505 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002506 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002507 }
2508 route++;
2509 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002510 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002511
Dan Carpenter60884c22012-04-13 22:25:43 +03002512 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002513}
2514EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2515
Mark Brownefcc3c62012-07-05 17:24:19 +01002516/**
2517 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2518 * @dapm: DAPM context
2519 * @route: audio routes
2520 * @num: number of routes
2521 *
2522 * Removes routes from the DAPM context.
2523 */
2524int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2525 const struct snd_soc_dapm_route *route, int num)
2526{
2527 int i, ret = 0;
2528
2529 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2530 for (i = 0; i < num; i++) {
2531 snd_soc_dapm_del_route(dapm, route);
2532 route++;
2533 }
2534 mutex_unlock(&dapm->card->dapm_mutex);
2535
2536 return ret;
2537}
2538EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2539
Mark Brownbf3a9e12011-06-13 16:42:29 +01002540static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2541 const struct snd_soc_dapm_route *route)
2542{
2543 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2544 route->source,
2545 true);
2546 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2547 route->sink,
2548 true);
2549 struct snd_soc_dapm_path *path;
2550 int count = 0;
2551
2552 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002553 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002554 route->source);
2555 return -ENODEV;
2556 }
2557
2558 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002559 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002560 route->sink);
2561 return -ENODEV;
2562 }
2563
2564 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002565 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002566 route->source, route->sink);
2567
2568 list_for_each_entry(path, &source->sinks, list_source) {
2569 if (path->sink == sink) {
2570 path->weak = 1;
2571 count++;
2572 }
2573 }
2574
2575 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002576 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002577 route->source, route->sink);
2578 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002579 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002580 count, route->source, route->sink);
2581
2582 return 0;
2583}
2584
2585/**
2586 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2587 * @dapm: DAPM context
2588 * @route: audio routes
2589 * @num: number of routes
2590 *
2591 * Mark existing routes matching those specified in the passed array
2592 * as being weak, meaning that they are ignored for the purpose of
2593 * power decisions. The main intended use case is for sidetone paths
2594 * which couple audio between other independent paths if they are both
2595 * active in order to make the combination work better at the user
2596 * level but which aren't intended to be "used".
2597 *
2598 * Note that CODEC drivers should not use this as sidetone type paths
2599 * can frequently also be used as bypass paths.
2600 */
2601int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2602 const struct snd_soc_dapm_route *route, int num)
2603{
2604 int i, err;
2605 int ret = 0;
2606
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002607 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002608 for (i = 0; i < num; i++) {
2609 err = snd_soc_dapm_weak_route(dapm, route);
2610 if (err)
2611 ret = err;
2612 route++;
2613 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002614 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002615
2616 return ret;
2617}
2618EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2619
Mark Brown105f1c22008-05-13 14:52:19 +02002620/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002621 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002622 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002623 *
2624 * Checks the codec for any new dapm widgets and creates them if found.
2625 *
2626 * Returns 0 for success.
2627 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002628int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002629{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002630 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002631 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002632 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002633
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002634 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002635
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002636 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002637 {
2638 if (w->new)
2639 continue;
2640
Stephen Warrenfad59882011-04-28 17:37:59 -06002641 if (w->num_kcontrols) {
2642 w->kcontrols = kzalloc(w->num_kcontrols *
2643 sizeof(struct snd_kcontrol *),
2644 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002645 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002646 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002647 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002648 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002649 }
2650
Richard Purdie2b97eab2006-10-06 18:32:18 +02002651 switch(w->id) {
2652 case snd_soc_dapm_switch:
2653 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002654 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002655 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002656 break;
2657 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002658 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002659 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002660 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002661 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002662 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002663 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002664 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002665 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002666 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002667 break;
2668 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002669
2670 /* Read the initial power state from the device */
2671 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002672 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002673 val &= 1 << w->shift;
2674 if (w->invert)
2675 val = !val;
2676
2677 if (val)
2678 w->power = 1;
2679 }
2680
Richard Purdie2b97eab2006-10-06 18:32:18 +02002681 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002682
Mark Brown7508b122011-10-05 12:09:12 +01002683 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002684 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002685 }
2686
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002687 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2688 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002689 return 0;
2690}
2691EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2692
2693/**
2694 * snd_soc_dapm_get_volsw - dapm mixer get callback
2695 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002696 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002697 *
2698 * Callback to get the value of a dapm mixer control.
2699 *
2700 * Returns 0 for success.
2701 */
2702int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2703 struct snd_ctl_elem_value *ucontrol)
2704{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002705 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002706 struct soc_mixer_control *mc =
2707 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002708 unsigned int reg = mc->reg;
2709 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002710 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002711 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002712 unsigned int invert = mc->invert;
2713
2714 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002715 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002716 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002717 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002718
Richard Purdie2b97eab2006-10-06 18:32:18 +02002719 ucontrol->value.integer.value[0] =
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002720 (snd_soc_read(codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002721 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002722 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002723 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002724
2725 return 0;
2726}
2727EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2728
2729/**
2730 * snd_soc_dapm_put_volsw - dapm mixer set callback
2731 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002732 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002733 *
2734 * Callback to set the value of a dapm mixer control.
2735 *
2736 * Returns 0 for success.
2737 */
2738int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2739 struct snd_ctl_elem_value *ucontrol)
2740{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002741 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002742 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002743 struct soc_mixer_control *mc =
2744 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002745 unsigned int reg = mc->reg;
2746 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002747 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002748 unsigned int mask = (1 << fls(max)) - 1;
2749 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002750 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002751 int connect, change;
2752 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002753
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002754 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002755 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002756 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002757 kcontrol->id.name);
2758
Richard Purdie2b97eab2006-10-06 18:32:18 +02002759 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002760 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002761
2762 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002763 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002764 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002765 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002766
Liam Girdwood3cd04342012-03-09 12:02:08 +00002767 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002768
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002769 change = snd_soc_test_bits(codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002770 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002771 update.kcontrol = kcontrol;
2772 update.reg = reg;
2773 update.mask = mask;
2774 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002775
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002776 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002777
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002778 soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002779
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002780 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002781 }
2782
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002783 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002784 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002785}
2786EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2787
2788/**
2789 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2790 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002791 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002792 *
2793 * Callback to get the value of a dapm enumerated double mixer control.
2794 *
2795 * Returns 0 for success.
2796 */
2797int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2798 struct snd_ctl_elem_value *ucontrol)
2799{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002800 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002801 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002802 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002803
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002804 val = snd_soc_read(codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002805 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002806 if (e->shift_l != e->shift_r)
2807 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002808 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002809
2810 return 0;
2811}
2812EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2813
2814/**
2815 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2816 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002817 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002818 *
2819 * Callback to set the value of a dapm enumerated double mixer control.
2820 *
2821 * Returns 0 for success.
2822 */
2823int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2824 struct snd_ctl_elem_value *ucontrol)
2825{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002826 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002827 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002828 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002829 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002830 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002831 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002832
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002833 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002834 return -EINVAL;
2835 mux = ucontrol->value.enumerated.item[0];
2836 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002837 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002838 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002839 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002840 return -EINVAL;
2841 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002842 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002843 }
2844
Liam Girdwood3cd04342012-03-09 12:02:08 +00002845 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002846
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002847 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002848 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002849 update.kcontrol = kcontrol;
2850 update.reg = e->reg;
2851 update.mask = mask;
2852 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002853 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002854
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002855 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002856
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002857 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002858 }
2859
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002860 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002861 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002862}
2863EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2864
2865/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002866 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2867 * @kcontrol: mixer control
2868 * @ucontrol: control element information
2869 *
2870 * Returns 0 for success.
2871 */
2872int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2873 struct snd_ctl_elem_value *ucontrol)
2874{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002875 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
Mark Brownd2b247a2009-10-06 15:21:04 +01002876 return 0;
2877}
2878EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2879
2880/**
2881 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2882 * @kcontrol: mixer control
2883 * @ucontrol: control element information
2884 *
2885 * Returns 0 for success.
2886 */
2887int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2888 struct snd_ctl_elem_value *ucontrol)
2889{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002890 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002891 struct snd_soc_card *card = codec->card;
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002892 unsigned int value;
Mark Brownd2b247a2009-10-06 15:21:04 +01002893 struct soc_enum *e =
2894 (struct soc_enum *)kcontrol->private_value;
2895 int change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002896
2897 if (ucontrol->value.enumerated.item[0] >= e->max)
2898 return -EINVAL;
2899
Liam Girdwood3cd04342012-03-09 12:02:08 +00002900 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002901
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002902 value = ucontrol->value.enumerated.item[0];
2903 change = dapm_kcontrol_set_value(kcontrol, value);
2904 if (change)
2905 soc_dapm_mux_update_power(card, kcontrol, value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002906
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002907 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002908 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002909}
2910EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2911
2912/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002913 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2914 * callback
2915 * @kcontrol: mixer control
2916 * @ucontrol: control element information
2917 *
2918 * Callback to get the value of a dapm semi enumerated double mixer control.
2919 *
2920 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2921 * used for handling bitfield coded enumeration for example.
2922 *
2923 * Returns 0 for success.
2924 */
2925int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2926 struct snd_ctl_elem_value *ucontrol)
2927{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002928 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002929 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002930 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002931
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002932 reg_val = snd_soc_read(codec, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002933 val = (reg_val >> e->shift_l) & e->mask;
2934 for (mux = 0; mux < e->max; mux++) {
2935 if (val == e->values[mux])
2936 break;
2937 }
2938 ucontrol->value.enumerated.item[0] = mux;
2939 if (e->shift_l != e->shift_r) {
2940 val = (reg_val >> e->shift_r) & e->mask;
2941 for (mux = 0; mux < e->max; mux++) {
2942 if (val == e->values[mux])
2943 break;
2944 }
2945 ucontrol->value.enumerated.item[1] = mux;
2946 }
2947
2948 return 0;
2949}
2950EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2951
2952/**
2953 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2954 * callback
2955 * @kcontrol: mixer control
2956 * @ucontrol: control element information
2957 *
2958 * Callback to set the value of a dapm semi enumerated double mixer control.
2959 *
2960 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2961 * used for handling bitfield coded enumeration for example.
2962 *
2963 * Returns 0 for success.
2964 */
2965int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2966 struct snd_ctl_elem_value *ucontrol)
2967{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002968 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002969 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002970 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002971 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002972 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002973 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002974
2975 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2976 return -EINVAL;
2977 mux = ucontrol->value.enumerated.item[0];
2978 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2979 mask = e->mask << e->shift_l;
2980 if (e->shift_l != e->shift_r) {
2981 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2982 return -EINVAL;
2983 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2984 mask |= e->mask << e->shift_r;
2985 }
2986
Liam Girdwood3cd04342012-03-09 12:02:08 +00002987 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002988
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002989 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002990 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002991 update.kcontrol = kcontrol;
2992 update.reg = e->reg;
2993 update.mask = mask;
2994 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002995 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002996
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002997 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002998
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002999 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003000 }
3001
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003002 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00003003 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003004}
3005EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3006
3007/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003008 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3009 *
3010 * @kcontrol: mixer control
3011 * @uinfo: control element information
3012 *
3013 * Callback to provide information about a pin switch control.
3014 */
3015int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3016 struct snd_ctl_elem_info *uinfo)
3017{
3018 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3019 uinfo->count = 1;
3020 uinfo->value.integer.min = 0;
3021 uinfo->value.integer.max = 1;
3022
3023 return 0;
3024}
3025EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3026
3027/**
3028 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3029 *
3030 * @kcontrol: mixer control
3031 * @ucontrol: Value
3032 */
3033int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3034 struct snd_ctl_elem_value *ucontrol)
3035{
Mark Brown48a8c392012-02-14 17:11:15 -08003036 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003037 const char *pin = (const char *)kcontrol->private_value;
3038
Liam Girdwood3cd04342012-03-09 12:02:08 +00003039 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003040
3041 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003042 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003043
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003044 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003045
3046 return 0;
3047}
3048EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3049
3050/**
3051 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3052 *
3053 * @kcontrol: mixer control
3054 * @ucontrol: Value
3055 */
3056int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3057 struct snd_ctl_elem_value *ucontrol)
3058{
Mark Brown48a8c392012-02-14 17:11:15 -08003059 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003060 const char *pin = (const char *)kcontrol->private_value;
3061
Liam Girdwood3cd04342012-03-09 12:02:08 +00003062 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003063
3064 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003065 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003066 else
Mark Brown48a8c392012-02-14 17:11:15 -08003067 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003068
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003069 mutex_unlock(&card->dapm_mutex);
3070
Mark Brown48a8c392012-02-14 17:11:15 -08003071 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003072 return 0;
3073}
3074EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3075
Mark Brown5ba06fc2012-02-16 11:07:13 -08003076static struct snd_soc_dapm_widget *
3077snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3078 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003079{
3080 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003081 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003082
3083 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003084 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003085
Mark Brown62ea8742012-01-21 21:14:48 +00003086 switch (w->id) {
3087 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003088 w->regulator = devm_regulator_get(dapm->dev, w->name);
3089 if (IS_ERR(w->regulator)) {
3090 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003091 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003092 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003093 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003094 }
Mark Brown8784c772013-01-10 19:33:47 +00003095
3096 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
3097 ret = regulator_allow_bypass(w->regulator, true);
3098 if (ret != 0)
3099 dev_warn(w->dapm->dev,
3100 "ASoC: Failed to unbypass %s: %d\n",
3101 w->name, ret);
3102 }
Mark Brown62ea8742012-01-21 21:14:48 +00003103 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003104 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003105#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003106 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003107 if (IS_ERR(w->clk)) {
3108 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003109 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003110 w->name, ret);
3111 return NULL;
3112 }
Mark Brownec029952012-06-04 08:16:20 +01003113#else
3114 return NULL;
3115#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003116 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003117 default:
3118 break;
3119 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003120
Mark Brown88e8b9a2011-03-02 18:18:24 +00003121 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003122 w->name = kasprintf(GFP_KERNEL, "%s %s",
3123 dapm->codec->name_prefix, widget->name);
3124 else
3125 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3126
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003127 if (w->name == NULL) {
3128 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003129 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003130 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003131
Mark Brown7ca3a182011-10-08 14:04:50 +01003132 switch (w->id) {
3133 case snd_soc_dapm_switch:
3134 case snd_soc_dapm_mixer:
3135 case snd_soc_dapm_mixer_named_ctl:
3136 w->power_check = dapm_generic_check_power;
3137 break;
3138 case snd_soc_dapm_mux:
3139 case snd_soc_dapm_virt_mux:
3140 case snd_soc_dapm_value_mux:
3141 w->power_check = dapm_generic_check_power;
3142 break;
Mark Brown46162742013-06-05 19:36:11 +01003143 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003144 w->power_check = dapm_adc_check_power;
3145 break;
Mark Brown46162742013-06-05 19:36:11 +01003146 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003147 w->power_check = dapm_dac_check_power;
3148 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003149 case snd_soc_dapm_adc:
3150 case snd_soc_dapm_aif_out:
3151 case snd_soc_dapm_dac:
3152 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003153 case snd_soc_dapm_pga:
3154 case snd_soc_dapm_out_drv:
3155 case snd_soc_dapm_input:
3156 case snd_soc_dapm_output:
3157 case snd_soc_dapm_micbias:
3158 case snd_soc_dapm_spk:
3159 case snd_soc_dapm_hp:
3160 case snd_soc_dapm_mic:
3161 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003162 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003163 w->power_check = dapm_generic_check_power;
3164 break;
3165 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003166 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003167 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003168 w->power_check = dapm_supply_check_power;
3169 break;
3170 default:
3171 w->power_check = dapm_always_on_check_power;
3172 break;
3173 }
3174
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003175 w->dapm = dapm;
3176 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003177 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003178 INIT_LIST_HEAD(&w->sources);
3179 INIT_LIST_HEAD(&w->sinks);
3180 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003181 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003182 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003183
3184 /* machine layer set ups unconnected pins and insertions */
3185 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003186 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003187}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003188
3189/**
Mark Brown4ba13272008-05-13 14:51:19 +02003190 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003191 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003192 * @widget: widget array
3193 * @num: number of widgets
3194 *
3195 * Creates new DAPM controls based upon the templates.
3196 *
3197 * Returns 0 for success else error.
3198 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003199int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003200 const struct snd_soc_dapm_widget *widget,
3201 int num)
3202{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003203 struct snd_soc_dapm_widget *w;
3204 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003205 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003206
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003207 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003208 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003209 w = snd_soc_dapm_new_control(dapm, widget);
3210 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003211 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003212 "ASoC: Failed to create DAPM control %s\n",
3213 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003214 ret = -ENOMEM;
3215 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003216 }
Mark Brown4ba13272008-05-13 14:51:19 +02003217 widget++;
3218 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003219 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003220 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003221}
3222EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3223
Mark Brownc74184e2012-04-04 22:12:09 +01003224static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3225 struct snd_kcontrol *kcontrol, int event)
3226{
3227 struct snd_soc_dapm_path *source_p, *sink_p;
3228 struct snd_soc_dai *source, *sink;
3229 const struct snd_soc_pcm_stream *config = w->params;
3230 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003231 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003232 u64 fmt;
3233 int ret;
3234
3235 BUG_ON(!config);
3236 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3237
3238 /* We only support a single source and sink, pick the first */
3239 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3240 list_sink);
3241 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3242 list_source);
3243
3244 BUG_ON(!source_p || !sink_p);
3245 BUG_ON(!sink_p->source || !source_p->sink);
3246 BUG_ON(!source_p->source || !sink_p->sink);
3247
3248 source = source_p->source->priv;
3249 sink = sink_p->sink->priv;
3250
3251 /* Be a little careful as we don't want to overflow the mask array */
3252 if (config->formats) {
3253 fmt = ffs(config->formats) - 1;
3254 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003255 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003256 config->formats);
3257 fmt = 0;
3258 }
3259
3260 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003261 params = kzalloc(sizeof(*params), GFP_KERNEL);
3262 if (!params) {
3263 ret = -ENOMEM;
3264 goto out;
3265 }
3266 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003267
Mark Brown9747cec2012-04-26 19:12:21 +01003268 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003269 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003270 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003271 config->rate_max;
3272
Mark Brown9747cec2012-04-26 19:12:21 +01003273 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003274 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003275 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003276 = config->channels_max;
3277
3278 memset(&substream, 0, sizeof(substream));
3279
3280 switch (event) {
3281 case SND_SOC_DAPM_PRE_PMU:
3282 if (source->driver->ops && source->driver->ops->hw_params) {
3283 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3284 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003285 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003286 if (ret != 0) {
3287 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003288 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003289 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003290 }
3291 }
3292
3293 if (sink->driver->ops && sink->driver->ops->hw_params) {
3294 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003295 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003296 sink);
3297 if (ret != 0) {
3298 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003299 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003300 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003301 }
3302 }
3303 break;
3304
3305 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003306 ret = snd_soc_dai_digital_mute(sink, 0,
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 unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003310 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003311 break;
3312
3313 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003314 ret = snd_soc_dai_digital_mute(sink, 1,
3315 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003316 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003317 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003318 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003319 break;
3320
3321 default:
3322 BUG();
3323 return -EINVAL;
3324 }
3325
Mark Brown9747cec2012-04-26 19:12:21 +01003326out:
3327 kfree(params);
3328 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003329}
3330
3331int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3332 const struct snd_soc_pcm_stream *params,
3333 struct snd_soc_dapm_widget *source,
3334 struct snd_soc_dapm_widget *sink)
3335{
3336 struct snd_soc_dapm_route routes[2];
3337 struct snd_soc_dapm_widget template;
3338 struct snd_soc_dapm_widget *w;
3339 size_t len;
3340 char *link_name;
3341
3342 len = strlen(source->name) + strlen(sink->name) + 2;
3343 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3344 if (!link_name)
3345 return -ENOMEM;
3346 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3347
3348 memset(&template, 0, sizeof(template));
3349 template.reg = SND_SOC_NOPM;
3350 template.id = snd_soc_dapm_dai_link;
3351 template.name = link_name;
3352 template.event = snd_soc_dai_link_event;
3353 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3354 SND_SOC_DAPM_PRE_PMD;
3355
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003356 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003357
3358 w = snd_soc_dapm_new_control(&card->dapm, &template);
3359 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003360 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003361 link_name);
3362 return -ENOMEM;
3363 }
3364
3365 w->params = params;
3366
3367 memset(&routes, 0, sizeof(routes));
3368
3369 routes[0].source = source->name;
3370 routes[0].sink = link_name;
3371 routes[1].source = link_name;
3372 routes[1].sink = sink->name;
3373
3374 return snd_soc_dapm_add_routes(&card->dapm, routes,
3375 ARRAY_SIZE(routes));
3376}
3377
Mark Brown888df3952012-02-16 19:37:51 -08003378int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3379 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003380{
Mark Brown888df3952012-02-16 19:37:51 -08003381 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003382 struct snd_soc_dapm_widget *w;
3383
Mark Brown888df3952012-02-16 19:37:51 -08003384 WARN_ON(dapm->dev != dai->dev);
3385
3386 memset(&template, 0, sizeof(template));
3387 template.reg = SND_SOC_NOPM;
3388
3389 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003390 template.id = snd_soc_dapm_dai_in;
Mark Brown888df3952012-02-16 19:37:51 -08003391 template.name = dai->driver->playback.stream_name;
3392 template.sname = dai->driver->playback.stream_name;
3393
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003394 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df3952012-02-16 19:37:51 -08003395 template.name);
3396
3397 w = snd_soc_dapm_new_control(dapm, &template);
3398 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003399 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df3952012-02-16 19:37:51 -08003400 dai->driver->playback.stream_name);
3401 }
3402
3403 w->priv = dai;
3404 dai->playback_widget = w;
3405 }
3406
3407 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003408 template.id = snd_soc_dapm_dai_out;
Mark Brown888df3952012-02-16 19:37:51 -08003409 template.name = dai->driver->capture.stream_name;
3410 template.sname = dai->driver->capture.stream_name;
3411
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003412 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df3952012-02-16 19:37:51 -08003413 template.name);
3414
3415 w = snd_soc_dapm_new_control(dapm, &template);
3416 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003417 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df3952012-02-16 19:37:51 -08003418 dai->driver->capture.stream_name);
3419 }
3420
3421 w->priv = dai;
3422 dai->capture_widget = w;
3423 }
3424
3425 return 0;
3426}
3427
3428int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3429{
3430 struct snd_soc_dapm_widget *dai_w, *w;
3431 struct snd_soc_dai *dai;
3432 struct snd_soc_dapm_route r;
3433
3434 memset(&r, 0, sizeof(r));
3435
3436 /* For each DAI widget... */
3437 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003438 switch (dai_w->id) {
3439 case snd_soc_dapm_dai_in:
3440 case snd_soc_dapm_dai_out:
3441 break;
3442 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003443 continue;
Mark Brown46162742013-06-05 19:36:11 +01003444 }
Mark Brown888df3952012-02-16 19:37:51 -08003445
3446 dai = dai_w->priv;
3447
3448 /* ...find all widgets with the same stream and link them */
3449 list_for_each_entry(w, &card->widgets, list) {
3450 if (w->dapm != dai_w->dapm)
3451 continue;
3452
Mark Brown46162742013-06-05 19:36:11 +01003453 switch (w->id) {
3454 case snd_soc_dapm_dai_in:
3455 case snd_soc_dapm_dai_out:
Mark Brown888df3952012-02-16 19:37:51 -08003456 continue;
Mark Brown46162742013-06-05 19:36:11 +01003457 default:
3458 break;
3459 }
Mark Brown888df3952012-02-16 19:37:51 -08003460
3461 if (!w->sname)
3462 continue;
3463
3464 if (dai->driver->playback.stream_name &&
3465 strstr(w->sname,
3466 dai->driver->playback.stream_name)) {
3467 r.source = dai->playback_widget->name;
3468 r.sink = w->name;
3469 dev_dbg(dai->dev, "%s -> %s\n",
3470 r.source, r.sink);
3471
3472 snd_soc_dapm_add_route(w->dapm, &r);
3473 }
3474
3475 if (dai->driver->capture.stream_name &&
3476 strstr(w->sname,
3477 dai->driver->capture.stream_name)) {
3478 r.source = w->name;
3479 r.sink = dai->capture_widget->name;
3480 dev_dbg(dai->dev, "%s -> %s\n",
3481 r.source, r.sink);
3482
3483 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003484 }
3485 }
3486 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003487
Mark Brown888df3952012-02-16 19:37:51 -08003488 return 0;
3489}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003490
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003491static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3492 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003493{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003494
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003495 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3496 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3497 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003498
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003499 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3500 w_cpu = cpu_dai->playback_widget;
3501 w_codec = codec_dai->playback_widget;
3502 } else {
3503 w_cpu = cpu_dai->capture_widget;
3504 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003505 }
3506
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003507 if (w_cpu) {
3508
3509 dapm_mark_dirty(w_cpu, "stream event");
3510
3511 switch (event) {
3512 case SND_SOC_DAPM_STREAM_START:
3513 w_cpu->active = 1;
3514 break;
3515 case SND_SOC_DAPM_STREAM_STOP:
3516 w_cpu->active = 0;
3517 break;
3518 case SND_SOC_DAPM_STREAM_SUSPEND:
3519 case SND_SOC_DAPM_STREAM_RESUME:
3520 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3521 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3522 break;
3523 }
3524 }
3525
3526 if (w_codec) {
3527
3528 dapm_mark_dirty(w_codec, "stream event");
3529
3530 switch (event) {
3531 case SND_SOC_DAPM_STREAM_START:
3532 w_codec->active = 1;
3533 break;
3534 case SND_SOC_DAPM_STREAM_STOP:
3535 w_codec->active = 0;
3536 break;
3537 case SND_SOC_DAPM_STREAM_SUSPEND:
3538 case SND_SOC_DAPM_STREAM_RESUME:
3539 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3540 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3541 break;
3542 }
3543 }
3544
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003545 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003546}
3547
3548/**
3549 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3550 * @rtd: PCM runtime data
3551 * @stream: stream name
3552 * @event: stream event
3553 *
3554 * Sends a stream event to the dapm core. The core then makes any
3555 * necessary widget power changes.
3556 *
3557 * Returns 0 for success else error.
3558 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003559void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3560 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003561{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003562 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003563
Liam Girdwood3cd04342012-03-09 12:02:08 +00003564 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003565 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003566 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003567}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003568
3569/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003570 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003571 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003572 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003573 *
Mark Brown74b8f952009-06-06 11:26:15 +01003574 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003575 * a valid audio route and active audio stream.
3576 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3577 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003578 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003579int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003580{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003581 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003582}
Liam Girdwooda5302182008-07-07 13:35:17 +01003583EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003584
3585/**
Mark Brownda341832010-03-15 19:23:37 +00003586 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003587 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003588 * @pin: pin name
3589 *
3590 * Enables input/output pin regardless of any other state. This is
3591 * intended for use with microphone bias supplies used in microphone
3592 * jack detection.
3593 *
3594 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3595 * do any widget power switching.
3596 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003597int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3598 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003599{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003600 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003601
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003602 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003603 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003604 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003605 }
3606
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003607 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003608 w->connected = 1;
3609 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003610 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003611
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003612 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003613}
3614EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3615
3616/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003617 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003618 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003619 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003620 *
Mark Brown74b8f952009-06-06 11:26:15 +01003621 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003622 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3623 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003624 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003625int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3626 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003627{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003628 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003629}
3630EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3631
3632/**
Mark Brown5817b522008-09-24 11:23:11 +01003633 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003634 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003635 * @pin: pin name
3636 *
3637 * Marks the specified pin as being not connected, disabling it along
3638 * any parent or child widgets. At present this is identical to
3639 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3640 * additional things such as disabling controls which only affect
3641 * paths through the pin.
3642 *
3643 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3644 * do any widget power switching.
3645 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003646int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003647{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003648 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003649}
3650EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3651
3652/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003653 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003654 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003655 * @pin: audio signal pin endpoint (or start point)
3656 *
3657 * Get audio pin status - connected or disconnected.
3658 *
3659 * Returns 1 for connected otherwise 0.
3660 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003661int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3662 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003663{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003664 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003665
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003666 if (w)
3667 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003668
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003669 return 0;
3670}
Liam Girdwooda5302182008-07-07 13:35:17 +01003671EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003672
3673/**
Mark Brown1547aba2010-05-07 21:11:40 +01003674 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003675 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003676 * @pin: audio signal pin endpoint (or start point)
3677 *
3678 * Mark the given endpoint or pin as ignoring suspend. When the
3679 * system is disabled a path between two endpoints flagged as ignoring
3680 * suspend will not be disabled. The path must already be enabled via
3681 * normal means at suspend time, it will not be turned on if it was not
3682 * already enabled.
3683 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003684int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3685 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003686{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003687 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003688
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003689 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003690 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003691 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003692 }
3693
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003694 w->ignore_suspend = 1;
3695
3696 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003697}
3698EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3699
Stephen Warren16332812011-11-23 12:42:04 -07003700static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3701 struct snd_soc_dapm_widget *w)
3702{
3703 struct snd_soc_dapm_path *p;
3704
3705 list_for_each_entry(p, &card->paths, list) {
3706 if ((p->source == w) || (p->sink == w)) {
3707 dev_dbg(card->dev,
3708 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3709 p->source->name, p->source->id, p->source->dapm,
3710 p->sink->name, p->sink->id, p->sink->dapm);
3711
3712 /* Connected to something other than the codec */
3713 if (p->source->dapm != p->sink->dapm)
3714 return true;
3715 /*
3716 * Loopback connection from codec external pin to
3717 * codec external pin
3718 */
3719 if (p->sink->id == snd_soc_dapm_input) {
3720 switch (p->source->id) {
3721 case snd_soc_dapm_output:
3722 case snd_soc_dapm_micbias:
3723 return true;
3724 default:
3725 break;
3726 }
3727 }
3728 }
3729 }
3730
3731 return false;
3732}
3733
3734/**
3735 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3736 * @codec: The codec whose pins should be processed
3737 *
3738 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3739 * which are unused. Pins are used if they are connected externally to the
3740 * codec, whether that be to some other device, or a loop-back connection to
3741 * the codec itself.
3742 */
3743void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3744{
3745 struct snd_soc_card *card = codec->card;
3746 struct snd_soc_dapm_context *dapm = &codec->dapm;
3747 struct snd_soc_dapm_widget *w;
3748
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003749 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003750 &card->dapm, &codec->dapm);
3751
3752 list_for_each_entry(w, &card->widgets, list) {
3753 if (w->dapm != dapm)
3754 continue;
3755 switch (w->id) {
3756 case snd_soc_dapm_input:
3757 case snd_soc_dapm_output:
3758 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003759 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003760 w->name);
3761 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003762 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003763 "... Not in map; disabling\n");
3764 snd_soc_dapm_nc_pin(dapm, w->name);
3765 }
3766 break;
3767 default:
3768 break;
3769 }
3770 }
3771}
3772
Mark Brown1547aba2010-05-07 21:11:40 +01003773/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003774 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003775 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003776 *
3777 * Free all dapm widgets and resources.
3778 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003779void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003780{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003781 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003782 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003783 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003784 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003785}
3786EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3787
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003788static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003789{
Liam Girdwood01005a72012-07-06 16:57:05 +01003790 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003791 struct snd_soc_dapm_widget *w;
3792 LIST_HEAD(down_list);
3793 int powerdown = 0;
3794
Liam Girdwood01005a72012-07-06 16:57:05 +01003795 mutex_lock(&card->dapm_mutex);
3796
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003797 list_for_each_entry(w, &dapm->card->widgets, list) {
3798 if (w->dapm != dapm)
3799 continue;
Mark Brown51737472009-06-22 13:16:51 +01003800 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003801 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003802 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003803 powerdown = 1;
3804 }
3805 }
3806
3807 /* If there were no widgets to power down we're already in
3808 * standby.
3809 */
3810 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003811 if (dapm->bias_level == SND_SOC_BIAS_ON)
3812 snd_soc_dapm_set_bias_level(dapm,
3813 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003814 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003815 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3816 snd_soc_dapm_set_bias_level(dapm,
3817 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003818 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003819
3820 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003821}
Mark Brown51737472009-06-22 13:16:51 +01003822
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003823/*
3824 * snd_soc_dapm_shutdown - callback for system shutdown
3825 */
3826void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3827{
3828 struct snd_soc_codec *codec;
3829
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003830 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003831 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003832 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3833 snd_soc_dapm_set_bias_level(&codec->dapm,
3834 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003835 }
Mark Brown51737472009-06-22 13:16:51 +01003836}
3837
Richard Purdie2b97eab2006-10-06 18:32:18 +02003838/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003839MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003840MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3841MODULE_LICENSE("GPL");