blob: cdc837ed144d7d1bd8d06287aa1111b85b67fc6d [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
Lars-Peter Clausen57295072013-08-05 11:27:31 +020050static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
51 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
52 const char *control,
53 int (*connected)(struct snd_soc_dapm_widget *source,
54 struct snd_soc_dapm_widget *sink));
55static struct snd_soc_dapm_widget *
56snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
57 const struct snd_soc_dapm_widget *widget);
58
Richard Purdie2b97eab2006-10-06 18:32:18 +020059/* dapm power sequences - make this per codec in the future */
60static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010061 [snd_soc_dapm_pre] = 0,
Mark Brown62ea8742012-01-21 21:14:48 +000062 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020063 [snd_soc_dapm_clock_supply] = 1,
Mark Brown1dd275b2013-10-09 13:56:37 +010064 [snd_soc_dapm_supply] = 2,
65 [snd_soc_dapm_micbias] = 3,
Mark Brownc74184e2012-04-04 22:12:09 +010066 [snd_soc_dapm_dai_link] = 2,
Mark Brown1dd275b2013-10-09 13:56:37 +010067 [snd_soc_dapm_dai_in] = 4,
68 [snd_soc_dapm_dai_out] = 4,
69 [snd_soc_dapm_aif_in] = 4,
70 [snd_soc_dapm_aif_out] = 4,
71 [snd_soc_dapm_mic] = 5,
72 [snd_soc_dapm_mux] = 6,
Mark Brown1dd275b2013-10-09 13:56:37 +010073 [snd_soc_dapm_dac] = 7,
74 [snd_soc_dapm_switch] = 8,
75 [snd_soc_dapm_mixer] = 8,
76 [snd_soc_dapm_mixer_named_ctl] = 8,
77 [snd_soc_dapm_pga] = 9,
78 [snd_soc_dapm_adc] = 10,
79 [snd_soc_dapm_out_drv] = 11,
80 [snd_soc_dapm_hp] = 11,
81 [snd_soc_dapm_spk] = 11,
82 [snd_soc_dapm_line] = 11,
83 [snd_soc_dapm_kcontrol] = 12,
84 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +020085};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000086
Richard Purdie2b97eab2006-10-06 18:32:18 +020087static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010088 [snd_soc_dapm_pre] = 0,
Lars-Peter Clausen57295072013-08-05 11:27:31 +020089 [snd_soc_dapm_kcontrol] = 1,
90 [snd_soc_dapm_adc] = 2,
91 [snd_soc_dapm_hp] = 3,
92 [snd_soc_dapm_spk] = 3,
93 [snd_soc_dapm_line] = 3,
94 [snd_soc_dapm_out_drv] = 3,
Mark Brown38357ab2009-06-06 19:03:23 +010095 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020096 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +010097 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010098 [snd_soc_dapm_mixer] = 5,
99 [snd_soc_dapm_dac] = 6,
100 [snd_soc_dapm_mic] = 7,
101 [snd_soc_dapm_micbias] = 8,
102 [snd_soc_dapm_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +0100103 [snd_soc_dapm_aif_in] = 10,
104 [snd_soc_dapm_aif_out] = 10,
Mark Brown46162742013-06-05 19:36:11 +0100105 [snd_soc_dapm_dai_in] = 10,
106 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100107 [snd_soc_dapm_dai_link] = 11,
Mark Brownc74184e2012-04-04 22:12:09 +0100108 [snd_soc_dapm_supply] = 12,
Mark Brown1dd275b2013-10-09 13:56:37 +0100109 [snd_soc_dapm_clock_supply] = 13,
110 [snd_soc_dapm_regulator_supply] = 13,
111 [snd_soc_dapm_post] = 14,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200112};
113
Mark Brownf9fa2b12014-03-06 16:49:11 +0800114static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
115{
116 if (dapm->card && dapm->card->instantiated)
117 lockdep_assert_held(&dapm->card->dapm_mutex);
118}
119
Troy Kisky12ef1932008-10-13 17:42:14 -0700120static void pop_wait(u32 pop_time)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100121{
122 if (pop_time)
123 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
124}
125
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200126static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100127{
128 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200129 char *buf;
130
131 if (!pop_time)
132 return;
133
134 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
135 if (buf == NULL)
136 return;
Mark Brown15e4c72f2008-07-02 11:51:20 +0100137
138 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200139 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100140 dev_info(dev, "%s", buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100141 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200142
143 kfree(buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100144}
145
Mark Browndb432b42011-10-03 21:06:40 +0100146static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
147{
148 return !list_empty(&w->dirty);
149}
150
Mark Brown492c0a12014-03-06 16:15:48 +0800151static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100152{
Mark Brownf9fa2b12014-03-06 16:49:11 +0800153 dapm_assert_locked(w->dapm);
154
Mark Brown75c1f892011-10-04 22:28:08 +0100155 if (!dapm_dirty_widget(w)) {
156 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
157 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100158 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100159 }
Mark Browndb432b42011-10-03 21:06:40 +0100160}
161
Mark Browne2d32ff2012-08-31 17:38:32 -0700162void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
163{
164 struct snd_soc_card *card = dapm->card;
165 struct snd_soc_dapm_widget *w;
166
167 mutex_lock(&card->dapm_mutex);
168
169 list_for_each_entry(w, &card->widgets, list) {
170 switch (w->id) {
171 case snd_soc_dapm_input:
172 case snd_soc_dapm_output:
173 dapm_mark_dirty(w, "Rechecking inputs and outputs");
174 break;
175 default:
176 break;
177 }
178 }
179
180 mutex_unlock(&card->dapm_mutex);
181}
182EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
183
Richard Purdie2b97eab2006-10-06 18:32:18 +0200184/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100185static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200186 const struct snd_soc_dapm_widget *_widget)
187{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100188 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200189}
190
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200191struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200192 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200193 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200194 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200195 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200196};
197
198static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
199 struct snd_kcontrol *kcontrol)
200{
201 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200202 struct soc_mixer_control *mc;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200203
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200204 data = kzalloc(sizeof(*data), GFP_KERNEL);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200205 if (!data) {
206 dev_err(widget->dapm->dev,
207 "ASoC: can't allocate kcontrol data for %s\n",
208 widget->name);
209 return -ENOMEM;
210 }
211
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200212 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200213
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200214 switch (widget->id) {
215 case snd_soc_dapm_switch:
216 case snd_soc_dapm_mixer:
217 case snd_soc_dapm_mixer_named_ctl:
218 mc = (struct soc_mixer_control *)kcontrol->private_value;
219
220 if (mc->autodisable) {
221 struct snd_soc_dapm_widget template;
222
223 memset(&template, 0, sizeof(template));
224 template.reg = mc->reg;
225 template.mask = (1 << fls(mc->max)) - 1;
226 template.shift = mc->shift;
227 if (mc->invert)
228 template.off_val = mc->max;
229 else
230 template.off_val = 0;
231 template.on_val = template.off_val;
232 template.id = snd_soc_dapm_kcontrol;
233 template.name = kcontrol->id.name;
234
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200235 data->value = template.on_val;
236
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200237 data->widget = snd_soc_dapm_new_control(widget->dapm,
238 &template);
239 if (!data->widget) {
240 kfree(data);
241 return -ENOMEM;
242 }
243 }
244 break;
245 default:
246 break;
247 }
248
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200249 kcontrol->private_data = data;
250
251 return 0;
252}
253
254static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
255{
256 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200257 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200258 kfree(data);
259}
260
261static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
262 const struct snd_kcontrol *kcontrol)
263{
264 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
265
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200266 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200267}
268
269static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
270 struct snd_soc_dapm_widget *widget)
271{
272 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200273 struct snd_soc_dapm_widget_list *new_wlist;
274 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200275
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200276 if (data->wlist)
277 n = data->wlist->num_widgets + 1;
278 else
279 n = 1;
280
281 new_wlist = krealloc(data->wlist,
282 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
283 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200284 return -ENOMEM;
285
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200286 new_wlist->widgets[n - 1] = widget;
287 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200288
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200289 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200290
291 return 0;
292}
293
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200294static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
295 struct snd_soc_dapm_path *path)
296{
297 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
298
299 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200300
301 if (data->widget) {
302 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
303 path->source, NULL, NULL);
304 }
305}
306
307static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
308{
309 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
310
311 if (!data->widget)
312 return true;
313
314 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200315}
316
317static struct list_head *dapm_kcontrol_get_path_list(
318 const struct snd_kcontrol *kcontrol)
319{
320 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
321
322 return &data->paths;
323}
324
325#define dapm_kcontrol_for_each_path(path, kcontrol) \
326 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
327 list_kcontrol)
328
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200329static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
330{
331 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
332
333 return data->value;
334}
335
336static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
337 unsigned int value)
338{
339 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
340
341 if (data->value == value)
342 return false;
343
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200344 if (data->widget)
345 data->widget->on_val = value;
346
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200347 data->value = value;
348
349 return true;
350}
351
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200352/**
353 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
354 * @kcontrol: The kcontrol
355 */
356struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
357{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200358 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200359}
360EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
361
Liam Girdwood6c120e12012-02-15 15:15:34 +0000362static void dapm_reset(struct snd_soc_card *card)
363{
364 struct snd_soc_dapm_widget *w;
365
Mark Brownf9fa2b12014-03-06 16:49:11 +0800366 lockdep_assert_held(&card->dapm_mutex);
367
Liam Girdwood6c120e12012-02-15 15:15:34 +0000368 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
369
370 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200371 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000372 w->power_checked = false;
373 w->inputs = -1;
374 w->outputs = -1;
375 }
376}
377
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800378static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg,
379 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100380{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200381 if (!w->dapm->component)
382 return -EIO;
383 return snd_soc_component_read(w->dapm->component, reg, value);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100384}
385
Lars-Peter Clausen23d54422014-04-22 13:23:16 +0200386static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
Bard Liao34775012014-04-17 20:12:56 +0800387 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100388{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200389 if (!w->dapm->component)
390 return -EIO;
391 return snd_soc_component_update_bits_async(w->dapm->component, reg,
392 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000393}
394
Mark Browneb270e92013-10-09 13:52:52 +0100395static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
396{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200397 if (dapm->component)
398 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100399}
400
Mark Brown452c5ea2009-05-17 21:41:23 +0100401/**
402 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800403 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100404 * @level: level to configure
405 *
406 * Configure the bias (power) levels for the SoC audio device.
407 *
408 * Returns 0 for success else error.
409 */
Mark Browned5a4c42011-02-18 11:12:42 -0800410static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200411 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100412{
Mark Browned5a4c42011-02-18 11:12:42 -0800413 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100414 int ret = 0;
415
Mark Brown84e90932010-11-04 00:07:02 -0400416 trace_snd_soc_bias_level_start(card, level);
417
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000418 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100419 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100420 if (ret != 0)
421 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100422
Mark Browncc4c6702011-06-06 19:03:34 +0100423 if (dapm->codec) {
424 if (dapm->codec->driver->set_bias_level)
425 ret = dapm->codec->driver->set_bias_level(dapm->codec,
426 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100427 else
428 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100429 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100430 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100431 }
Liam Girdwood41231282012-07-06 16:56:16 +0100432
Mark Brown171ec6b2011-06-06 18:15:19 +0100433 if (ret != 0)
434 goto out;
435
436 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100437 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100438out:
Mark Brown84e90932010-11-04 00:07:02 -0400439 trace_snd_soc_bias_level_done(card, level);
440
Mark Brown452c5ea2009-05-17 21:41:23 +0100441 return ret;
442}
443
Mark Brown74b8f952009-06-06 11:26:15 +0100444/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200445static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200446 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
447 struct snd_soc_dapm_path *path, const char *control_name,
448 const struct snd_kcontrol_new *kcontrol)
449{
450 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100451 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200452 int i;
453
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100454 if (e->reg != SND_SOC_NOPM) {
455 soc_widget_read(dest, e->reg, &val);
456 val = (val >> e->shift_l) & e->mask;
457 item = snd_soc_enum_val_to_item(e, val);
458 } else {
459 /* since a virtual mux has no backing registers to
460 * decide which path to connect, it will try to match
461 * with the first enumeration. This is to ensure
462 * that the default mux choice (the first) will be
463 * correctly powered up during initialization.
464 */
465 item = 0;
466 }
467
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100468 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200469 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200470 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200471 list_add(&path->list_sink, &dest->sources);
472 list_add(&path->list_source, &src->sinks);
473 path->name = (char*)e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100474 if (i == item)
475 path->connect = 1;
476 else
477 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200478 return 0;
479 }
480 }
481
482 return -ENODEV;
483}
484
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100485/* set up initial codec paths */
486static void dapm_set_mixer_path_status(struct snd_soc_dapm_widget *w,
487 struct snd_soc_dapm_path *p, int i)
488{
489 struct soc_mixer_control *mc = (struct soc_mixer_control *)
490 w->kcontrol_news[i].private_value;
491 unsigned int reg = mc->reg;
492 unsigned int shift = mc->shift;
493 unsigned int max = mc->max;
494 unsigned int mask = (1 << fls(max)) - 1;
495 unsigned int invert = mc->invert;
496 unsigned int val;
497
498 if (reg != SND_SOC_NOPM) {
499 soc_widget_read(w, reg, &val);
500 val = (val >> shift) & mask;
501 if (invert)
502 val = max - val;
503 p->connect = !!val;
504 } else {
505 p->connect = 0;
506 }
507}
508
Mark Brown74b8f952009-06-06 11:26:15 +0100509/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200510static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200511 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
512 struct snd_soc_dapm_path *path, const char *control_name)
513{
514 int i;
515
516 /* search for mixer kcontrol */
517 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600518 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200519 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200520 list_add(&path->list_sink, &dest->sources);
521 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600522 path->name = dest->kcontrol_news[i].name;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100523 dapm_set_mixer_path_status(dest, path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200524 return 0;
525 }
526 }
527 return -ENODEV;
528}
529
Stephen Warrenaf468002011-04-28 17:38:01 -0600530static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600531 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600532 const struct snd_kcontrol_new *kcontrol_new,
533 struct snd_kcontrol **kcontrol)
534{
535 struct snd_soc_dapm_widget *w;
536 int i;
537
538 *kcontrol = NULL;
539
540 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600541 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
542 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600543 for (i = 0; i < w->num_kcontrols; i++) {
544 if (&w->kcontrol_news[i] == kcontrol_new) {
545 if (w->kcontrols)
546 *kcontrol = w->kcontrols[i];
547 return 1;
548 }
549 }
550 }
551
552 return 0;
553}
554
Stephen Warren85762e72013-03-29 15:40:10 -0600555/*
556 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
557 * create it. Either way, add the widget into the control's widget list
558 */
559static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100560 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200561{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200562 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000563 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000564 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600565 size_t prefix_len;
566 int shared;
567 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600568 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600569 char *long_name;
570 const char *name;
571 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000572
573 if (dapm->codec)
574 prefix = dapm->codec->name_prefix;
575 else
576 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200577
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000578 if (prefix)
579 prefix_len = strlen(prefix) + 1;
580 else
581 prefix_len = 0;
582
Stephen Warren85762e72013-03-29 15:40:10 -0600583 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
584 &kcontrol);
585
Stephen Warren85762e72013-03-29 15:40:10 -0600586 if (!kcontrol) {
587 if (shared) {
588 wname_in_long_name = false;
589 kcname_in_long_name = true;
590 } else {
591 switch (w->id) {
592 case snd_soc_dapm_switch:
593 case snd_soc_dapm_mixer:
594 wname_in_long_name = true;
595 kcname_in_long_name = true;
596 break;
597 case snd_soc_dapm_mixer_named_ctl:
598 wname_in_long_name = false;
599 kcname_in_long_name = true;
600 break;
601 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600602 wname_in_long_name = true;
603 kcname_in_long_name = false;
604 break;
605 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600606 return -EINVAL;
607 }
608 }
609
610 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600611 /*
612 * The control will get a prefix from the control
613 * creation process but we're also using the same
614 * prefix for widgets so cut the prefix off the
615 * front of the widget name.
616 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200617 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600618 w->name + prefix_len,
619 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200620 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200621 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600622
623 name = long_name;
624 } else if (wname_in_long_name) {
625 long_name = NULL;
626 name = w->name + prefix_len;
627 } else {
628 long_name = NULL;
629 name = w->kcontrol_news[kci].name;
630 }
631
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200632 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600633 prefix);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200634 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200635 if (!kcontrol)
636 return -ENOMEM;
637 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200638
639 ret = dapm_kcontrol_data_alloc(w, kcontrol);
640 if (ret) {
641 snd_ctl_free_one(kcontrol);
642 return ret;
643 }
644
Stephen Warren85762e72013-03-29 15:40:10 -0600645 ret = snd_ctl_add(card, kcontrol);
646 if (ret < 0) {
647 dev_err(dapm->dev,
648 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
649 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600650 return ret;
651 }
Stephen Warren85762e72013-03-29 15:40:10 -0600652 }
653
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200654 ret = dapm_kcontrol_add_widget(kcontrol, w);
655 if (ret)
656 return ret;
657
Stephen Warren85762e72013-03-29 15:40:10 -0600658 w->kcontrols[kci] = kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600659
660 return 0;
661}
662
663/* create new dapm mixer control */
664static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
665{
666 int i, ret;
667 struct snd_soc_dapm_path *path;
668
Richard Purdie2b97eab2006-10-06 18:32:18 +0200669 /* add kcontrol */
670 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200671 /* match name */
672 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200673 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600674 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200675 continue;
676
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200677 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200678 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200679 continue;
680 }
681
Mark Brown946d92a2013-08-12 23:28:42 +0100682 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600683 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200684 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100685
686 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200687 }
688 }
Stephen Warren85762e72013-03-29 15:40:10 -0600689
690 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200691}
692
693/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200694static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200695{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200696 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600697 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600698 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200699
Stephen Warrenaf468002011-04-28 17:38:01 -0600700 if (w->num_kcontrols != 1) {
701 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000702 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600703 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200704 return -EINVAL;
705 }
706
Lars-Peter Clausenfe581392013-08-01 18:30:38 +0200707 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600708 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
709 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600710 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200711
Mark Brown946d92a2013-08-12 23:28:42 +0100712 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600713 if (ret < 0)
714 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600715
Richard Purdie2b97eab2006-10-06 18:32:18 +0200716 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200717 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200718
Stephen Warrenaf468002011-04-28 17:38:01 -0600719 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200720}
721
722/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200723static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200724{
Mark Browna6c65732010-03-03 17:45:21 +0000725 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200726 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000727 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200728
Mark Browna6c65732010-03-03 17:45:21 +0000729 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200730}
731
732/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100733static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
734 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200735{
736 struct snd_soc_dapm_path *p;
737
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100738 list_for_each_entry(p, sink, list_source) {
739 if (p->walked) {
740 p->walked = 0;
741 dapm_clear_walk_output(dapm, &p->sink->sinks);
742 }
743 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200744}
745
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100746static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
747 struct list_head *source)
748{
749 struct snd_soc_dapm_path *p;
750
751 list_for_each_entry(p, source, list_sink) {
752 if (p->walked) {
753 p->walked = 0;
754 dapm_clear_walk_input(dapm, &p->source->sources);
755 }
756 }
757}
758
759
Mark Brown9949788b2010-05-07 20:24:05 +0100760/* We implement power down on suspend by checking the power state of
761 * the ALSA card - when we are suspending the ALSA state for the card
762 * is set to D3.
763 */
764static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
765{
Mark Brown12ea2c72011-03-02 18:17:32 +0000766 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100767
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000768 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100769 case SNDRV_CTL_POWER_D3hot:
770 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100771 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000772 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200773 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100774 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100775 default:
776 return 1;
777 }
778}
779
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100780/* add widget to list if it's not already in the list */
781static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
782 struct snd_soc_dapm_widget *w)
783{
784 struct snd_soc_dapm_widget_list *wlist;
785 int wlistsize, wlistentries, i;
786
787 if (*list == NULL)
788 return -EINVAL;
789
790 wlist = *list;
791
792 /* is this widget already in the list */
793 for (i = 0; i < wlist->num_widgets; i++) {
794 if (wlist->widgets[i] == w)
795 return 0;
796 }
797
798 /* allocate some new space */
799 wlistentries = wlist->num_widgets + 1;
800 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
801 wlistentries * sizeof(struct snd_soc_dapm_widget *);
802 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
803 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000804 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100805 w->name);
806 return -ENOMEM;
807 }
808 wlist = *list;
809
810 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000811 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100812 w->name, wlist->num_widgets);
813
814 wlist->widgets[wlist->num_widgets] = w;
815 wlist->num_widgets++;
816 return 1;
817}
818
Richard Purdie2b97eab2006-10-06 18:32:18 +0200819/*
820 * Recursively check for a completed path to an active or physically connected
821 * output widget. Returns number of complete paths.
822 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100823static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
824 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200825{
826 struct snd_soc_dapm_path *path;
827 int con = 0;
828
Mark Brown024dc072011-10-09 11:52:05 +0100829 if (widget->outputs >= 0)
830 return widget->outputs;
831
Mark Brownde02d072011-09-20 21:43:24 +0100832 DAPM_UPDATE_STAT(widget, path_checks);
833
Mark Brown62ea8742012-01-21 21:14:48 +0000834 switch (widget->id) {
835 case snd_soc_dapm_supply:
836 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200837 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200838 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100839 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000840 default:
841 break;
842 }
Mark Brown246d0a12009-04-22 18:24:55 +0100843
Mark Brown010ff262009-08-17 17:39:22 +0100844 switch (widget->id) {
845 case snd_soc_dapm_adc:
846 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100847 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100848 if (widget->active) {
849 widget->outputs = snd_soc_dapm_suspend_check(widget);
850 return widget->outputs;
851 }
Mark Brown010ff262009-08-17 17:39:22 +0100852 default:
853 break;
854 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200855
856 if (widget->connected) {
857 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100858 if (widget->id == snd_soc_dapm_output && !widget->ext) {
859 widget->outputs = snd_soc_dapm_suspend_check(widget);
860 return widget->outputs;
861 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200862
863 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100864 if (widget->id == snd_soc_dapm_hp ||
865 widget->id == snd_soc_dapm_spk ||
866 (widget->id == snd_soc_dapm_line &&
867 !list_empty(&widget->sources))) {
868 widget->outputs = snd_soc_dapm_suspend_check(widget);
869 return widget->outputs;
870 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200871 }
872
873 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100874 DAPM_UPDATE_STAT(widget, neighbour_checks);
875
Mark Brownbf3a9e12011-06-13 16:42:29 +0100876 if (path->weak)
877 continue;
878
Mark Brown8af294b2013-02-22 17:48:15 +0000879 if (path->walking)
880 return 1;
881
Richard Purdie2b97eab2006-10-06 18:32:18 +0200882 if (path->walked)
883 continue;
884
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100885 trace_snd_soc_dapm_output_path(widget, path);
886
Richard Purdie2b97eab2006-10-06 18:32:18 +0200887 if (path->sink && path->connect) {
888 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000889 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100890
891 /* do we need to add this widget to the list ? */
892 if (list) {
893 int err;
894 err = dapm_list_add_widget(list, path->sink);
895 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000896 dev_err(widget->dapm->dev,
897 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100898 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000899 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100900 return con;
901 }
902 }
903
904 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000905
906 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200907 }
908 }
909
Mark Brown024dc072011-10-09 11:52:05 +0100910 widget->outputs = con;
911
Richard Purdie2b97eab2006-10-06 18:32:18 +0200912 return con;
913}
914
915/*
916 * Recursively check for a completed path to an active or physically connected
917 * input widget. Returns number of complete paths.
918 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100919static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
920 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200921{
922 struct snd_soc_dapm_path *path;
923 int con = 0;
924
Mark Brown024dc072011-10-09 11:52:05 +0100925 if (widget->inputs >= 0)
926 return widget->inputs;
927
Mark Brownde02d072011-09-20 21:43:24 +0100928 DAPM_UPDATE_STAT(widget, path_checks);
929
Mark Brown62ea8742012-01-21 21:14:48 +0000930 switch (widget->id) {
931 case snd_soc_dapm_supply:
932 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200933 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200934 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100935 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000936 default:
937 break;
938 }
Mark Brown246d0a12009-04-22 18:24:55 +0100939
Richard Purdie2b97eab2006-10-06 18:32:18 +0200940 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100941 switch (widget->id) {
942 case snd_soc_dapm_dac:
943 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +0100944 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +0100945 if (widget->active) {
946 widget->inputs = snd_soc_dapm_suspend_check(widget);
947 return widget->inputs;
948 }
Mark Brown010ff262009-08-17 17:39:22 +0100949 default:
950 break;
951 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200952
953 if (widget->connected) {
954 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100955 if (widget->id == snd_soc_dapm_input && !widget->ext) {
956 widget->inputs = snd_soc_dapm_suspend_check(widget);
957 return widget->inputs;
958 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200959
960 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100961 if (widget->id == snd_soc_dapm_vmid) {
962 widget->inputs = snd_soc_dapm_suspend_check(widget);
963 return widget->inputs;
964 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200965
966 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300967 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100968 (widget->id == snd_soc_dapm_line &&
969 !list_empty(&widget->sinks))) {
970 widget->inputs = snd_soc_dapm_suspend_check(widget);
971 return widget->inputs;
972 }
973
Mark Brown1ab97c82011-11-27 16:21:51 +0000974 /* signal generator */
975 if (widget->id == snd_soc_dapm_siggen) {
976 widget->inputs = snd_soc_dapm_suspend_check(widget);
977 return widget->inputs;
978 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200979 }
980
981 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +0100982 DAPM_UPDATE_STAT(widget, neighbour_checks);
983
Mark Brownbf3a9e12011-06-13 16:42:29 +0100984 if (path->weak)
985 continue;
986
Mark Brown8af294b2013-02-22 17:48:15 +0000987 if (path->walking)
988 return 1;
989
Richard Purdie2b97eab2006-10-06 18:32:18 +0200990 if (path->walked)
991 continue;
992
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100993 trace_snd_soc_dapm_input_path(widget, path);
994
Richard Purdie2b97eab2006-10-06 18:32:18 +0200995 if (path->source && path->connect) {
996 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000997 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100998
999 /* do we need to add this widget to the list ? */
1000 if (list) {
1001 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001002 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001003 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001004 dev_err(widget->dapm->dev,
1005 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001006 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001007 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001008 return con;
1009 }
1010 }
1011
1012 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001013
1014 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001015 }
1016 }
1017
Mark Brown024dc072011-10-09 11:52:05 +01001018 widget->inputs = con;
1019
Richard Purdie2b97eab2006-10-06 18:32:18 +02001020 return con;
1021}
1022
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001023/**
1024 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1025 * @dai: the soc DAI.
1026 * @stream: stream direction.
1027 * @list: list of active widgets for this stream.
1028 *
1029 * Queries DAPM graph as to whether an valid audio stream path exists for
1030 * the initial stream specified by name. This takes into account
1031 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1032 *
1033 * Returns the number of valid paths or negative error.
1034 */
1035int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1036 struct snd_soc_dapm_widget_list **list)
1037{
1038 struct snd_soc_card *card = dai->card;
1039 int paths;
1040
1041 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1042 dapm_reset(card);
1043
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001044 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001045 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001046 dapm_clear_walk_output(&card->dapm,
1047 &dai->playback_widget->sinks);
1048 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001049 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001050 dapm_clear_walk_input(&card->dapm,
1051 &dai->capture_widget->sources);
1052 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001053
1054 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001055 mutex_unlock(&card->dapm_mutex);
1056
1057 return paths;
1058}
1059
Richard Purdie2b97eab2006-10-06 18:32:18 +02001060/*
Mark Brown62ea8742012-01-21 21:14:48 +00001061 * Handler for regulator supply widget.
1062 */
1063int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1064 struct snd_kcontrol *kcontrol, int event)
1065{
Mark Brownc05b84d2012-09-07 12:57:11 +08001066 int ret;
1067
Mark Browneb270e92013-10-09 13:52:52 +01001068 soc_dapm_async_complete(w->dapm);
1069
Mark Brownc05b84d2012-09-07 12:57:11 +08001070 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001071 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001072 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001073 if (ret != 0)
1074 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001075 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001076 w->name, ret);
1077 }
1078
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001079 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001080 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001081 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001082 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001083 if (ret != 0)
1084 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001085 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001086 w->name, ret);
1087 }
1088
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001089 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001090 }
Mark Brown62ea8742012-01-21 21:14:48 +00001091}
1092EXPORT_SYMBOL_GPL(dapm_regulator_event);
1093
Ola Liljad7e7eb92012-05-24 15:26:25 +02001094/*
1095 * Handler for clock supply widget.
1096 */
1097int dapm_clock_event(struct snd_soc_dapm_widget *w,
1098 struct snd_kcontrol *kcontrol, int event)
1099{
1100 if (!w->clk)
1101 return -EIO;
1102
Mark Browneb270e92013-10-09 13:52:52 +01001103 soc_dapm_async_complete(w->dapm);
1104
Mark Brownec029952012-06-04 08:16:20 +01001105#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001106 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001107 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001108 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001109 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001110 return 0;
1111 }
Mark Brownec029952012-06-04 08:16:20 +01001112#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001113 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001114}
1115EXPORT_SYMBOL_GPL(dapm_clock_event);
1116
Mark Brownd805002b2011-09-28 18:28:23 +01001117static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1118{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001119 if (w->power_checked)
1120 return w->new_power;
1121
Mark Brownd805002b2011-09-28 18:28:23 +01001122 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001123 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +01001124 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001125 w->new_power = w->power_check(w);
1126
1127 w->power_checked = true;
1128
1129 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +01001130}
1131
Mark Browncd0f2d42009-04-20 16:56:59 +01001132/* Generic check to see if a widget should be powered.
1133 */
1134static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1135{
1136 int in, out;
1137
Mark Brownde02d072011-09-20 21:43:24 +01001138 DAPM_UPDATE_STAT(w, power_checks);
1139
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001140 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001141 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001142 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001143 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001144 return out != 0 && in != 0;
1145}
1146
Mark Brown6ea31b92009-04-20 17:15:41 +01001147/* Check to see if an ADC has power */
1148static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1149{
1150 int in;
1151
Mark Brownde02d072011-09-20 21:43:24 +01001152 DAPM_UPDATE_STAT(w, power_checks);
1153
Mark Brown6ea31b92009-04-20 17:15:41 +01001154 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001155 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001156 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001157 return in != 0;
1158 } else {
1159 return dapm_generic_check_power(w);
1160 }
1161}
1162
1163/* Check to see if a DAC has power */
1164static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1165{
1166 int out;
1167
Mark Brownde02d072011-09-20 21:43:24 +01001168 DAPM_UPDATE_STAT(w, power_checks);
1169
Mark Brown6ea31b92009-04-20 17:15:41 +01001170 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001171 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001172 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001173 return out != 0;
1174 } else {
1175 return dapm_generic_check_power(w);
1176 }
1177}
1178
Mark Brown246d0a12009-04-22 18:24:55 +01001179/* Check to see if a power supply is needed */
1180static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1181{
1182 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001183
Mark Brownde02d072011-09-20 21:43:24 +01001184 DAPM_UPDATE_STAT(w, power_checks);
1185
Mark Brown246d0a12009-04-22 18:24:55 +01001186 /* Check if one of our outputs is connected */
1187 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001188 DAPM_UPDATE_STAT(w, neighbour_checks);
1189
Mark Brownbf3a9e12011-06-13 16:42:29 +01001190 if (path->weak)
1191 continue;
1192
Mark Brown215edda2009-09-08 18:59:05 +01001193 if (path->connected &&
1194 !path->connected(path->source, path->sink))
1195 continue;
1196
Mark Brown30173582011-02-11 11:42:19 +00001197 if (!path->sink)
1198 continue;
1199
Mark Brownf68d7e12011-10-04 22:57:50 +01001200 if (dapm_widget_power_check(path->sink))
1201 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001202 }
1203
Mark Brownf68d7e12011-10-04 22:57:50 +01001204 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001205}
1206
Mark Brown35c64bc2011-09-28 18:23:53 +01001207static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1208{
1209 return 1;
1210}
1211
Mark Brown38357ab2009-06-06 19:03:23 +01001212static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1213 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001214 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001215{
Mark Brown828a8422011-01-15 13:14:30 +00001216 int *sort;
1217
1218 if (power_up)
1219 sort = dapm_up_seq;
1220 else
1221 sort = dapm_down_seq;
1222
Mark Brown38357ab2009-06-06 19:03:23 +01001223 if (sort[a->id] != sort[b->id])
1224 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001225 if (a->subseq != b->subseq) {
1226 if (power_up)
1227 return a->subseq - b->subseq;
1228 else
1229 return b->subseq - a->subseq;
1230 }
Mark Brownb22ead22009-06-07 12:51:26 +01001231 if (a->reg != b->reg)
1232 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001233 if (a->dapm != b->dapm)
1234 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001235
Mark Brown38357ab2009-06-06 19:03:23 +01001236 return 0;
1237}
Mark Brown42aa3412009-03-01 19:21:10 +00001238
Mark Brown38357ab2009-06-06 19:03:23 +01001239/* Insert a widget in order into a DAPM power sequence. */
1240static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1241 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001242 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001243{
1244 struct snd_soc_dapm_widget *w;
1245
1246 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001247 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001248 list_add_tail(&new_widget->power_list, &w->power_list);
1249 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001250 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001251
Mark Brown38357ab2009-06-06 19:03:23 +01001252 list_add_tail(&new_widget->power_list, list);
1253}
Mark Brown42aa3412009-03-01 19:21:10 +00001254
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001255static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001256 struct snd_soc_dapm_widget *w, int event)
1257{
Mark Brown68f89ad2010-11-03 23:51:49 -04001258 const char *ev_name;
1259 int power, ret;
1260
1261 switch (event) {
1262 case SND_SOC_DAPM_PRE_PMU:
1263 ev_name = "PRE_PMU";
1264 power = 1;
1265 break;
1266 case SND_SOC_DAPM_POST_PMU:
1267 ev_name = "POST_PMU";
1268 power = 1;
1269 break;
1270 case SND_SOC_DAPM_PRE_PMD:
1271 ev_name = "PRE_PMD";
1272 power = 0;
1273 break;
1274 case SND_SOC_DAPM_POST_PMD:
1275 ev_name = "POST_PMD";
1276 power = 0;
1277 break;
Mark Brown80114122013-02-25 15:14:19 +00001278 case SND_SOC_DAPM_WILL_PMU:
1279 ev_name = "WILL_PMU";
1280 power = 1;
1281 break;
1282 case SND_SOC_DAPM_WILL_PMD:
1283 ev_name = "WILL_PMD";
1284 power = 0;
1285 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001286 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001287 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001288 return;
1289 }
1290
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001291 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001292 return;
1293
1294 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001295 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001296 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001297 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001298 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001299 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001300 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001301 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001302 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001303 ev_name, w->name, ret);
1304 }
1305}
1306
Mark Brownb22ead22009-06-07 12:51:26 +01001307/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001308static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001309 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001310{
Mark Brown68f89ad2010-11-03 23:51:49 -04001311 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001312 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001313 unsigned int value = 0;
1314 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001315
1316 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1317 power_list)->reg;
1318
1319 list_for_each_entry(w, pending, power_list) {
Takashi Iwaibf4edea2013-11-07 18:38:47 +01001320 WARN_ON(reg != w->reg);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001321 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001322
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001323 mask |= w->mask << w->shift;
1324 if (w->power)
1325 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001326 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001327 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001328
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001329 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001330 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1331 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001332
Mark Brown68f89ad2010-11-03 23:51:49 -04001333 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001334 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1335 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001336 }
1337
Mark Brown81628102009-06-07 13:21:24 +01001338 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001339 /* Any widget will do, they should all be updating the
1340 * same register.
1341 */
1342 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1343 power_list);
1344
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001345 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001346 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001347 value, mask, reg, card->pop_time);
1348 pop_wait(card->pop_time);
Lars-Peter Clausen23d54422014-04-22 13:23:16 +02001349 soc_widget_update_bits(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001350 }
1351
1352 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001353 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1354 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001355 }
Mark Brown42aa3412009-03-01 19:21:10 +00001356}
1357
Mark Brownb22ead22009-06-07 12:51:26 +01001358/* Apply a DAPM power sequence.
1359 *
1360 * We walk over a pre-sorted list of widgets to apply power to. In
1361 * order to minimise the number of writes to the device required
1362 * multiple widgets will be updated in a single write where possible.
1363 * Currently anything that requires more than a single write is not
1364 * handled.
1365 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001366static void dapm_seq_run(struct snd_soc_card *card,
1367 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001368{
1369 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001370 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001371 LIST_HEAD(pending);
1372 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001373 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001374 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001375 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001376 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001377 int *sort;
1378
1379 if (power_up)
1380 sort = dapm_up_seq;
1381 else
1382 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001383
Mark Brownb22ead22009-06-07 12:51:26 +01001384 list_for_each_entry_safe(w, n, list, power_list) {
1385 ret = 0;
1386
1387 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001388 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001389 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001390 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001391 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001392
Mark Brown474b62d2011-01-18 16:14:44 +00001393 if (cur_dapm && cur_dapm->seq_notifier) {
1394 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1395 if (sort[i] == cur_sort)
1396 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001397 i,
1398 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001399 }
1400
Mark Browneb270e92013-10-09 13:52:52 +01001401 if (cur_dapm && w->dapm != cur_dapm)
1402 soc_dapm_async_complete(cur_dapm);
1403
Mark Brownb22ead22009-06-07 12:51:26 +01001404 INIT_LIST_HEAD(&pending);
1405 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001406 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001407 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001408 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001409 }
1410
Mark Brown163cac02009-06-07 10:12:52 +01001411 switch (w->id) {
1412 case snd_soc_dapm_pre:
1413 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001414 list_for_each_entry_safe_continue(w, n, list,
1415 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001416
Mark Brownb22ead22009-06-07 12:51:26 +01001417 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001418 ret = w->event(w,
1419 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001420 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001421 ret = w->event(w,
1422 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001423 break;
1424
1425 case snd_soc_dapm_post:
1426 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001427 list_for_each_entry_safe_continue(w, n, list,
1428 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001429
Mark Brownb22ead22009-06-07 12:51:26 +01001430 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001431 ret = w->event(w,
1432 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001433 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001434 ret = w->event(w,
1435 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001436 break;
1437
Mark Brown163cac02009-06-07 10:12:52 +01001438 default:
Mark Brown81628102009-06-07 13:21:24 +01001439 /* Queue it up for application */
1440 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001441 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001442 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001443 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001444 list_move(&w->power_list, &pending);
1445 break;
Mark Brown163cac02009-06-07 10:12:52 +01001446 }
Mark Brownb22ead22009-06-07 12:51:26 +01001447
1448 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001449 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001450 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001451 }
Mark Brownb22ead22009-06-07 12:51:26 +01001452
1453 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001454 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001455
1456 if (cur_dapm && cur_dapm->seq_notifier) {
1457 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1458 if (sort[i] == cur_sort)
1459 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001460 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001461 }
Mark Browneb270e92013-10-09 13:52:52 +01001462
1463 list_for_each_entry(d, &card->dapm_list, list) {
1464 soc_dapm_async_complete(d);
1465 }
Mark Brown163cac02009-06-07 10:12:52 +01001466}
1467
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001468static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001469{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001470 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001471 struct snd_soc_dapm_widget_list *wlist;
1472 struct snd_soc_dapm_widget *w = NULL;
1473 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001474 int ret;
1475
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001476 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001477 return;
1478
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001479 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001480
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001481 for (wi = 0; wi < wlist->num_widgets; wi++) {
1482 w = wlist->widgets[wi];
1483
1484 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1485 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1486 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001487 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001488 w->name, ret);
1489 }
Mark Brown97404f22010-12-14 16:13:57 +00001490 }
1491
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001492 if (!w)
1493 return;
1494
Lars-Peter Clausen23d54422014-04-22 13:23:16 +02001495 ret = soc_widget_update_bits(w, update->reg, update->mask, update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001496 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001497 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001498 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001499
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001500 for (wi = 0; wi < wlist->num_widgets; wi++) {
1501 w = wlist->widgets[wi];
1502
1503 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1504 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1505 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001506 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001507 w->name, ret);
1508 }
Mark Brown97404f22010-12-14 16:13:57 +00001509 }
1510}
1511
Mark Brown9d0624a2011-02-18 11:49:43 -08001512/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1513 * they're changing state.
1514 */
1515static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1516{
1517 struct snd_soc_dapm_context *d = data;
1518 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001519
Mark Brown56fba412011-06-04 11:25:10 +01001520 /* If we're off and we're not supposed to be go into STANDBY */
1521 if (d->bias_level == SND_SOC_BIAS_OFF &&
1522 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001523 if (d->dev)
1524 pm_runtime_get_sync(d->dev);
1525
Mark Brown9d0624a2011-02-18 11:49:43 -08001526 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1527 if (ret != 0)
1528 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001529 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001530 }
1531
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001532 /* Prepare for a transition to ON or away from ON */
1533 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1534 d->bias_level != SND_SOC_BIAS_ON) ||
1535 (d->target_bias_level != SND_SOC_BIAS_ON &&
1536 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001537 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1538 if (ret != 0)
1539 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001540 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001541 }
1542}
1543
1544/* Async callback run prior to DAPM sequences - brings to their final
1545 * state.
1546 */
1547static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1548{
1549 struct snd_soc_dapm_context *d = data;
1550 int ret;
1551
1552 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001553 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1554 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1555 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001556 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1557 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001558 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001559 ret);
1560 }
1561
1562 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001563 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1564 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001565 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1566 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001567 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1568 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001569
1570 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001571 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001572 }
1573
1574 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001575 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1576 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001577 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1578 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001579 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001580 ret);
1581 }
1582}
Mark Brown97404f22010-12-14 16:13:57 +00001583
Mark Brownfe4fda52011-10-03 22:36:57 +01001584static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1585 bool power, bool connect)
1586{
1587 /* If a connection is being made or broken then that update
1588 * will have marked the peer dirty, otherwise the widgets are
1589 * not connected and this update has no impact. */
1590 if (!connect)
1591 return;
1592
1593 /* If the peer is already in the state we're moving to then we
1594 * won't have an impact on it. */
1595 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001596 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001597}
1598
Mark Brown05623c42011-09-28 17:02:31 +01001599static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1600 struct list_head *up_list,
1601 struct list_head *down_list)
1602{
Mark Browndb432b42011-10-03 21:06:40 +01001603 struct snd_soc_dapm_path *path;
1604
Mark Brown05623c42011-09-28 17:02:31 +01001605 if (w->power == power)
1606 return;
1607
1608 trace_snd_soc_dapm_widget_power(w, power);
1609
Mark Browndb432b42011-10-03 21:06:40 +01001610 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001611 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001612 */
1613 list_for_each_entry(path, &w->sources, list_sink) {
1614 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001615 dapm_widget_set_peer_power(path->source, power,
1616 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001617 }
1618 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001619 switch (w->id) {
1620 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001621 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001622 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001623 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001624 /* Supplies can't affect their outputs, only their inputs */
1625 break;
1626 default:
1627 list_for_each_entry(path, &w->sinks, list_source) {
1628 if (path->sink) {
1629 dapm_widget_set_peer_power(path->sink, power,
1630 path->connect);
1631 }
Mark Browndb432b42011-10-03 21:06:40 +01001632 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001633 break;
Mark Browndb432b42011-10-03 21:06:40 +01001634 }
1635
Mark Brown05623c42011-09-28 17:02:31 +01001636 if (power)
1637 dapm_seq_insert(w, up_list, true);
1638 else
1639 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001640}
1641
Mark Brown7c81beb2011-09-20 22:22:32 +01001642static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1643 struct list_head *up_list,
1644 struct list_head *down_list)
1645{
Mark Brown7c81beb2011-09-20 22:22:32 +01001646 int power;
1647
1648 switch (w->id) {
1649 case snd_soc_dapm_pre:
1650 dapm_seq_insert(w, down_list, false);
1651 break;
1652 case snd_soc_dapm_post:
1653 dapm_seq_insert(w, up_list, true);
1654 break;
1655
1656 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001657 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001658
Mark Brown05623c42011-09-28 17:02:31 +01001659 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001660 break;
1661 }
1662}
1663
Mark Brown42aa3412009-03-01 19:21:10 +00001664/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001665 * Scan each dapm widget for complete audio path.
1666 * A complete path is a route that has valid endpoints i.e.:-
1667 *
1668 * o DAC to output pin.
1669 * o Input Pin to ADC.
1670 * o Input pin to Output pin (bypass, sidetone)
1671 * o DAC to ADC (loopback).
1672 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001673static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001674{
1675 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001676 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001677 LIST_HEAD(up_list);
1678 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001679 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001680 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001681
Mark Brownf9fa2b12014-03-06 16:49:11 +08001682 lockdep_assert_held(&card->dapm_mutex);
1683
Mark Brown84e90932010-11-04 00:07:02 -04001684 trace_snd_soc_dapm_start(card);
1685
Mark Brown56fba412011-06-04 11:25:10 +01001686 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001687 if (d->idle_bias_off)
1688 d->target_bias_level = SND_SOC_BIAS_OFF;
1689 else
1690 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001691 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001692
Liam Girdwood6c120e12012-02-15 15:15:34 +00001693 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001694
Mark Brown6d3ddc82009-05-16 17:47:29 +01001695 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001696 * lists indicating if they should be powered up or down. We
1697 * only check widgets that have been flagged as dirty but note
1698 * that new widgets may be added to the dirty list while we
1699 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001700 */
Mark Browndb432b42011-10-03 21:06:40 +01001701 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001702 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001703 }
1704
Mark Brownf9de6d72011-09-28 17:19:47 +01001705 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001706 switch (w->id) {
1707 case snd_soc_dapm_pre:
1708 case snd_soc_dapm_post:
1709 /* These widgets always need to be powered */
1710 break;
1711 default:
1712 list_del_init(&w->dirty);
1713 break;
1714 }
Mark Browndb432b42011-10-03 21:06:40 +01001715
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001716 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001717 d = w->dapm;
1718
1719 /* Supplies and micbiases only bring the
1720 * context up to STANDBY as unless something
1721 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001722 * generally don't require full power. Signal
1723 * generators are virtual pins and have no
1724 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001725 */
1726 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001727 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001728 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001729 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001730 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001731 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001732 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001733 case snd_soc_dapm_micbias:
1734 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1735 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1736 break;
1737 default:
1738 d->target_bias_level = SND_SOC_BIAS_ON;
1739 break;
1740 }
1741 }
1742
1743 }
1744
Mark Brown85a843c2011-09-21 21:29:47 +01001745 /* Force all contexts in the card to the same bias state if
1746 * they're not ground referenced.
1747 */
Mark Brown56fba412011-06-04 11:25:10 +01001748 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001749 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001750 if (d->target_bias_level > bias)
1751 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001752 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001753 if (!d->idle_bias_off)
1754 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001755
Mark Brownde02d072011-09-20 21:43:24 +01001756 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001757
Xiang Xiao17282ba2014-03-02 00:04:03 +08001758 /* Run card bias changes at first */
1759 dapm_pre_sequence_async(&card->dapm, 0);
1760 /* Run other bias changes in parallel */
1761 list_for_each_entry(d, &card->dapm_list, list) {
1762 if (d != &card->dapm)
1763 async_schedule_domain(dapm_pre_sequence_async, d,
1764 &async_domain);
1765 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001766 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001767
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001768 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001769 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001770 }
1771
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001772 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001773 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001774 }
1775
Mark Brown6d3ddc82009-05-16 17:47:29 +01001776 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001777 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001778
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001779 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001780
Mark Brown6d3ddc82009-05-16 17:47:29 +01001781 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001782 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001783
Mark Brown9d0624a2011-02-18 11:49:43 -08001784 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001785 list_for_each_entry(d, &card->dapm_list, list) {
1786 if (d != &card->dapm)
1787 async_schedule_domain(dapm_post_sequence_async, d,
1788 &async_domain);
1789 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001790 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001791 /* Run card bias changes at last */
1792 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001793
Liam Girdwood8078d872012-02-15 15:15:35 +00001794 /* do we need to notify any clients that DAPM event is complete */
1795 list_for_each_entry(d, &card->dapm_list, list) {
1796 if (d->stream_event)
1797 d->stream_event(d, event);
1798 }
1799
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001800 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001801 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001802 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001803
Mark Brown84e90932010-11-04 00:07:02 -04001804 trace_snd_soc_dapm_done(card);
1805
Mark Brown42aa3412009-03-01 19:21:10 +00001806 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001807}
1808
Mark Brown79fb9382009-08-21 16:38:13 +01001809#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001810static ssize_t dapm_widget_power_read_file(struct file *file,
1811 char __user *user_buf,
1812 size_t count, loff_t *ppos)
1813{
1814 struct snd_soc_dapm_widget *w = file->private_data;
1815 char *buf;
1816 int in, out;
1817 ssize_t ret;
1818 struct snd_soc_dapm_path *p = NULL;
1819
1820 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1821 if (!buf)
1822 return -ENOMEM;
1823
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001824 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001825 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001826 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001827 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001828
Mark Brownf13ebad2012-03-03 18:01:01 +00001829 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1830 w->name, w->power ? "On" : "Off",
1831 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001832
Mark Brownd033c362009-12-04 15:25:56 +00001833 if (w->reg >= 0)
1834 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001835 " - R%d(0x%x) mask 0x%x",
1836 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001837
1838 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1839
Mark Brown3eef08b2009-09-14 16:49:00 +01001840 if (w->sname)
1841 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1842 w->sname,
1843 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001844
1845 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001846 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001847 continue;
1848
Mark Brown79fb9382009-08-21 16:38:13 +01001849 if (p->connect)
1850 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001851 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001852 p->name ? p->name : "static",
1853 p->source->name);
1854 }
1855 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001856 if (p->connected && !p->connected(w, p->sink))
1857 continue;
1858
Mark Brown79fb9382009-08-21 16:38:13 +01001859 if (p->connect)
1860 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001861 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001862 p->name ? p->name : "static",
1863 p->sink->name);
1864 }
1865
1866 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1867
1868 kfree(buf);
1869 return ret;
1870}
1871
1872static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001873 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001874 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001875 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001876};
1877
Mark Brownef49e4f2011-04-04 20:48:13 +09001878static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1879 size_t count, loff_t *ppos)
1880{
1881 struct snd_soc_dapm_context *dapm = file->private_data;
1882 char *level;
1883
1884 switch (dapm->bias_level) {
1885 case SND_SOC_BIAS_ON:
1886 level = "On\n";
1887 break;
1888 case SND_SOC_BIAS_PREPARE:
1889 level = "Prepare\n";
1890 break;
1891 case SND_SOC_BIAS_STANDBY:
1892 level = "Standby\n";
1893 break;
1894 case SND_SOC_BIAS_OFF:
1895 level = "Off\n";
1896 break;
1897 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001898 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09001899 level = "Unknown\n";
1900 break;
1901 }
1902
1903 return simple_read_from_buffer(user_buf, count, ppos, level,
1904 strlen(level));
1905}
1906
1907static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001908 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001909 .read = dapm_bias_read_file,
1910 .llseek = default_llseek,
1911};
1912
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001913void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1914 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001915{
Mark Brown79fb9382009-08-21 16:38:13 +01001916 struct dentry *d;
1917
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001918 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1919
1920 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001921 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001922 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001923 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001924 }
Mark Brown79fb9382009-08-21 16:38:13 +01001925
Mark Brownef49e4f2011-04-04 20:48:13 +09001926 d = debugfs_create_file("bias_level", 0444,
1927 dapm->debugfs_dapm, dapm,
1928 &dapm_bias_fops);
1929 if (!d)
1930 dev_warn(dapm->dev,
1931 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001932}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001933
1934static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1935{
1936 struct snd_soc_dapm_context *dapm = w->dapm;
1937 struct dentry *d;
1938
1939 if (!dapm->debugfs_dapm || !w->name)
1940 return;
1941
1942 d = debugfs_create_file(w->name, 0444,
1943 dapm->debugfs_dapm, w,
1944 &dapm_widget_power_fops);
1945 if (!d)
1946 dev_warn(w->dapm->dev,
1947 "ASoC: Failed to create %s debugfs file\n",
1948 w->name);
1949}
1950
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001951static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1952{
1953 debugfs_remove_recursive(dapm->debugfs_dapm);
1954}
1955
Mark Brown79fb9382009-08-21 16:38:13 +01001956#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001957void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1958 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001959{
1960}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001961
1962static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1963{
1964}
1965
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001966static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1967{
1968}
1969
Mark Brown79fb9382009-08-21 16:38:13 +01001970#endif
1971
Richard Purdie2b97eab2006-10-06 18:32:18 +02001972/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001973static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001974 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001975{
1976 struct snd_soc_dapm_path *path;
1977 int found = 0;
1978
Mark Brownf9fa2b12014-03-06 16:49:11 +08001979 lockdep_assert_held(&card->dapm_mutex);
1980
Richard Purdie2b97eab2006-10-06 18:32:18 +02001981 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02001982 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001983 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001984 continue;
1985
1986 found = 1;
1987 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001988 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001989 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001990 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001991 } else {
1992 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001993 dapm_mark_dirty(path->source,
1994 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001995 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001996 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001997 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001998 }
1999
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002000 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002001 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002002
Liam Girdwood618dae12012-04-25 12:12:51 +01002003 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002004}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002005
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002006int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002007 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2008 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002009{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002010 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002011 int ret;
2012
Liam Girdwood3cd04342012-03-09 12:02:08 +00002013 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002014 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002015 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002016 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002017 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002018 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002019 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002020 return ret;
2021}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002022EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002023
Milan plzik1b075e32008-01-10 14:39:46 +01002024/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002025static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002026 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002027{
2028 struct snd_soc_dapm_path *path;
2029 int found = 0;
2030
Mark Brownf9fa2b12014-03-06 16:49:11 +08002031 lockdep_assert_held(&card->dapm_mutex);
2032
Richard Purdie2b97eab2006-10-06 18:32:18 +02002033 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002034 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002035 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002036 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002037 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002038 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002039 }
2040
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002041 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002042 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002043
Liam Girdwood618dae12012-04-25 12:12:51 +01002044 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002045}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002046
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002047int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002048 struct snd_kcontrol *kcontrol, int connect,
2049 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002050{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002051 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002052 int ret;
2053
Liam Girdwood3cd04342012-03-09 12:02:08 +00002054 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002055 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002056 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002057 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002058 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002059 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002060 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002061 return ret;
2062}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002063EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002064
2065/* show dapm widget status in sys fs */
2066static ssize_t dapm_widget_show(struct device *dev,
2067 struct device_attribute *attr, char *buf)
2068{
Mark Brown36ae1a92012-01-06 17:12:45 -08002069 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002070 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002071 struct snd_soc_dapm_widget *w;
2072 int count = 0;
2073 char *state = "not set";
2074
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002075 list_for_each_entry(w, &codec->card->widgets, list) {
2076 if (w->dapm != &codec->dapm)
2077 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002078
2079 /* only display widgets that burnm power */
2080 switch (w->id) {
2081 case snd_soc_dapm_hp:
2082 case snd_soc_dapm_mic:
2083 case snd_soc_dapm_spk:
2084 case snd_soc_dapm_line:
2085 case snd_soc_dapm_micbias:
2086 case snd_soc_dapm_dac:
2087 case snd_soc_dapm_adc:
2088 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002089 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002090 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002091 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002092 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002093 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002094 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002095 if (w->name)
2096 count += sprintf(buf + count, "%s: %s\n",
2097 w->name, w->power ? "On":"Off");
2098 break;
2099 default:
2100 break;
2101 }
2102 }
2103
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002104 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002105 case SND_SOC_BIAS_ON:
2106 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002107 break;
Mark Brown0be98982008-05-19 12:31:28 +02002108 case SND_SOC_BIAS_PREPARE:
2109 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002110 break;
Mark Brown0be98982008-05-19 12:31:28 +02002111 case SND_SOC_BIAS_STANDBY:
2112 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002113 break;
Mark Brown0be98982008-05-19 12:31:28 +02002114 case SND_SOC_BIAS_OFF:
2115 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002116 break;
2117 }
2118 count += sprintf(buf + count, "PM State: %s\n", state);
2119
2120 return count;
2121}
2122
2123static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2124
2125int snd_soc_dapm_sys_add(struct device *dev)
2126{
Troy Kisky12ef1932008-10-13 17:42:14 -07002127 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002128}
2129
2130static void snd_soc_dapm_sys_remove(struct device *dev)
2131{
Mark Brownaef90842009-05-16 17:53:16 +01002132 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002133}
2134
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002135static void dapm_free_path(struct snd_soc_dapm_path *path)
2136{
2137 list_del(&path->list_sink);
2138 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002139 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002140 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002141 kfree(path);
2142}
2143
Richard Purdie2b97eab2006-10-06 18:32:18 +02002144/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002145static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002146{
2147 struct snd_soc_dapm_widget *w, *next_w;
2148 struct snd_soc_dapm_path *p, *next_p;
2149
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002150 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2151 if (w->dapm != dapm)
2152 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002153 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002154 /*
2155 * remove source and sink paths associated to this widget.
2156 * While removing the path, remove reference to it from both
2157 * source and sink widgets so that path is removed only once.
2158 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002159 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2160 dapm_free_path(p);
2161
2162 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2163 dapm_free_path(p);
2164
Stephen Warrenfad59882011-04-28 17:37:59 -06002165 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002166 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002167 kfree(w);
2168 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002169}
2170
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002171static struct snd_soc_dapm_widget *dapm_find_widget(
2172 struct snd_soc_dapm_context *dapm, const char *pin,
2173 bool search_other_contexts)
2174{
2175 struct snd_soc_dapm_widget *w;
2176 struct snd_soc_dapm_widget *fallback = NULL;
2177
2178 list_for_each_entry(w, &dapm->card->widgets, list) {
2179 if (!strcmp(w->name, pin)) {
2180 if (w->dapm == dapm)
2181 return w;
2182 else
2183 fallback = w;
2184 }
2185 }
2186
2187 if (search_other_contexts)
2188 return fallback;
2189
2190 return NULL;
2191}
2192
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002193static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002194 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002195{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002196 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002197
Mark Brownf9fa2b12014-03-06 16:49:11 +08002198 dapm_assert_locked(dapm);
2199
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002200 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002201 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002202 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002203 }
2204
Mark Brown1a8b2d92012-02-16 11:50:07 -08002205 if (w->connected != status)
2206 dapm_mark_dirty(w, "pin configuration");
2207
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002208 w->connected = status;
2209 if (status == 0)
2210 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002211
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002212 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002213}
2214
Richard Purdie2b97eab2006-10-06 18:32:18 +02002215/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002216 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2217 * @dapm: DAPM context
2218 *
2219 * Walks all dapm audio paths and powers widgets according to their
2220 * stream or path usage.
2221 *
2222 * Requires external locking.
2223 *
2224 * Returns 0 for success.
2225 */
2226int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2227{
2228 /*
2229 * Suppress early reports (eg, jacks syncing their state) to avoid
2230 * silly DAPM runs during card startup.
2231 */
2232 if (!dapm->card || !dapm->card->instantiated)
2233 return 0;
2234
2235 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2236}
2237EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2238
2239/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002240 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002241 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002242 *
2243 * Walks all dapm audio paths and powers widgets according to their
2244 * stream or path usage.
2245 *
2246 * Returns 0 for success.
2247 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002248int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002249{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002250 int ret;
2251
Liam Girdwood3cd04342012-03-09 12:02:08 +00002252 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002253 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002254 mutex_unlock(&dapm->card->dapm_mutex);
2255 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002256}
Liam Girdwooda5302182008-07-07 13:35:17 +01002257EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002258
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002259static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2260 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2261 const char *control,
2262 int (*connected)(struct snd_soc_dapm_widget *source,
2263 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002264{
2265 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002266 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002267
2268 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2269 if (!path)
2270 return -ENOMEM;
2271
2272 path->source = wsource;
2273 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002274 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002275 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002276 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002277 INIT_LIST_HEAD(&path->list_source);
2278 INIT_LIST_HEAD(&path->list_sink);
2279
2280 /* check for external widgets */
2281 if (wsink->id == snd_soc_dapm_input) {
2282 if (wsource->id == snd_soc_dapm_micbias ||
2283 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002284 wsource->id == snd_soc_dapm_line ||
2285 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002286 wsink->ext = 1;
2287 }
2288 if (wsource->id == snd_soc_dapm_output) {
2289 if (wsink->id == snd_soc_dapm_spk ||
2290 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002291 wsink->id == snd_soc_dapm_line ||
2292 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002293 wsource->ext = 1;
2294 }
2295
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002296 dapm_mark_dirty(wsource, "Route added");
2297 dapm_mark_dirty(wsink, "Route added");
2298
Richard Purdie2b97eab2006-10-06 18:32:18 +02002299 /* connect static paths */
2300 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002301 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002302 list_add(&path->list_sink, &wsink->sources);
2303 list_add(&path->list_source, &wsource->sinks);
2304 path->connect = 1;
2305 return 0;
2306 }
2307
2308 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002309 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002310 case snd_soc_dapm_adc:
2311 case snd_soc_dapm_dac:
2312 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002313 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002314 case snd_soc_dapm_input:
2315 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002316 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002317 case snd_soc_dapm_micbias:
2318 case snd_soc_dapm_vmid:
2319 case snd_soc_dapm_pre:
2320 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002321 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002322 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002323 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002324 case snd_soc_dapm_aif_in:
2325 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002326 case snd_soc_dapm_dai_in:
2327 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002328 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002329 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002330 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002331 list_add(&path->list_sink, &wsink->sources);
2332 list_add(&path->list_source, &wsource->sinks);
2333 path->connect = 1;
2334 return 0;
2335 case snd_soc_dapm_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002336 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002337 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002338 if (ret != 0)
2339 goto err;
2340 break;
2341 case snd_soc_dapm_switch:
2342 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002343 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002344 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002345 if (ret != 0)
2346 goto err;
2347 break;
2348 case snd_soc_dapm_hp:
2349 case snd_soc_dapm_mic:
2350 case snd_soc_dapm_line:
2351 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002352 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002353 list_add(&path->list_sink, &wsink->sources);
2354 list_add(&path->list_source, &wsource->sinks);
2355 path->connect = 0;
2356 return 0;
2357 }
Mark Brownfabd0382012-07-05 17:20:06 +01002358
Richard Purdie2b97eab2006-10-06 18:32:18 +02002359 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002360err:
2361 kfree(path);
2362 return ret;
2363}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002364
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002365static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002366 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002367{
2368 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2369 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2370 const char *sink;
2371 const char *source;
2372 char prefixed_sink[80];
2373 char prefixed_source[80];
2374 int ret;
2375
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002376 if (dapm->codec && dapm->codec->name_prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002377 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2378 dapm->codec->name_prefix, route->sink);
2379 sink = prefixed_sink;
2380 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2381 dapm->codec->name_prefix, route->source);
2382 source = prefixed_source;
2383 } else {
2384 sink = route->sink;
2385 source = route->source;
2386 }
2387
2388 /*
2389 * find src and dest widgets over all widgets but favor a widget from
2390 * current DAPM context
2391 */
2392 list_for_each_entry(w, &dapm->card->widgets, list) {
2393 if (!wsink && !(strcmp(w->name, sink))) {
2394 wtsink = w;
2395 if (w->dapm == dapm)
2396 wsink = w;
2397 continue;
2398 }
2399 if (!wsource && !(strcmp(w->name, source))) {
2400 wtsource = w;
2401 if (w->dapm == dapm)
2402 wsource = w;
2403 }
2404 }
2405 /* use widget from another DAPM context if not found from this */
2406 if (!wsink)
2407 wsink = wtsink;
2408 if (!wsource)
2409 wsource = wtsource;
2410
2411 if (wsource == NULL) {
2412 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2413 route->source);
2414 return -ENODEV;
2415 }
2416 if (wsink == NULL) {
2417 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2418 route->sink);
2419 return -ENODEV;
2420 }
2421
2422 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2423 route->connected);
2424 if (ret)
2425 goto err;
2426
2427 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002428err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002429 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002430 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002431 return ret;
2432}
Mark Brown105f1c22008-05-13 14:52:19 +02002433
Mark Brownefcc3c62012-07-05 17:24:19 +01002434static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2435 const struct snd_soc_dapm_route *route)
2436{
2437 struct snd_soc_dapm_path *path, *p;
2438 const char *sink;
2439 const char *source;
2440 char prefixed_sink[80];
2441 char prefixed_source[80];
2442
2443 if (route->control) {
2444 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002445 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002446 return -EINVAL;
2447 }
2448
2449 if (dapm->codec && dapm->codec->name_prefix) {
2450 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2451 dapm->codec->name_prefix, route->sink);
2452 sink = prefixed_sink;
2453 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2454 dapm->codec->name_prefix, route->source);
2455 source = prefixed_source;
2456 } else {
2457 sink = route->sink;
2458 source = route->source;
2459 }
2460
2461 path = NULL;
2462 list_for_each_entry(p, &dapm->card->paths, list) {
2463 if (strcmp(p->source->name, source) != 0)
2464 continue;
2465 if (strcmp(p->sink->name, sink) != 0)
2466 continue;
2467 path = p;
2468 break;
2469 }
2470
2471 if (path) {
2472 dapm_mark_dirty(path->source, "Route removed");
2473 dapm_mark_dirty(path->sink, "Route removed");
2474
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002475 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002476 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002477 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002478 source, sink);
2479 }
2480
2481 return 0;
2482}
2483
Mark Brown105f1c22008-05-13 14:52:19 +02002484/**
Mark Brown105f1c22008-05-13 14:52:19 +02002485 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002486 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002487 * @route: audio routes
2488 * @num: number of routes
2489 *
2490 * Connects 2 dapm widgets together via a named audio path. The sink is
2491 * the widget receiving the audio signal, whilst the source is the sender
2492 * of the audio signal.
2493 *
2494 * Returns 0 for success else error. On error all resources can be freed
2495 * with a call to snd_soc_card_free().
2496 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002497int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002498 const struct snd_soc_dapm_route *route, int num)
2499{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002500 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002501
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002502 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002503 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002504 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002505 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002506 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2507 route->source,
2508 route->control ? route->control : "direct",
2509 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002510 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002511 }
2512 route++;
2513 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002514 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002515
Dan Carpenter60884c22012-04-13 22:25:43 +03002516 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002517}
2518EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2519
Mark Brownefcc3c62012-07-05 17:24:19 +01002520/**
2521 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2522 * @dapm: DAPM context
2523 * @route: audio routes
2524 * @num: number of routes
2525 *
2526 * Removes routes from the DAPM context.
2527 */
2528int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2529 const struct snd_soc_dapm_route *route, int num)
2530{
2531 int i, ret = 0;
2532
2533 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2534 for (i = 0; i < num; i++) {
2535 snd_soc_dapm_del_route(dapm, route);
2536 route++;
2537 }
2538 mutex_unlock(&dapm->card->dapm_mutex);
2539
2540 return ret;
2541}
2542EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2543
Mark Brownbf3a9e12011-06-13 16:42:29 +01002544static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2545 const struct snd_soc_dapm_route *route)
2546{
2547 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2548 route->source,
2549 true);
2550 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2551 route->sink,
2552 true);
2553 struct snd_soc_dapm_path *path;
2554 int count = 0;
2555
2556 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002557 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002558 route->source);
2559 return -ENODEV;
2560 }
2561
2562 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002563 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002564 route->sink);
2565 return -ENODEV;
2566 }
2567
2568 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002569 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002570 route->source, route->sink);
2571
2572 list_for_each_entry(path, &source->sinks, list_source) {
2573 if (path->sink == sink) {
2574 path->weak = 1;
2575 count++;
2576 }
2577 }
2578
2579 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002580 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002581 route->source, route->sink);
2582 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002583 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002584 count, route->source, route->sink);
2585
2586 return 0;
2587}
2588
2589/**
2590 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2591 * @dapm: DAPM context
2592 * @route: audio routes
2593 * @num: number of routes
2594 *
2595 * Mark existing routes matching those specified in the passed array
2596 * as being weak, meaning that they are ignored for the purpose of
2597 * power decisions. The main intended use case is for sidetone paths
2598 * which couple audio between other independent paths if they are both
2599 * active in order to make the combination work better at the user
2600 * level but which aren't intended to be "used".
2601 *
2602 * Note that CODEC drivers should not use this as sidetone type paths
2603 * can frequently also be used as bypass paths.
2604 */
2605int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2606 const struct snd_soc_dapm_route *route, int num)
2607{
2608 int i, err;
2609 int ret = 0;
2610
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002611 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002612 for (i = 0; i < num; i++) {
2613 err = snd_soc_dapm_weak_route(dapm, route);
2614 if (err)
2615 ret = err;
2616 route++;
2617 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002618 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002619
2620 return ret;
2621}
2622EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2623
Mark Brown105f1c22008-05-13 14:52:19 +02002624/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002625 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002626 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002627 *
2628 * Checks the codec for any new dapm widgets and creates them if found.
2629 *
2630 * Returns 0 for success.
2631 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002632int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002633{
2634 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002635 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002636
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002637 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002638
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002639 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002640 {
2641 if (w->new)
2642 continue;
2643
Stephen Warrenfad59882011-04-28 17:37:59 -06002644 if (w->num_kcontrols) {
2645 w->kcontrols = kzalloc(w->num_kcontrols *
2646 sizeof(struct snd_kcontrol *),
2647 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002648 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002649 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002650 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002651 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002652 }
2653
Richard Purdie2b97eab2006-10-06 18:32:18 +02002654 switch(w->id) {
2655 case snd_soc_dapm_switch:
2656 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002657 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002658 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002659 break;
2660 case snd_soc_dapm_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002661 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002662 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002663 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002664 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002665 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002666 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002667 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002668 break;
2669 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002670
2671 /* Read the initial power state from the device */
2672 if (w->reg >= 0) {
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002673 soc_widget_read(w, w->reg, &val);
2674 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002675 val &= w->mask;
2676 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002677 w->power = 1;
2678 }
2679
Richard Purdie2b97eab2006-10-06 18:32:18 +02002680 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002681
Mark Brown7508b122011-10-05 12:09:12 +01002682 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002683 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002684 }
2685
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002686 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2687 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002688 return 0;
2689}
2690EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2691
2692/**
2693 * snd_soc_dapm_get_volsw - dapm mixer get callback
2694 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002695 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002696 *
2697 * Callback to get the value of a dapm mixer control.
2698 *
2699 * Returns 0 for success.
2700 */
2701int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2702 struct snd_ctl_elem_value *ucontrol)
2703{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002704 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002705 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002706 struct soc_mixer_control *mc =
2707 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002708 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002709 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002710 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002711 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002712 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002713 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002714
2715 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002716 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002717 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002718 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002719
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002720 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002721 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002722 val = (snd_soc_read(codec, reg) >> shift) & mask;
2723 else
2724 val = dapm_kcontrol_get_value(kcontrol);
2725 mutex_unlock(&card->dapm_mutex);
2726
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002727 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002728 ucontrol->value.integer.value[0] = max - val;
2729 else
2730 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002731
2732 return 0;
2733}
2734EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2735
2736/**
2737 * snd_soc_dapm_put_volsw - dapm mixer set callback
2738 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002739 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002740 *
2741 * Callback to set the value of a dapm mixer control.
2742 *
2743 * Returns 0 for success.
2744 */
2745int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2746 struct snd_ctl_elem_value *ucontrol)
2747{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002748 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002749 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002750 struct soc_mixer_control *mc =
2751 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002752 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002753 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002754 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002755 unsigned int mask = (1 << fls(max)) - 1;
2756 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002757 unsigned int val;
Jarkko Nikula18626c72014-06-09 14:20:29 +03002758 int connect, change, reg_change = 0;
Mark Brown97404f22010-12-14 16:13:57 +00002759 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002760 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002761
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002762 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002763 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002764 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002765 kcontrol->id.name);
2766
Richard Purdie2b97eab2006-10-06 18:32:18 +02002767 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002768 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002769
2770 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002771 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002772
Liam Girdwood3cd04342012-03-09 12:02:08 +00002773 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002774
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002775 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown283375c2009-12-07 18:09:03 +00002776
Jarkko Nikula18626c72014-06-09 14:20:29 +03002777 if (reg != SND_SOC_NOPM) {
2778 mask = mask << shift;
2779 val = val << shift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02002780
Jarkko Nikula18626c72014-06-09 14:20:29 +03002781 reg_change = snd_soc_test_bits(codec, reg, mask, val);
2782 }
2783
2784 if (change || reg_change) {
2785 if (reg_change) {
2786 update.kcontrol = kcontrol;
2787 update.reg = reg;
2788 update.mask = mask;
2789 update.val = val;
2790 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002791 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03002792 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00002793
Nenghua Cao52765972013-12-13 20:13:49 +08002794 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002795
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002796 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002797 }
2798
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002799 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002800
2801 if (ret > 0)
2802 soc_dpcm_runtime_update(card);
2803
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002804 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002805}
2806EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2807
2808/**
2809 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2810 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002811 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002812 *
2813 * Callback to get the value of a dapm enumerated double mixer control.
2814 *
2815 * Returns 0 for success.
2816 */
2817int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2818 struct snd_ctl_elem_value *ucontrol)
2819{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002820 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002821 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002822 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002823
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002824 if (e->reg != SND_SOC_NOPM)
2825 reg_val = snd_soc_read(codec, e->reg);
2826 else
2827 reg_val = dapm_kcontrol_get_value(kcontrol);
2828
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002829 val = (reg_val >> e->shift_l) & e->mask;
2830 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2831 if (e->shift_l != e->shift_r) {
2832 val = (reg_val >> e->shift_r) & e->mask;
2833 val = snd_soc_enum_val_to_item(e, val);
2834 ucontrol->value.enumerated.item[1] = val;
2835 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002836
2837 return 0;
2838}
2839EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2840
2841/**
2842 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2843 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002844 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002845 *
2846 * Callback to set the value of a dapm enumerated double mixer control.
2847 *
2848 * Returns 0 for success.
2849 */
2850int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2851 struct snd_ctl_elem_value *ucontrol)
2852{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002853 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002854 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002855 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002856 unsigned int *item = ucontrol->value.enumerated.item;
2857 unsigned int val, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002858 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002859 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002860 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002861
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002862 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002863 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002864
2865 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002866 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002867 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002868 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002869 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002870 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002871 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002872 }
2873
Liam Girdwood3cd04342012-03-09 12:02:08 +00002874 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002875
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002876 if (e->reg != SND_SOC_NOPM)
2877 change = snd_soc_test_bits(codec, e->reg, mask, val);
2878 else
2879 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002880
Richard Purdie2b97eab2006-10-06 18:32:18 +02002881 if (change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002882 if (e->reg != SND_SOC_NOPM) {
2883 update.kcontrol = kcontrol;
2884 update.reg = e->reg;
2885 update.mask = mask;
2886 update.val = val;
2887 card->update = &update;
2888 }
Mark Brown3a655772009-10-05 17:23:30 +01002889
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002890 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002891
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002892 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002893 }
2894
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002895 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002896
2897 if (ret > 0)
2898 soc_dpcm_runtime_update(card);
2899
Mark Brown97404f22010-12-14 16:13:57 +00002900 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002901}
2902EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2903
2904/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002905 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2906 *
2907 * @kcontrol: mixer control
2908 * @uinfo: control element information
2909 *
2910 * Callback to provide information about a pin switch control.
2911 */
2912int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2913 struct snd_ctl_elem_info *uinfo)
2914{
2915 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2916 uinfo->count = 1;
2917 uinfo->value.integer.min = 0;
2918 uinfo->value.integer.max = 1;
2919
2920 return 0;
2921}
2922EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2923
2924/**
2925 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2926 *
2927 * @kcontrol: mixer control
2928 * @ucontrol: Value
2929 */
2930int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2931 struct snd_ctl_elem_value *ucontrol)
2932{
Mark Brown48a8c392012-02-14 17:11:15 -08002933 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002934 const char *pin = (const char *)kcontrol->private_value;
2935
Liam Girdwood3cd04342012-03-09 12:02:08 +00002936 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002937
2938 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002939 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002940
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002941 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002942
2943 return 0;
2944}
2945EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2946
2947/**
2948 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2949 *
2950 * @kcontrol: mixer control
2951 * @ucontrol: Value
2952 */
2953int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2954 struct snd_ctl_elem_value *ucontrol)
2955{
Mark Brown48a8c392012-02-14 17:11:15 -08002956 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002957 const char *pin = (const char *)kcontrol->private_value;
2958
Mark Brown8b37dbd2009-02-28 21:14:20 +00002959 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002960 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002961 else
Mark Brown48a8c392012-02-14 17:11:15 -08002962 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002963
Mark Brown48a8c392012-02-14 17:11:15 -08002964 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002965 return 0;
2966}
2967EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2968
Mark Brown5ba06fc2012-02-16 11:07:13 -08002969static struct snd_soc_dapm_widget *
2970snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2971 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002972{
2973 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00002974 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002975
2976 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08002977 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002978
Mark Brown62ea8742012-01-21 21:14:48 +00002979 switch (w->id) {
2980 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00002981 w->regulator = devm_regulator_get(dapm->dev, w->name);
2982 if (IS_ERR(w->regulator)) {
2983 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002984 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00002985 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002986 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00002987 }
Mark Brown8784c772013-01-10 19:33:47 +00002988
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002989 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00002990 ret = regulator_allow_bypass(w->regulator, true);
2991 if (ret != 0)
2992 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00002993 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00002994 w->name, ret);
2995 }
Mark Brown62ea8742012-01-21 21:14:48 +00002996 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02002997 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01002998#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01002999 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003000 if (IS_ERR(w->clk)) {
3001 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003002 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003003 w->name, ret);
3004 return NULL;
3005 }
Mark Brownec029952012-06-04 08:16:20 +01003006#else
3007 return NULL;
3008#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003009 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003010 default:
3011 break;
3012 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003013
Mark Brown88e8b9a2011-03-02 18:18:24 +00003014 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003015 w->name = kasprintf(GFP_KERNEL, "%s %s",
3016 dapm->codec->name_prefix, widget->name);
3017 else
3018 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3019
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003020 if (w->name == NULL) {
3021 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003022 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003023 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003024
Mark Brown7ca3a182011-10-08 14:04:50 +01003025 switch (w->id) {
3026 case snd_soc_dapm_switch:
3027 case snd_soc_dapm_mixer:
3028 case snd_soc_dapm_mixer_named_ctl:
3029 w->power_check = dapm_generic_check_power;
3030 break;
3031 case snd_soc_dapm_mux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003032 w->power_check = dapm_generic_check_power;
3033 break;
Mark Brown46162742013-06-05 19:36:11 +01003034 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003035 w->power_check = dapm_adc_check_power;
3036 break;
Mark Brown46162742013-06-05 19:36:11 +01003037 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003038 w->power_check = dapm_dac_check_power;
3039 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003040 case snd_soc_dapm_adc:
3041 case snd_soc_dapm_aif_out:
3042 case snd_soc_dapm_dac:
3043 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003044 case snd_soc_dapm_pga:
3045 case snd_soc_dapm_out_drv:
3046 case snd_soc_dapm_input:
3047 case snd_soc_dapm_output:
3048 case snd_soc_dapm_micbias:
3049 case snd_soc_dapm_spk:
3050 case snd_soc_dapm_hp:
3051 case snd_soc_dapm_mic:
3052 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003053 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003054 w->power_check = dapm_generic_check_power;
3055 break;
3056 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003057 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003058 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003059 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003060 w->power_check = dapm_supply_check_power;
3061 break;
3062 default:
3063 w->power_check = dapm_always_on_check_power;
3064 break;
3065 }
3066
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003067 w->dapm = dapm;
3068 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003069 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003070 INIT_LIST_HEAD(&w->sources);
3071 INIT_LIST_HEAD(&w->sinks);
3072 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003073 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003074 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003075
3076 /* machine layer set ups unconnected pins and insertions */
3077 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003078 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003079}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003080
3081/**
Mark Brown4ba13272008-05-13 14:51:19 +02003082 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003083 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003084 * @widget: widget array
3085 * @num: number of widgets
3086 *
3087 * Creates new DAPM controls based upon the templates.
3088 *
3089 * Returns 0 for success else error.
3090 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003091int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003092 const struct snd_soc_dapm_widget *widget,
3093 int num)
3094{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003095 struct snd_soc_dapm_widget *w;
3096 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003097 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003098
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003099 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003100 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003101 w = snd_soc_dapm_new_control(dapm, widget);
3102 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003103 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003104 "ASoC: Failed to create DAPM control %s\n",
3105 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003106 ret = -ENOMEM;
3107 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003108 }
Mark Brown4ba13272008-05-13 14:51:19 +02003109 widget++;
3110 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003111 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003112 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003113}
3114EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3115
Mark Brownc74184e2012-04-04 22:12:09 +01003116static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3117 struct snd_kcontrol *kcontrol, int event)
3118{
3119 struct snd_soc_dapm_path *source_p, *sink_p;
3120 struct snd_soc_dai *source, *sink;
3121 const struct snd_soc_pcm_stream *config = w->params;
3122 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003123 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003124 u64 fmt;
3125 int ret;
3126
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003127 if (WARN_ON(!config) ||
3128 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3129 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003130
3131 /* We only support a single source and sink, pick the first */
3132 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3133 list_sink);
3134 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3135 list_source);
3136
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003137 if (WARN_ON(!source_p || !sink_p) ||
3138 WARN_ON(!sink_p->source || !source_p->sink) ||
3139 WARN_ON(!source_p->source || !sink_p->sink))
3140 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003141
3142 source = source_p->source->priv;
3143 sink = sink_p->sink->priv;
3144
3145 /* Be a little careful as we don't want to overflow the mask array */
3146 if (config->formats) {
3147 fmt = ffs(config->formats) - 1;
3148 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003149 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003150 config->formats);
3151 fmt = 0;
3152 }
3153
3154 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003155 params = kzalloc(sizeof(*params), GFP_KERNEL);
3156 if (!params) {
3157 ret = -ENOMEM;
3158 goto out;
3159 }
3160 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003161
Mark Brown9747cec2012-04-26 19:12:21 +01003162 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003163 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003164 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003165 config->rate_max;
3166
Mark Brown9747cec2012-04-26 19:12:21 +01003167 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003168 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003169 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003170 = config->channels_max;
3171
3172 memset(&substream, 0, sizeof(substream));
3173
3174 switch (event) {
3175 case SND_SOC_DAPM_PRE_PMU:
3176 if (source->driver->ops && source->driver->ops->hw_params) {
3177 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3178 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003179 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003180 if (ret != 0) {
3181 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003182 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003183 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003184 }
3185 }
3186
3187 if (sink->driver->ops && sink->driver->ops->hw_params) {
3188 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003189 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003190 sink);
3191 if (ret != 0) {
3192 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003193 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003194 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003195 }
3196 }
3197 break;
3198
3199 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003200 ret = snd_soc_dai_digital_mute(sink, 0,
3201 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003202 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003203 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003204 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003205 break;
3206
3207 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003208 ret = snd_soc_dai_digital_mute(sink, 1,
3209 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003210 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003211 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003212 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003213 break;
3214
3215 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003216 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003217 return -EINVAL;
3218 }
3219
Mark Brown9747cec2012-04-26 19:12:21 +01003220out:
3221 kfree(params);
3222 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003223}
3224
3225int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3226 const struct snd_soc_pcm_stream *params,
3227 struct snd_soc_dapm_widget *source,
3228 struct snd_soc_dapm_widget *sink)
3229{
Mark Brownc74184e2012-04-04 22:12:09 +01003230 struct snd_soc_dapm_widget template;
3231 struct snd_soc_dapm_widget *w;
3232 size_t len;
3233 char *link_name;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003234 int ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003235
3236 len = strlen(source->name) + strlen(sink->name) + 2;
3237 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3238 if (!link_name)
3239 return -ENOMEM;
3240 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3241
3242 memset(&template, 0, sizeof(template));
3243 template.reg = SND_SOC_NOPM;
3244 template.id = snd_soc_dapm_dai_link;
3245 template.name = link_name;
3246 template.event = snd_soc_dai_link_event;
3247 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3248 SND_SOC_DAPM_PRE_PMD;
3249
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003250 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003251
3252 w = snd_soc_dapm_new_control(&card->dapm, &template);
3253 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003254 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003255 link_name);
3256 return -ENOMEM;
3257 }
3258
3259 w->params = params;
3260
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003261 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3262 if (ret)
3263 return ret;
3264 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Mark Brownc74184e2012-04-04 22:12:09 +01003265}
3266
Mark Brown888df392012-02-16 19:37:51 -08003267int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3268 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003269{
Mark Brown888df392012-02-16 19:37:51 -08003270 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003271 struct snd_soc_dapm_widget *w;
3272
Mark Brown888df392012-02-16 19:37:51 -08003273 WARN_ON(dapm->dev != dai->dev);
3274
3275 memset(&template, 0, sizeof(template));
3276 template.reg = SND_SOC_NOPM;
3277
3278 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003279 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003280 template.name = dai->driver->playback.stream_name;
3281 template.sname = dai->driver->playback.stream_name;
3282
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003283 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003284 template.name);
3285
3286 w = snd_soc_dapm_new_control(dapm, &template);
3287 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003288 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003289 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003290 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003291 }
3292
3293 w->priv = dai;
3294 dai->playback_widget = w;
3295 }
3296
3297 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003298 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003299 template.name = dai->driver->capture.stream_name;
3300 template.sname = dai->driver->capture.stream_name;
3301
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003302 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003303 template.name);
3304
3305 w = snd_soc_dapm_new_control(dapm, &template);
3306 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003307 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003308 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003309 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003310 }
3311
3312 w->priv = dai;
3313 dai->capture_widget = w;
3314 }
3315
3316 return 0;
3317}
3318
3319int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3320{
3321 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003322 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003323 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003324
3325 /* For each DAI widget... */
3326 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003327 switch (dai_w->id) {
3328 case snd_soc_dapm_dai_in:
3329 case snd_soc_dapm_dai_out:
3330 break;
3331 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003332 continue;
Mark Brown46162742013-06-05 19:36:11 +01003333 }
Mark Brown888df392012-02-16 19:37:51 -08003334
3335 dai = dai_w->priv;
3336
3337 /* ...find all widgets with the same stream and link them */
3338 list_for_each_entry(w, &card->widgets, list) {
3339 if (w->dapm != dai_w->dapm)
3340 continue;
3341
Mark Brown46162742013-06-05 19:36:11 +01003342 switch (w->id) {
3343 case snd_soc_dapm_dai_in:
3344 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003345 continue;
Mark Brown46162742013-06-05 19:36:11 +01003346 default:
3347 break;
3348 }
Mark Brown888df392012-02-16 19:37:51 -08003349
Russell King19c2c5f2013-08-04 20:24:03 +01003350 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003351 continue;
3352
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003353 if (dai_w->id == snd_soc_dapm_dai_in) {
3354 src = dai_w;
3355 sink = w;
3356 } else {
3357 src = w;
3358 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003359 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003360 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3361 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003362 }
3363 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003364
Mark Brown888df392012-02-16 19:37:51 -08003365 return 0;
3366}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003367
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003368void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3369{
3370 struct snd_soc_pcm_runtime *rtd = card->rtd;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003371 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003372 struct snd_soc_dai *cpu_dai, *codec_dai;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003373 int i;
3374
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003375 /* for each BE DAI link... */
3376 for (i = 0; i < card->num_rtd; i++) {
3377 rtd = &card->rtd[i];
3378 cpu_dai = rtd->cpu_dai;
3379 codec_dai = rtd->codec_dai;
3380
Lars-Peter Clausenca5106a2014-05-07 16:20:24 +02003381 /*
3382 * dynamic FE links have no fixed DAI mapping.
3383 * CODEC<->CODEC links have no direct connection.
3384 */
3385 if (rtd->dai_link->dynamic || rtd->dai_link->params)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003386 continue;
3387
3388 /* there is no point in connecting BE DAI links with dummies */
3389 if (snd_soc_dai_is_dummy(codec_dai) ||
3390 snd_soc_dai_is_dummy(cpu_dai))
3391 continue;
3392
3393 /* connect BE DAI playback if widgets are valid */
3394 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003395 source = cpu_dai->playback_widget;
3396 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003397 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003398 cpu_dai->codec->name, source->name,
3399 codec_dai->platform->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003400
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003401 snd_soc_dapm_add_path(&card->dapm, source, sink,
3402 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003403 }
3404
3405 /* connect BE DAI capture if widgets are valid */
3406 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003407 source = codec_dai->capture_widget;
3408 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003409 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003410 codec_dai->codec->name, source->name,
3411 cpu_dai->platform->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003412
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003413 snd_soc_dapm_add_path(&card->dapm, source, sink,
3414 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003415 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003416 }
3417}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003418
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003419static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3420 int event)
3421{
3422 struct snd_soc_dapm_widget *w;
3423
3424 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3425 w = dai->playback_widget;
3426 else
3427 w = dai->capture_widget;
3428
3429 if (w) {
3430 dapm_mark_dirty(w, "stream event");
3431
3432 switch (event) {
3433 case SND_SOC_DAPM_STREAM_START:
3434 w->active = 1;
3435 break;
3436 case SND_SOC_DAPM_STREAM_STOP:
3437 w->active = 0;
3438 break;
3439 case SND_SOC_DAPM_STREAM_SUSPEND:
3440 case SND_SOC_DAPM_STREAM_RESUME:
3441 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3442 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3443 break;
3444 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003445 }
3446}
3447
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003448static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3449 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003450{
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003451 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
3452 soc_dapm_dai_stream_event(rtd->codec_dai, stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003453
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003454 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003455}
3456
3457/**
3458 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3459 * @rtd: PCM runtime data
3460 * @stream: stream name
3461 * @event: stream event
3462 *
3463 * Sends a stream event to the dapm core. The core then makes any
3464 * necessary widget power changes.
3465 *
3466 * Returns 0 for success else error.
3467 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003468void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3469 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003470{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003471 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003472
Liam Girdwood3cd04342012-03-09 12:02:08 +00003473 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003474 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003475 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003476}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003477
3478/**
Charles Keepax11391102014-02-18 15:22:14 +00003479 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3480 * @dapm: DAPM context
3481 * @pin: pin name
3482 *
3483 * Enables input/output pin and its parents or children widgets iff there is
3484 * a valid audio route and active audio stream.
3485 *
3486 * Requires external locking.
3487 *
3488 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3489 * do any widget power switching.
3490 */
3491int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3492 const char *pin)
3493{
3494 return snd_soc_dapm_set_pin(dapm, pin, 1);
3495}
3496EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3497
3498/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003499 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003500 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003501 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003502 *
Mark Brown74b8f952009-06-06 11:26:15 +01003503 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003504 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003505 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003506 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3507 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003508 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003509int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003510{
Charles Keepax11391102014-02-18 15:22:14 +00003511 int ret;
3512
3513 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3514
3515 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3516
3517 mutex_unlock(&dapm->card->dapm_mutex);
3518
3519 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003520}
Liam Girdwooda5302182008-07-07 13:35:17 +01003521EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003522
3523/**
Charles Keepax11391102014-02-18 15:22:14 +00003524 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3525 * @dapm: DAPM context
3526 * @pin: pin name
3527 *
3528 * Enables input/output pin regardless of any other state. This is
3529 * intended for use with microphone bias supplies used in microphone
3530 * jack detection.
3531 *
3532 * Requires external locking.
3533 *
3534 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3535 * do any widget power switching.
3536 */
3537int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3538 const char *pin)
3539{
3540 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3541
3542 if (!w) {
3543 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3544 return -EINVAL;
3545 }
3546
3547 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3548 w->connected = 1;
3549 w->force = 1;
3550 dapm_mark_dirty(w, "force enable");
3551
3552 return 0;
3553}
3554EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3555
3556/**
Mark Brownda341832010-03-15 19:23:37 +00003557 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003558 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003559 * @pin: pin name
3560 *
3561 * Enables input/output pin regardless of any other state. This is
3562 * intended for use with microphone bias supplies used in microphone
3563 * jack detection.
3564 *
3565 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3566 * do any widget power switching.
3567 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003568int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3569 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003570{
Charles Keepax11391102014-02-18 15:22:14 +00003571 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003572
Charles Keepax11391102014-02-18 15:22:14 +00003573 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003574
Charles Keepax11391102014-02-18 15:22:14 +00003575 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003576
Charles Keepax11391102014-02-18 15:22:14 +00003577 mutex_unlock(&dapm->card->dapm_mutex);
3578
3579 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003580}
3581EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3582
3583/**
Charles Keepax11391102014-02-18 15:22:14 +00003584 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3585 * @dapm: DAPM context
3586 * @pin: pin name
3587 *
3588 * Disables input/output pin and its parents or children widgets.
3589 *
3590 * Requires external locking.
3591 *
3592 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3593 * do any widget power switching.
3594 */
3595int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3596 const char *pin)
3597{
3598 return snd_soc_dapm_set_pin(dapm, pin, 0);
3599}
3600EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3601
3602/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003603 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003604 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003605 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003606 *
Mark Brown74b8f952009-06-06 11:26:15 +01003607 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003608 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003609 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3610 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003611 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003612int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3613 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003614{
Charles Keepax11391102014-02-18 15:22:14 +00003615 int ret;
3616
3617 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3618
3619 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3620
3621 mutex_unlock(&dapm->card->dapm_mutex);
3622
3623 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003624}
3625EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3626
3627/**
Charles Keepax11391102014-02-18 15:22:14 +00003628 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3629 * @dapm: DAPM context
3630 * @pin: pin name
3631 *
3632 * Marks the specified pin as being not connected, disabling it along
3633 * any parent or child widgets. At present this is identical to
3634 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3635 * additional things such as disabling controls which only affect
3636 * paths through the pin.
3637 *
3638 * Requires external locking.
3639 *
3640 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3641 * do any widget power switching.
3642 */
3643int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3644 const char *pin)
3645{
3646 return snd_soc_dapm_set_pin(dapm, pin, 0);
3647}
3648EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3649
3650/**
Mark Brown5817b522008-09-24 11:23:11 +01003651 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003652 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003653 * @pin: pin name
3654 *
3655 * Marks the specified pin as being not connected, disabling it along
3656 * any parent or child widgets. At present this is identical to
3657 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3658 * additional things such as disabling controls which only affect
3659 * paths through the pin.
3660 *
3661 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3662 * do any widget power switching.
3663 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003664int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003665{
Charles Keepax11391102014-02-18 15:22:14 +00003666 int ret;
3667
3668 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3669
3670 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3671
3672 mutex_unlock(&dapm->card->dapm_mutex);
3673
3674 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003675}
3676EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3677
3678/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003679 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003680 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003681 * @pin: audio signal pin endpoint (or start point)
3682 *
3683 * Get audio pin status - connected or disconnected.
3684 *
3685 * Returns 1 for connected otherwise 0.
3686 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003687int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3688 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003689{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003690 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003691
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003692 if (w)
3693 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003694
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003695 return 0;
3696}
Liam Girdwooda5302182008-07-07 13:35:17 +01003697EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003698
3699/**
Mark Brown1547aba2010-05-07 21:11:40 +01003700 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003701 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003702 * @pin: audio signal pin endpoint (or start point)
3703 *
3704 * Mark the given endpoint or pin as ignoring suspend. When the
3705 * system is disabled a path between two endpoints flagged as ignoring
3706 * suspend will not be disabled. The path must already be enabled via
3707 * normal means at suspend time, it will not be turned on if it was not
3708 * already enabled.
3709 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003710int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3711 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003712{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003713 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003714
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003715 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003716 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003717 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003718 }
3719
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003720 w->ignore_suspend = 1;
3721
3722 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003723}
3724EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3725
Stephen Warren16332812011-11-23 12:42:04 -07003726static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3727 struct snd_soc_dapm_widget *w)
3728{
3729 struct snd_soc_dapm_path *p;
3730
3731 list_for_each_entry(p, &card->paths, list) {
3732 if ((p->source == w) || (p->sink == w)) {
3733 dev_dbg(card->dev,
3734 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3735 p->source->name, p->source->id, p->source->dapm,
3736 p->sink->name, p->sink->id, p->sink->dapm);
3737
3738 /* Connected to something other than the codec */
3739 if (p->source->dapm != p->sink->dapm)
3740 return true;
3741 /*
3742 * Loopback connection from codec external pin to
3743 * codec external pin
3744 */
3745 if (p->sink->id == snd_soc_dapm_input) {
3746 switch (p->source->id) {
3747 case snd_soc_dapm_output:
3748 case snd_soc_dapm_micbias:
3749 return true;
3750 default:
3751 break;
3752 }
3753 }
3754 }
3755 }
3756
3757 return false;
3758}
3759
3760/**
3761 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3762 * @codec: The codec whose pins should be processed
3763 *
3764 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3765 * which are unused. Pins are used if they are connected externally to the
3766 * codec, whether that be to some other device, or a loop-back connection to
3767 * the codec itself.
3768 */
3769void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3770{
3771 struct snd_soc_card *card = codec->card;
3772 struct snd_soc_dapm_context *dapm = &codec->dapm;
3773 struct snd_soc_dapm_widget *w;
3774
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003775 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003776 &card->dapm, &codec->dapm);
3777
3778 list_for_each_entry(w, &card->widgets, list) {
3779 if (w->dapm != dapm)
3780 continue;
3781 switch (w->id) {
3782 case snd_soc_dapm_input:
3783 case snd_soc_dapm_output:
3784 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003785 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003786 w->name);
3787 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003788 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003789 "... Not in map; disabling\n");
3790 snd_soc_dapm_nc_pin(dapm, w->name);
3791 }
3792 break;
3793 default:
3794 break;
3795 }
3796 }
3797}
3798
Mark Brown1547aba2010-05-07 21:11:40 +01003799/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003800 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003801 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003802 *
3803 * Free all dapm widgets and resources.
3804 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003805void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003806{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003807 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003808 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003809 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003810 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003811}
3812EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3813
Xiang Xiao57996352014-03-02 00:04:02 +08003814static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003815{
Liam Girdwood01005a72012-07-06 16:57:05 +01003816 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003817 struct snd_soc_dapm_widget *w;
3818 LIST_HEAD(down_list);
3819 int powerdown = 0;
3820
Liam Girdwood01005a72012-07-06 16:57:05 +01003821 mutex_lock(&card->dapm_mutex);
3822
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003823 list_for_each_entry(w, &dapm->card->widgets, list) {
3824 if (w->dapm != dapm)
3825 continue;
Mark Brown51737472009-06-22 13:16:51 +01003826 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003827 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003828 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003829 powerdown = 1;
3830 }
3831 }
3832
3833 /* If there were no widgets to power down we're already in
3834 * standby.
3835 */
3836 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003837 if (dapm->bias_level == SND_SOC_BIAS_ON)
3838 snd_soc_dapm_set_bias_level(dapm,
3839 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003840 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003841 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3842 snd_soc_dapm_set_bias_level(dapm,
3843 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003844 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003845
3846 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003847}
Mark Brown51737472009-06-22 13:16:51 +01003848
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003849/*
3850 * snd_soc_dapm_shutdown - callback for system shutdown
3851 */
3852void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3853{
Xiang Xiao57996352014-03-02 00:04:02 +08003854 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003855
Xiang Xiao57996352014-03-02 00:04:02 +08003856 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08003857 if (dapm != &card->dapm) {
3858 soc_dapm_shutdown_dapm(dapm);
3859 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3860 snd_soc_dapm_set_bias_level(dapm,
3861 SND_SOC_BIAS_OFF);
3862 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003863 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08003864
3865 soc_dapm_shutdown_dapm(&card->dapm);
3866 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3867 snd_soc_dapm_set_bias_level(&card->dapm,
3868 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01003869}
3870
Richard Purdie2b97eab2006-10-06 18:32:18 +02003871/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003872MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003873MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3874MODULE_LICENSE("GPL");