blob: da35b10ce6d199a213c9b2fb6c08e7c1098031d3 [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 {
178 struct snd_soc_dapm_widget_list wlist;
179};
180
181static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
182 struct snd_kcontrol *kcontrol)
183{
184 struct dapm_kcontrol_data *data;
185
186 data = kzalloc(sizeof(*data) + sizeof(widget), GFP_KERNEL);
187 if (!data) {
188 dev_err(widget->dapm->dev,
189 "ASoC: can't allocate kcontrol data for %s\n",
190 widget->name);
191 return -ENOMEM;
192 }
193
194 data->wlist.widgets[0] = widget;
195 data->wlist.num_widgets = 1;
196
197 kcontrol->private_data = data;
198
199 return 0;
200}
201
202static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
203{
204 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
205 kfree(data);
206}
207
208static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
209 const struct snd_kcontrol *kcontrol)
210{
211 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
212
213 return &data->wlist;
214}
215
216static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
217 struct snd_soc_dapm_widget *widget)
218{
219 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
220 struct dapm_kcontrol_data *new_data;
221 unsigned int n = data->wlist.num_widgets + 1;
222
223 new_data = krealloc(data, sizeof(*data) + sizeof(widget) * n,
224 GFP_KERNEL);
225 if (!data)
226 return -ENOMEM;
227
228 data->wlist.widgets[n - 1] = widget;
229 data->wlist.num_widgets = n;
230
231 kcontrol->private_data = data;
232
233 return 0;
234}
235
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200236/**
237 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
238 * @kcontrol: The kcontrol
239 */
240struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
241{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200242 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200243}
244EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
245
Liam Girdwood6c120e12012-02-15 15:15:34 +0000246static void dapm_reset(struct snd_soc_card *card)
247{
248 struct snd_soc_dapm_widget *w;
249
250 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
251
252 list_for_each_entry(w, &card->widgets, list) {
253 w->power_checked = false;
254 w->inputs = -1;
255 w->outputs = -1;
256 }
257}
258
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100259static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
260{
261 if (w->codec)
262 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100263 else if (w->platform)
264 return snd_soc_platform_read(w->platform, reg);
265
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000266 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100267 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100268}
269
270static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
271{
272 if (w->codec)
273 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100274 else if (w->platform)
275 return snd_soc_platform_write(w->platform, reg, val);
276
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000277 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100278 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100279}
280
Liam Girdwood49575fb52012-03-06 18:16:19 +0000281static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
282{
Mark Browne06ab3b2012-03-06 23:58:22 +0000283 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000284 mutex_lock(&w->codec->mutex);
285 else if (w->platform)
286 mutex_lock(&w->platform->mutex);
287}
288
289static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
290{
Mark Browne06ab3b2012-03-06 23:58:22 +0000291 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000292 mutex_unlock(&w->codec->mutex);
293 else if (w->platform)
294 mutex_unlock(&w->platform->mutex);
295}
296
297static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100298 unsigned short reg, unsigned int mask, unsigned int value)
299{
Mark Brown8a713da2011-12-03 12:33:55 +0000300 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100301 unsigned int old, new;
302 int ret;
303
Mark Brown8a713da2011-12-03 12:33:55 +0000304 if (w->codec && w->codec->using_regmap) {
305 ret = regmap_update_bits_check(w->codec->control_data,
306 reg, mask, value, &change);
307 if (ret != 0)
308 return ret;
309 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000310 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000311 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000312 if (ret < 0) {
313 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100314 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000315 }
Mark Brown8a713da2011-12-03 12:33:55 +0000316
317 old = ret;
318 new = (old & ~mask) | (value & mask);
319 change = old != new;
320 if (change) {
321 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000322 if (ret < 0) {
323 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000324 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000325 }
Mark Brown8a713da2011-12-03 12:33:55 +0000326 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000327 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100328 }
329
330 return change;
331}
332
Mark Brown452c5ea2009-05-17 21:41:23 +0100333/**
334 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800335 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100336 * @level: level to configure
337 *
338 * Configure the bias (power) levels for the SoC audio device.
339 *
340 * Returns 0 for success else error.
341 */
Mark Browned5a4c42011-02-18 11:12:42 -0800342static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200343 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100344{
Mark Browned5a4c42011-02-18 11:12:42 -0800345 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100346 int ret = 0;
347
Mark Brown84e90932010-11-04 00:07:02 -0400348 trace_snd_soc_bias_level_start(card, level);
349
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000350 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100351 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100352 if (ret != 0)
353 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100354
Mark Browncc4c6702011-06-06 19:03:34 +0100355 if (dapm->codec) {
356 if (dapm->codec->driver->set_bias_level)
357 ret = dapm->codec->driver->set_bias_level(dapm->codec,
358 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100359 else
360 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100361 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100362 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100363 }
Liam Girdwood41231282012-07-06 16:56:16 +0100364
Mark Brown171ec6b2011-06-06 18:15:19 +0100365 if (ret != 0)
366 goto out;
367
368 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100369 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100370out:
Mark Brown84e90932010-11-04 00:07:02 -0400371 trace_snd_soc_bias_level_done(card, level);
372
Mark Brown452c5ea2009-05-17 21:41:23 +0100373 return ret;
374}
375
Richard Purdie2b97eab2006-10-06 18:32:18 +0200376/* set up initial codec paths */
377static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
378 struct snd_soc_dapm_path *p, int i)
379{
380 switch (w->id) {
381 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000382 case snd_soc_dapm_mixer:
383 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200384 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100385 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600386 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400387 unsigned int reg = mc->reg;
388 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100389 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400390 unsigned int mask = (1 << fls(max)) - 1;
391 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200392
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100393 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200394 val = (val >> shift) & mask;
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200395 if (invert)
396 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200397
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200398 p->connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200399 }
400 break;
401 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600402 struct soc_enum *e = (struct soc_enum *)
403 w->kcontrol_news[i].private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200404 int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200405
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100406 val = soc_widget_read(w, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200407 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200408
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200409 if (item < e->max && !strcmp(p->name, e->texts[item]))
410 p->connect = 1;
411 else
412 p->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200413 }
414 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000415 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600416 struct soc_enum *e = (struct soc_enum *)
417 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000418
419 p->connect = 0;
420 /* since a virtual mux has no backing registers to
421 * decide which path to connect, it will try to match
422 * with the first enumeration. This is to ensure
423 * that the default mux choice (the first) will be
424 * correctly powered up during initialization.
425 */
426 if (!strcmp(p->name, e->texts[0]))
427 p->connect = 1;
428 }
429 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200430 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200431 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600432 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200433 int val, item;
434
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100435 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200436 val = (val >> e->shift_l) & e->mask;
437 for (item = 0; item < e->max; item++) {
438 if (val == e->values[item])
439 break;
440 }
441
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200442 if (item < e->max && !strcmp(p->name, e->texts[item]))
443 p->connect = 1;
444 else
445 p->connect = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200446 }
447 break;
Mark Brown56563102011-10-03 22:41:09 +0100448 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200449 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600450 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200451 case snd_soc_dapm_output:
452 case snd_soc_dapm_adc:
453 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000454 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200455 case snd_soc_dapm_dac:
456 case snd_soc_dapm_micbias:
457 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100458 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000459 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200460 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100461 case snd_soc_dapm_aif_in:
462 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100463 case snd_soc_dapm_dai_in:
464 case snd_soc_dapm_dai_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200465 case snd_soc_dapm_hp:
466 case snd_soc_dapm_mic:
467 case snd_soc_dapm_spk:
468 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100469 case snd_soc_dapm_dai_link:
Mark Brown56563102011-10-03 22:41:09 +0100470 p->connect = 1;
471 break;
472 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200473 case snd_soc_dapm_pre:
474 case snd_soc_dapm_post:
475 p->connect = 0;
476 break;
477 }
478}
479
Mark Brown74b8f952009-06-06 11:26:15 +0100480/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200481static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200482 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
483 struct snd_soc_dapm_path *path, const char *control_name,
484 const struct snd_kcontrol_new *kcontrol)
485{
486 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
487 int i;
488
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100489 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200490 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200491 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200492 list_add(&path->list_sink, &dest->sources);
493 list_add(&path->list_source, &src->sinks);
494 path->name = (char*)e->texts[i];
495 dapm_set_path_status(dest, path, 0);
496 return 0;
497 }
498 }
499
500 return -ENODEV;
501}
502
Mark Brown74b8f952009-06-06 11:26:15 +0100503/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200504static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200505 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
506 struct snd_soc_dapm_path *path, const char *control_name)
507{
508 int i;
509
510 /* search for mixer kcontrol */
511 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600512 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200513 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200514 list_add(&path->list_sink, &dest->sources);
515 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600516 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200517 dapm_set_path_status(dest, path, i);
518 return 0;
519 }
520 }
521 return -ENODEV;
522}
523
Stephen Warrenaf468002011-04-28 17:38:01 -0600524static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600525 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600526 const struct snd_kcontrol_new *kcontrol_new,
527 struct snd_kcontrol **kcontrol)
528{
529 struct snd_soc_dapm_widget *w;
530 int i;
531
532 *kcontrol = NULL;
533
534 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600535 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
536 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600537 for (i = 0; i < w->num_kcontrols; i++) {
538 if (&w->kcontrol_news[i] == kcontrol_new) {
539 if (w->kcontrols)
540 *kcontrol = w->kcontrols[i];
541 return 1;
542 }
543 }
544 }
545
546 return 0;
547}
548
Stephen Warren85762e72013-03-29 15:40:10 -0600549/*
550 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
551 * create it. Either way, add the widget into the control's widget list
552 */
553static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
554 int kci, struct snd_soc_dapm_path *path)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200555{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200556 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000557 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000558 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600559 size_t prefix_len;
560 int shared;
561 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600562 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600563 char *long_name;
564 const char *name;
565 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000566
567 if (dapm->codec)
568 prefix = dapm->codec->name_prefix;
569 else
570 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200571
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000572 if (prefix)
573 prefix_len = strlen(prefix) + 1;
574 else
575 prefix_len = 0;
576
Stephen Warren85762e72013-03-29 15:40:10 -0600577 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
578 &kcontrol);
579
Stephen Warren85762e72013-03-29 15:40:10 -0600580 if (!kcontrol) {
581 if (shared) {
582 wname_in_long_name = false;
583 kcname_in_long_name = true;
584 } else {
585 switch (w->id) {
586 case snd_soc_dapm_switch:
587 case snd_soc_dapm_mixer:
588 wname_in_long_name = true;
589 kcname_in_long_name = true;
590 break;
591 case snd_soc_dapm_mixer_named_ctl:
592 wname_in_long_name = false;
593 kcname_in_long_name = true;
594 break;
595 case snd_soc_dapm_mux:
596 case snd_soc_dapm_virt_mux:
597 case snd_soc_dapm_value_mux:
598 wname_in_long_name = true;
599 kcname_in_long_name = false;
600 break;
601 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600602 return -EINVAL;
603 }
604 }
605
606 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600607 /*
608 * The control will get a prefix from the control
609 * creation process but we're also using the same
610 * prefix for widgets so cut the prefix off the
611 * front of the widget name.
612 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200613 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600614 w->name + prefix_len,
615 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200616 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200617 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600618
619 name = long_name;
620 } else if (wname_in_long_name) {
621 long_name = NULL;
622 name = w->name + prefix_len;
623 } else {
624 long_name = NULL;
625 name = w->kcontrol_news[kci].name;
626 }
627
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200628 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600629 prefix);
Lars-Peter Clausen6b75bf02013-06-14 13:16:51 +0200630 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200631 kfree(long_name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200632
633 ret = dapm_kcontrol_data_alloc(w, kcontrol);
634 if (ret) {
635 snd_ctl_free_one(kcontrol);
636 return ret;
637 }
638
Stephen Warren85762e72013-03-29 15:40:10 -0600639 ret = snd_ctl_add(card, kcontrol);
640 if (ret < 0) {
641 dev_err(dapm->dev,
642 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
643 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600644 return ret;
645 }
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200646 } else {
647 ret = dapm_kcontrol_add_widget(kcontrol, w);
648 if (ret)
649 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600650 }
651
Stephen Warren85762e72013-03-29 15:40:10 -0600652 w->kcontrols[kci] = kcontrol;
653 path->kcontrol = kcontrol;
654
655 return 0;
656}
657
658/* create new dapm mixer control */
659static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
660{
661 int i, ret;
662 struct snd_soc_dapm_path *path;
663
Richard Purdie2b97eab2006-10-06 18:32:18 +0200664 /* add kcontrol */
665 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200666 /* match name */
667 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200668 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600669 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200670 continue;
671
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200672 if (w->kcontrols[i]) {
673 path->kcontrol = w->kcontrols[i];
674 continue;
675 }
676
Stephen Warren85762e72013-03-29 15:40:10 -0600677 ret = dapm_create_or_share_mixmux_kcontrol(w, i, path);
678 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200679 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200680 }
681 }
Stephen Warren85762e72013-03-29 15:40:10 -0600682
683 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200684}
685
686/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200687static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200688{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200689 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600690 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600691 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200692
Stephen Warrenaf468002011-04-28 17:38:01 -0600693 if (w->num_kcontrols != 1) {
694 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000695 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600696 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200697 return -EINVAL;
698 }
699
Stephen Warren85762e72013-03-29 15:40:10 -0600700 path = list_first_entry(&w->sources, struct snd_soc_dapm_path,
701 list_sink);
702 if (!path) {
703 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
704 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600705 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200706
Stephen Warren85762e72013-03-29 15:40:10 -0600707 ret = dapm_create_or_share_mixmux_kcontrol(w, 0, path);
708 if (ret < 0)
709 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600710
Richard Purdie2b97eab2006-10-06 18:32:18 +0200711 list_for_each_entry(path, &w->sources, list_sink)
Stephen Warren85762e72013-03-29 15:40:10 -0600712 path->kcontrol = w->kcontrols[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200713
Stephen Warrenaf468002011-04-28 17:38:01 -0600714 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200715}
716
717/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200718static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200719{
Mark Browna6c65732010-03-03 17:45:21 +0000720 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200721 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000722 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200723
Mark Browna6c65732010-03-03 17:45:21 +0000724 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200725}
726
727/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100728static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
729 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200730{
731 struct snd_soc_dapm_path *p;
732
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100733 list_for_each_entry(p, sink, list_source) {
734 if (p->walked) {
735 p->walked = 0;
736 dapm_clear_walk_output(dapm, &p->sink->sinks);
737 }
738 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200739}
740
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100741static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
742 struct list_head *source)
743{
744 struct snd_soc_dapm_path *p;
745
746 list_for_each_entry(p, source, list_sink) {
747 if (p->walked) {
748 p->walked = 0;
749 dapm_clear_walk_input(dapm, &p->source->sources);
750 }
751 }
752}
753
754
Mark Brown99497882010-05-07 20:24:05 +0100755/* We implement power down on suspend by checking the power state of
756 * the ALSA card - when we are suspending the ALSA state for the card
757 * is set to D3.
758 */
759static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
760{
Mark Brown12ea2c72011-03-02 18:17:32 +0000761 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100762
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000763 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100764 case SNDRV_CTL_POWER_D3hot:
765 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100766 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000767 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200768 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100769 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100770 default:
771 return 1;
772 }
773}
774
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100775/* add widget to list if it's not already in the list */
776static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
777 struct snd_soc_dapm_widget *w)
778{
779 struct snd_soc_dapm_widget_list *wlist;
780 int wlistsize, wlistentries, i;
781
782 if (*list == NULL)
783 return -EINVAL;
784
785 wlist = *list;
786
787 /* is this widget already in the list */
788 for (i = 0; i < wlist->num_widgets; i++) {
789 if (wlist->widgets[i] == w)
790 return 0;
791 }
792
793 /* allocate some new space */
794 wlistentries = wlist->num_widgets + 1;
795 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
796 wlistentries * sizeof(struct snd_soc_dapm_widget *);
797 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
798 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000799 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100800 w->name);
801 return -ENOMEM;
802 }
803 wlist = *list;
804
805 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000806 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100807 w->name, wlist->num_widgets);
808
809 wlist->widgets[wlist->num_widgets] = w;
810 wlist->num_widgets++;
811 return 1;
812}
813
Richard Purdie2b97eab2006-10-06 18:32:18 +0200814/*
815 * Recursively check for a completed path to an active or physically connected
816 * output widget. Returns number of complete paths.
817 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100818static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
819 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200820{
821 struct snd_soc_dapm_path *path;
822 int con = 0;
823
Mark Brown024dc072011-10-09 11:52:05 +0100824 if (widget->outputs >= 0)
825 return widget->outputs;
826
Mark Brownde02d072011-09-20 21:43:24 +0100827 DAPM_UPDATE_STAT(widget, path_checks);
828
Mark Brown62ea8742012-01-21 21:14:48 +0000829 switch (widget->id) {
830 case snd_soc_dapm_supply:
831 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200832 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100833 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000834 default:
835 break;
836 }
Mark Brown246d0a12009-04-22 18:24:55 +0100837
Mark Brown010ff262009-08-17 17:39:22 +0100838 switch (widget->id) {
839 case snd_soc_dapm_adc:
840 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100841 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100842 if (widget->active) {
843 widget->outputs = snd_soc_dapm_suspend_check(widget);
844 return widget->outputs;
845 }
Mark Brown010ff262009-08-17 17:39:22 +0100846 default:
847 break;
848 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200849
850 if (widget->connected) {
851 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100852 if (widget->id == snd_soc_dapm_output && !widget->ext) {
853 widget->outputs = snd_soc_dapm_suspend_check(widget);
854 return widget->outputs;
855 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200856
857 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100858 if (widget->id == snd_soc_dapm_hp ||
859 widget->id == snd_soc_dapm_spk ||
860 (widget->id == snd_soc_dapm_line &&
861 !list_empty(&widget->sources))) {
862 widget->outputs = snd_soc_dapm_suspend_check(widget);
863 return widget->outputs;
864 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200865 }
866
867 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100868 DAPM_UPDATE_STAT(widget, neighbour_checks);
869
Mark Brownbf3a9e12011-06-13 16:42:29 +0100870 if (path->weak)
871 continue;
872
Mark Brown8af294b2013-02-22 17:48:15 +0000873 if (path->walking)
874 return 1;
875
Richard Purdie2b97eab2006-10-06 18:32:18 +0200876 if (path->walked)
877 continue;
878
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100879 trace_snd_soc_dapm_output_path(widget, path);
880
Richard Purdie2b97eab2006-10-06 18:32:18 +0200881 if (path->sink && path->connect) {
882 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000883 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100884
885 /* do we need to add this widget to the list ? */
886 if (list) {
887 int err;
888 err = dapm_list_add_widget(list, path->sink);
889 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000890 dev_err(widget->dapm->dev,
891 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100892 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000893 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100894 return con;
895 }
896 }
897
898 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000899
900 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200901 }
902 }
903
Mark Brown024dc072011-10-09 11:52:05 +0100904 widget->outputs = con;
905
Richard Purdie2b97eab2006-10-06 18:32:18 +0200906 return con;
907}
908
909/*
910 * Recursively check for a completed path to an active or physically connected
911 * input widget. Returns number of complete paths.
912 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100913static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
914 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200915{
916 struct snd_soc_dapm_path *path;
917 int con = 0;
918
Mark Brown024dc072011-10-09 11:52:05 +0100919 if (widget->inputs >= 0)
920 return widget->inputs;
921
Mark Brownde02d072011-09-20 21:43:24 +0100922 DAPM_UPDATE_STAT(widget, path_checks);
923
Mark Brown62ea8742012-01-21 21:14:48 +0000924 switch (widget->id) {
925 case snd_soc_dapm_supply:
926 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200927 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100928 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000929 default:
930 break;
931 }
Mark Brown246d0a12009-04-22 18:24:55 +0100932
Richard Purdie2b97eab2006-10-06 18:32:18 +0200933 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100934 switch (widget->id) {
935 case snd_soc_dapm_dac:
936 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +0100937 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +0100938 if (widget->active) {
939 widget->inputs = snd_soc_dapm_suspend_check(widget);
940 return widget->inputs;
941 }
Mark Brown010ff262009-08-17 17:39:22 +0100942 default:
943 break;
944 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200945
946 if (widget->connected) {
947 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100948 if (widget->id == snd_soc_dapm_input && !widget->ext) {
949 widget->inputs = snd_soc_dapm_suspend_check(widget);
950 return widget->inputs;
951 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200952
953 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100954 if (widget->id == snd_soc_dapm_vmid) {
955 widget->inputs = snd_soc_dapm_suspend_check(widget);
956 return widget->inputs;
957 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200958
959 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300960 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100961 (widget->id == snd_soc_dapm_line &&
962 !list_empty(&widget->sinks))) {
963 widget->inputs = snd_soc_dapm_suspend_check(widget);
964 return widget->inputs;
965 }
966
Mark Brown1ab97c82011-11-27 16:21:51 +0000967 /* signal generator */
968 if (widget->id == snd_soc_dapm_siggen) {
969 widget->inputs = snd_soc_dapm_suspend_check(widget);
970 return widget->inputs;
971 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200972 }
973
974 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +0100975 DAPM_UPDATE_STAT(widget, neighbour_checks);
976
Mark Brownbf3a9e12011-06-13 16:42:29 +0100977 if (path->weak)
978 continue;
979
Mark Brown8af294b2013-02-22 17:48:15 +0000980 if (path->walking)
981 return 1;
982
Richard Purdie2b97eab2006-10-06 18:32:18 +0200983 if (path->walked)
984 continue;
985
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100986 trace_snd_soc_dapm_input_path(widget, path);
987
Richard Purdie2b97eab2006-10-06 18:32:18 +0200988 if (path->source && path->connect) {
989 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000990 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100991
992 /* do we need to add this widget to the list ? */
993 if (list) {
994 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +0100995 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100996 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000997 dev_err(widget->dapm->dev,
998 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100999 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001000 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001001 return con;
1002 }
1003 }
1004
1005 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001006
1007 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001008 }
1009 }
1010
Mark Brown024dc072011-10-09 11:52:05 +01001011 widget->inputs = con;
1012
Richard Purdie2b97eab2006-10-06 18:32:18 +02001013 return con;
1014}
1015
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001016/**
1017 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1018 * @dai: the soc DAI.
1019 * @stream: stream direction.
1020 * @list: list of active widgets for this stream.
1021 *
1022 * Queries DAPM graph as to whether an valid audio stream path exists for
1023 * the initial stream specified by name. This takes into account
1024 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1025 *
1026 * Returns the number of valid paths or negative error.
1027 */
1028int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1029 struct snd_soc_dapm_widget_list **list)
1030{
1031 struct snd_soc_card *card = dai->card;
1032 int paths;
1033
1034 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1035 dapm_reset(card);
1036
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001037 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001038 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001039 dapm_clear_walk_output(&card->dapm,
1040 &dai->playback_widget->sinks);
1041 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001042 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001043 dapm_clear_walk_input(&card->dapm,
1044 &dai->capture_widget->sources);
1045 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001046
1047 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001048 mutex_unlock(&card->dapm_mutex);
1049
1050 return paths;
1051}
1052
Richard Purdie2b97eab2006-10-06 18:32:18 +02001053/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001054 * Handler for generic register modifier widget.
1055 */
1056int dapm_reg_event(struct snd_soc_dapm_widget *w,
1057 struct snd_kcontrol *kcontrol, int event)
1058{
1059 unsigned int val;
1060
1061 if (SND_SOC_DAPM_EVENT_ON(event))
1062 val = w->on_val;
1063 else
1064 val = w->off_val;
1065
Liam Girdwood49575fb52012-03-06 18:16:19 +00001066 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001067 w->mask << w->shift, val << w->shift);
1068
1069 return 0;
1070}
Mark Brown11589412008-07-29 11:42:23 +01001071EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001072
Mark Brown62ea8742012-01-21 21:14:48 +00001073/*
1074 * Handler for regulator supply widget.
1075 */
1076int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1077 struct snd_kcontrol *kcontrol, int event)
1078{
Mark Brownc05b84d2012-09-07 12:57:11 +08001079 int ret;
1080
1081 if (SND_SOC_DAPM_EVENT_ON(event)) {
1082 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001083 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001084 if (ret != 0)
1085 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001086 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001087 w->name, ret);
1088 }
1089
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001090 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001091 } else {
1092 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001093 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001094 if (ret != 0)
1095 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001096 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001097 w->name, ret);
1098 }
1099
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001100 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001101 }
Mark Brown62ea8742012-01-21 21:14:48 +00001102}
1103EXPORT_SYMBOL_GPL(dapm_regulator_event);
1104
Ola Liljad7e7eb92012-05-24 15:26:25 +02001105/*
1106 * Handler for clock supply widget.
1107 */
1108int dapm_clock_event(struct snd_soc_dapm_widget *w,
1109 struct snd_kcontrol *kcontrol, int event)
1110{
1111 if (!w->clk)
1112 return -EIO;
1113
Mark Brownec029952012-06-04 08:16:20 +01001114#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001115 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001116 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001117 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001118 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001119 return 0;
1120 }
Mark Brownec029952012-06-04 08:16:20 +01001121#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001122 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001123}
1124EXPORT_SYMBOL_GPL(dapm_clock_event);
1125
Mark Brownd8050022011-09-28 18:28:23 +01001126static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1127{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001128 if (w->power_checked)
1129 return w->new_power;
1130
Mark Brownd8050022011-09-28 18:28:23 +01001131 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001132 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001133 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001134 w->new_power = w->power_check(w);
1135
1136 w->power_checked = true;
1137
1138 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001139}
1140
Mark Browncd0f2d42009-04-20 16:56:59 +01001141/* Generic check to see if a widget should be powered.
1142 */
1143static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1144{
1145 int in, out;
1146
Mark Brownde02d072011-09-20 21:43:24 +01001147 DAPM_UPDATE_STAT(w, power_checks);
1148
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001149 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001150 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001151 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001152 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001153 return out != 0 && in != 0;
1154}
1155
Mark Brown6ea31b92009-04-20 17:15:41 +01001156/* Check to see if an ADC has power */
1157static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1158{
1159 int in;
1160
Mark Brownde02d072011-09-20 21:43:24 +01001161 DAPM_UPDATE_STAT(w, power_checks);
1162
Mark Brown6ea31b92009-04-20 17:15:41 +01001163 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001164 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001165 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001166 return in != 0;
1167 } else {
1168 return dapm_generic_check_power(w);
1169 }
1170}
1171
1172/* Check to see if a DAC has power */
1173static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1174{
1175 int out;
1176
Mark Brownde02d072011-09-20 21:43:24 +01001177 DAPM_UPDATE_STAT(w, power_checks);
1178
Mark Brown6ea31b92009-04-20 17:15:41 +01001179 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001180 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001181 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001182 return out != 0;
1183 } else {
1184 return dapm_generic_check_power(w);
1185 }
1186}
1187
Mark Brown246d0a12009-04-22 18:24:55 +01001188/* Check to see if a power supply is needed */
1189static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1190{
1191 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001192
Mark Brownde02d072011-09-20 21:43:24 +01001193 DAPM_UPDATE_STAT(w, power_checks);
1194
Mark Brown246d0a12009-04-22 18:24:55 +01001195 /* Check if one of our outputs is connected */
1196 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001197 DAPM_UPDATE_STAT(w, neighbour_checks);
1198
Mark Brownbf3a9e12011-06-13 16:42:29 +01001199 if (path->weak)
1200 continue;
1201
Mark Brown215edda2009-09-08 18:59:05 +01001202 if (path->connected &&
1203 !path->connected(path->source, path->sink))
1204 continue;
1205
Mark Brown30173582011-02-11 11:42:19 +00001206 if (!path->sink)
1207 continue;
1208
Mark Brownf68d7e12011-10-04 22:57:50 +01001209 if (dapm_widget_power_check(path->sink))
1210 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001211 }
1212
Mark Brownf68d7e12011-10-04 22:57:50 +01001213 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001214}
1215
Mark Brown35c64bc2011-09-28 18:23:53 +01001216static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1217{
1218 return 1;
1219}
1220
Mark Brown38357ab2009-06-06 19:03:23 +01001221static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1222 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001223 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001224{
Mark Brown828a8422011-01-15 13:14:30 +00001225 int *sort;
1226
1227 if (power_up)
1228 sort = dapm_up_seq;
1229 else
1230 sort = dapm_down_seq;
1231
Mark Brown38357ab2009-06-06 19:03:23 +01001232 if (sort[a->id] != sort[b->id])
1233 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001234 if (a->subseq != b->subseq) {
1235 if (power_up)
1236 return a->subseq - b->subseq;
1237 else
1238 return b->subseq - a->subseq;
1239 }
Mark Brownb22ead22009-06-07 12:51:26 +01001240 if (a->reg != b->reg)
1241 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001242 if (a->dapm != b->dapm)
1243 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001244
Mark Brown38357ab2009-06-06 19:03:23 +01001245 return 0;
1246}
Mark Brown42aa3412009-03-01 19:21:10 +00001247
Mark Brown38357ab2009-06-06 19:03:23 +01001248/* Insert a widget in order into a DAPM power sequence. */
1249static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1250 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001251 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001252{
1253 struct snd_soc_dapm_widget *w;
1254
1255 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001256 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001257 list_add_tail(&new_widget->power_list, &w->power_list);
1258 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001259 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001260
Mark Brown38357ab2009-06-06 19:03:23 +01001261 list_add_tail(&new_widget->power_list, list);
1262}
Mark Brown42aa3412009-03-01 19:21:10 +00001263
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001264static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001265 struct snd_soc_dapm_widget *w, int event)
1266{
Mark Brown68f89ad2010-11-03 23:51:49 -04001267 const char *ev_name;
1268 int power, ret;
1269
1270 switch (event) {
1271 case SND_SOC_DAPM_PRE_PMU:
1272 ev_name = "PRE_PMU";
1273 power = 1;
1274 break;
1275 case SND_SOC_DAPM_POST_PMU:
1276 ev_name = "POST_PMU";
1277 power = 1;
1278 break;
1279 case SND_SOC_DAPM_PRE_PMD:
1280 ev_name = "PRE_PMD";
1281 power = 0;
1282 break;
1283 case SND_SOC_DAPM_POST_PMD:
1284 ev_name = "POST_PMD";
1285 power = 0;
1286 break;
Mark Brown80114122013-02-25 15:14:19 +00001287 case SND_SOC_DAPM_WILL_PMU:
1288 ev_name = "WILL_PMU";
1289 power = 1;
1290 break;
1291 case SND_SOC_DAPM_WILL_PMD:
1292 ev_name = "WILL_PMD";
1293 power = 0;
1294 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001295 default:
1296 BUG();
1297 return;
1298 }
1299
1300 if (w->power != power)
1301 return;
1302
1303 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001304 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001305 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001306 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001307 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001308 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001309 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001310 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001311 ev_name, w->name, ret);
1312 }
1313}
1314
Mark Brownb22ead22009-06-07 12:51:26 +01001315/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001316static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001317 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001318{
Mark Brown68f89ad2010-11-03 23:51:49 -04001319 struct snd_soc_dapm_widget *w;
1320 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001321 unsigned int value = 0;
1322 unsigned int mask = 0;
1323 unsigned int cur_mask;
1324
1325 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1326 power_list)->reg;
1327
1328 list_for_each_entry(w, pending, power_list) {
1329 cur_mask = 1 << w->shift;
1330 BUG_ON(reg != w->reg);
1331
1332 if (w->invert)
1333 power = !w->power;
1334 else
1335 power = w->power;
1336
1337 mask |= cur_mask;
1338 if (power)
1339 value |= cur_mask;
1340
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001341 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001342 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1343 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001344
Mark Brown68f89ad2010-11-03 23:51:49 -04001345 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001346 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1347 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001348 }
1349
Mark Brown81628102009-06-07 13:21:24 +01001350 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001351 /* Any widget will do, they should all be updating the
1352 * same register.
1353 */
1354 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1355 power_list);
1356
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001357 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001358 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001359 value, mask, reg, card->pop_time);
1360 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001361 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001362 }
1363
1364 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001365 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1366 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001367 }
Mark Brown42aa3412009-03-01 19:21:10 +00001368}
1369
Mark Brownb22ead22009-06-07 12:51:26 +01001370/* Apply a DAPM power sequence.
1371 *
1372 * We walk over a pre-sorted list of widgets to apply power to. In
1373 * order to minimise the number of writes to the device required
1374 * multiple widgets will be updated in a single write where possible.
1375 * Currently anything that requires more than a single write is not
1376 * handled.
1377 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001378static void dapm_seq_run(struct snd_soc_card *card,
1379 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001380{
1381 struct snd_soc_dapm_widget *w, *n;
1382 LIST_HEAD(pending);
1383 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001384 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001385 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001386 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001387 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001388 int *sort;
1389
1390 if (power_up)
1391 sort = dapm_up_seq;
1392 else
1393 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001394
Mark Brownb22ead22009-06-07 12:51:26 +01001395 list_for_each_entry_safe(w, n, list, power_list) {
1396 ret = 0;
1397
1398 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001399 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001400 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001401 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001402 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001403
Mark Brown474b62d2011-01-18 16:14:44 +00001404 if (cur_dapm && cur_dapm->seq_notifier) {
1405 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1406 if (sort[i] == cur_sort)
1407 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001408 i,
1409 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001410 }
1411
Mark Brownb22ead22009-06-07 12:51:26 +01001412 INIT_LIST_HEAD(&pending);
1413 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001414 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001415 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001416 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001417 }
1418
Mark Brown163cac02009-06-07 10:12:52 +01001419 switch (w->id) {
1420 case snd_soc_dapm_pre:
1421 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001422 list_for_each_entry_safe_continue(w, n, list,
1423 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001424
Mark Brownb22ead22009-06-07 12:51:26 +01001425 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001426 ret = w->event(w,
1427 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001428 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001429 ret = w->event(w,
1430 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001431 break;
1432
1433 case snd_soc_dapm_post:
1434 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001435 list_for_each_entry_safe_continue(w, n, list,
1436 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001437
Mark Brownb22ead22009-06-07 12:51:26 +01001438 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001439 ret = w->event(w,
1440 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001441 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001442 ret = w->event(w,
1443 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001444 break;
1445
Mark Brown163cac02009-06-07 10:12:52 +01001446 default:
Mark Brown81628102009-06-07 13:21:24 +01001447 /* Queue it up for application */
1448 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001449 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001450 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001451 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001452 list_move(&w->power_list, &pending);
1453 break;
Mark Brown163cac02009-06-07 10:12:52 +01001454 }
Mark Brownb22ead22009-06-07 12:51:26 +01001455
1456 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001457 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001458 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001459 }
Mark Brownb22ead22009-06-07 12:51:26 +01001460
1461 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001462 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001463
1464 if (cur_dapm && cur_dapm->seq_notifier) {
1465 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1466 if (sort[i] == cur_sort)
1467 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001468 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001469 }
Mark Brown163cac02009-06-07 10:12:52 +01001470}
1471
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001472static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001473{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001474 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001475 struct snd_soc_dapm_widget_list *wlist;
1476 struct snd_soc_dapm_widget *w = NULL;
1477 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001478 int ret;
1479
1480 if (!update)
1481 return;
1482
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001483 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001484
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001485 for (wi = 0; wi < wlist->num_widgets; wi++) {
1486 w = wlist->widgets[wi];
1487
1488 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1489 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1490 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001491 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001492 w->name, ret);
1493 }
Mark Brown97404f22010-12-14 16:13:57 +00001494 }
1495
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001496 if (!w)
1497 return;
1498
Liam Girdwood49575fb52012-03-06 18:16:19 +00001499 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001500 update->val);
1501 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001502 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001503 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001504
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001505 for (wi = 0; wi < wlist->num_widgets; wi++) {
1506 w = wlist->widgets[wi];
1507
1508 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1509 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1510 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001511 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001512 w->name, ret);
1513 }
Mark Brown97404f22010-12-14 16:13:57 +00001514 }
1515}
1516
Mark Brown9d0624a2011-02-18 11:49:43 -08001517/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1518 * they're changing state.
1519 */
1520static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1521{
1522 struct snd_soc_dapm_context *d = data;
1523 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001524
Mark Brown56fba412011-06-04 11:25:10 +01001525 /* If we're off and we're not supposed to be go into STANDBY */
1526 if (d->bias_level == SND_SOC_BIAS_OFF &&
1527 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001528 if (d->dev)
1529 pm_runtime_get_sync(d->dev);
1530
Mark Brown9d0624a2011-02-18 11:49:43 -08001531 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1532 if (ret != 0)
1533 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001534 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001535 }
1536
Mark Brown56fba412011-06-04 11:25:10 +01001537 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1538 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001539 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1540 if (ret != 0)
1541 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001542 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001543 }
1544}
1545
1546/* Async callback run prior to DAPM sequences - brings to their final
1547 * state.
1548 */
1549static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1550{
1551 struct snd_soc_dapm_context *d = data;
1552 int ret;
1553
1554 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001555 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1556 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1557 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001558 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1559 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001560 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001561 ret);
1562 }
1563
1564 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001565 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1566 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001567 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1568 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001569 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1570 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001571
1572 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001573 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001574 }
1575
1576 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001577 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1578 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001579 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1580 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001581 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001582 ret);
1583 }
1584}
Mark Brown97404f22010-12-14 16:13:57 +00001585
Mark Brownfe4fda52011-10-03 22:36:57 +01001586static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1587 bool power, bool connect)
1588{
1589 /* If a connection is being made or broken then that update
1590 * will have marked the peer dirty, otherwise the widgets are
1591 * not connected and this update has no impact. */
1592 if (!connect)
1593 return;
1594
1595 /* If the peer is already in the state we're moving to then we
1596 * won't have an impact on it. */
1597 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001598 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001599}
1600
Mark Brown05623c42011-09-28 17:02:31 +01001601static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1602 struct list_head *up_list,
1603 struct list_head *down_list)
1604{
Mark Browndb432b42011-10-03 21:06:40 +01001605 struct snd_soc_dapm_path *path;
1606
Mark Brown05623c42011-09-28 17:02:31 +01001607 if (w->power == power)
1608 return;
1609
1610 trace_snd_soc_dapm_widget_power(w, power);
1611
Mark Browndb432b42011-10-03 21:06:40 +01001612 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001613 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001614 */
1615 list_for_each_entry(path, &w->sources, list_sink) {
1616 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001617 dapm_widget_set_peer_power(path->source, power,
1618 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001619 }
1620 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001621 switch (w->id) {
1622 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001623 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001624 case snd_soc_dapm_clock_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001625 /* Supplies can't affect their outputs, only their inputs */
1626 break;
1627 default:
1628 list_for_each_entry(path, &w->sinks, list_source) {
1629 if (path->sink) {
1630 dapm_widget_set_peer_power(path->sink, power,
1631 path->connect);
1632 }
Mark Browndb432b42011-10-03 21:06:40 +01001633 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001634 break;
Mark Browndb432b42011-10-03 21:06:40 +01001635 }
1636
Mark Brown05623c42011-09-28 17:02:31 +01001637 if (power)
1638 dapm_seq_insert(w, up_list, true);
1639 else
1640 dapm_seq_insert(w, down_list, false);
1641
1642 w->power = power;
1643}
1644
Mark Brown7c81beb2011-09-20 22:22:32 +01001645static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1646 struct list_head *up_list,
1647 struct list_head *down_list)
1648{
Mark Brown7c81beb2011-09-20 22:22:32 +01001649 int power;
1650
1651 switch (w->id) {
1652 case snd_soc_dapm_pre:
1653 dapm_seq_insert(w, down_list, false);
1654 break;
1655 case snd_soc_dapm_post:
1656 dapm_seq_insert(w, up_list, true);
1657 break;
1658
1659 default:
Mark Brownd8050022011-09-28 18:28:23 +01001660 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001661
Mark Brown05623c42011-09-28 17:02:31 +01001662 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001663 break;
1664 }
1665}
1666
Mark Brown42aa3412009-03-01 19:21:10 +00001667/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001668 * Scan each dapm widget for complete audio path.
1669 * A complete path is a route that has valid endpoints i.e.:-
1670 *
1671 * o DAC to output pin.
1672 * o Input Pin to ADC.
1673 * o Input pin to Output pin (bypass, sidetone)
1674 * o DAC to ADC (loopback).
1675 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001676static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001677{
1678 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001679 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001680 LIST_HEAD(up_list);
1681 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001682 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001683 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001684
Mark Brown84e90932010-11-04 00:07:02 -04001685 trace_snd_soc_dapm_start(card);
1686
Mark Brown56fba412011-06-04 11:25:10 +01001687 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001688 if (d->idle_bias_off)
1689 d->target_bias_level = SND_SOC_BIAS_OFF;
1690 else
1691 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001692 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001693
Liam Girdwood6c120e12012-02-15 15:15:34 +00001694 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001695
Mark Brown6d3ddc82009-05-16 17:47:29 +01001696 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001697 * lists indicating if they should be powered up or down. We
1698 * only check widgets that have been flagged as dirty but note
1699 * that new widgets may be added to the dirty list while we
1700 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001701 */
Mark Browndb432b42011-10-03 21:06:40 +01001702 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001703 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001704 }
1705
Mark Brownf9de6d72011-09-28 17:19:47 +01001706 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001707 switch (w->id) {
1708 case snd_soc_dapm_pre:
1709 case snd_soc_dapm_post:
1710 /* These widgets always need to be powered */
1711 break;
1712 default:
1713 list_del_init(&w->dirty);
1714 break;
1715 }
Mark Browndb432b42011-10-03 21:06:40 +01001716
Mark Brownf9de6d72011-09-28 17:19:47 +01001717 if (w->power) {
1718 d = w->dapm;
1719
1720 /* Supplies and micbiases only bring the
1721 * context up to STANDBY as unless something
1722 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001723 * generally don't require full power. Signal
1724 * generators are virtual pins and have no
1725 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001726 */
1727 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001728 case snd_soc_dapm_siggen:
1729 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001730 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001731 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001732 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001733 case snd_soc_dapm_micbias:
1734 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1735 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1736 break;
1737 default:
1738 d->target_bias_level = SND_SOC_BIAS_ON;
1739 break;
1740 }
1741 }
1742
1743 }
1744
Mark Brown85a843c2011-09-21 21:29:47 +01001745 /* Force all contexts in the card to the same bias state if
1746 * they're not ground referenced.
1747 */
Mark Brown56fba412011-06-04 11:25:10 +01001748 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001749 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001750 if (d->target_bias_level > bias)
1751 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001752 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001753 if (!d->idle_bias_off)
1754 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001755
Mark Brownde02d072011-09-20 21:43:24 +01001756 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001757
Mark Brown9d0624a2011-02-18 11:49:43 -08001758 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001759 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001760 async_schedule_domain(dapm_pre_sequence_async, d,
1761 &async_domain);
1762 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001763
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001764 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001765 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001766 }
1767
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001768 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001769 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001770 }
1771
Mark Brown6d3ddc82009-05-16 17:47:29 +01001772 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001773 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001774
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001775 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001776
Mark Brown6d3ddc82009-05-16 17:47:29 +01001777 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001778 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001779
Mark Brown9d0624a2011-02-18 11:49:43 -08001780 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001781 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001782 async_schedule_domain(dapm_post_sequence_async, d,
1783 &async_domain);
1784 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001785
Liam Girdwood8078d872012-02-15 15:15:35 +00001786 /* do we need to notify any clients that DAPM event is complete */
1787 list_for_each_entry(d, &card->dapm_list, list) {
1788 if (d->stream_event)
1789 d->stream_event(d, event);
1790 }
1791
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001792 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001793 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001794 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001795
Mark Brown84e90932010-11-04 00:07:02 -04001796 trace_snd_soc_dapm_done(card);
1797
Mark Brown42aa3412009-03-01 19:21:10 +00001798 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001799}
1800
Mark Brown79fb9382009-08-21 16:38:13 +01001801#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001802static ssize_t dapm_widget_power_read_file(struct file *file,
1803 char __user *user_buf,
1804 size_t count, loff_t *ppos)
1805{
1806 struct snd_soc_dapm_widget *w = file->private_data;
1807 char *buf;
1808 int in, out;
1809 ssize_t ret;
1810 struct snd_soc_dapm_path *p = NULL;
1811
1812 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1813 if (!buf)
1814 return -ENOMEM;
1815
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001816 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001817 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001818 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001819 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001820
Mark Brownf13ebad2012-03-03 18:01:01 +00001821 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1822 w->name, w->power ? "On" : "Off",
1823 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001824
Mark Brownd033c362009-12-04 15:25:56 +00001825 if (w->reg >= 0)
1826 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1827 " - R%d(0x%x) bit %d",
1828 w->reg, w->reg, w->shift);
1829
1830 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1831
Mark Brown3eef08b2009-09-14 16:49:00 +01001832 if (w->sname)
1833 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1834 w->sname,
1835 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001836
1837 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001838 if (p->connected && !p->connected(w, p->sink))
1839 continue;
1840
Mark Brown79fb9382009-08-21 16:38:13 +01001841 if (p->connect)
1842 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001843 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001844 p->name ? p->name : "static",
1845 p->source->name);
1846 }
1847 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001848 if (p->connected && !p->connected(w, p->sink))
1849 continue;
1850
Mark Brown79fb9382009-08-21 16:38:13 +01001851 if (p->connect)
1852 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001853 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001854 p->name ? p->name : "static",
1855 p->sink->name);
1856 }
1857
1858 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1859
1860 kfree(buf);
1861 return ret;
1862}
1863
1864static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001865 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001866 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001867 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001868};
1869
Mark Brownef49e4f2011-04-04 20:48:13 +09001870static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1871 size_t count, loff_t *ppos)
1872{
1873 struct snd_soc_dapm_context *dapm = file->private_data;
1874 char *level;
1875
1876 switch (dapm->bias_level) {
1877 case SND_SOC_BIAS_ON:
1878 level = "On\n";
1879 break;
1880 case SND_SOC_BIAS_PREPARE:
1881 level = "Prepare\n";
1882 break;
1883 case SND_SOC_BIAS_STANDBY:
1884 level = "Standby\n";
1885 break;
1886 case SND_SOC_BIAS_OFF:
1887 level = "Off\n";
1888 break;
1889 default:
1890 BUG();
1891 level = "Unknown\n";
1892 break;
1893 }
1894
1895 return simple_read_from_buffer(user_buf, count, ppos, level,
1896 strlen(level));
1897}
1898
1899static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001900 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001901 .read = dapm_bias_read_file,
1902 .llseek = default_llseek,
1903};
1904
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001905void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1906 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001907{
Mark Brown79fb9382009-08-21 16:38:13 +01001908 struct dentry *d;
1909
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001910 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1911
1912 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001913 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001914 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001915 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001916 }
Mark Brown79fb9382009-08-21 16:38:13 +01001917
Mark Brownef49e4f2011-04-04 20:48:13 +09001918 d = debugfs_create_file("bias_level", 0444,
1919 dapm->debugfs_dapm, dapm,
1920 &dapm_bias_fops);
1921 if (!d)
1922 dev_warn(dapm->dev,
1923 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001924}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001925
1926static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1927{
1928 struct snd_soc_dapm_context *dapm = w->dapm;
1929 struct dentry *d;
1930
1931 if (!dapm->debugfs_dapm || !w->name)
1932 return;
1933
1934 d = debugfs_create_file(w->name, 0444,
1935 dapm->debugfs_dapm, w,
1936 &dapm_widget_power_fops);
1937 if (!d)
1938 dev_warn(w->dapm->dev,
1939 "ASoC: Failed to create %s debugfs file\n",
1940 w->name);
1941}
1942
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001943static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1944{
1945 debugfs_remove_recursive(dapm->debugfs_dapm);
1946}
1947
Mark Brown79fb9382009-08-21 16:38:13 +01001948#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001949void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1950 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001951{
1952}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001953
1954static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1955{
1956}
1957
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001958static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1959{
1960}
1961
Mark Brown79fb9382009-08-21 16:38:13 +01001962#endif
1963
Richard Purdie2b97eab2006-10-06 18:32:18 +02001964/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001965static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001966 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001967{
1968 struct snd_soc_dapm_path *path;
1969 int found = 0;
1970
Richard Purdie2b97eab2006-10-06 18:32:18 +02001971 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001972 list_for_each_entry(path, &card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001973 if (path->kcontrol != kcontrol)
1974 continue;
1975
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001976 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001977 continue;
1978
1979 found = 1;
1980 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001981 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001982 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001983 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001984 } else {
1985 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001986 dapm_mark_dirty(path->source,
1987 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001988 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001989 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001990 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001991 }
1992
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001993 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001994 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001995
Liam Girdwood618dae12012-04-25 12:12:51 +01001996 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001997}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001998
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001999int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002000 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2001 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002002{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002003 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002004 int ret;
2005
Liam Girdwood3cd04342012-03-09 12:02:08 +00002006 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002007 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002008 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002009 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002010 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002011 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002012 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002013 return ret;
2014}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002015EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002016
Milan plzik1b075e32008-01-10 14:39:46 +01002017/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002018static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002019 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002020{
2021 struct snd_soc_dapm_path *path;
2022 int found = 0;
2023
Richard Purdie2b97eab2006-10-06 18:32:18 +02002024 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002025 list_for_each_entry(path, &card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002026 if (path->kcontrol != kcontrol)
2027 continue;
2028
2029 /* found, now check type */
2030 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002031 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002032 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002033 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002034 }
2035
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002036 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002037 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002038
Liam Girdwood618dae12012-04-25 12:12:51 +01002039 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002040}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002041
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002042int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002043 struct snd_kcontrol *kcontrol, int connect,
2044 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002045{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002046 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002047 int ret;
2048
Liam Girdwood3cd04342012-03-09 12:02:08 +00002049 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002050 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002051 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002052 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002053 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002054 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002055 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002056 return ret;
2057}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002058EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002059
2060/* show dapm widget status in sys fs */
2061static ssize_t dapm_widget_show(struct device *dev,
2062 struct device_attribute *attr, char *buf)
2063{
Mark Brown36ae1a92012-01-06 17:12:45 -08002064 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002065 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002066 struct snd_soc_dapm_widget *w;
2067 int count = 0;
2068 char *state = "not set";
2069
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002070 list_for_each_entry(w, &codec->card->widgets, list) {
2071 if (w->dapm != &codec->dapm)
2072 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002073
2074 /* only display widgets that burnm power */
2075 switch (w->id) {
2076 case snd_soc_dapm_hp:
2077 case snd_soc_dapm_mic:
2078 case snd_soc_dapm_spk:
2079 case snd_soc_dapm_line:
2080 case snd_soc_dapm_micbias:
2081 case snd_soc_dapm_dac:
2082 case snd_soc_dapm_adc:
2083 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002084 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002085 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002086 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002087 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002088 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002089 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002090 if (w->name)
2091 count += sprintf(buf + count, "%s: %s\n",
2092 w->name, w->power ? "On":"Off");
2093 break;
2094 default:
2095 break;
2096 }
2097 }
2098
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002099 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002100 case SND_SOC_BIAS_ON:
2101 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002102 break;
Mark Brown0be98982008-05-19 12:31:28 +02002103 case SND_SOC_BIAS_PREPARE:
2104 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002105 break;
Mark Brown0be98982008-05-19 12:31:28 +02002106 case SND_SOC_BIAS_STANDBY:
2107 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002108 break;
Mark Brown0be98982008-05-19 12:31:28 +02002109 case SND_SOC_BIAS_OFF:
2110 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002111 break;
2112 }
2113 count += sprintf(buf + count, "PM State: %s\n", state);
2114
2115 return count;
2116}
2117
2118static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2119
2120int snd_soc_dapm_sys_add(struct device *dev)
2121{
Troy Kisky12ef1932008-10-13 17:42:14 -07002122 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002123}
2124
2125static void snd_soc_dapm_sys_remove(struct device *dev)
2126{
Mark Brownaef90842009-05-16 17:53:16 +01002127 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002128}
2129
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002130static void dapm_free_path(struct snd_soc_dapm_path *path)
2131{
2132 list_del(&path->list_sink);
2133 list_del(&path->list_source);
2134 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002135 kfree(path);
2136}
2137
Richard Purdie2b97eab2006-10-06 18:32:18 +02002138/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002139static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002140{
2141 struct snd_soc_dapm_widget *w, *next_w;
2142 struct snd_soc_dapm_path *p, *next_p;
2143
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002144 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2145 if (w->dapm != dapm)
2146 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002147 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002148 /*
2149 * remove source and sink paths associated to this widget.
2150 * While removing the path, remove reference to it from both
2151 * source and sink widgets so that path is removed only once.
2152 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002153 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2154 dapm_free_path(p);
2155
2156 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2157 dapm_free_path(p);
2158
Stephen Warrenfad59882011-04-28 17:37:59 -06002159 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002160 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002161 kfree(w);
2162 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002163}
2164
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002165static struct snd_soc_dapm_widget *dapm_find_widget(
2166 struct snd_soc_dapm_context *dapm, const char *pin,
2167 bool search_other_contexts)
2168{
2169 struct snd_soc_dapm_widget *w;
2170 struct snd_soc_dapm_widget *fallback = NULL;
2171
2172 list_for_each_entry(w, &dapm->card->widgets, list) {
2173 if (!strcmp(w->name, pin)) {
2174 if (w->dapm == dapm)
2175 return w;
2176 else
2177 fallback = w;
2178 }
2179 }
2180
2181 if (search_other_contexts)
2182 return fallback;
2183
2184 return NULL;
2185}
2186
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002187static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002188 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002189{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002190 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002191
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002192 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002193 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002194 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002195 }
2196
Mark Brown1a8b2d92012-02-16 11:50:07 -08002197 if (w->connected != status)
2198 dapm_mark_dirty(w, "pin configuration");
2199
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002200 w->connected = status;
2201 if (status == 0)
2202 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002203
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002204 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002205}
2206
Richard Purdie2b97eab2006-10-06 18:32:18 +02002207/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002208 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002209 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002210 *
2211 * Walks all dapm audio paths and powers widgets according to their
2212 * stream or path usage.
2213 *
2214 * Returns 0 for success.
2215 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002216int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002217{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002218 int ret;
2219
Mark Brown4f4c0072011-10-07 14:29:19 +01002220 /*
2221 * Suppress early reports (eg, jacks syncing their state) to avoid
2222 * silly DAPM runs during card startup.
2223 */
2224 if (!dapm->card || !dapm->card->instantiated)
2225 return 0;
2226
Liam Girdwood3cd04342012-03-09 12:02:08 +00002227 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002228 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002229 mutex_unlock(&dapm->card->dapm_mutex);
2230 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002231}
Liam Girdwooda5302182008-07-07 13:35:17 +01002232EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002233
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002234static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002235 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002236{
2237 struct snd_soc_dapm_path *path;
2238 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002239 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002240 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002241 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002242 const char *source;
2243 char prefixed_sink[80];
2244 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002245 int ret = 0;
2246
Mark Brown88e8b9a2011-03-02 18:18:24 +00002247 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002248 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2249 dapm->codec->name_prefix, route->sink);
2250 sink = prefixed_sink;
2251 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2252 dapm->codec->name_prefix, route->source);
2253 source = prefixed_source;
2254 } else {
2255 sink = route->sink;
2256 source = route->source;
2257 }
2258
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002259 /*
2260 * find src and dest widgets over all widgets but favor a widget from
2261 * current DAPM context
2262 */
2263 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002264 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002265 wtsink = w;
2266 if (w->dapm == dapm)
2267 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002268 continue;
2269 }
2270 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002271 wtsource = w;
2272 if (w->dapm == dapm)
2273 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002274 }
2275 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002276 /* use widget from another DAPM context if not found from this */
2277 if (!wsink)
2278 wsink = wtsink;
2279 if (!wsource)
2280 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002281
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002282 if (wsource == NULL) {
2283 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2284 route->source);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002285 return -ENODEV;
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002286 }
2287 if (wsink == NULL) {
2288 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2289 route->sink);
2290 return -ENODEV;
2291 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002292
2293 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2294 if (!path)
2295 return -ENOMEM;
2296
2297 path->source = wsource;
2298 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002299 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002300 INIT_LIST_HEAD(&path->list);
2301 INIT_LIST_HEAD(&path->list_source);
2302 INIT_LIST_HEAD(&path->list_sink);
2303
2304 /* check for external widgets */
2305 if (wsink->id == snd_soc_dapm_input) {
2306 if (wsource->id == snd_soc_dapm_micbias ||
2307 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002308 wsource->id == snd_soc_dapm_line ||
2309 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002310 wsink->ext = 1;
2311 }
2312 if (wsource->id == snd_soc_dapm_output) {
2313 if (wsink->id == snd_soc_dapm_spk ||
2314 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002315 wsink->id == snd_soc_dapm_line ||
2316 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002317 wsource->ext = 1;
2318 }
2319
2320 /* connect static paths */
2321 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002322 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002323 list_add(&path->list_sink, &wsink->sources);
2324 list_add(&path->list_source, &wsource->sinks);
2325 path->connect = 1;
2326 return 0;
2327 }
2328
2329 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002330 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002331 case snd_soc_dapm_adc:
2332 case snd_soc_dapm_dac:
2333 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002334 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002335 case snd_soc_dapm_input:
2336 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002337 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002338 case snd_soc_dapm_micbias:
2339 case snd_soc_dapm_vmid:
2340 case snd_soc_dapm_pre:
2341 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002342 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002343 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002344 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002345 case snd_soc_dapm_aif_in:
2346 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002347 case snd_soc_dapm_dai_in:
2348 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002349 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002350 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002351 list_add(&path->list_sink, &wsink->sources);
2352 list_add(&path->list_source, &wsource->sinks);
2353 path->connect = 1;
2354 return 0;
2355 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002356 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002357 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002358 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002359 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002360 if (ret != 0)
2361 goto err;
2362 break;
2363 case snd_soc_dapm_switch:
2364 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002365 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002366 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002367 if (ret != 0)
2368 goto err;
2369 break;
2370 case snd_soc_dapm_hp:
2371 case snd_soc_dapm_mic:
2372 case snd_soc_dapm_line:
2373 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002374 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002375 list_add(&path->list_sink, &wsink->sources);
2376 list_add(&path->list_source, &wsource->sinks);
2377 path->connect = 0;
2378 return 0;
2379 }
Mark Brownfabd0382012-07-05 17:20:06 +01002380
2381 dapm_mark_dirty(wsource, "Route added");
2382 dapm_mark_dirty(wsink, "Route added");
2383
Richard Purdie2b97eab2006-10-06 18:32:18 +02002384 return 0;
2385
2386err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002387 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002388 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002389 kfree(path);
2390 return ret;
2391}
Mark Brown105f1c22008-05-13 14:52:19 +02002392
Mark Brownefcc3c62012-07-05 17:24:19 +01002393static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2394 const struct snd_soc_dapm_route *route)
2395{
2396 struct snd_soc_dapm_path *path, *p;
2397 const char *sink;
2398 const char *source;
2399 char prefixed_sink[80];
2400 char prefixed_source[80];
2401
2402 if (route->control) {
2403 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002404 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002405 return -EINVAL;
2406 }
2407
2408 if (dapm->codec && dapm->codec->name_prefix) {
2409 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2410 dapm->codec->name_prefix, route->sink);
2411 sink = prefixed_sink;
2412 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2413 dapm->codec->name_prefix, route->source);
2414 source = prefixed_source;
2415 } else {
2416 sink = route->sink;
2417 source = route->source;
2418 }
2419
2420 path = NULL;
2421 list_for_each_entry(p, &dapm->card->paths, list) {
2422 if (strcmp(p->source->name, source) != 0)
2423 continue;
2424 if (strcmp(p->sink->name, sink) != 0)
2425 continue;
2426 path = p;
2427 break;
2428 }
2429
2430 if (path) {
2431 dapm_mark_dirty(path->source, "Route removed");
2432 dapm_mark_dirty(path->sink, "Route removed");
2433
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002434 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002435 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002436 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002437 source, sink);
2438 }
2439
2440 return 0;
2441}
2442
Mark Brown105f1c22008-05-13 14:52:19 +02002443/**
Mark Brown105f1c22008-05-13 14:52:19 +02002444 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002445 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002446 * @route: audio routes
2447 * @num: number of routes
2448 *
2449 * Connects 2 dapm widgets together via a named audio path. The sink is
2450 * the widget receiving the audio signal, whilst the source is the sender
2451 * of the audio signal.
2452 *
2453 * Returns 0 for success else error. On error all resources can be freed
2454 * with a call to snd_soc_card_free().
2455 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002456int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002457 const struct snd_soc_dapm_route *route, int num)
2458{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002459 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002460
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002461 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002462 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002463 r = snd_soc_dapm_add_route(dapm, route);
2464 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002465 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2466 route->source,
2467 route->control ? route->control : "direct",
2468 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002469 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002470 }
2471 route++;
2472 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002473 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002474
Dan Carpenter60884c22012-04-13 22:25:43 +03002475 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002476}
2477EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2478
Mark Brownefcc3c62012-07-05 17:24:19 +01002479/**
2480 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2481 * @dapm: DAPM context
2482 * @route: audio routes
2483 * @num: number of routes
2484 *
2485 * Removes routes from the DAPM context.
2486 */
2487int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2488 const struct snd_soc_dapm_route *route, int num)
2489{
2490 int i, ret = 0;
2491
2492 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2493 for (i = 0; i < num; i++) {
2494 snd_soc_dapm_del_route(dapm, route);
2495 route++;
2496 }
2497 mutex_unlock(&dapm->card->dapm_mutex);
2498
2499 return ret;
2500}
2501EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2502
Mark Brownbf3a9e12011-06-13 16:42:29 +01002503static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2504 const struct snd_soc_dapm_route *route)
2505{
2506 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2507 route->source,
2508 true);
2509 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2510 route->sink,
2511 true);
2512 struct snd_soc_dapm_path *path;
2513 int count = 0;
2514
2515 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002516 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002517 route->source);
2518 return -ENODEV;
2519 }
2520
2521 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002522 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002523 route->sink);
2524 return -ENODEV;
2525 }
2526
2527 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002528 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002529 route->source, route->sink);
2530
2531 list_for_each_entry(path, &source->sinks, list_source) {
2532 if (path->sink == sink) {
2533 path->weak = 1;
2534 count++;
2535 }
2536 }
2537
2538 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002539 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002540 route->source, route->sink);
2541 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002542 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002543 count, route->source, route->sink);
2544
2545 return 0;
2546}
2547
2548/**
2549 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2550 * @dapm: DAPM context
2551 * @route: audio routes
2552 * @num: number of routes
2553 *
2554 * Mark existing routes matching those specified in the passed array
2555 * as being weak, meaning that they are ignored for the purpose of
2556 * power decisions. The main intended use case is for sidetone paths
2557 * which couple audio between other independent paths if they are both
2558 * active in order to make the combination work better at the user
2559 * level but which aren't intended to be "used".
2560 *
2561 * Note that CODEC drivers should not use this as sidetone type paths
2562 * can frequently also be used as bypass paths.
2563 */
2564int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2565 const struct snd_soc_dapm_route *route, int num)
2566{
2567 int i, err;
2568 int ret = 0;
2569
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002570 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002571 for (i = 0; i < num; i++) {
2572 err = snd_soc_dapm_weak_route(dapm, route);
2573 if (err)
2574 ret = err;
2575 route++;
2576 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002577 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002578
2579 return ret;
2580}
2581EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2582
Mark Brown105f1c22008-05-13 14:52:19 +02002583/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002584 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002585 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002586 *
2587 * Checks the codec for any new dapm widgets and creates them if found.
2588 *
2589 * Returns 0 for success.
2590 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002591int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002592{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002593 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002594 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002595 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002596
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002597 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002598
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002599 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002600 {
2601 if (w->new)
2602 continue;
2603
Stephen Warrenfad59882011-04-28 17:37:59 -06002604 if (w->num_kcontrols) {
2605 w->kcontrols = kzalloc(w->num_kcontrols *
2606 sizeof(struct snd_kcontrol *),
2607 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002608 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002609 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002610 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002611 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002612 }
2613
Richard Purdie2b97eab2006-10-06 18:32:18 +02002614 switch(w->id) {
2615 case snd_soc_dapm_switch:
2616 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002617 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002618 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002619 break;
2620 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002621 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002622 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002623 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002624 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002625 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002626 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002627 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002628 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002629 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002630 break;
2631 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002632
2633 /* Read the initial power state from the device */
2634 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002635 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002636 val &= 1 << w->shift;
2637 if (w->invert)
2638 val = !val;
2639
2640 if (val)
2641 w->power = 1;
2642 }
2643
Richard Purdie2b97eab2006-10-06 18:32:18 +02002644 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002645
Mark Brown7508b122011-10-05 12:09:12 +01002646 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002647 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002648 }
2649
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002650 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2651 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002652 return 0;
2653}
2654EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2655
2656/**
2657 * snd_soc_dapm_get_volsw - dapm mixer get callback
2658 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002659 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002660 *
2661 * Callback to get the value of a dapm mixer control.
2662 *
2663 * Returns 0 for success.
2664 */
2665int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2666 struct snd_ctl_elem_value *ucontrol)
2667{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002668 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002669 struct soc_mixer_control *mc =
2670 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002671 unsigned int reg = mc->reg;
2672 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002673 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002674 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002675 unsigned int invert = mc->invert;
2676
2677 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002678 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002679 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002680 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002681
Richard Purdie2b97eab2006-10-06 18:32:18 +02002682 ucontrol->value.integer.value[0] =
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002683 (snd_soc_read(codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002684 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002685 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002686 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002687
2688 return 0;
2689}
2690EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2691
2692/**
2693 * snd_soc_dapm_put_volsw - dapm mixer set callback
2694 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002695 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002696 *
2697 * Callback to set the value of a dapm mixer control.
2698 *
2699 * Returns 0 for success.
2700 */
2701int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2702 struct snd_ctl_elem_value *ucontrol)
2703{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002704 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002705 struct snd_soc_card *card = codec->card;
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;
2712 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002713 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002714 int connect, change;
2715 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002716
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002717 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002718 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002719 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002720 kcontrol->id.name);
2721
Richard Purdie2b97eab2006-10-06 18:32:18 +02002722 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002723 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002724
2725 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002726 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002727 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002728 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002729
Liam Girdwood3cd04342012-03-09 12:02:08 +00002730 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002731
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002732 change = snd_soc_test_bits(codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002733 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002734 update.kcontrol = kcontrol;
2735 update.reg = reg;
2736 update.mask = mask;
2737 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002738
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002739 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002740
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002741 soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002742
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002743 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002744 }
2745
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002746 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002747 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002748}
2749EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2750
2751/**
2752 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2753 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002754 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002755 *
2756 * Callback to get the value of a dapm enumerated double mixer control.
2757 *
2758 * Returns 0 for success.
2759 */
2760int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2761 struct snd_ctl_elem_value *ucontrol)
2762{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002763 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002764 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002765 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002766
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002767 val = snd_soc_read(codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002768 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002769 if (e->shift_l != e->shift_r)
2770 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002771 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002772
2773 return 0;
2774}
2775EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2776
2777/**
2778 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2779 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002780 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002781 *
2782 * Callback to set the value of a dapm enumerated double mixer control.
2783 *
2784 * Returns 0 for success.
2785 */
2786int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2787 struct snd_ctl_elem_value *ucontrol)
2788{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02002789 struct snd_soc_dapm_widget_list *wlist = dapm_kcontrol_get_wlist(kcontrol);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002790 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2791 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002792 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002793 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002794 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002795 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002796 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002797
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002798 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002799 return -EINVAL;
2800 mux = ucontrol->value.enumerated.item[0];
2801 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002802 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002803 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002804 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002805 return -EINVAL;
2806 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002807 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002808 }
2809
Liam Girdwood3cd04342012-03-09 12:02:08 +00002810 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002811
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002812 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002813 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002814 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002815
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002816 update.kcontrol = kcontrol;
2817 update.reg = e->reg;
2818 update.mask = mask;
2819 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002820 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002821
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002822 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002823
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002824 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002825 }
2826
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002827 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002828 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002829}
2830EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2831
2832/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002833 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2834 * @kcontrol: mixer control
2835 * @ucontrol: control element information
2836 *
2837 * Returns 0 for success.
2838 */
2839int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2840 struct snd_ctl_elem_value *ucontrol)
2841{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02002842 struct snd_soc_dapm_widget_list *wlist = dapm_kcontrol_get_wlist(kcontrol);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002843 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002844
2845 ucontrol->value.enumerated.item[0] = widget->value;
2846
2847 return 0;
2848}
2849EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2850
2851/**
2852 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2853 * @kcontrol: mixer control
2854 * @ucontrol: control element information
2855 *
2856 * Returns 0 for success.
2857 */
2858int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2859 struct snd_ctl_elem_value *ucontrol)
2860{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02002861 struct snd_soc_dapm_widget_list *wlist = dapm_kcontrol_get_wlist(kcontrol);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002862 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2863 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002864 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002865 struct soc_enum *e =
2866 (struct soc_enum *)kcontrol->private_value;
2867 int change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002868
2869 if (ucontrol->value.enumerated.item[0] >= e->max)
2870 return -EINVAL;
2871
Liam Girdwood3cd04342012-03-09 12:02:08 +00002872 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002873
2874 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002875 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002876 widget->value = ucontrol->value.enumerated.item[0];
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002877 soc_dapm_mux_update_power(card, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002878 }
2879
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002880 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002881 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002882}
2883EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2884
2885/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002886 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2887 * callback
2888 * @kcontrol: mixer control
2889 * @ucontrol: control element information
2890 *
2891 * Callback to get the value of a dapm semi enumerated double mixer control.
2892 *
2893 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2894 * used for handling bitfield coded enumeration for example.
2895 *
2896 * Returns 0 for success.
2897 */
2898int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2899 struct snd_ctl_elem_value *ucontrol)
2900{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002901 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002902 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002903 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002904
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002905 reg_val = snd_soc_read(codec, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002906 val = (reg_val >> e->shift_l) & e->mask;
2907 for (mux = 0; mux < e->max; mux++) {
2908 if (val == e->values[mux])
2909 break;
2910 }
2911 ucontrol->value.enumerated.item[0] = mux;
2912 if (e->shift_l != e->shift_r) {
2913 val = (reg_val >> e->shift_r) & e->mask;
2914 for (mux = 0; mux < e->max; mux++) {
2915 if (val == e->values[mux])
2916 break;
2917 }
2918 ucontrol->value.enumerated.item[1] = mux;
2919 }
2920
2921 return 0;
2922}
2923EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2924
2925/**
2926 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2927 * callback
2928 * @kcontrol: mixer control
2929 * @ucontrol: control element information
2930 *
2931 * Callback to set the value of a dapm semi enumerated double mixer control.
2932 *
2933 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2934 * used for handling bitfield coded enumeration for example.
2935 *
2936 * Returns 0 for success.
2937 */
2938int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2939 struct snd_ctl_elem_value *ucontrol)
2940{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02002941 struct snd_soc_dapm_widget_list *wlist = dapm_kcontrol_get_wlist(kcontrol);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002942 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2943 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002944 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002945 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002946 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002947 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002948 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002949
2950 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2951 return -EINVAL;
2952 mux = ucontrol->value.enumerated.item[0];
2953 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2954 mask = e->mask << e->shift_l;
2955 if (e->shift_l != e->shift_r) {
2956 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2957 return -EINVAL;
2958 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2959 mask |= e->mask << e->shift_r;
2960 }
2961
Liam Girdwood3cd04342012-03-09 12:02:08 +00002962 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002963
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002964 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002965 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002966 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002967
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002968 update.kcontrol = kcontrol;
2969 update.reg = e->reg;
2970 update.mask = mask;
2971 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002972 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002973
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002974 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002975
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002976 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002977 }
2978
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002979 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002980 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002981}
2982EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2983
2984/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002985 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2986 *
2987 * @kcontrol: mixer control
2988 * @uinfo: control element information
2989 *
2990 * Callback to provide information about a pin switch control.
2991 */
2992int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2993 struct snd_ctl_elem_info *uinfo)
2994{
2995 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2996 uinfo->count = 1;
2997 uinfo->value.integer.min = 0;
2998 uinfo->value.integer.max = 1;
2999
3000 return 0;
3001}
3002EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3003
3004/**
3005 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3006 *
3007 * @kcontrol: mixer control
3008 * @ucontrol: Value
3009 */
3010int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3011 struct snd_ctl_elem_value *ucontrol)
3012{
Mark Brown48a8c392012-02-14 17:11:15 -08003013 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003014 const char *pin = (const char *)kcontrol->private_value;
3015
Liam Girdwood3cd04342012-03-09 12:02:08 +00003016 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003017
3018 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003019 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003020
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003021 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003022
3023 return 0;
3024}
3025EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3026
3027/**
3028 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3029 *
3030 * @kcontrol: mixer control
3031 * @ucontrol: Value
3032 */
3033int snd_soc_dapm_put_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 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003042 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003043 else
Mark Brown48a8c392012-02-14 17:11:15 -08003044 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003045
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003046 mutex_unlock(&card->dapm_mutex);
3047
Mark Brown48a8c392012-02-14 17:11:15 -08003048 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003049 return 0;
3050}
3051EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3052
Mark Brown5ba06fc2012-02-16 11:07:13 -08003053static struct snd_soc_dapm_widget *
3054snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3055 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003056{
3057 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003058 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003059
3060 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003061 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003062
Mark Brown62ea8742012-01-21 21:14:48 +00003063 switch (w->id) {
3064 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003065 w->regulator = devm_regulator_get(dapm->dev, w->name);
3066 if (IS_ERR(w->regulator)) {
3067 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003068 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003069 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003070 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003071 }
Mark Brown8784c772013-01-10 19:33:47 +00003072
3073 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
3074 ret = regulator_allow_bypass(w->regulator, true);
3075 if (ret != 0)
3076 dev_warn(w->dapm->dev,
3077 "ASoC: Failed to unbypass %s: %d\n",
3078 w->name, ret);
3079 }
Mark Brown62ea8742012-01-21 21:14:48 +00003080 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003081 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003082#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003083 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003084 if (IS_ERR(w->clk)) {
3085 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003086 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003087 w->name, ret);
3088 return NULL;
3089 }
Mark Brownec029952012-06-04 08:16:20 +01003090#else
3091 return NULL;
3092#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003093 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003094 default:
3095 break;
3096 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003097
Mark Brown88e8b9a2011-03-02 18:18:24 +00003098 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003099 w->name = kasprintf(GFP_KERNEL, "%s %s",
3100 dapm->codec->name_prefix, widget->name);
3101 else
3102 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3103
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003104 if (w->name == NULL) {
3105 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003106 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003107 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003108
Mark Brown7ca3a182011-10-08 14:04:50 +01003109 switch (w->id) {
3110 case snd_soc_dapm_switch:
3111 case snd_soc_dapm_mixer:
3112 case snd_soc_dapm_mixer_named_ctl:
3113 w->power_check = dapm_generic_check_power;
3114 break;
3115 case snd_soc_dapm_mux:
3116 case snd_soc_dapm_virt_mux:
3117 case snd_soc_dapm_value_mux:
3118 w->power_check = dapm_generic_check_power;
3119 break;
Mark Brown46162742013-06-05 19:36:11 +01003120 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003121 w->power_check = dapm_adc_check_power;
3122 break;
Mark Brown46162742013-06-05 19:36:11 +01003123 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003124 w->power_check = dapm_dac_check_power;
3125 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003126 case snd_soc_dapm_adc:
3127 case snd_soc_dapm_aif_out:
3128 case snd_soc_dapm_dac:
3129 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003130 case snd_soc_dapm_pga:
3131 case snd_soc_dapm_out_drv:
3132 case snd_soc_dapm_input:
3133 case snd_soc_dapm_output:
3134 case snd_soc_dapm_micbias:
3135 case snd_soc_dapm_spk:
3136 case snd_soc_dapm_hp:
3137 case snd_soc_dapm_mic:
3138 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003139 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003140 w->power_check = dapm_generic_check_power;
3141 break;
3142 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003143 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003144 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003145 w->power_check = dapm_supply_check_power;
3146 break;
3147 default:
3148 w->power_check = dapm_always_on_check_power;
3149 break;
3150 }
3151
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003152 w->dapm = dapm;
3153 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003154 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003155 INIT_LIST_HEAD(&w->sources);
3156 INIT_LIST_HEAD(&w->sinks);
3157 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003158 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003159 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003160
3161 /* machine layer set ups unconnected pins and insertions */
3162 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003163 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003164}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003165
3166/**
Mark Brown4ba13272008-05-13 14:51:19 +02003167 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003168 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003169 * @widget: widget array
3170 * @num: number of widgets
3171 *
3172 * Creates new DAPM controls based upon the templates.
3173 *
3174 * Returns 0 for success else error.
3175 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003176int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003177 const struct snd_soc_dapm_widget *widget,
3178 int num)
3179{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003180 struct snd_soc_dapm_widget *w;
3181 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003182 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003183
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003184 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003185 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003186 w = snd_soc_dapm_new_control(dapm, widget);
3187 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003188 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003189 "ASoC: Failed to create DAPM control %s\n",
3190 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003191 ret = -ENOMEM;
3192 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003193 }
Mark Brown4ba13272008-05-13 14:51:19 +02003194 widget++;
3195 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003196 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003197 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003198}
3199EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3200
Mark Brownc74184e2012-04-04 22:12:09 +01003201static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3202 struct snd_kcontrol *kcontrol, int event)
3203{
3204 struct snd_soc_dapm_path *source_p, *sink_p;
3205 struct snd_soc_dai *source, *sink;
3206 const struct snd_soc_pcm_stream *config = w->params;
3207 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003208 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003209 u64 fmt;
3210 int ret;
3211
3212 BUG_ON(!config);
3213 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3214
3215 /* We only support a single source and sink, pick the first */
3216 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3217 list_sink);
3218 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3219 list_source);
3220
3221 BUG_ON(!source_p || !sink_p);
3222 BUG_ON(!sink_p->source || !source_p->sink);
3223 BUG_ON(!source_p->source || !sink_p->sink);
3224
3225 source = source_p->source->priv;
3226 sink = sink_p->sink->priv;
3227
3228 /* Be a little careful as we don't want to overflow the mask array */
3229 if (config->formats) {
3230 fmt = ffs(config->formats) - 1;
3231 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003232 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003233 config->formats);
3234 fmt = 0;
3235 }
3236
3237 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003238 params = kzalloc(sizeof(*params), GFP_KERNEL);
3239 if (!params) {
3240 ret = -ENOMEM;
3241 goto out;
3242 }
3243 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003244
Mark Brown9747cec2012-04-26 19:12:21 +01003245 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003246 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003247 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003248 config->rate_max;
3249
Mark Brown9747cec2012-04-26 19:12:21 +01003250 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003251 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003252 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003253 = config->channels_max;
3254
3255 memset(&substream, 0, sizeof(substream));
3256
3257 switch (event) {
3258 case SND_SOC_DAPM_PRE_PMU:
3259 if (source->driver->ops && source->driver->ops->hw_params) {
3260 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3261 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003262 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003263 if (ret != 0) {
3264 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003265 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003266 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003267 }
3268 }
3269
3270 if (sink->driver->ops && sink->driver->ops->hw_params) {
3271 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003272 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003273 sink);
3274 if (ret != 0) {
3275 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003276 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003277 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003278 }
3279 }
3280 break;
3281
3282 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003283 ret = snd_soc_dai_digital_mute(sink, 0,
3284 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003285 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003286 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003287 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003288 break;
3289
3290 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003291 ret = snd_soc_dai_digital_mute(sink, 1,
3292 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003293 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003294 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003295 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003296 break;
3297
3298 default:
3299 BUG();
3300 return -EINVAL;
3301 }
3302
Mark Brown9747cec2012-04-26 19:12:21 +01003303out:
3304 kfree(params);
3305 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003306}
3307
3308int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3309 const struct snd_soc_pcm_stream *params,
3310 struct snd_soc_dapm_widget *source,
3311 struct snd_soc_dapm_widget *sink)
3312{
3313 struct snd_soc_dapm_route routes[2];
3314 struct snd_soc_dapm_widget template;
3315 struct snd_soc_dapm_widget *w;
3316 size_t len;
3317 char *link_name;
3318
3319 len = strlen(source->name) + strlen(sink->name) + 2;
3320 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3321 if (!link_name)
3322 return -ENOMEM;
3323 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3324
3325 memset(&template, 0, sizeof(template));
3326 template.reg = SND_SOC_NOPM;
3327 template.id = snd_soc_dapm_dai_link;
3328 template.name = link_name;
3329 template.event = snd_soc_dai_link_event;
3330 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3331 SND_SOC_DAPM_PRE_PMD;
3332
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003333 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003334
3335 w = snd_soc_dapm_new_control(&card->dapm, &template);
3336 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003337 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003338 link_name);
3339 return -ENOMEM;
3340 }
3341
3342 w->params = params;
3343
3344 memset(&routes, 0, sizeof(routes));
3345
3346 routes[0].source = source->name;
3347 routes[0].sink = link_name;
3348 routes[1].source = link_name;
3349 routes[1].sink = sink->name;
3350
3351 return snd_soc_dapm_add_routes(&card->dapm, routes,
3352 ARRAY_SIZE(routes));
3353}
3354
Mark Brown888df392012-02-16 19:37:51 -08003355int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3356 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003357{
Mark Brown888df392012-02-16 19:37:51 -08003358 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003359 struct snd_soc_dapm_widget *w;
3360
Mark Brown888df392012-02-16 19:37:51 -08003361 WARN_ON(dapm->dev != dai->dev);
3362
3363 memset(&template, 0, sizeof(template));
3364 template.reg = SND_SOC_NOPM;
3365
3366 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003367 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003368 template.name = dai->driver->playback.stream_name;
3369 template.sname = dai->driver->playback.stream_name;
3370
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003371 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003372 template.name);
3373
3374 w = snd_soc_dapm_new_control(dapm, &template);
3375 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003376 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003377 dai->driver->playback.stream_name);
3378 }
3379
3380 w->priv = dai;
3381 dai->playback_widget = w;
3382 }
3383
3384 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003385 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003386 template.name = dai->driver->capture.stream_name;
3387 template.sname = dai->driver->capture.stream_name;
3388
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003389 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003390 template.name);
3391
3392 w = snd_soc_dapm_new_control(dapm, &template);
3393 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003394 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003395 dai->driver->capture.stream_name);
3396 }
3397
3398 w->priv = dai;
3399 dai->capture_widget = w;
3400 }
3401
3402 return 0;
3403}
3404
3405int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3406{
3407 struct snd_soc_dapm_widget *dai_w, *w;
3408 struct snd_soc_dai *dai;
3409 struct snd_soc_dapm_route r;
3410
3411 memset(&r, 0, sizeof(r));
3412
3413 /* For each DAI widget... */
3414 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003415 switch (dai_w->id) {
3416 case snd_soc_dapm_dai_in:
3417 case snd_soc_dapm_dai_out:
3418 break;
3419 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003420 continue;
Mark Brown46162742013-06-05 19:36:11 +01003421 }
Mark Brown888df392012-02-16 19:37:51 -08003422
3423 dai = dai_w->priv;
3424
3425 /* ...find all widgets with the same stream and link them */
3426 list_for_each_entry(w, &card->widgets, list) {
3427 if (w->dapm != dai_w->dapm)
3428 continue;
3429
Mark Brown46162742013-06-05 19:36:11 +01003430 switch (w->id) {
3431 case snd_soc_dapm_dai_in:
3432 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003433 continue;
Mark Brown46162742013-06-05 19:36:11 +01003434 default:
3435 break;
3436 }
Mark Brown888df392012-02-16 19:37:51 -08003437
3438 if (!w->sname)
3439 continue;
3440
3441 if (dai->driver->playback.stream_name &&
3442 strstr(w->sname,
3443 dai->driver->playback.stream_name)) {
3444 r.source = dai->playback_widget->name;
3445 r.sink = w->name;
3446 dev_dbg(dai->dev, "%s -> %s\n",
3447 r.source, r.sink);
3448
3449 snd_soc_dapm_add_route(w->dapm, &r);
3450 }
3451
3452 if (dai->driver->capture.stream_name &&
3453 strstr(w->sname,
3454 dai->driver->capture.stream_name)) {
3455 r.source = w->name;
3456 r.sink = dai->capture_widget->name;
3457 dev_dbg(dai->dev, "%s -> %s\n",
3458 r.source, r.sink);
3459
3460 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003461 }
3462 }
3463 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003464
Mark Brown888df392012-02-16 19:37:51 -08003465 return 0;
3466}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003467
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003468static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3469 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003470{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003471
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003472 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3473 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3474 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003475
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003476 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3477 w_cpu = cpu_dai->playback_widget;
3478 w_codec = codec_dai->playback_widget;
3479 } else {
3480 w_cpu = cpu_dai->capture_widget;
3481 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003482 }
3483
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003484 if (w_cpu) {
3485
3486 dapm_mark_dirty(w_cpu, "stream event");
3487
3488 switch (event) {
3489 case SND_SOC_DAPM_STREAM_START:
3490 w_cpu->active = 1;
3491 break;
3492 case SND_SOC_DAPM_STREAM_STOP:
3493 w_cpu->active = 0;
3494 break;
3495 case SND_SOC_DAPM_STREAM_SUSPEND:
3496 case SND_SOC_DAPM_STREAM_RESUME:
3497 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3498 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3499 break;
3500 }
3501 }
3502
3503 if (w_codec) {
3504
3505 dapm_mark_dirty(w_codec, "stream event");
3506
3507 switch (event) {
3508 case SND_SOC_DAPM_STREAM_START:
3509 w_codec->active = 1;
3510 break;
3511 case SND_SOC_DAPM_STREAM_STOP:
3512 w_codec->active = 0;
3513 break;
3514 case SND_SOC_DAPM_STREAM_SUSPEND:
3515 case SND_SOC_DAPM_STREAM_RESUME:
3516 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3517 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3518 break;
3519 }
3520 }
3521
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003522 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003523}
3524
3525/**
3526 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3527 * @rtd: PCM runtime data
3528 * @stream: stream name
3529 * @event: stream event
3530 *
3531 * Sends a stream event to the dapm core. The core then makes any
3532 * necessary widget power changes.
3533 *
3534 * Returns 0 for success else error.
3535 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003536void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3537 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003538{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003539 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003540
Liam Girdwood3cd04342012-03-09 12:02:08 +00003541 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003542 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003543 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003544}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003545
3546/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003547 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003548 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003549 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003550 *
Mark Brown74b8f952009-06-06 11:26:15 +01003551 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003552 * a valid audio route and active audio stream.
3553 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3554 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003555 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003556int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003557{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003558 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003559}
Liam Girdwooda5302182008-07-07 13:35:17 +01003560EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003561
3562/**
Mark Brownda341832010-03-15 19:23:37 +00003563 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003564 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003565 * @pin: pin name
3566 *
3567 * Enables input/output pin regardless of any other state. This is
3568 * intended for use with microphone bias supplies used in microphone
3569 * jack detection.
3570 *
3571 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3572 * do any widget power switching.
3573 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003574int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3575 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003576{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003577 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003578
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003579 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003580 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003581 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003582 }
3583
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003584 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003585 w->connected = 1;
3586 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003587 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003588
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003589 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003590}
3591EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3592
3593/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003594 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003595 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003596 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003597 *
Mark Brown74b8f952009-06-06 11:26:15 +01003598 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003599 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3600 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003601 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003602int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3603 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003604{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003605 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003606}
3607EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3608
3609/**
Mark Brown5817b522008-09-24 11:23:11 +01003610 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003611 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003612 * @pin: pin name
3613 *
3614 * Marks the specified pin as being not connected, disabling it along
3615 * any parent or child widgets. At present this is identical to
3616 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3617 * additional things such as disabling controls which only affect
3618 * paths through the pin.
3619 *
3620 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3621 * do any widget power switching.
3622 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003623int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003624{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003625 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003626}
3627EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3628
3629/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003630 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003631 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003632 * @pin: audio signal pin endpoint (or start point)
3633 *
3634 * Get audio pin status - connected or disconnected.
3635 *
3636 * Returns 1 for connected otherwise 0.
3637 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003638int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3639 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003640{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003641 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003642
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003643 if (w)
3644 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003645
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003646 return 0;
3647}
Liam Girdwooda5302182008-07-07 13:35:17 +01003648EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003649
3650/**
Mark Brown1547aba2010-05-07 21:11:40 +01003651 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003652 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003653 * @pin: audio signal pin endpoint (or start point)
3654 *
3655 * Mark the given endpoint or pin as ignoring suspend. When the
3656 * system is disabled a path between two endpoints flagged as ignoring
3657 * suspend will not be disabled. The path must already be enabled via
3658 * normal means at suspend time, it will not be turned on if it was not
3659 * already enabled.
3660 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003661int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3662 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003663{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003664 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003665
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003666 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003667 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003668 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003669 }
3670
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003671 w->ignore_suspend = 1;
3672
3673 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003674}
3675EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3676
Stephen Warren16332812011-11-23 12:42:04 -07003677static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3678 struct snd_soc_dapm_widget *w)
3679{
3680 struct snd_soc_dapm_path *p;
3681
3682 list_for_each_entry(p, &card->paths, list) {
3683 if ((p->source == w) || (p->sink == w)) {
3684 dev_dbg(card->dev,
3685 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3686 p->source->name, p->source->id, p->source->dapm,
3687 p->sink->name, p->sink->id, p->sink->dapm);
3688
3689 /* Connected to something other than the codec */
3690 if (p->source->dapm != p->sink->dapm)
3691 return true;
3692 /*
3693 * Loopback connection from codec external pin to
3694 * codec external pin
3695 */
3696 if (p->sink->id == snd_soc_dapm_input) {
3697 switch (p->source->id) {
3698 case snd_soc_dapm_output:
3699 case snd_soc_dapm_micbias:
3700 return true;
3701 default:
3702 break;
3703 }
3704 }
3705 }
3706 }
3707
3708 return false;
3709}
3710
3711/**
3712 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3713 * @codec: The codec whose pins should be processed
3714 *
3715 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3716 * which are unused. Pins are used if they are connected externally to the
3717 * codec, whether that be to some other device, or a loop-back connection to
3718 * the codec itself.
3719 */
3720void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3721{
3722 struct snd_soc_card *card = codec->card;
3723 struct snd_soc_dapm_context *dapm = &codec->dapm;
3724 struct snd_soc_dapm_widget *w;
3725
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003726 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003727 &card->dapm, &codec->dapm);
3728
3729 list_for_each_entry(w, &card->widgets, list) {
3730 if (w->dapm != dapm)
3731 continue;
3732 switch (w->id) {
3733 case snd_soc_dapm_input:
3734 case snd_soc_dapm_output:
3735 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003736 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003737 w->name);
3738 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003739 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003740 "... Not in map; disabling\n");
3741 snd_soc_dapm_nc_pin(dapm, w->name);
3742 }
3743 break;
3744 default:
3745 break;
3746 }
3747 }
3748}
3749
Mark Brown1547aba2010-05-07 21:11:40 +01003750/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003751 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003752 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003753 *
3754 * Free all dapm widgets and resources.
3755 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003756void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003757{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003758 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003759 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003760 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003761 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003762}
3763EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3764
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003765static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003766{
Liam Girdwood01005a72012-07-06 16:57:05 +01003767 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003768 struct snd_soc_dapm_widget *w;
3769 LIST_HEAD(down_list);
3770 int powerdown = 0;
3771
Liam Girdwood01005a72012-07-06 16:57:05 +01003772 mutex_lock(&card->dapm_mutex);
3773
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003774 list_for_each_entry(w, &dapm->card->widgets, list) {
3775 if (w->dapm != dapm)
3776 continue;
Mark Brown51737472009-06-22 13:16:51 +01003777 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003778 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003779 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003780 powerdown = 1;
3781 }
3782 }
3783
3784 /* If there were no widgets to power down we're already in
3785 * standby.
3786 */
3787 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003788 if (dapm->bias_level == SND_SOC_BIAS_ON)
3789 snd_soc_dapm_set_bias_level(dapm,
3790 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003791 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003792 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3793 snd_soc_dapm_set_bias_level(dapm,
3794 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003795 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003796
3797 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003798}
Mark Brown51737472009-06-22 13:16:51 +01003799
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003800/*
3801 * snd_soc_dapm_shutdown - callback for system shutdown
3802 */
3803void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3804{
3805 struct snd_soc_codec *codec;
3806
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003807 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003808 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003809 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3810 snd_soc_dapm_set_bias_level(&codec->dapm,
3811 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003812 }
Mark Brown51737472009-06-22 13:16:51 +01003813}
3814
Richard Purdie2b97eab2006-10-06 18:32:18 +02003815/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003816MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003817MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3818MODULE_LICENSE("GPL");