blob: 5f64c16336adbc9171d3713caf384f10f5eab359 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
Liam Girdwood612a3fe2012-02-06 16:05:29 +000017 * mic/headphone insertion events.
Richard Purdie2b97eab2006-10-06 18:32:18 +020018 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Liam Girdwood612a3fe2012-02-06 16:05:29 +000021 * o Delayed power down of audio subsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020024 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080029#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020030#include <linux/delay.h>
31#include <linux/pm.h>
32#include <linux/bitops.h>
33#include <linux/platform_device.h>
34#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020035#include <linux/debugfs.h>
Mark Brownf1aac482011-12-05 15:17:06 +000036#include <linux/pm_runtime.h>
Mark Brown62ea8742012-01-21 21:14:48 +000037#include <linux/regulator/consumer.h>
Ola Liljad7e7eb92012-05-24 15:26:25 +020038#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020040#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020043#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020044#include <sound/initval.h>
45
Mark Brown84e90932010-11-04 00:07:02 -040046#include <trace/events/asoc.h>
47
Mark Brownde02d072011-09-20 21:43:24 +010048#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49
Richard Purdie2b97eab2006-10-06 18:32:18 +020050/* dapm power sequences - make this per codec in the future */
51static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010052 [snd_soc_dapm_pre] = 0,
53 [snd_soc_dapm_supply] = 1,
Mark Brown62ea8742012-01-21 21:14:48 +000054 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020055 [snd_soc_dapm_clock_supply] = 1,
Mark Brown38357ab2009-06-06 19:03:23 +010056 [snd_soc_dapm_micbias] = 2,
Mark Brownc74184e2012-04-04 22:12:09 +010057 [snd_soc_dapm_dai_link] = 2,
Mark Brown46162742013-06-05 19:36:11 +010058 [snd_soc_dapm_dai_in] = 3,
59 [snd_soc_dapm_dai_out] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010060 [snd_soc_dapm_aif_in] = 3,
61 [snd_soc_dapm_aif_out] = 3,
62 [snd_soc_dapm_mic] = 4,
63 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000064 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010065 [snd_soc_dapm_value_mux] = 5,
66 [snd_soc_dapm_dac] = 6,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020067 [snd_soc_dapm_switch] = 7,
Mark Brown010ff262009-08-17 17:39:22 +010068 [snd_soc_dapm_mixer] = 7,
69 [snd_soc_dapm_mixer_named_ctl] = 7,
70 [snd_soc_dapm_pga] = 8,
71 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060072 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010073 [snd_soc_dapm_hp] = 10,
74 [snd_soc_dapm_spk] = 10,
Mark Brown7e1f7c82012-04-12 17:29:36 +010075 [snd_soc_dapm_line] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010076 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020077};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000078
Richard Purdie2b97eab2006-10-06 18:32:18 +020079static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010080 [snd_soc_dapm_pre] = 0,
81 [snd_soc_dapm_adc] = 1,
82 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010083 [snd_soc_dapm_spk] = 2,
Mark Brown7e1f7c82012-04-12 17:29:36 +010084 [snd_soc_dapm_line] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060085 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010086 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020087 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +010088 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010089 [snd_soc_dapm_mixer] = 5,
90 [snd_soc_dapm_dac] = 6,
91 [snd_soc_dapm_mic] = 7,
92 [snd_soc_dapm_micbias] = 8,
93 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000094 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010095 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010096 [snd_soc_dapm_aif_in] = 10,
97 [snd_soc_dapm_aif_out] = 10,
Mark Brown46162742013-06-05 19:36:11 +010098 [snd_soc_dapm_dai_in] = 10,
99 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100100 [snd_soc_dapm_dai_link] = 11,
Ola Liljad7e7eb92012-05-24 15:26:25 +0200101 [snd_soc_dapm_clock_supply] = 12,
Mark Brownc74184e2012-04-04 22:12:09 +0100102 [snd_soc_dapm_regulator_supply] = 12,
103 [snd_soc_dapm_supply] = 12,
104 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200105};
106
Troy Kisky12ef1932008-10-13 17:42:14 -0700107static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100108{
109 if (pop_time)
110 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
111}
112
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200113static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100114{
115 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200116 char *buf;
117
118 if (!pop_time)
119 return;
120
121 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
122 if (buf == NULL)
123 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100124
125 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200126 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100127 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100128 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200129
130 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100131}
132
Mark Browndb432b42011-10-03 21:06:40 +0100133static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
134{
135 return !list_empty(&w->dirty);
136}
137
Mark Brown25c77c52011-10-08 13:36:03 +0100138void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100139{
Mark Brown75c1f892011-10-04 22:28:08 +0100140 if (!dapm_dirty_widget(w)) {
141 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
142 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100143 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100144 }
Mark Browndb432b42011-10-03 21:06:40 +0100145}
Mark Brown25c77c52011-10-08 13:36:03 +0100146EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100147
Mark Browne2d32ff2012-08-31 17:38:32 -0700148void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
149{
150 struct snd_soc_card *card = dapm->card;
151 struct snd_soc_dapm_widget *w;
152
153 mutex_lock(&card->dapm_mutex);
154
155 list_for_each_entry(w, &card->widgets, list) {
156 switch (w->id) {
157 case snd_soc_dapm_input:
158 case snd_soc_dapm_output:
159 dapm_mark_dirty(w, "Rechecking inputs and outputs");
160 break;
161 default:
162 break;
163 }
164 }
165
166 mutex_unlock(&card->dapm_mutex);
167}
168EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
169
Richard Purdie2b97eab2006-10-06 18:32:18 +0200170/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100171static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200172 const struct snd_soc_dapm_widget *_widget)
173{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100174 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200175}
176
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200177struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200178 unsigned int value;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200179 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200180 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200181};
182
183static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
184 struct snd_kcontrol *kcontrol)
185{
186 struct dapm_kcontrol_data *data;
187
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200188 data = kzalloc(sizeof(*data), GFP_KERNEL);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200189 if (!data) {
190 dev_err(widget->dapm->dev,
191 "ASoC: can't allocate kcontrol data for %s\n",
192 widget->name);
193 return -ENOMEM;
194 }
195
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200196 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200197
198 kcontrol->private_data = data;
199
200 return 0;
201}
202
203static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
204{
205 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200206 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200207 kfree(data);
208}
209
210static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
211 const struct snd_kcontrol *kcontrol)
212{
213 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
214
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200215 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200216}
217
218static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
219 struct snd_soc_dapm_widget *widget)
220{
221 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200222 struct snd_soc_dapm_widget_list *new_wlist;
223 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200224
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200225 if (data->wlist)
226 n = data->wlist->num_widgets + 1;
227 else
228 n = 1;
229
230 new_wlist = krealloc(data->wlist,
231 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
232 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200233 return -ENOMEM;
234
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200235 new_wlist->widgets[n - 1] = widget;
236 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200237
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200238 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200239
240 return 0;
241}
242
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200243static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
244 struct snd_soc_dapm_path *path)
245{
246 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
247
248 list_add_tail(&path->list_kcontrol, &data->paths);
249}
250
251static struct list_head *dapm_kcontrol_get_path_list(
252 const struct snd_kcontrol *kcontrol)
253{
254 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
255
256 return &data->paths;
257}
258
259#define dapm_kcontrol_for_each_path(path, kcontrol) \
260 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
261 list_kcontrol)
262
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200263static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
264{
265 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
266
267 return data->value;
268}
269
270static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
271 unsigned int value)
272{
273 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
274
275 if (data->value == value)
276 return false;
277
278 data->value = value;
279
280 return true;
281}
282
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200283/**
284 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
285 * @kcontrol: The kcontrol
286 */
287struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
288{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200289 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200290}
291EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
292
Liam Girdwood6c120e12012-02-15 15:15:34 +0000293static void dapm_reset(struct snd_soc_card *card)
294{
295 struct snd_soc_dapm_widget *w;
296
297 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
298
299 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200300 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000301 w->power_checked = false;
302 w->inputs = -1;
303 w->outputs = -1;
304 }
305}
306
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100307static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
308{
309 if (w->codec)
310 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100311 else if (w->platform)
312 return snd_soc_platform_read(w->platform, reg);
313
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000314 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100315 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100316}
317
318static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
319{
320 if (w->codec)
321 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100322 else if (w->platform)
323 return snd_soc_platform_write(w->platform, reg, val);
324
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000325 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100326 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100327}
328
Liam Girdwood49575fb52012-03-06 18:16:19 +0000329static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
330{
Mark Browne06ab3b2012-03-06 23:58:22 +0000331 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000332 mutex_lock(&w->codec->mutex);
333 else if (w->platform)
334 mutex_lock(&w->platform->mutex);
335}
336
337static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
338{
Mark Browne06ab3b2012-03-06 23:58:22 +0000339 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000340 mutex_unlock(&w->codec->mutex);
341 else if (w->platform)
342 mutex_unlock(&w->platform->mutex);
343}
344
345static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100346 unsigned short reg, unsigned int mask, unsigned int value)
347{
Mark Brown8a713da2011-12-03 12:33:55 +0000348 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100349 unsigned int old, new;
350 int ret;
351
Mark Brown8a713da2011-12-03 12:33:55 +0000352 if (w->codec && w->codec->using_regmap) {
353 ret = regmap_update_bits_check(w->codec->control_data,
354 reg, mask, value, &change);
355 if (ret != 0)
356 return ret;
357 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000358 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000359 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000360 if (ret < 0) {
361 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100362 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000363 }
Mark Brown8a713da2011-12-03 12:33:55 +0000364
365 old = ret;
366 new = (old & ~mask) | (value & mask);
367 change = old != new;
368 if (change) {
369 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000370 if (ret < 0) {
371 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000372 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000373 }
Mark Brown8a713da2011-12-03 12:33:55 +0000374 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000375 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100376 }
377
378 return change;
379}
380
Mark Brown452c5ea2009-05-17 21:41:23 +0100381/**
382 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800383 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100384 * @level: level to configure
385 *
386 * Configure the bias (power) levels for the SoC audio device.
387 *
388 * Returns 0 for success else error.
389 */
Mark Browned5a4c42011-02-18 11:12:42 -0800390static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200391 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100392{
Mark Browned5a4c42011-02-18 11:12:42 -0800393 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100394 int ret = 0;
395
Mark Brown84e90932010-11-04 00:07:02 -0400396 trace_snd_soc_bias_level_start(card, level);
397
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000398 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100399 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100400 if (ret != 0)
401 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100402
Mark Browncc4c6702011-06-06 19:03:34 +0100403 if (dapm->codec) {
404 if (dapm->codec->driver->set_bias_level)
405 ret = dapm->codec->driver->set_bias_level(dapm->codec,
406 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100407 else
408 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100409 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100410 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100411 }
Liam Girdwood41231282012-07-06 16:56:16 +0100412
Mark Brown171ec6b2011-06-06 18:15:19 +0100413 if (ret != 0)
414 goto out;
415
416 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100417 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100418out:
Mark Brown84e90932010-11-04 00:07:02 -0400419 trace_snd_soc_bias_level_done(card, level);
420
Mark Brown452c5ea2009-05-17 21:41:23 +0100421 return ret;
422}
423
Richard Purdie2b97eab2006-10-06 18:32:18 +0200424/* set up initial codec paths */
425static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
426 struct snd_soc_dapm_path *p, int i)
427{
428 switch (w->id) {
429 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000430 case snd_soc_dapm_mixer:
431 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200432 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100433 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600434 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400435 unsigned int reg = mc->reg;
436 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100437 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400438 unsigned int mask = (1 << fls(max)) - 1;
439 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200440
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100441 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200442 val = (val >> shift) & mask;
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200443 if (invert)
444 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200445
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200446 p->connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200447 }
448 break;
449 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600450 struct soc_enum *e = (struct soc_enum *)
451 w->kcontrol_news[i].private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200452 int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200453
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100454 val = soc_widget_read(w, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200455 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200456
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200457 if (item < e->max && !strcmp(p->name, e->texts[item]))
458 p->connect = 1;
459 else
460 p->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200461 }
462 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000463 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600464 struct soc_enum *e = (struct soc_enum *)
465 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000466
467 p->connect = 0;
468 /* since a virtual mux has no backing registers to
469 * decide which path to connect, it will try to match
470 * with the first enumeration. This is to ensure
471 * that the default mux choice (the first) will be
472 * correctly powered up during initialization.
473 */
474 if (!strcmp(p->name, e->texts[0]))
475 p->connect = 1;
476 }
477 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200478 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200479 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600480 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200481 int val, item;
482
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100483 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200484 val = (val >> e->shift_l) & e->mask;
485 for (item = 0; item < e->max; item++) {
486 if (val == e->values[item])
487 break;
488 }
489
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200490 if (item < e->max && !strcmp(p->name, e->texts[item]))
491 p->connect = 1;
492 else
493 p->connect = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200494 }
495 break;
Mark Brown56563102011-10-03 22:41:09 +0100496 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200497 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600498 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200499 case snd_soc_dapm_output:
500 case snd_soc_dapm_adc:
501 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000502 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200503 case snd_soc_dapm_dac:
504 case snd_soc_dapm_micbias:
505 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100506 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000507 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200508 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100509 case snd_soc_dapm_aif_in:
510 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100511 case snd_soc_dapm_dai_in:
512 case snd_soc_dapm_dai_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200513 case snd_soc_dapm_hp:
514 case snd_soc_dapm_mic:
515 case snd_soc_dapm_spk:
516 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100517 case snd_soc_dapm_dai_link:
Mark Brown56563102011-10-03 22:41:09 +0100518 p->connect = 1;
519 break;
520 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200521 case snd_soc_dapm_pre:
522 case snd_soc_dapm_post:
523 p->connect = 0;
524 break;
525 }
526}
527
Mark Brown74b8f952009-06-06 11:26:15 +0100528/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200529static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200530 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
531 struct snd_soc_dapm_path *path, const char *control_name,
532 const struct snd_kcontrol_new *kcontrol)
533{
534 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
535 int i;
536
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100537 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200538 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200539 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200540 list_add(&path->list_sink, &dest->sources);
541 list_add(&path->list_source, &src->sinks);
542 path->name = (char*)e->texts[i];
543 dapm_set_path_status(dest, path, 0);
544 return 0;
545 }
546 }
547
548 return -ENODEV;
549}
550
Mark Brown74b8f952009-06-06 11:26:15 +0100551/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200552static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200553 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
554 struct snd_soc_dapm_path *path, const char *control_name)
555{
556 int i;
557
558 /* search for mixer kcontrol */
559 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600560 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200561 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200562 list_add(&path->list_sink, &dest->sources);
563 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600564 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200565 dapm_set_path_status(dest, path, i);
566 return 0;
567 }
568 }
569 return -ENODEV;
570}
571
Stephen Warrenaf468002011-04-28 17:38:01 -0600572static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600573 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600574 const struct snd_kcontrol_new *kcontrol_new,
575 struct snd_kcontrol **kcontrol)
576{
577 struct snd_soc_dapm_widget *w;
578 int i;
579
580 *kcontrol = NULL;
581
582 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600583 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
584 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600585 for (i = 0; i < w->num_kcontrols; i++) {
586 if (&w->kcontrol_news[i] == kcontrol_new) {
587 if (w->kcontrols)
588 *kcontrol = w->kcontrols[i];
589 return 1;
590 }
591 }
592 }
593
594 return 0;
595}
596
Stephen Warren85762e72013-03-29 15:40:10 -0600597/*
598 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
599 * create it. Either way, add the widget into the control's widget list
600 */
601static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
602 int kci, struct snd_soc_dapm_path *path)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200603{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200604 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000605 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000606 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600607 size_t prefix_len;
608 int shared;
609 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600610 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600611 char *long_name;
612 const char *name;
613 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000614
615 if (dapm->codec)
616 prefix = dapm->codec->name_prefix;
617 else
618 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200619
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000620 if (prefix)
621 prefix_len = strlen(prefix) + 1;
622 else
623 prefix_len = 0;
624
Stephen Warren85762e72013-03-29 15:40:10 -0600625 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
626 &kcontrol);
627
Stephen Warren85762e72013-03-29 15:40:10 -0600628 if (!kcontrol) {
629 if (shared) {
630 wname_in_long_name = false;
631 kcname_in_long_name = true;
632 } else {
633 switch (w->id) {
634 case snd_soc_dapm_switch:
635 case snd_soc_dapm_mixer:
636 wname_in_long_name = true;
637 kcname_in_long_name = true;
638 break;
639 case snd_soc_dapm_mixer_named_ctl:
640 wname_in_long_name = false;
641 kcname_in_long_name = true;
642 break;
643 case snd_soc_dapm_mux:
644 case snd_soc_dapm_virt_mux:
645 case snd_soc_dapm_value_mux:
646 wname_in_long_name = true;
647 kcname_in_long_name = false;
648 break;
649 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600650 return -EINVAL;
651 }
652 }
653
654 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600655 /*
656 * The control will get a prefix from the control
657 * creation process but we're also using the same
658 * prefix for widgets so cut the prefix off the
659 * front of the widget name.
660 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200661 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600662 w->name + prefix_len,
663 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200664 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200665 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600666
667 name = long_name;
668 } else if (wname_in_long_name) {
669 long_name = NULL;
670 name = w->name + prefix_len;
671 } else {
672 long_name = NULL;
673 name = w->kcontrol_news[kci].name;
674 }
675
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200676 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600677 prefix);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200678 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200679 if (!kcontrol)
680 return -ENOMEM;
681 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200682
683 ret = dapm_kcontrol_data_alloc(w, kcontrol);
684 if (ret) {
685 snd_ctl_free_one(kcontrol);
686 return ret;
687 }
688
Stephen Warren85762e72013-03-29 15:40:10 -0600689 ret = snd_ctl_add(card, kcontrol);
690 if (ret < 0) {
691 dev_err(dapm->dev,
692 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
693 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600694 return ret;
695 }
Stephen Warren85762e72013-03-29 15:40:10 -0600696 }
697
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200698 ret = dapm_kcontrol_add_widget(kcontrol, w);
699 if (ret)
700 return ret;
701
Stephen Warren85762e72013-03-29 15:40:10 -0600702 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200703 dapm_kcontrol_add_path(kcontrol, path);
Stephen Warren85762e72013-03-29 15:40:10 -0600704
705 return 0;
706}
707
708/* create new dapm mixer control */
709static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
710{
711 int i, ret;
712 struct snd_soc_dapm_path *path;
713
Richard Purdie2b97eab2006-10-06 18:32:18 +0200714 /* add kcontrol */
715 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200716 /* match name */
717 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200718 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600719 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200720 continue;
721
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200722 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200723 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200724 continue;
725 }
726
Stephen Warren85762e72013-03-29 15:40:10 -0600727 ret = dapm_create_or_share_mixmux_kcontrol(w, i, path);
728 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200729 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200730 }
731 }
Stephen Warren85762e72013-03-29 15:40:10 -0600732
733 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200734}
735
736/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200737static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200738{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200739 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600740 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600741 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200742
Stephen Warrenaf468002011-04-28 17:38:01 -0600743 if (w->num_kcontrols != 1) {
744 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000745 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600746 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200747 return -EINVAL;
748 }
749
Stephen Warren85762e72013-03-29 15:40:10 -0600750 path = list_first_entry(&w->sources, struct snd_soc_dapm_path,
751 list_sink);
752 if (!path) {
753 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
754 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600755 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200756
Stephen Warren85762e72013-03-29 15:40:10 -0600757 ret = dapm_create_or_share_mixmux_kcontrol(w, 0, path);
758 if (ret < 0)
759 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600760
Richard Purdie2b97eab2006-10-06 18:32:18 +0200761 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200762 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200763
Stephen Warrenaf468002011-04-28 17:38:01 -0600764 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200765}
766
767/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200768static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200769{
Mark Browna6c65732010-03-03 17:45:21 +0000770 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200771 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000772 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200773
Mark Browna6c65732010-03-03 17:45:21 +0000774 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200775}
776
777/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100778static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
779 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200780{
781 struct snd_soc_dapm_path *p;
782
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100783 list_for_each_entry(p, sink, list_source) {
784 if (p->walked) {
785 p->walked = 0;
786 dapm_clear_walk_output(dapm, &p->sink->sinks);
787 }
788 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200789}
790
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100791static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
792 struct list_head *source)
793{
794 struct snd_soc_dapm_path *p;
795
796 list_for_each_entry(p, source, list_sink) {
797 if (p->walked) {
798 p->walked = 0;
799 dapm_clear_walk_input(dapm, &p->source->sources);
800 }
801 }
802}
803
804
Mark Brown99497882010-05-07 20:24:05 +0100805/* We implement power down on suspend by checking the power state of
806 * the ALSA card - when we are suspending the ALSA state for the card
807 * is set to D3.
808 */
809static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
810{
Mark Brown12ea2c72011-03-02 18:17:32 +0000811 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100812
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000813 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100814 case SNDRV_CTL_POWER_D3hot:
815 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100816 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000817 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200818 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100819 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100820 default:
821 return 1;
822 }
823}
824
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100825/* add widget to list if it's not already in the list */
826static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
827 struct snd_soc_dapm_widget *w)
828{
829 struct snd_soc_dapm_widget_list *wlist;
830 int wlistsize, wlistentries, i;
831
832 if (*list == NULL)
833 return -EINVAL;
834
835 wlist = *list;
836
837 /* is this widget already in the list */
838 for (i = 0; i < wlist->num_widgets; i++) {
839 if (wlist->widgets[i] == w)
840 return 0;
841 }
842
843 /* allocate some new space */
844 wlistentries = wlist->num_widgets + 1;
845 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
846 wlistentries * sizeof(struct snd_soc_dapm_widget *);
847 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
848 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000849 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100850 w->name);
851 return -ENOMEM;
852 }
853 wlist = *list;
854
855 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000856 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100857 w->name, wlist->num_widgets);
858
859 wlist->widgets[wlist->num_widgets] = w;
860 wlist->num_widgets++;
861 return 1;
862}
863
Richard Purdie2b97eab2006-10-06 18:32:18 +0200864/*
865 * Recursively check for a completed path to an active or physically connected
866 * output widget. Returns number of complete paths.
867 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100868static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
869 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200870{
871 struct snd_soc_dapm_path *path;
872 int con = 0;
873
Mark Brown024dc072011-10-09 11:52:05 +0100874 if (widget->outputs >= 0)
875 return widget->outputs;
876
Mark Brownde02d072011-09-20 21:43:24 +0100877 DAPM_UPDATE_STAT(widget, path_checks);
878
Mark Brown62ea8742012-01-21 21:14:48 +0000879 switch (widget->id) {
880 case snd_soc_dapm_supply:
881 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200882 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100883 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000884 default:
885 break;
886 }
Mark Brown246d0a12009-04-22 18:24:55 +0100887
Mark Brown010ff262009-08-17 17:39:22 +0100888 switch (widget->id) {
889 case snd_soc_dapm_adc:
890 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100891 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100892 if (widget->active) {
893 widget->outputs = snd_soc_dapm_suspend_check(widget);
894 return widget->outputs;
895 }
Mark Brown010ff262009-08-17 17:39:22 +0100896 default:
897 break;
898 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200899
900 if (widget->connected) {
901 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100902 if (widget->id == snd_soc_dapm_output && !widget->ext) {
903 widget->outputs = snd_soc_dapm_suspend_check(widget);
904 return widget->outputs;
905 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200906
907 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100908 if (widget->id == snd_soc_dapm_hp ||
909 widget->id == snd_soc_dapm_spk ||
910 (widget->id == snd_soc_dapm_line &&
911 !list_empty(&widget->sources))) {
912 widget->outputs = snd_soc_dapm_suspend_check(widget);
913 return widget->outputs;
914 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200915 }
916
917 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100918 DAPM_UPDATE_STAT(widget, neighbour_checks);
919
Mark Brownbf3a9e12011-06-13 16:42:29 +0100920 if (path->weak)
921 continue;
922
Mark Brown8af294b2013-02-22 17:48:15 +0000923 if (path->walking)
924 return 1;
925
Richard Purdie2b97eab2006-10-06 18:32:18 +0200926 if (path->walked)
927 continue;
928
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100929 trace_snd_soc_dapm_output_path(widget, path);
930
Richard Purdie2b97eab2006-10-06 18:32:18 +0200931 if (path->sink && path->connect) {
932 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000933 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100934
935 /* do we need to add this widget to the list ? */
936 if (list) {
937 int err;
938 err = dapm_list_add_widget(list, path->sink);
939 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000940 dev_err(widget->dapm->dev,
941 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100942 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000943 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100944 return con;
945 }
946 }
947
948 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000949
950 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200951 }
952 }
953
Mark Brown024dc072011-10-09 11:52:05 +0100954 widget->outputs = con;
955
Richard Purdie2b97eab2006-10-06 18:32:18 +0200956 return con;
957}
958
959/*
960 * Recursively check for a completed path to an active or physically connected
961 * input widget. Returns number of complete paths.
962 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100963static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
964 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200965{
966 struct snd_soc_dapm_path *path;
967 int con = 0;
968
Mark Brown024dc072011-10-09 11:52:05 +0100969 if (widget->inputs >= 0)
970 return widget->inputs;
971
Mark Brownde02d072011-09-20 21:43:24 +0100972 DAPM_UPDATE_STAT(widget, path_checks);
973
Mark Brown62ea8742012-01-21 21:14:48 +0000974 switch (widget->id) {
975 case snd_soc_dapm_supply:
976 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200977 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100978 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000979 default:
980 break;
981 }
Mark Brown246d0a12009-04-22 18:24:55 +0100982
Richard Purdie2b97eab2006-10-06 18:32:18 +0200983 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100984 switch (widget->id) {
985 case snd_soc_dapm_dac:
986 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +0100987 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +0100988 if (widget->active) {
989 widget->inputs = snd_soc_dapm_suspend_check(widget);
990 return widget->inputs;
991 }
Mark Brown010ff262009-08-17 17:39:22 +0100992 default:
993 break;
994 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200995
996 if (widget->connected) {
997 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100998 if (widget->id == snd_soc_dapm_input && !widget->ext) {
999 widget->inputs = snd_soc_dapm_suspend_check(widget);
1000 return widget->inputs;
1001 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001002
1003 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +01001004 if (widget->id == snd_soc_dapm_vmid) {
1005 widget->inputs = snd_soc_dapm_suspend_check(widget);
1006 return widget->inputs;
1007 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001008
1009 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +03001010 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +01001011 (widget->id == snd_soc_dapm_line &&
1012 !list_empty(&widget->sinks))) {
1013 widget->inputs = snd_soc_dapm_suspend_check(widget);
1014 return widget->inputs;
1015 }
1016
Mark Brown1ab97c82011-11-27 16:21:51 +00001017 /* signal generator */
1018 if (widget->id == snd_soc_dapm_siggen) {
1019 widget->inputs = snd_soc_dapm_suspend_check(widget);
1020 return widget->inputs;
1021 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001022 }
1023
1024 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +01001025 DAPM_UPDATE_STAT(widget, neighbour_checks);
1026
Mark Brownbf3a9e12011-06-13 16:42:29 +01001027 if (path->weak)
1028 continue;
1029
Mark Brown8af294b2013-02-22 17:48:15 +00001030 if (path->walking)
1031 return 1;
1032
Richard Purdie2b97eab2006-10-06 18:32:18 +02001033 if (path->walked)
1034 continue;
1035
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001036 trace_snd_soc_dapm_input_path(widget, path);
1037
Richard Purdie2b97eab2006-10-06 18:32:18 +02001038 if (path->source && path->connect) {
1039 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001040 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001041
1042 /* do we need to add this widget to the list ? */
1043 if (list) {
1044 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001045 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001046 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001047 dev_err(widget->dapm->dev,
1048 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001049 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001050 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001051 return con;
1052 }
1053 }
1054
1055 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001056
1057 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001058 }
1059 }
1060
Mark Brown024dc072011-10-09 11:52:05 +01001061 widget->inputs = con;
1062
Richard Purdie2b97eab2006-10-06 18:32:18 +02001063 return con;
1064}
1065
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001066/**
1067 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1068 * @dai: the soc DAI.
1069 * @stream: stream direction.
1070 * @list: list of active widgets for this stream.
1071 *
1072 * Queries DAPM graph as to whether an valid audio stream path exists for
1073 * the initial stream specified by name. This takes into account
1074 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1075 *
1076 * Returns the number of valid paths or negative error.
1077 */
1078int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1079 struct snd_soc_dapm_widget_list **list)
1080{
1081 struct snd_soc_card *card = dai->card;
1082 int paths;
1083
1084 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1085 dapm_reset(card);
1086
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001087 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001088 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001089 dapm_clear_walk_output(&card->dapm,
1090 &dai->playback_widget->sinks);
1091 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001092 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001093 dapm_clear_walk_input(&card->dapm,
1094 &dai->capture_widget->sources);
1095 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001096
1097 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001098 mutex_unlock(&card->dapm_mutex);
1099
1100 return paths;
1101}
1102
Richard Purdie2b97eab2006-10-06 18:32:18 +02001103/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001104 * Handler for generic register modifier widget.
1105 */
1106int dapm_reg_event(struct snd_soc_dapm_widget *w,
1107 struct snd_kcontrol *kcontrol, int event)
1108{
1109 unsigned int val;
1110
1111 if (SND_SOC_DAPM_EVENT_ON(event))
1112 val = w->on_val;
1113 else
1114 val = w->off_val;
1115
Liam Girdwood49575fb52012-03-06 18:16:19 +00001116 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001117 w->mask << w->shift, val << w->shift);
1118
1119 return 0;
1120}
Mark Brown11589412008-07-29 11:42:23 +01001121EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001122
Mark Brown62ea8742012-01-21 21:14:48 +00001123/*
1124 * Handler for regulator supply widget.
1125 */
1126int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1127 struct snd_kcontrol *kcontrol, int event)
1128{
Mark Brownc05b84d2012-09-07 12:57:11 +08001129 int ret;
1130
1131 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001132 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001133 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001134 if (ret != 0)
1135 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001136 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001137 w->name, ret);
1138 }
1139
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001140 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001141 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001142 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001143 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001144 if (ret != 0)
1145 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001146 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001147 w->name, ret);
1148 }
1149
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001150 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001151 }
Mark Brown62ea8742012-01-21 21:14:48 +00001152}
1153EXPORT_SYMBOL_GPL(dapm_regulator_event);
1154
Ola Liljad7e7eb92012-05-24 15:26:25 +02001155/*
1156 * Handler for clock supply widget.
1157 */
1158int dapm_clock_event(struct snd_soc_dapm_widget *w,
1159 struct snd_kcontrol *kcontrol, int event)
1160{
1161 if (!w->clk)
1162 return -EIO;
1163
Mark Brownec029952012-06-04 08:16:20 +01001164#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001165 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001166 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001167 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001168 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001169 return 0;
1170 }
Mark Brownec029952012-06-04 08:16:20 +01001171#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001172 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001173}
1174EXPORT_SYMBOL_GPL(dapm_clock_event);
1175
Mark Brownd8050022011-09-28 18:28:23 +01001176static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1177{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001178 if (w->power_checked)
1179 return w->new_power;
1180
Mark Brownd8050022011-09-28 18:28:23 +01001181 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001182 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001183 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001184 w->new_power = w->power_check(w);
1185
1186 w->power_checked = true;
1187
1188 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001189}
1190
Mark Browncd0f2d42009-04-20 16:56:59 +01001191/* Generic check to see if a widget should be powered.
1192 */
1193static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1194{
1195 int in, out;
1196
Mark Brownde02d072011-09-20 21:43:24 +01001197 DAPM_UPDATE_STAT(w, power_checks);
1198
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001199 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001200 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001201 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001202 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001203 return out != 0 && in != 0;
1204}
1205
Mark Brown6ea31b92009-04-20 17:15:41 +01001206/* Check to see if an ADC has power */
1207static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1208{
1209 int in;
1210
Mark Brownde02d072011-09-20 21:43:24 +01001211 DAPM_UPDATE_STAT(w, power_checks);
1212
Mark Brown6ea31b92009-04-20 17:15:41 +01001213 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001214 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001215 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001216 return in != 0;
1217 } else {
1218 return dapm_generic_check_power(w);
1219 }
1220}
1221
1222/* Check to see if a DAC has power */
1223static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1224{
1225 int out;
1226
Mark Brownde02d072011-09-20 21:43:24 +01001227 DAPM_UPDATE_STAT(w, power_checks);
1228
Mark Brown6ea31b92009-04-20 17:15:41 +01001229 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001230 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001231 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001232 return out != 0;
1233 } else {
1234 return dapm_generic_check_power(w);
1235 }
1236}
1237
Mark Brown246d0a12009-04-22 18:24:55 +01001238/* Check to see if a power supply is needed */
1239static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1240{
1241 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001242
Mark Brownde02d072011-09-20 21:43:24 +01001243 DAPM_UPDATE_STAT(w, power_checks);
1244
Mark Brown246d0a12009-04-22 18:24:55 +01001245 /* Check if one of our outputs is connected */
1246 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001247 DAPM_UPDATE_STAT(w, neighbour_checks);
1248
Mark Brownbf3a9e12011-06-13 16:42:29 +01001249 if (path->weak)
1250 continue;
1251
Mark Brown215edda2009-09-08 18:59:05 +01001252 if (path->connected &&
1253 !path->connected(path->source, path->sink))
1254 continue;
1255
Mark Brown30173582011-02-11 11:42:19 +00001256 if (!path->sink)
1257 continue;
1258
Mark Brownf68d7e12011-10-04 22:57:50 +01001259 if (dapm_widget_power_check(path->sink))
1260 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001261 }
1262
Mark Brownf68d7e12011-10-04 22:57:50 +01001263 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001264}
1265
Mark Brown35c64bc2011-09-28 18:23:53 +01001266static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1267{
1268 return 1;
1269}
1270
Mark Brown38357ab2009-06-06 19:03:23 +01001271static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1272 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001273 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001274{
Mark Brown828a8422011-01-15 13:14:30 +00001275 int *sort;
1276
1277 if (power_up)
1278 sort = dapm_up_seq;
1279 else
1280 sort = dapm_down_seq;
1281
Mark Brown38357ab2009-06-06 19:03:23 +01001282 if (sort[a->id] != sort[b->id])
1283 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001284 if (a->subseq != b->subseq) {
1285 if (power_up)
1286 return a->subseq - b->subseq;
1287 else
1288 return b->subseq - a->subseq;
1289 }
Mark Brownb22ead22009-06-07 12:51:26 +01001290 if (a->reg != b->reg)
1291 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001292 if (a->dapm != b->dapm)
1293 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001294
Mark Brown38357ab2009-06-06 19:03:23 +01001295 return 0;
1296}
Mark Brown42aa3412009-03-01 19:21:10 +00001297
Mark Brown38357ab2009-06-06 19:03:23 +01001298/* Insert a widget in order into a DAPM power sequence. */
1299static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1300 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001301 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001302{
1303 struct snd_soc_dapm_widget *w;
1304
1305 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001306 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001307 list_add_tail(&new_widget->power_list, &w->power_list);
1308 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001309 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001310
Mark Brown38357ab2009-06-06 19:03:23 +01001311 list_add_tail(&new_widget->power_list, list);
1312}
Mark Brown42aa3412009-03-01 19:21:10 +00001313
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001314static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001315 struct snd_soc_dapm_widget *w, int event)
1316{
Mark Brown68f89ad2010-11-03 23:51:49 -04001317 const char *ev_name;
1318 int power, ret;
1319
1320 switch (event) {
1321 case SND_SOC_DAPM_PRE_PMU:
1322 ev_name = "PRE_PMU";
1323 power = 1;
1324 break;
1325 case SND_SOC_DAPM_POST_PMU:
1326 ev_name = "POST_PMU";
1327 power = 1;
1328 break;
1329 case SND_SOC_DAPM_PRE_PMD:
1330 ev_name = "PRE_PMD";
1331 power = 0;
1332 break;
1333 case SND_SOC_DAPM_POST_PMD:
1334 ev_name = "POST_PMD";
1335 power = 0;
1336 break;
Mark Brown80114122013-02-25 15:14:19 +00001337 case SND_SOC_DAPM_WILL_PMU:
1338 ev_name = "WILL_PMU";
1339 power = 1;
1340 break;
1341 case SND_SOC_DAPM_WILL_PMD:
1342 ev_name = "WILL_PMD";
1343 power = 0;
1344 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001345 default:
1346 BUG();
1347 return;
1348 }
1349
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001350 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001351 return;
1352
1353 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001354 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001355 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001356 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001357 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001358 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001359 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001360 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001361 ev_name, w->name, ret);
1362 }
1363}
1364
Mark Brownb22ead22009-06-07 12:51:26 +01001365/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001366static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001367 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001368{
Mark Brown68f89ad2010-11-03 23:51:49 -04001369 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001370 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001371 unsigned int value = 0;
1372 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001373
1374 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1375 power_list)->reg;
1376
1377 list_for_each_entry(w, pending, power_list) {
Mark Brownb22ead22009-06-07 12:51:26 +01001378 BUG_ON(reg != w->reg);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001379 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001380
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001381 mask |= w->mask << w->shift;
1382 if (w->power)
1383 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001384 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001385 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001386
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001387 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001388 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1389 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001390
Mark Brown68f89ad2010-11-03 23:51:49 -04001391 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001392 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1393 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001394 }
1395
Mark Brown81628102009-06-07 13:21:24 +01001396 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001397 /* Any widget will do, they should all be updating the
1398 * same register.
1399 */
1400 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1401 power_list);
1402
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001403 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001404 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001405 value, mask, reg, card->pop_time);
1406 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001407 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001408 }
1409
1410 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001411 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1412 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001413 }
Mark Brown42aa3412009-03-01 19:21:10 +00001414}
1415
Mark Brownb22ead22009-06-07 12:51:26 +01001416/* Apply a DAPM power sequence.
1417 *
1418 * We walk over a pre-sorted list of widgets to apply power to. In
1419 * order to minimise the number of writes to the device required
1420 * multiple widgets will be updated in a single write where possible.
1421 * Currently anything that requires more than a single write is not
1422 * handled.
1423 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001424static void dapm_seq_run(struct snd_soc_card *card,
1425 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001426{
1427 struct snd_soc_dapm_widget *w, *n;
1428 LIST_HEAD(pending);
1429 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001430 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001431 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001432 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001433 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001434 int *sort;
1435
1436 if (power_up)
1437 sort = dapm_up_seq;
1438 else
1439 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001440
Mark Brownb22ead22009-06-07 12:51:26 +01001441 list_for_each_entry_safe(w, n, list, power_list) {
1442 ret = 0;
1443
1444 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001445 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001446 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001447 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001448 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001449
Mark Brown474b62d2011-01-18 16:14:44 +00001450 if (cur_dapm && cur_dapm->seq_notifier) {
1451 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1452 if (sort[i] == cur_sort)
1453 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001454 i,
1455 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001456 }
1457
Mark Brownb22ead22009-06-07 12:51:26 +01001458 INIT_LIST_HEAD(&pending);
1459 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001460 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001461 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001462 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001463 }
1464
Mark Brown163cac02009-06-07 10:12:52 +01001465 switch (w->id) {
1466 case snd_soc_dapm_pre:
1467 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001468 list_for_each_entry_safe_continue(w, n, list,
1469 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001470
Mark Brownb22ead22009-06-07 12:51:26 +01001471 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001472 ret = w->event(w,
1473 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001474 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001475 ret = w->event(w,
1476 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001477 break;
1478
1479 case snd_soc_dapm_post:
1480 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001481 list_for_each_entry_safe_continue(w, n, list,
1482 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001483
Mark Brownb22ead22009-06-07 12:51:26 +01001484 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001485 ret = w->event(w,
1486 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001487 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001488 ret = w->event(w,
1489 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001490 break;
1491
Mark Brown163cac02009-06-07 10:12:52 +01001492 default:
Mark Brown81628102009-06-07 13:21:24 +01001493 /* Queue it up for application */
1494 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001495 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001496 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001497 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001498 list_move(&w->power_list, &pending);
1499 break;
Mark Brown163cac02009-06-07 10:12:52 +01001500 }
Mark Brownb22ead22009-06-07 12:51:26 +01001501
1502 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001503 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001504 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001505 }
Mark Brownb22ead22009-06-07 12:51:26 +01001506
1507 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001508 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001509
1510 if (cur_dapm && cur_dapm->seq_notifier) {
1511 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1512 if (sort[i] == cur_sort)
1513 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001514 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001515 }
Mark Brown163cac02009-06-07 10:12:52 +01001516}
1517
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001518static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001519{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001520 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001521 struct snd_soc_dapm_widget_list *wlist;
1522 struct snd_soc_dapm_widget *w = NULL;
1523 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001524 int ret;
1525
1526 if (!update)
1527 return;
1528
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001529 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001530
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001531 for (wi = 0; wi < wlist->num_widgets; wi++) {
1532 w = wlist->widgets[wi];
1533
1534 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1535 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1536 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001537 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001538 w->name, ret);
1539 }
Mark Brown97404f22010-12-14 16:13:57 +00001540 }
1541
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001542 if (!w)
1543 return;
1544
Liam Girdwood49575fb52012-03-06 18:16:19 +00001545 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001546 update->val);
1547 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001548 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001549 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001550
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001551 for (wi = 0; wi < wlist->num_widgets; wi++) {
1552 w = wlist->widgets[wi];
1553
1554 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1555 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1556 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001557 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001558 w->name, ret);
1559 }
Mark Brown97404f22010-12-14 16:13:57 +00001560 }
1561}
1562
Mark Brown9d0624a2011-02-18 11:49:43 -08001563/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1564 * they're changing state.
1565 */
1566static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1567{
1568 struct snd_soc_dapm_context *d = data;
1569 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001570
Mark Brown56fba412011-06-04 11:25:10 +01001571 /* If we're off and we're not supposed to be go into STANDBY */
1572 if (d->bias_level == SND_SOC_BIAS_OFF &&
1573 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001574 if (d->dev)
1575 pm_runtime_get_sync(d->dev);
1576
Mark Brown9d0624a2011-02-18 11:49:43 -08001577 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1578 if (ret != 0)
1579 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001580 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001581 }
1582
Mark Brown56fba412011-06-04 11:25:10 +01001583 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1584 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001585 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1586 if (ret != 0)
1587 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001588 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001589 }
1590}
1591
1592/* Async callback run prior to DAPM sequences - brings to their final
1593 * state.
1594 */
1595static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1596{
1597 struct snd_soc_dapm_context *d = data;
1598 int ret;
1599
1600 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001601 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1602 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1603 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001604 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1605 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001606 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001607 ret);
1608 }
1609
1610 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001611 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1612 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001613 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1614 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001615 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1616 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001617
1618 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001619 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001620 }
1621
1622 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001623 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1624 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001625 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1626 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001627 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001628 ret);
1629 }
1630}
Mark Brown97404f22010-12-14 16:13:57 +00001631
Mark Brownfe4fda52011-10-03 22:36:57 +01001632static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1633 bool power, bool connect)
1634{
1635 /* If a connection is being made or broken then that update
1636 * will have marked the peer dirty, otherwise the widgets are
1637 * not connected and this update has no impact. */
1638 if (!connect)
1639 return;
1640
1641 /* If the peer is already in the state we're moving to then we
1642 * won't have an impact on it. */
1643 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001644 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001645}
1646
Mark Brown05623c42011-09-28 17:02:31 +01001647static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1648 struct list_head *up_list,
1649 struct list_head *down_list)
1650{
Mark Browndb432b42011-10-03 21:06:40 +01001651 struct snd_soc_dapm_path *path;
1652
Mark Brown05623c42011-09-28 17:02:31 +01001653 if (w->power == power)
1654 return;
1655
1656 trace_snd_soc_dapm_widget_power(w, power);
1657
Mark Browndb432b42011-10-03 21:06:40 +01001658 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001659 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001660 */
1661 list_for_each_entry(path, &w->sources, list_sink) {
1662 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001663 dapm_widget_set_peer_power(path->source, power,
1664 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001665 }
1666 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001667 switch (w->id) {
1668 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001669 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001670 case snd_soc_dapm_clock_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001671 /* Supplies can't affect their outputs, only their inputs */
1672 break;
1673 default:
1674 list_for_each_entry(path, &w->sinks, list_source) {
1675 if (path->sink) {
1676 dapm_widget_set_peer_power(path->sink, power,
1677 path->connect);
1678 }
Mark Browndb432b42011-10-03 21:06:40 +01001679 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001680 break;
Mark Browndb432b42011-10-03 21:06:40 +01001681 }
1682
Mark Brown05623c42011-09-28 17:02:31 +01001683 if (power)
1684 dapm_seq_insert(w, up_list, true);
1685 else
1686 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001687}
1688
Mark Brown7c81beb2011-09-20 22:22:32 +01001689static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1690 struct list_head *up_list,
1691 struct list_head *down_list)
1692{
Mark Brown7c81beb2011-09-20 22:22:32 +01001693 int power;
1694
1695 switch (w->id) {
1696 case snd_soc_dapm_pre:
1697 dapm_seq_insert(w, down_list, false);
1698 break;
1699 case snd_soc_dapm_post:
1700 dapm_seq_insert(w, up_list, true);
1701 break;
1702
1703 default:
Mark Brownd8050022011-09-28 18:28:23 +01001704 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001705
Mark Brown05623c42011-09-28 17:02:31 +01001706 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001707 break;
1708 }
1709}
1710
Mark Brown42aa3412009-03-01 19:21:10 +00001711/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001712 * Scan each dapm widget for complete audio path.
1713 * A complete path is a route that has valid endpoints i.e.:-
1714 *
1715 * o DAC to output pin.
1716 * o Input Pin to ADC.
1717 * o Input pin to Output pin (bypass, sidetone)
1718 * o DAC to ADC (loopback).
1719 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001720static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001721{
1722 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001723 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001724 LIST_HEAD(up_list);
1725 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001726 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001727 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001728
Mark Brown84e90932010-11-04 00:07:02 -04001729 trace_snd_soc_dapm_start(card);
1730
Mark Brown56fba412011-06-04 11:25:10 +01001731 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001732 if (d->idle_bias_off)
1733 d->target_bias_level = SND_SOC_BIAS_OFF;
1734 else
1735 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001736 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001737
Liam Girdwood6c120e12012-02-15 15:15:34 +00001738 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001739
Mark Brown6d3ddc82009-05-16 17:47:29 +01001740 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001741 * lists indicating if they should be powered up or down. We
1742 * only check widgets that have been flagged as dirty but note
1743 * that new widgets may be added to the dirty list while we
1744 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001745 */
Mark Browndb432b42011-10-03 21:06:40 +01001746 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001747 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001748 }
1749
Mark Brownf9de6d72011-09-28 17:19:47 +01001750 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001751 switch (w->id) {
1752 case snd_soc_dapm_pre:
1753 case snd_soc_dapm_post:
1754 /* These widgets always need to be powered */
1755 break;
1756 default:
1757 list_del_init(&w->dirty);
1758 break;
1759 }
Mark Browndb432b42011-10-03 21:06:40 +01001760
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001761 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001762 d = w->dapm;
1763
1764 /* Supplies and micbiases only bring the
1765 * context up to STANDBY as unless something
1766 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001767 * generally don't require full power. Signal
1768 * generators are virtual pins and have no
1769 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001770 */
1771 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001772 case snd_soc_dapm_siggen:
1773 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001774 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001775 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001776 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001777 case snd_soc_dapm_micbias:
1778 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1779 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1780 break;
1781 default:
1782 d->target_bias_level = SND_SOC_BIAS_ON;
1783 break;
1784 }
1785 }
1786
1787 }
1788
Mark Brown85a843c2011-09-21 21:29:47 +01001789 /* Force all contexts in the card to the same bias state if
1790 * they're not ground referenced.
1791 */
Mark Brown56fba412011-06-04 11:25:10 +01001792 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001793 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001794 if (d->target_bias_level > bias)
1795 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001796 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001797 if (!d->idle_bias_off)
1798 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001799
Mark Brownde02d072011-09-20 21:43:24 +01001800 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001801
Mark Brown9d0624a2011-02-18 11:49:43 -08001802 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001803 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001804 async_schedule_domain(dapm_pre_sequence_async, d,
1805 &async_domain);
1806 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001807
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001808 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001809 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001810 }
1811
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001812 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001813 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001814 }
1815
Mark Brown6d3ddc82009-05-16 17:47:29 +01001816 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001817 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001818
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001819 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001820
Mark Brown6d3ddc82009-05-16 17:47:29 +01001821 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001822 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001823
Mark Brown9d0624a2011-02-18 11:49:43 -08001824 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001825 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001826 async_schedule_domain(dapm_post_sequence_async, d,
1827 &async_domain);
1828 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001829
Liam Girdwood8078d872012-02-15 15:15:35 +00001830 /* do we need to notify any clients that DAPM event is complete */
1831 list_for_each_entry(d, &card->dapm_list, list) {
1832 if (d->stream_event)
1833 d->stream_event(d, event);
1834 }
1835
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001836 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001837 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001838 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001839
Mark Brown84e90932010-11-04 00:07:02 -04001840 trace_snd_soc_dapm_done(card);
1841
Mark Brown42aa3412009-03-01 19:21:10 +00001842 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001843}
1844
Mark Brown79fb9382009-08-21 16:38:13 +01001845#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001846static ssize_t dapm_widget_power_read_file(struct file *file,
1847 char __user *user_buf,
1848 size_t count, loff_t *ppos)
1849{
1850 struct snd_soc_dapm_widget *w = file->private_data;
1851 char *buf;
1852 int in, out;
1853 ssize_t ret;
1854 struct snd_soc_dapm_path *p = NULL;
1855
1856 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1857 if (!buf)
1858 return -ENOMEM;
1859
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001860 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001861 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001862 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001863 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001864
Mark Brownf13ebad2012-03-03 18:01:01 +00001865 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1866 w->name, w->power ? "On" : "Off",
1867 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001868
Mark Brownd033c362009-12-04 15:25:56 +00001869 if (w->reg >= 0)
1870 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001871 " - R%d(0x%x) mask 0x%x",
1872 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001873
1874 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1875
Mark Brown3eef08b2009-09-14 16:49:00 +01001876 if (w->sname)
1877 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1878 w->sname,
1879 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001880
1881 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001882 if (p->connected && !p->connected(w, p->sink))
1883 continue;
1884
Mark Brown79fb9382009-08-21 16:38:13 +01001885 if (p->connect)
1886 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001887 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001888 p->name ? p->name : "static",
1889 p->source->name);
1890 }
1891 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001892 if (p->connected && !p->connected(w, p->sink))
1893 continue;
1894
Mark Brown79fb9382009-08-21 16:38:13 +01001895 if (p->connect)
1896 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001897 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001898 p->name ? p->name : "static",
1899 p->sink->name);
1900 }
1901
1902 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1903
1904 kfree(buf);
1905 return ret;
1906}
1907
1908static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001909 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001910 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001911 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001912};
1913
Mark Brownef49e4f2011-04-04 20:48:13 +09001914static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1915 size_t count, loff_t *ppos)
1916{
1917 struct snd_soc_dapm_context *dapm = file->private_data;
1918 char *level;
1919
1920 switch (dapm->bias_level) {
1921 case SND_SOC_BIAS_ON:
1922 level = "On\n";
1923 break;
1924 case SND_SOC_BIAS_PREPARE:
1925 level = "Prepare\n";
1926 break;
1927 case SND_SOC_BIAS_STANDBY:
1928 level = "Standby\n";
1929 break;
1930 case SND_SOC_BIAS_OFF:
1931 level = "Off\n";
1932 break;
1933 default:
1934 BUG();
1935 level = "Unknown\n";
1936 break;
1937 }
1938
1939 return simple_read_from_buffer(user_buf, count, ppos, level,
1940 strlen(level));
1941}
1942
1943static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001944 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001945 .read = dapm_bias_read_file,
1946 .llseek = default_llseek,
1947};
1948
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{
Mark Brown79fb9382009-08-21 16:38:13 +01001952 struct dentry *d;
1953
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001954 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1955
1956 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001957 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001958 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001959 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001960 }
Mark Brown79fb9382009-08-21 16:38:13 +01001961
Mark Brownef49e4f2011-04-04 20:48:13 +09001962 d = debugfs_create_file("bias_level", 0444,
1963 dapm->debugfs_dapm, dapm,
1964 &dapm_bias_fops);
1965 if (!d)
1966 dev_warn(dapm->dev,
1967 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001968}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001969
1970static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1971{
1972 struct snd_soc_dapm_context *dapm = w->dapm;
1973 struct dentry *d;
1974
1975 if (!dapm->debugfs_dapm || !w->name)
1976 return;
1977
1978 d = debugfs_create_file(w->name, 0444,
1979 dapm->debugfs_dapm, w,
1980 &dapm_widget_power_fops);
1981 if (!d)
1982 dev_warn(w->dapm->dev,
1983 "ASoC: Failed to create %s debugfs file\n",
1984 w->name);
1985}
1986
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001987static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1988{
1989 debugfs_remove_recursive(dapm->debugfs_dapm);
1990}
1991
Mark Brown79fb9382009-08-21 16:38:13 +01001992#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001993void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1994 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001995{
1996}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001997
1998static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1999{
2000}
2001
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002002static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2003{
2004}
2005
Mark Brown79fb9382009-08-21 16:38:13 +01002006#endif
2007
Richard Purdie2b97eab2006-10-06 18:32:18 +02002008/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002009static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002010 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002011{
2012 struct snd_soc_dapm_path *path;
2013 int found = 0;
2014
Richard Purdie2b97eab2006-10-06 18:32:18 +02002015 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002016 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002017 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002018 continue;
2019
2020 found = 1;
2021 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002022 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002023 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002024 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002025 } else {
2026 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002027 dapm_mark_dirty(path->source,
2028 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002029 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002030 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002031 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002032 }
2033
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002034 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002035 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002036
Liam Girdwood618dae12012-04-25 12:12:51 +01002037 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002038}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002039
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002040int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002041 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2042 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002043{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002044 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002045 int ret;
2046
Liam Girdwood3cd04342012-03-09 12:02:08 +00002047 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002048 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002049 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002050 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002051 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002052 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002053 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002054 return ret;
2055}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002056EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002057
Milan plzik1b075e32008-01-10 14:39:46 +01002058/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002059static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002060 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002061{
2062 struct snd_soc_dapm_path *path;
2063 int found = 0;
2064
Richard Purdie2b97eab2006-10-06 18:32:18 +02002065 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002066 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002067 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002068 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002069 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002070 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002071 }
2072
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002073 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002074 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002075
Liam Girdwood618dae12012-04-25 12:12:51 +01002076 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002077}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002078
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002079int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002080 struct snd_kcontrol *kcontrol, int connect,
2081 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002082{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002083 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002084 int ret;
2085
Liam Girdwood3cd04342012-03-09 12:02:08 +00002086 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002087 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002088 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002089 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002090 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002091 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002092 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002093 return ret;
2094}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002095EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002096
2097/* show dapm widget status in sys fs */
2098static ssize_t dapm_widget_show(struct device *dev,
2099 struct device_attribute *attr, char *buf)
2100{
Mark Brown36ae1a92012-01-06 17:12:45 -08002101 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002102 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002103 struct snd_soc_dapm_widget *w;
2104 int count = 0;
2105 char *state = "not set";
2106
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002107 list_for_each_entry(w, &codec->card->widgets, list) {
2108 if (w->dapm != &codec->dapm)
2109 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002110
2111 /* only display widgets that burnm power */
2112 switch (w->id) {
2113 case snd_soc_dapm_hp:
2114 case snd_soc_dapm_mic:
2115 case snd_soc_dapm_spk:
2116 case snd_soc_dapm_line:
2117 case snd_soc_dapm_micbias:
2118 case snd_soc_dapm_dac:
2119 case snd_soc_dapm_adc:
2120 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002121 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002122 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002123 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002124 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002125 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002126 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002127 if (w->name)
2128 count += sprintf(buf + count, "%s: %s\n",
2129 w->name, w->power ? "On":"Off");
2130 break;
2131 default:
2132 break;
2133 }
2134 }
2135
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002136 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002137 case SND_SOC_BIAS_ON:
2138 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002139 break;
Mark Brown0be98982008-05-19 12:31:28 +02002140 case SND_SOC_BIAS_PREPARE:
2141 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002142 break;
Mark Brown0be98982008-05-19 12:31:28 +02002143 case SND_SOC_BIAS_STANDBY:
2144 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002145 break;
Mark Brown0be98982008-05-19 12:31:28 +02002146 case SND_SOC_BIAS_OFF:
2147 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002148 break;
2149 }
2150 count += sprintf(buf + count, "PM State: %s\n", state);
2151
2152 return count;
2153}
2154
2155static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2156
2157int snd_soc_dapm_sys_add(struct device *dev)
2158{
Troy Kisky12ef1932008-10-13 17:42:14 -07002159 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002160}
2161
2162static void snd_soc_dapm_sys_remove(struct device *dev)
2163{
Mark Brownaef90842009-05-16 17:53:16 +01002164 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002165}
2166
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002167static void dapm_free_path(struct snd_soc_dapm_path *path)
2168{
2169 list_del(&path->list_sink);
2170 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002171 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002172 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002173 kfree(path);
2174}
2175
Richard Purdie2b97eab2006-10-06 18:32:18 +02002176/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002177static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002178{
2179 struct snd_soc_dapm_widget *w, *next_w;
2180 struct snd_soc_dapm_path *p, *next_p;
2181
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002182 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2183 if (w->dapm != dapm)
2184 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002185 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002186 /*
2187 * remove source and sink paths associated to this widget.
2188 * While removing the path, remove reference to it from both
2189 * source and sink widgets so that path is removed only once.
2190 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002191 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2192 dapm_free_path(p);
2193
2194 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2195 dapm_free_path(p);
2196
Stephen Warrenfad59882011-04-28 17:37:59 -06002197 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002198 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002199 kfree(w);
2200 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002201}
2202
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002203static struct snd_soc_dapm_widget *dapm_find_widget(
2204 struct snd_soc_dapm_context *dapm, const char *pin,
2205 bool search_other_contexts)
2206{
2207 struct snd_soc_dapm_widget *w;
2208 struct snd_soc_dapm_widget *fallback = NULL;
2209
2210 list_for_each_entry(w, &dapm->card->widgets, list) {
2211 if (!strcmp(w->name, pin)) {
2212 if (w->dapm == dapm)
2213 return w;
2214 else
2215 fallback = w;
2216 }
2217 }
2218
2219 if (search_other_contexts)
2220 return fallback;
2221
2222 return NULL;
2223}
2224
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002225static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002226 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002227{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002228 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002229
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002230 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002231 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002232 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002233 }
2234
Mark Brown1a8b2d92012-02-16 11:50:07 -08002235 if (w->connected != status)
2236 dapm_mark_dirty(w, "pin configuration");
2237
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002238 w->connected = status;
2239 if (status == 0)
2240 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002241
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002242 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002243}
2244
Richard Purdie2b97eab2006-10-06 18:32:18 +02002245/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002246 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002247 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002248 *
2249 * Walks all dapm audio paths and powers widgets according to their
2250 * stream or path usage.
2251 *
2252 * Returns 0 for success.
2253 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002254int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002255{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002256 int ret;
2257
Mark Brown4f4c0072011-10-07 14:29:19 +01002258 /*
2259 * Suppress early reports (eg, jacks syncing their state) to avoid
2260 * silly DAPM runs during card startup.
2261 */
2262 if (!dapm->card || !dapm->card->instantiated)
2263 return 0;
2264
Liam Girdwood3cd04342012-03-09 12:02:08 +00002265 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002266 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002267 mutex_unlock(&dapm->card->dapm_mutex);
2268 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002269}
Liam Girdwooda5302182008-07-07 13:35:17 +01002270EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002271
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002272static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2273 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2274 const char *control,
2275 int (*connected)(struct snd_soc_dapm_widget *source,
2276 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002277{
2278 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002279 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002280
2281 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2282 if (!path)
2283 return -ENOMEM;
2284
2285 path->source = wsource;
2286 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002287 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002288 INIT_LIST_HEAD(&path->list);
2289 INIT_LIST_HEAD(&path->list_source);
2290 INIT_LIST_HEAD(&path->list_sink);
2291
2292 /* check for external widgets */
2293 if (wsink->id == snd_soc_dapm_input) {
2294 if (wsource->id == snd_soc_dapm_micbias ||
2295 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002296 wsource->id == snd_soc_dapm_line ||
2297 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002298 wsink->ext = 1;
2299 }
2300 if (wsource->id == snd_soc_dapm_output) {
2301 if (wsink->id == snd_soc_dapm_spk ||
2302 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002303 wsink->id == snd_soc_dapm_line ||
2304 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002305 wsource->ext = 1;
2306 }
2307
2308 /* connect static paths */
2309 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002310 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002311 list_add(&path->list_sink, &wsink->sources);
2312 list_add(&path->list_source, &wsource->sinks);
2313 path->connect = 1;
2314 return 0;
2315 }
2316
2317 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002318 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002319 case snd_soc_dapm_adc:
2320 case snd_soc_dapm_dac:
2321 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002322 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002323 case snd_soc_dapm_input:
2324 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002325 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002326 case snd_soc_dapm_micbias:
2327 case snd_soc_dapm_vmid:
2328 case snd_soc_dapm_pre:
2329 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002330 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002331 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002332 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002333 case snd_soc_dapm_aif_in:
2334 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002335 case snd_soc_dapm_dai_in:
2336 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002337 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002338 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002339 list_add(&path->list_sink, &wsink->sources);
2340 list_add(&path->list_source, &wsource->sinks);
2341 path->connect = 1;
2342 return 0;
2343 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002344 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002345 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002346 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002347 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002348 if (ret != 0)
2349 goto err;
2350 break;
2351 case snd_soc_dapm_switch:
2352 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002353 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002354 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002355 if (ret != 0)
2356 goto err;
2357 break;
2358 case snd_soc_dapm_hp:
2359 case snd_soc_dapm_mic:
2360 case snd_soc_dapm_line:
2361 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002362 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002363 list_add(&path->list_sink, &wsink->sources);
2364 list_add(&path->list_source, &wsource->sinks);
2365 path->connect = 0;
2366 return 0;
2367 }
Mark Brownfabd0382012-07-05 17:20:06 +01002368
2369 dapm_mark_dirty(wsource, "Route added");
2370 dapm_mark_dirty(wsink, "Route added");
2371
Richard Purdie2b97eab2006-10-06 18:32:18 +02002372 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002373err:
2374 kfree(path);
2375 return ret;
2376}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002377
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002378static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2379 const struct snd_soc_dapm_route *route)
2380{
2381 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2382 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2383 const char *sink;
2384 const char *source;
2385 char prefixed_sink[80];
2386 char prefixed_source[80];
2387 int ret;
2388
2389 if (dapm->codec && dapm->codec->name_prefix) {
2390 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2391 dapm->codec->name_prefix, route->sink);
2392 sink = prefixed_sink;
2393 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2394 dapm->codec->name_prefix, route->source);
2395 source = prefixed_source;
2396 } else {
2397 sink = route->sink;
2398 source = route->source;
2399 }
2400
2401 /*
2402 * find src and dest widgets over all widgets but favor a widget from
2403 * current DAPM context
2404 */
2405 list_for_each_entry(w, &dapm->card->widgets, list) {
2406 if (!wsink && !(strcmp(w->name, sink))) {
2407 wtsink = w;
2408 if (w->dapm == dapm)
2409 wsink = w;
2410 continue;
2411 }
2412 if (!wsource && !(strcmp(w->name, source))) {
2413 wtsource = w;
2414 if (w->dapm == dapm)
2415 wsource = w;
2416 }
2417 }
2418 /* use widget from another DAPM context if not found from this */
2419 if (!wsink)
2420 wsink = wtsink;
2421 if (!wsource)
2422 wsource = wtsource;
2423
2424 if (wsource == NULL) {
2425 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2426 route->source);
2427 return -ENODEV;
2428 }
2429 if (wsink == NULL) {
2430 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2431 route->sink);
2432 return -ENODEV;
2433 }
2434
2435 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2436 route->connected);
2437 if (ret)
2438 goto err;
2439
2440 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002441err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002442 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002443 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002444 return ret;
2445}
Mark Brown105f1c22008-05-13 14:52:19 +02002446
Mark Brownefcc3c62012-07-05 17:24:19 +01002447static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2448 const struct snd_soc_dapm_route *route)
2449{
2450 struct snd_soc_dapm_path *path, *p;
2451 const char *sink;
2452 const char *source;
2453 char prefixed_sink[80];
2454 char prefixed_source[80];
2455
2456 if (route->control) {
2457 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002458 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002459 return -EINVAL;
2460 }
2461
2462 if (dapm->codec && dapm->codec->name_prefix) {
2463 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2464 dapm->codec->name_prefix, route->sink);
2465 sink = prefixed_sink;
2466 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2467 dapm->codec->name_prefix, route->source);
2468 source = prefixed_source;
2469 } else {
2470 sink = route->sink;
2471 source = route->source;
2472 }
2473
2474 path = NULL;
2475 list_for_each_entry(p, &dapm->card->paths, list) {
2476 if (strcmp(p->source->name, source) != 0)
2477 continue;
2478 if (strcmp(p->sink->name, sink) != 0)
2479 continue;
2480 path = p;
2481 break;
2482 }
2483
2484 if (path) {
2485 dapm_mark_dirty(path->source, "Route removed");
2486 dapm_mark_dirty(path->sink, "Route removed");
2487
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002488 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002489 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002490 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002491 source, sink);
2492 }
2493
2494 return 0;
2495}
2496
Mark Brown105f1c22008-05-13 14:52:19 +02002497/**
Mark Brown105f1c22008-05-13 14:52:19 +02002498 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002499 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002500 * @route: audio routes
2501 * @num: number of routes
2502 *
2503 * Connects 2 dapm widgets together via a named audio path. The sink is
2504 * the widget receiving the audio signal, whilst the source is the sender
2505 * of the audio signal.
2506 *
2507 * Returns 0 for success else error. On error all resources can be freed
2508 * with a call to snd_soc_card_free().
2509 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002510int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002511 const struct snd_soc_dapm_route *route, int num)
2512{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002513 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002514
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002515 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002516 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002517 r = snd_soc_dapm_add_route(dapm, route);
2518 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002519 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2520 route->source,
2521 route->control ? route->control : "direct",
2522 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002523 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002524 }
2525 route++;
2526 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002527 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002528
Dan Carpenter60884c22012-04-13 22:25:43 +03002529 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002530}
2531EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2532
Mark Brownefcc3c62012-07-05 17:24:19 +01002533/**
2534 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2535 * @dapm: DAPM context
2536 * @route: audio routes
2537 * @num: number of routes
2538 *
2539 * Removes routes from the DAPM context.
2540 */
2541int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2542 const struct snd_soc_dapm_route *route, int num)
2543{
2544 int i, ret = 0;
2545
2546 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2547 for (i = 0; i < num; i++) {
2548 snd_soc_dapm_del_route(dapm, route);
2549 route++;
2550 }
2551 mutex_unlock(&dapm->card->dapm_mutex);
2552
2553 return ret;
2554}
2555EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2556
Mark Brownbf3a9e12011-06-13 16:42:29 +01002557static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2558 const struct snd_soc_dapm_route *route)
2559{
2560 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2561 route->source,
2562 true);
2563 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2564 route->sink,
2565 true);
2566 struct snd_soc_dapm_path *path;
2567 int count = 0;
2568
2569 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002570 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002571 route->source);
2572 return -ENODEV;
2573 }
2574
2575 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002576 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002577 route->sink);
2578 return -ENODEV;
2579 }
2580
2581 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002582 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002583 route->source, route->sink);
2584
2585 list_for_each_entry(path, &source->sinks, list_source) {
2586 if (path->sink == sink) {
2587 path->weak = 1;
2588 count++;
2589 }
2590 }
2591
2592 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002593 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002594 route->source, route->sink);
2595 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002596 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002597 count, route->source, route->sink);
2598
2599 return 0;
2600}
2601
2602/**
2603 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2604 * @dapm: DAPM context
2605 * @route: audio routes
2606 * @num: number of routes
2607 *
2608 * Mark existing routes matching those specified in the passed array
2609 * as being weak, meaning that they are ignored for the purpose of
2610 * power decisions. The main intended use case is for sidetone paths
2611 * which couple audio between other independent paths if they are both
2612 * active in order to make the combination work better at the user
2613 * level but which aren't intended to be "used".
2614 *
2615 * Note that CODEC drivers should not use this as sidetone type paths
2616 * can frequently also be used as bypass paths.
2617 */
2618int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2619 const struct snd_soc_dapm_route *route, int num)
2620{
2621 int i, err;
2622 int ret = 0;
2623
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002624 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002625 for (i = 0; i < num; i++) {
2626 err = snd_soc_dapm_weak_route(dapm, route);
2627 if (err)
2628 ret = err;
2629 route++;
2630 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002631 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002632
2633 return ret;
2634}
2635EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2636
Mark Brown105f1c22008-05-13 14:52:19 +02002637/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002638 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002639 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002640 *
2641 * Checks the codec for any new dapm widgets and creates them if found.
2642 *
2643 * Returns 0 for success.
2644 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002645int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002646{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002647 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002648 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002649 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002650
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002651 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002652
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002653 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002654 {
2655 if (w->new)
2656 continue;
2657
Stephen Warrenfad59882011-04-28 17:37:59 -06002658 if (w->num_kcontrols) {
2659 w->kcontrols = kzalloc(w->num_kcontrols *
2660 sizeof(struct snd_kcontrol *),
2661 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002662 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002663 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002664 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002665 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002666 }
2667
Richard Purdie2b97eab2006-10-06 18:32:18 +02002668 switch(w->id) {
2669 case snd_soc_dapm_switch:
2670 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002671 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002672 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002673 break;
2674 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002675 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002676 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002677 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002678 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002679 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002680 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002681 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002682 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002683 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002684 break;
2685 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002686
2687 /* Read the initial power state from the device */
2688 if (w->reg >= 0) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002689 val = soc_widget_read(w, w->reg) >> w->shift;
2690 val &= w->mask;
2691 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002692 w->power = 1;
2693 }
2694
Richard Purdie2b97eab2006-10-06 18:32:18 +02002695 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002696
Mark Brown7508b122011-10-05 12:09:12 +01002697 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002698 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002699 }
2700
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002701 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2702 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002703 return 0;
2704}
2705EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2706
2707/**
2708 * snd_soc_dapm_get_volsw - dapm mixer get callback
2709 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002710 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002711 *
2712 * Callback to get the value of a dapm mixer control.
2713 *
2714 * Returns 0 for success.
2715 */
2716int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2717 struct snd_ctl_elem_value *ucontrol)
2718{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002719 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002720 struct soc_mixer_control *mc =
2721 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002722 unsigned int reg = mc->reg;
2723 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002724 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002725 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002726 unsigned int invert = mc->invert;
2727
2728 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002729 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002730 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002731 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002732
Richard Purdie2b97eab2006-10-06 18:32:18 +02002733 ucontrol->value.integer.value[0] =
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002734 (snd_soc_read(codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002735 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002736 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002737 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002738
2739 return 0;
2740}
2741EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2742
2743/**
2744 * snd_soc_dapm_put_volsw - dapm mixer set callback
2745 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002746 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002747 *
2748 * Callback to set the value of a dapm mixer control.
2749 *
2750 * Returns 0 for success.
2751 */
2752int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2753 struct snd_ctl_elem_value *ucontrol)
2754{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002755 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002756 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002757 struct soc_mixer_control *mc =
2758 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002759 unsigned int reg = mc->reg;
2760 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002761 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002762 unsigned int mask = (1 << fls(max)) - 1;
2763 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002764 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002765 int connect, change;
2766 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002767
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002768 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002769 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002770 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002771 kcontrol->id.name);
2772
Richard Purdie2b97eab2006-10-06 18:32:18 +02002773 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002774 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002775
2776 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002777 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002778 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002779 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002780
Liam Girdwood3cd04342012-03-09 12:02:08 +00002781 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002782
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002783 change = snd_soc_test_bits(codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002784 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002785 update.kcontrol = kcontrol;
2786 update.reg = reg;
2787 update.mask = mask;
2788 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002789
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002790 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002791
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002792 soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002793
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002794 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002795 }
2796
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002797 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002798 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002799}
2800EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2801
2802/**
2803 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2804 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002805 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002806 *
2807 * Callback to get the value of a dapm enumerated double mixer control.
2808 *
2809 * Returns 0 for success.
2810 */
2811int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2812 struct snd_ctl_elem_value *ucontrol)
2813{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002814 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002815 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002816 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002817
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002818 val = snd_soc_read(codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002819 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002820 if (e->shift_l != e->shift_r)
2821 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002822 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002823
2824 return 0;
2825}
2826EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2827
2828/**
2829 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2830 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002831 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002832 *
2833 * Callback to set the value of a dapm enumerated double mixer control.
2834 *
2835 * Returns 0 for success.
2836 */
2837int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2838 struct snd_ctl_elem_value *ucontrol)
2839{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002840 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002841 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002842 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002843 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002844 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002845 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002846
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002847 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002848 return -EINVAL;
2849 mux = ucontrol->value.enumerated.item[0];
2850 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002851 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002852 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002853 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002854 return -EINVAL;
2855 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002856 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002857 }
2858
Liam Girdwood3cd04342012-03-09 12:02:08 +00002859 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002860
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002861 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002862 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002863 update.kcontrol = kcontrol;
2864 update.reg = e->reg;
2865 update.mask = mask;
2866 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002867 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002868
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002869 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002870
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002871 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002872 }
2873
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002874 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002875 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002876}
2877EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2878
2879/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002880 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2881 * @kcontrol: mixer control
2882 * @ucontrol: control element information
2883 *
2884 * Returns 0 for success.
2885 */
2886int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2887 struct snd_ctl_elem_value *ucontrol)
2888{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002889 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
Mark Brownd2b247a2009-10-06 15:21:04 +01002890 return 0;
2891}
2892EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2893
2894/**
2895 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2896 * @kcontrol: mixer control
2897 * @ucontrol: control element information
2898 *
2899 * Returns 0 for success.
2900 */
2901int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2902 struct snd_ctl_elem_value *ucontrol)
2903{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002904 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002905 struct snd_soc_card *card = codec->card;
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002906 unsigned int value;
Mark Brownd2b247a2009-10-06 15:21:04 +01002907 struct soc_enum *e =
2908 (struct soc_enum *)kcontrol->private_value;
2909 int change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002910
2911 if (ucontrol->value.enumerated.item[0] >= e->max)
2912 return -EINVAL;
2913
Liam Girdwood3cd04342012-03-09 12:02:08 +00002914 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002915
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002916 value = ucontrol->value.enumerated.item[0];
2917 change = dapm_kcontrol_set_value(kcontrol, value);
2918 if (change)
2919 soc_dapm_mux_update_power(card, kcontrol, value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002920
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002921 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002922 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002923}
2924EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2925
2926/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002927 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2928 * callback
2929 * @kcontrol: mixer control
2930 * @ucontrol: control element information
2931 *
2932 * Callback to get the value of a dapm semi enumerated double mixer control.
2933 *
2934 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2935 * used for handling bitfield coded enumeration for example.
2936 *
2937 * Returns 0 for success.
2938 */
2939int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2940 struct snd_ctl_elem_value *ucontrol)
2941{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002942 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002943 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002944 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002945
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002946 reg_val = snd_soc_read(codec, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002947 val = (reg_val >> e->shift_l) & e->mask;
2948 for (mux = 0; mux < e->max; mux++) {
2949 if (val == e->values[mux])
2950 break;
2951 }
2952 ucontrol->value.enumerated.item[0] = mux;
2953 if (e->shift_l != e->shift_r) {
2954 val = (reg_val >> e->shift_r) & e->mask;
2955 for (mux = 0; mux < e->max; mux++) {
2956 if (val == e->values[mux])
2957 break;
2958 }
2959 ucontrol->value.enumerated.item[1] = mux;
2960 }
2961
2962 return 0;
2963}
2964EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2965
2966/**
2967 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2968 * callback
2969 * @kcontrol: mixer control
2970 * @ucontrol: control element information
2971 *
2972 * Callback to set the value of a dapm semi enumerated double mixer control.
2973 *
2974 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2975 * used for handling bitfield coded enumeration for example.
2976 *
2977 * Returns 0 for success.
2978 */
2979int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2980 struct snd_ctl_elem_value *ucontrol)
2981{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002982 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002983 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002984 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002985 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002986 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002987 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002988
2989 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2990 return -EINVAL;
2991 mux = ucontrol->value.enumerated.item[0];
2992 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2993 mask = e->mask << e->shift_l;
2994 if (e->shift_l != e->shift_r) {
2995 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2996 return -EINVAL;
2997 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2998 mask |= e->mask << e->shift_r;
2999 }
3000
Liam Girdwood3cd04342012-03-09 12:02:08 +00003001 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003002
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003003 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003004 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02003005 update.kcontrol = kcontrol;
3006 update.reg = e->reg;
3007 update.mask = mask;
3008 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003009 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00003010
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003011 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003012
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003013 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003014 }
3015
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003016 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00003017 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003018}
3019EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3020
3021/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003022 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3023 *
3024 * @kcontrol: mixer control
3025 * @uinfo: control element information
3026 *
3027 * Callback to provide information about a pin switch control.
3028 */
3029int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3030 struct snd_ctl_elem_info *uinfo)
3031{
3032 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3033 uinfo->count = 1;
3034 uinfo->value.integer.min = 0;
3035 uinfo->value.integer.max = 1;
3036
3037 return 0;
3038}
3039EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3040
3041/**
3042 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3043 *
3044 * @kcontrol: mixer control
3045 * @ucontrol: Value
3046 */
3047int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3048 struct snd_ctl_elem_value *ucontrol)
3049{
Mark Brown48a8c392012-02-14 17:11:15 -08003050 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003051 const char *pin = (const char *)kcontrol->private_value;
3052
Liam Girdwood3cd04342012-03-09 12:02:08 +00003053 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003054
3055 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003056 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003057
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003058 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003059
3060 return 0;
3061}
3062EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3063
3064/**
3065 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3066 *
3067 * @kcontrol: mixer control
3068 * @ucontrol: Value
3069 */
3070int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3071 struct snd_ctl_elem_value *ucontrol)
3072{
Mark Brown48a8c392012-02-14 17:11:15 -08003073 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003074 const char *pin = (const char *)kcontrol->private_value;
3075
Liam Girdwood3cd04342012-03-09 12:02:08 +00003076 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003077
3078 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003079 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003080 else
Mark Brown48a8c392012-02-14 17:11:15 -08003081 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003082
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003083 mutex_unlock(&card->dapm_mutex);
3084
Mark Brown48a8c392012-02-14 17:11:15 -08003085 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003086 return 0;
3087}
3088EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3089
Mark Brown5ba06fc2012-02-16 11:07:13 -08003090static struct snd_soc_dapm_widget *
3091snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3092 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003093{
3094 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003095 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003096
3097 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003098 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003099
Mark Brown62ea8742012-01-21 21:14:48 +00003100 switch (w->id) {
3101 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003102 w->regulator = devm_regulator_get(dapm->dev, w->name);
3103 if (IS_ERR(w->regulator)) {
3104 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003105 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003106 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003107 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003108 }
Mark Brown8784c772013-01-10 19:33:47 +00003109
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003110 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003111 ret = regulator_allow_bypass(w->regulator, true);
3112 if (ret != 0)
3113 dev_warn(w->dapm->dev,
3114 "ASoC: Failed to unbypass %s: %d\n",
3115 w->name, ret);
3116 }
Mark Brown62ea8742012-01-21 21:14:48 +00003117 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003118 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003119#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003120 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003121 if (IS_ERR(w->clk)) {
3122 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003123 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003124 w->name, ret);
3125 return NULL;
3126 }
Mark Brownec029952012-06-04 08:16:20 +01003127#else
3128 return NULL;
3129#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003130 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003131 default:
3132 break;
3133 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003134
Mark Brown88e8b9a2011-03-02 18:18:24 +00003135 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003136 w->name = kasprintf(GFP_KERNEL, "%s %s",
3137 dapm->codec->name_prefix, widget->name);
3138 else
3139 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3140
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003141 if (w->name == NULL) {
3142 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003143 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003144 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003145
Mark Brown7ca3a182011-10-08 14:04:50 +01003146 switch (w->id) {
3147 case snd_soc_dapm_switch:
3148 case snd_soc_dapm_mixer:
3149 case snd_soc_dapm_mixer_named_ctl:
3150 w->power_check = dapm_generic_check_power;
3151 break;
3152 case snd_soc_dapm_mux:
3153 case snd_soc_dapm_virt_mux:
3154 case snd_soc_dapm_value_mux:
3155 w->power_check = dapm_generic_check_power;
3156 break;
Mark Brown46162742013-06-05 19:36:11 +01003157 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003158 w->power_check = dapm_adc_check_power;
3159 break;
Mark Brown46162742013-06-05 19:36:11 +01003160 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003161 w->power_check = dapm_dac_check_power;
3162 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003163 case snd_soc_dapm_adc:
3164 case snd_soc_dapm_aif_out:
3165 case snd_soc_dapm_dac:
3166 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003167 case snd_soc_dapm_pga:
3168 case snd_soc_dapm_out_drv:
3169 case snd_soc_dapm_input:
3170 case snd_soc_dapm_output:
3171 case snd_soc_dapm_micbias:
3172 case snd_soc_dapm_spk:
3173 case snd_soc_dapm_hp:
3174 case snd_soc_dapm_mic:
3175 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003176 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003177 w->power_check = dapm_generic_check_power;
3178 break;
3179 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003180 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003181 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003182 w->power_check = dapm_supply_check_power;
3183 break;
3184 default:
3185 w->power_check = dapm_always_on_check_power;
3186 break;
3187 }
3188
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003189 w->dapm = dapm;
3190 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003191 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003192 INIT_LIST_HEAD(&w->sources);
3193 INIT_LIST_HEAD(&w->sinks);
3194 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003195 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003196 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003197
3198 /* machine layer set ups unconnected pins and insertions */
3199 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003200 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003201}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003202
3203/**
Mark Brown4ba13272008-05-13 14:51:19 +02003204 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003205 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003206 * @widget: widget array
3207 * @num: number of widgets
3208 *
3209 * Creates new DAPM controls based upon the templates.
3210 *
3211 * Returns 0 for success else error.
3212 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003213int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003214 const struct snd_soc_dapm_widget *widget,
3215 int num)
3216{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003217 struct snd_soc_dapm_widget *w;
3218 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003219 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003220
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003221 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003222 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003223 w = snd_soc_dapm_new_control(dapm, widget);
3224 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003225 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003226 "ASoC: Failed to create DAPM control %s\n",
3227 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003228 ret = -ENOMEM;
3229 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003230 }
Mark Brown4ba13272008-05-13 14:51:19 +02003231 widget++;
3232 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003233 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003234 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003235}
3236EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3237
Mark Brownc74184e2012-04-04 22:12:09 +01003238static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3239 struct snd_kcontrol *kcontrol, int event)
3240{
3241 struct snd_soc_dapm_path *source_p, *sink_p;
3242 struct snd_soc_dai *source, *sink;
3243 const struct snd_soc_pcm_stream *config = w->params;
3244 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003245 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003246 u64 fmt;
3247 int ret;
3248
3249 BUG_ON(!config);
3250 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3251
3252 /* We only support a single source and sink, pick the first */
3253 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3254 list_sink);
3255 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3256 list_source);
3257
3258 BUG_ON(!source_p || !sink_p);
3259 BUG_ON(!sink_p->source || !source_p->sink);
3260 BUG_ON(!source_p->source || !sink_p->sink);
3261
3262 source = source_p->source->priv;
3263 sink = sink_p->sink->priv;
3264
3265 /* Be a little careful as we don't want to overflow the mask array */
3266 if (config->formats) {
3267 fmt = ffs(config->formats) - 1;
3268 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003269 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003270 config->formats);
3271 fmt = 0;
3272 }
3273
3274 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003275 params = kzalloc(sizeof(*params), GFP_KERNEL);
3276 if (!params) {
3277 ret = -ENOMEM;
3278 goto out;
3279 }
3280 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003281
Mark Brown9747cec2012-04-26 19:12:21 +01003282 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003283 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003284 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003285 config->rate_max;
3286
Mark Brown9747cec2012-04-26 19:12:21 +01003287 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003288 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003289 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003290 = config->channels_max;
3291
3292 memset(&substream, 0, sizeof(substream));
3293
3294 switch (event) {
3295 case SND_SOC_DAPM_PRE_PMU:
3296 if (source->driver->ops && source->driver->ops->hw_params) {
3297 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3298 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003299 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003300 if (ret != 0) {
3301 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003302 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003303 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003304 }
3305 }
3306
3307 if (sink->driver->ops && sink->driver->ops->hw_params) {
3308 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003309 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003310 sink);
3311 if (ret != 0) {
3312 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003313 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003314 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003315 }
3316 }
3317 break;
3318
3319 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003320 ret = snd_soc_dai_digital_mute(sink, 0,
3321 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003322 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003323 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003324 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003325 break;
3326
3327 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003328 ret = snd_soc_dai_digital_mute(sink, 1,
3329 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003330 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003331 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003332 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003333 break;
3334
3335 default:
3336 BUG();
3337 return -EINVAL;
3338 }
3339
Mark Brown9747cec2012-04-26 19:12:21 +01003340out:
3341 kfree(params);
3342 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003343}
3344
3345int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3346 const struct snd_soc_pcm_stream *params,
3347 struct snd_soc_dapm_widget *source,
3348 struct snd_soc_dapm_widget *sink)
3349{
3350 struct snd_soc_dapm_route routes[2];
3351 struct snd_soc_dapm_widget template;
3352 struct snd_soc_dapm_widget *w;
3353 size_t len;
3354 char *link_name;
3355
3356 len = strlen(source->name) + strlen(sink->name) + 2;
3357 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3358 if (!link_name)
3359 return -ENOMEM;
3360 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3361
3362 memset(&template, 0, sizeof(template));
3363 template.reg = SND_SOC_NOPM;
3364 template.id = snd_soc_dapm_dai_link;
3365 template.name = link_name;
3366 template.event = snd_soc_dai_link_event;
3367 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3368 SND_SOC_DAPM_PRE_PMD;
3369
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003370 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003371
3372 w = snd_soc_dapm_new_control(&card->dapm, &template);
3373 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003374 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003375 link_name);
3376 return -ENOMEM;
3377 }
3378
3379 w->params = params;
3380
3381 memset(&routes, 0, sizeof(routes));
3382
3383 routes[0].source = source->name;
3384 routes[0].sink = link_name;
3385 routes[1].source = link_name;
3386 routes[1].sink = sink->name;
3387
3388 return snd_soc_dapm_add_routes(&card->dapm, routes,
3389 ARRAY_SIZE(routes));
3390}
3391
Mark Brown888df392012-02-16 19:37:51 -08003392int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3393 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003394{
Mark Brown888df392012-02-16 19:37:51 -08003395 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003396 struct snd_soc_dapm_widget *w;
3397
Mark Brown888df392012-02-16 19:37:51 -08003398 WARN_ON(dapm->dev != dai->dev);
3399
3400 memset(&template, 0, sizeof(template));
3401 template.reg = SND_SOC_NOPM;
3402
3403 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003404 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003405 template.name = dai->driver->playback.stream_name;
3406 template.sname = dai->driver->playback.stream_name;
3407
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003408 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003409 template.name);
3410
3411 w = snd_soc_dapm_new_control(dapm, &template);
3412 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003413 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003414 dai->driver->playback.stream_name);
3415 }
3416
3417 w->priv = dai;
3418 dai->playback_widget = w;
3419 }
3420
3421 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003422 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003423 template.name = dai->driver->capture.stream_name;
3424 template.sname = dai->driver->capture.stream_name;
3425
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003426 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003427 template.name);
3428
3429 w = snd_soc_dapm_new_control(dapm, &template);
3430 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003431 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003432 dai->driver->capture.stream_name);
3433 }
3434
3435 w->priv = dai;
3436 dai->capture_widget = w;
3437 }
3438
3439 return 0;
3440}
3441
3442int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3443{
3444 struct snd_soc_dapm_widget *dai_w, *w;
3445 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003446
3447 /* For each DAI widget... */
3448 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003449 switch (dai_w->id) {
3450 case snd_soc_dapm_dai_in:
3451 case snd_soc_dapm_dai_out:
3452 break;
3453 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003454 continue;
Mark Brown46162742013-06-05 19:36:11 +01003455 }
Mark Brown888df392012-02-16 19:37:51 -08003456
3457 dai = dai_w->priv;
3458
3459 /* ...find all widgets with the same stream and link them */
3460 list_for_each_entry(w, &card->widgets, list) {
3461 if (w->dapm != dai_w->dapm)
3462 continue;
3463
Mark Brown46162742013-06-05 19:36:11 +01003464 switch (w->id) {
3465 case snd_soc_dapm_dai_in:
3466 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003467 continue;
Mark Brown46162742013-06-05 19:36:11 +01003468 default:
3469 break;
3470 }
Mark Brown888df392012-02-16 19:37:51 -08003471
3472 if (!w->sname)
3473 continue;
3474
3475 if (dai->driver->playback.stream_name &&
3476 strstr(w->sname,
3477 dai->driver->playback.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003478 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003479 dai->playback_widget->name, w->name);
Mark Brown888df392012-02-16 19:37:51 -08003480
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003481 snd_soc_dapm_add_path(w->dapm,
3482 dai->playback_widget, w, NULL, NULL);
Mark Brown888df392012-02-16 19:37:51 -08003483 }
3484
3485 if (dai->driver->capture.stream_name &&
3486 strstr(w->sname,
3487 dai->driver->capture.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003488 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003489 w->name, dai->capture_widget->name);
Mark Brown888df392012-02-16 19:37:51 -08003490
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003491 snd_soc_dapm_add_path(w->dapm, w,
3492 dai->capture_widget, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003493 }
3494 }
3495 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003496
Mark Brown888df392012-02-16 19:37:51 -08003497 return 0;
3498}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003499
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003500static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3501 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003502{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003503
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003504 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3505 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3506 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003507
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003508 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3509 w_cpu = cpu_dai->playback_widget;
3510 w_codec = codec_dai->playback_widget;
3511 } else {
3512 w_cpu = cpu_dai->capture_widget;
3513 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003514 }
3515
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003516 if (w_cpu) {
3517
3518 dapm_mark_dirty(w_cpu, "stream event");
3519
3520 switch (event) {
3521 case SND_SOC_DAPM_STREAM_START:
3522 w_cpu->active = 1;
3523 break;
3524 case SND_SOC_DAPM_STREAM_STOP:
3525 w_cpu->active = 0;
3526 break;
3527 case SND_SOC_DAPM_STREAM_SUSPEND:
3528 case SND_SOC_DAPM_STREAM_RESUME:
3529 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3530 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3531 break;
3532 }
3533 }
3534
3535 if (w_codec) {
3536
3537 dapm_mark_dirty(w_codec, "stream event");
3538
3539 switch (event) {
3540 case SND_SOC_DAPM_STREAM_START:
3541 w_codec->active = 1;
3542 break;
3543 case SND_SOC_DAPM_STREAM_STOP:
3544 w_codec->active = 0;
3545 break;
3546 case SND_SOC_DAPM_STREAM_SUSPEND:
3547 case SND_SOC_DAPM_STREAM_RESUME:
3548 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3549 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3550 break;
3551 }
3552 }
3553
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003554 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003555}
3556
3557/**
3558 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3559 * @rtd: PCM runtime data
3560 * @stream: stream name
3561 * @event: stream event
3562 *
3563 * Sends a stream event to the dapm core. The core then makes any
3564 * necessary widget power changes.
3565 *
3566 * Returns 0 for success else error.
3567 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003568void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3569 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003570{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003571 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003572
Liam Girdwood3cd04342012-03-09 12:02:08 +00003573 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003574 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003575 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003576}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003577
3578/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003579 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003580 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003581 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003582 *
Mark Brown74b8f952009-06-06 11:26:15 +01003583 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003584 * a valid audio route and active audio stream.
3585 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3586 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003587 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003588int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003589{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003590 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003591}
Liam Girdwooda5302182008-07-07 13:35:17 +01003592EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003593
3594/**
Mark Brownda341832010-03-15 19:23:37 +00003595 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003596 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003597 * @pin: pin name
3598 *
3599 * Enables input/output pin regardless of any other state. This is
3600 * intended for use with microphone bias supplies used in microphone
3601 * jack detection.
3602 *
3603 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3604 * do any widget power switching.
3605 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003606int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3607 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003608{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003609 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003610
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003611 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003612 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003613 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003614 }
3615
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003616 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003617 w->connected = 1;
3618 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003619 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003620
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003621 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003622}
3623EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3624
3625/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003626 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003627 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003628 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003629 *
Mark Brown74b8f952009-06-06 11:26:15 +01003630 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003631 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3632 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003633 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003634int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3635 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003636{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003637 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003638}
3639EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3640
3641/**
Mark Brown5817b522008-09-24 11:23:11 +01003642 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003643 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003644 * @pin: pin name
3645 *
3646 * Marks the specified pin as being not connected, disabling it along
3647 * any parent or child widgets. At present this is identical to
3648 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3649 * additional things such as disabling controls which only affect
3650 * paths through the pin.
3651 *
3652 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3653 * do any widget power switching.
3654 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003655int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003656{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003657 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003658}
3659EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3660
3661/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003662 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003663 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003664 * @pin: audio signal pin endpoint (or start point)
3665 *
3666 * Get audio pin status - connected or disconnected.
3667 *
3668 * Returns 1 for connected otherwise 0.
3669 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003670int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3671 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003672{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003673 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003674
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003675 if (w)
3676 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003677
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003678 return 0;
3679}
Liam Girdwooda5302182008-07-07 13:35:17 +01003680EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003681
3682/**
Mark Brown1547aba2010-05-07 21:11:40 +01003683 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003684 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003685 * @pin: audio signal pin endpoint (or start point)
3686 *
3687 * Mark the given endpoint or pin as ignoring suspend. When the
3688 * system is disabled a path between two endpoints flagged as ignoring
3689 * suspend will not be disabled. The path must already be enabled via
3690 * normal means at suspend time, it will not be turned on if it was not
3691 * already enabled.
3692 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003693int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3694 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003695{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003696 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003697
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003698 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003699 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003700 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003701 }
3702
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003703 w->ignore_suspend = 1;
3704
3705 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003706}
3707EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3708
Stephen Warren16332812011-11-23 12:42:04 -07003709static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3710 struct snd_soc_dapm_widget *w)
3711{
3712 struct snd_soc_dapm_path *p;
3713
3714 list_for_each_entry(p, &card->paths, list) {
3715 if ((p->source == w) || (p->sink == w)) {
3716 dev_dbg(card->dev,
3717 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3718 p->source->name, p->source->id, p->source->dapm,
3719 p->sink->name, p->sink->id, p->sink->dapm);
3720
3721 /* Connected to something other than the codec */
3722 if (p->source->dapm != p->sink->dapm)
3723 return true;
3724 /*
3725 * Loopback connection from codec external pin to
3726 * codec external pin
3727 */
3728 if (p->sink->id == snd_soc_dapm_input) {
3729 switch (p->source->id) {
3730 case snd_soc_dapm_output:
3731 case snd_soc_dapm_micbias:
3732 return true;
3733 default:
3734 break;
3735 }
3736 }
3737 }
3738 }
3739
3740 return false;
3741}
3742
3743/**
3744 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3745 * @codec: The codec whose pins should be processed
3746 *
3747 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3748 * which are unused. Pins are used if they are connected externally to the
3749 * codec, whether that be to some other device, or a loop-back connection to
3750 * the codec itself.
3751 */
3752void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3753{
3754 struct snd_soc_card *card = codec->card;
3755 struct snd_soc_dapm_context *dapm = &codec->dapm;
3756 struct snd_soc_dapm_widget *w;
3757
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003758 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003759 &card->dapm, &codec->dapm);
3760
3761 list_for_each_entry(w, &card->widgets, list) {
3762 if (w->dapm != dapm)
3763 continue;
3764 switch (w->id) {
3765 case snd_soc_dapm_input:
3766 case snd_soc_dapm_output:
3767 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003768 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003769 w->name);
3770 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003771 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003772 "... Not in map; disabling\n");
3773 snd_soc_dapm_nc_pin(dapm, w->name);
3774 }
3775 break;
3776 default:
3777 break;
3778 }
3779 }
3780}
3781
Mark Brown1547aba2010-05-07 21:11:40 +01003782/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003783 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003784 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003785 *
3786 * Free all dapm widgets and resources.
3787 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003788void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003789{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003790 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003791 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003792 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003793 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003794}
3795EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3796
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003797static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003798{
Liam Girdwood01005a72012-07-06 16:57:05 +01003799 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003800 struct snd_soc_dapm_widget *w;
3801 LIST_HEAD(down_list);
3802 int powerdown = 0;
3803
Liam Girdwood01005a72012-07-06 16:57:05 +01003804 mutex_lock(&card->dapm_mutex);
3805
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003806 list_for_each_entry(w, &dapm->card->widgets, list) {
3807 if (w->dapm != dapm)
3808 continue;
Mark Brown51737472009-06-22 13:16:51 +01003809 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003810 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003811 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003812 powerdown = 1;
3813 }
3814 }
3815
3816 /* If there were no widgets to power down we're already in
3817 * standby.
3818 */
3819 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003820 if (dapm->bias_level == SND_SOC_BIAS_ON)
3821 snd_soc_dapm_set_bias_level(dapm,
3822 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003823 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003824 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3825 snd_soc_dapm_set_bias_level(dapm,
3826 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003827 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003828
3829 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003830}
Mark Brown51737472009-06-22 13:16:51 +01003831
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003832/*
3833 * snd_soc_dapm_shutdown - callback for system shutdown
3834 */
3835void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3836{
3837 struct snd_soc_codec *codec;
3838
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003839 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003840 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003841 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3842 snd_soc_dapm_set_bias_level(&codec->dapm,
3843 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003844 }
Mark Brown51737472009-06-22 13:16:51 +01003845}
3846
Richard Purdie2b97eab2006-10-06 18:32:18 +02003847/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003848MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003849MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3850MODULE_LICENSE("GPL");