blob: a2025a6b6a2956917ba767dd3ae874296e232fbc [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 Brown15e4c722008-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 Brown15e4c722008-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 Brown15e4c722008-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 Brown15e4c722008-07-02 11:51:20 +0100141 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200142
143 kfree(buf);
Mark Brown15e4c722008-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/**
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200353 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
354 * kcontrol
355 * @kcontrol: The kcontrol
356 *
357 * Note: This function must only be used on kcontrols that are known to have
358 * been registered for a CODEC. Otherwise the behaviour is undefined.
359 */
360struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
361 struct snd_kcontrol *kcontrol)
362{
363 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
364}
365EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
366
367/**
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200368 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
369 * @kcontrol: The kcontrol
370 */
371struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
372{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200373 return snd_soc_dapm_to_codec(snd_soc_dapm_kcontrol_dapm(kcontrol));
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200374}
375EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
376
Liam Girdwood6c120e12012-02-15 15:15:34 +0000377static void dapm_reset(struct snd_soc_card *card)
378{
379 struct snd_soc_dapm_widget *w;
380
Mark Brownf9fa2b12014-03-06 16:49:11 +0800381 lockdep_assert_held(&card->dapm_mutex);
382
Liam Girdwood6c120e12012-02-15 15:15:34 +0000383 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
384
385 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200386 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000387 w->power_checked = false;
388 w->inputs = -1;
389 w->outputs = -1;
390 }
391}
392
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200393static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
394{
395 if (!dapm->component)
396 return NULL;
397 return dapm->component->name_prefix;
398}
399
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200400static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800401 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100402{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200403 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200404 return -EIO;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200405 return snd_soc_component_read(dapm->component, reg, value);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100406}
407
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200408static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
Bard Liao34775012014-04-17 20:12:56 +0800409 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100410{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200411 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200412 return -EIO;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200413 return snd_soc_component_update_bits_async(dapm->component, reg,
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200414 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000415}
416
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200417static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
418 int reg, unsigned int mask, unsigned int value)
419{
420 if (!dapm->component)
421 return -EIO;
422 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
423}
424
Mark Browneb270e92013-10-09 13:52:52 +0100425static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
426{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200427 if (dapm->component)
428 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100429}
430
Mark Brown452c5ea2009-05-17 21:41:23 +0100431/**
432 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800433 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100434 * @level: level to configure
435 *
436 * Configure the bias (power) levels for the SoC audio device.
437 *
438 * Returns 0 for success else error.
439 */
Mark Browned5a4c42011-02-18 11:12:42 -0800440static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200441 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100442{
Mark Browned5a4c42011-02-18 11:12:42 -0800443 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100444 int ret = 0;
445
Mark Brown84e90932010-11-04 00:07:02 -0400446 trace_snd_soc_bias_level_start(card, level);
447
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000448 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100449 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100450 if (ret != 0)
451 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100452
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +0200453 if (dapm->set_bias_level)
454 ret = dapm->set_bias_level(dapm, level);
455 else if (!card || dapm != &card->dapm)
Liam Girdwood41231282012-07-06 16:56:16 +0100456 dapm->bias_level = level;
457
Mark Brown171ec6b2011-06-06 18:15:19 +0100458 if (ret != 0)
459 goto out;
460
461 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100462 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100463out:
Mark Brown84e90932010-11-04 00:07:02 -0400464 trace_snd_soc_bias_level_done(card, level);
465
Mark Brown452c5ea2009-05-17 21:41:23 +0100466 return ret;
467}
468
Mark Brown74b8f952009-06-06 11:26:15 +0100469/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200470static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200471 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
472 struct snd_soc_dapm_path *path, const char *control_name,
473 const struct snd_kcontrol_new *kcontrol)
474{
475 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100476 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200477 int i;
478
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100479 if (e->reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200480 soc_dapm_read(dapm, e->reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100481 val = (val >> e->shift_l) & e->mask;
482 item = snd_soc_enum_val_to_item(e, val);
483 } else {
484 /* since a virtual mux has no backing registers to
485 * decide which path to connect, it will try to match
486 * with the first enumeration. This is to ensure
487 * that the default mux choice (the first) will be
488 * correctly powered up during initialization.
489 */
490 item = 0;
491 }
492
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100493 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200494 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200495 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200496 list_add(&path->list_sink, &dest->sources);
497 list_add(&path->list_source, &src->sinks);
498 path->name = (char*)e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100499 if (i == item)
500 path->connect = 1;
501 else
502 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200503 return 0;
504 }
505 }
506
507 return -ENODEV;
508}
509
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100510/* set up initial codec paths */
511static void dapm_set_mixer_path_status(struct snd_soc_dapm_widget *w,
512 struct snd_soc_dapm_path *p, int i)
513{
514 struct soc_mixer_control *mc = (struct soc_mixer_control *)
515 w->kcontrol_news[i].private_value;
516 unsigned int reg = mc->reg;
517 unsigned int shift = mc->shift;
518 unsigned int max = mc->max;
519 unsigned int mask = (1 << fls(max)) - 1;
520 unsigned int invert = mc->invert;
521 unsigned int val;
522
523 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200524 soc_dapm_read(w->dapm, reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100525 val = (val >> shift) & mask;
526 if (invert)
527 val = max - val;
528 p->connect = !!val;
529 } else {
530 p->connect = 0;
531 }
532}
533
Mark Brown74b8f952009-06-06 11:26:15 +0100534/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200535static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200536 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
537 struct snd_soc_dapm_path *path, const char *control_name)
538{
539 int i;
540
541 /* search for mixer kcontrol */
542 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600543 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200544 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200545 list_add(&path->list_sink, &dest->sources);
546 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600547 path->name = dest->kcontrol_news[i].name;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100548 dapm_set_mixer_path_status(dest, path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200549 return 0;
550 }
551 }
552 return -ENODEV;
553}
554
Stephen Warrenaf468002011-04-28 17:38:01 -0600555static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600556 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600557 const struct snd_kcontrol_new *kcontrol_new,
558 struct snd_kcontrol **kcontrol)
559{
560 struct snd_soc_dapm_widget *w;
561 int i;
562
563 *kcontrol = NULL;
564
565 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600566 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
567 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600568 for (i = 0; i < w->num_kcontrols; i++) {
569 if (&w->kcontrol_news[i] == kcontrol_new) {
570 if (w->kcontrols)
571 *kcontrol = w->kcontrols[i];
572 return 1;
573 }
574 }
575 }
576
577 return 0;
578}
579
Stephen Warren85762e72013-03-29 15:40:10 -0600580/*
581 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
582 * create it. Either way, add the widget into the control's widget list
583 */
584static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100585 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200586{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200587 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000588 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000589 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600590 size_t prefix_len;
591 int shared;
592 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600593 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600594 char *long_name;
595 const char *name;
596 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000597
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200598 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000599 if (prefix)
600 prefix_len = strlen(prefix) + 1;
601 else
602 prefix_len = 0;
603
Stephen Warren85762e72013-03-29 15:40:10 -0600604 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
605 &kcontrol);
606
Stephen Warren85762e72013-03-29 15:40:10 -0600607 if (!kcontrol) {
608 if (shared) {
609 wname_in_long_name = false;
610 kcname_in_long_name = true;
611 } else {
612 switch (w->id) {
613 case snd_soc_dapm_switch:
614 case snd_soc_dapm_mixer:
615 wname_in_long_name = true;
616 kcname_in_long_name = true;
617 break;
618 case snd_soc_dapm_mixer_named_ctl:
619 wname_in_long_name = false;
620 kcname_in_long_name = true;
621 break;
622 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600623 wname_in_long_name = true;
624 kcname_in_long_name = false;
625 break;
626 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600627 return -EINVAL;
628 }
629 }
630
631 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600632 /*
633 * The control will get a prefix from the control
634 * creation process but we're also using the same
635 * prefix for widgets so cut the prefix off the
636 * front of the widget name.
637 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200638 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600639 w->name + prefix_len,
640 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200641 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200642 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600643
644 name = long_name;
645 } else if (wname_in_long_name) {
646 long_name = NULL;
647 name = w->name + prefix_len;
648 } else {
649 long_name = NULL;
650 name = w->kcontrol_news[kci].name;
651 }
652
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200653 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600654 prefix);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200655 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200656 if (!kcontrol)
657 return -ENOMEM;
658 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200659
660 ret = dapm_kcontrol_data_alloc(w, kcontrol);
661 if (ret) {
662 snd_ctl_free_one(kcontrol);
663 return ret;
664 }
665
Stephen Warren85762e72013-03-29 15:40:10 -0600666 ret = snd_ctl_add(card, kcontrol);
667 if (ret < 0) {
668 dev_err(dapm->dev,
669 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
670 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600671 return ret;
672 }
Stephen Warren85762e72013-03-29 15:40:10 -0600673 }
674
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200675 ret = dapm_kcontrol_add_widget(kcontrol, w);
676 if (ret)
677 return ret;
678
Stephen Warren85762e72013-03-29 15:40:10 -0600679 w->kcontrols[kci] = kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600680
681 return 0;
682}
683
684/* create new dapm mixer control */
685static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
686{
687 int i, ret;
688 struct snd_soc_dapm_path *path;
689
Richard Purdie2b97eab2006-10-06 18:32:18 +0200690 /* add kcontrol */
691 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200692 /* match name */
693 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200694 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600695 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200696 continue;
697
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200698 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200699 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200700 continue;
701 }
702
Mark Brown946d92a2013-08-12 23:28:42 +0100703 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600704 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200705 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100706
707 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200708 }
709 }
Stephen Warren85762e72013-03-29 15:40:10 -0600710
711 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200712}
713
714/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200715static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200716{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200717 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600718 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600719 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200720
Stephen Warrenaf468002011-04-28 17:38:01 -0600721 if (w->num_kcontrols != 1) {
722 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000723 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600724 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200725 return -EINVAL;
726 }
727
Lars-Peter Clausenfe5813912013-08-01 18:30:38 +0200728 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600729 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
730 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600731 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200732
Mark Brown946d92a2013-08-12 23:28:42 +0100733 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600734 if (ret < 0)
735 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600736
Richard Purdie2b97eab2006-10-06 18:32:18 +0200737 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200738 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200739
Stephen Warrenaf468002011-04-28 17:38:01 -0600740 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200741}
742
743/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200744static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200745{
Mark Browna6c65732010-03-03 17:45:21 +0000746 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200747 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000748 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200749
Mark Browna6c65732010-03-03 17:45:21 +0000750 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200751}
752
753/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100754static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
755 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200756{
757 struct snd_soc_dapm_path *p;
758
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100759 list_for_each_entry(p, sink, list_source) {
760 if (p->walked) {
761 p->walked = 0;
762 dapm_clear_walk_output(dapm, &p->sink->sinks);
763 }
764 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200765}
766
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100767static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
768 struct list_head *source)
769{
770 struct snd_soc_dapm_path *p;
771
772 list_for_each_entry(p, source, list_sink) {
773 if (p->walked) {
774 p->walked = 0;
775 dapm_clear_walk_input(dapm, &p->source->sources);
776 }
777 }
778}
779
780
Mark Brown99497882010-05-07 20:24:05 +0100781/* We implement power down on suspend by checking the power state of
782 * the ALSA card - when we are suspending the ALSA state for the card
783 * is set to D3.
784 */
785static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
786{
Mark Brown12ea2c72011-03-02 18:17:32 +0000787 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100788
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000789 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100790 case SNDRV_CTL_POWER_D3hot:
791 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100792 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000793 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200794 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100795 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100796 default:
797 return 1;
798 }
799}
800
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100801/* add widget to list if it's not already in the list */
802static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
803 struct snd_soc_dapm_widget *w)
804{
805 struct snd_soc_dapm_widget_list *wlist;
806 int wlistsize, wlistentries, i;
807
808 if (*list == NULL)
809 return -EINVAL;
810
811 wlist = *list;
812
813 /* is this widget already in the list */
814 for (i = 0; i < wlist->num_widgets; i++) {
815 if (wlist->widgets[i] == w)
816 return 0;
817 }
818
819 /* allocate some new space */
820 wlistentries = wlist->num_widgets + 1;
821 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
822 wlistentries * sizeof(struct snd_soc_dapm_widget *);
823 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
824 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000825 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100826 w->name);
827 return -ENOMEM;
828 }
829 wlist = *list;
830
831 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000832 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100833 w->name, wlist->num_widgets);
834
835 wlist->widgets[wlist->num_widgets] = w;
836 wlist->num_widgets++;
837 return 1;
838}
839
Richard Purdie2b97eab2006-10-06 18:32:18 +0200840/*
841 * Recursively check for a completed path to an active or physically connected
842 * output widget. Returns number of complete paths.
843 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100844static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
845 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200846{
847 struct snd_soc_dapm_path *path;
848 int con = 0;
849
Mark Brown024dc072011-10-09 11:52:05 +0100850 if (widget->outputs >= 0)
851 return widget->outputs;
852
Mark Brownde02d072011-09-20 21:43:24 +0100853 DAPM_UPDATE_STAT(widget, path_checks);
854
Mark Brown62ea8742012-01-21 21:14:48 +0000855 switch (widget->id) {
856 case snd_soc_dapm_supply:
857 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200858 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200859 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100860 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000861 default:
862 break;
863 }
Mark Brown246d0a12009-04-22 18:24:55 +0100864
Mark Brown010ff262009-08-17 17:39:22 +0100865 switch (widget->id) {
866 case snd_soc_dapm_adc:
867 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100868 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100869 if (widget->active) {
870 widget->outputs = snd_soc_dapm_suspend_check(widget);
871 return widget->outputs;
872 }
Mark Brown010ff262009-08-17 17:39:22 +0100873 default:
874 break;
875 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200876
877 if (widget->connected) {
878 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100879 if (widget->id == snd_soc_dapm_output && !widget->ext) {
880 widget->outputs = snd_soc_dapm_suspend_check(widget);
881 return widget->outputs;
882 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200883
884 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100885 if (widget->id == snd_soc_dapm_hp ||
886 widget->id == snd_soc_dapm_spk ||
887 (widget->id == snd_soc_dapm_line &&
888 !list_empty(&widget->sources))) {
889 widget->outputs = snd_soc_dapm_suspend_check(widget);
890 return widget->outputs;
891 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200892 }
893
894 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100895 DAPM_UPDATE_STAT(widget, neighbour_checks);
896
Mark Brownbf3a9e12011-06-13 16:42:29 +0100897 if (path->weak)
898 continue;
899
Mark Brown8af294b2013-02-22 17:48:15 +0000900 if (path->walking)
901 return 1;
902
Richard Purdie2b97eab2006-10-06 18:32:18 +0200903 if (path->walked)
904 continue;
905
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100906 trace_snd_soc_dapm_output_path(widget, path);
907
Richard Purdie2b97eab2006-10-06 18:32:18 +0200908 if (path->sink && path->connect) {
909 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000910 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100911
912 /* do we need to add this widget to the list ? */
913 if (list) {
914 int err;
915 err = dapm_list_add_widget(list, path->sink);
916 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000917 dev_err(widget->dapm->dev,
918 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100919 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000920 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100921 return con;
922 }
923 }
924
925 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000926
927 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200928 }
929 }
930
Mark Brown024dc072011-10-09 11:52:05 +0100931 widget->outputs = con;
932
Richard Purdie2b97eab2006-10-06 18:32:18 +0200933 return con;
934}
935
936/*
937 * Recursively check for a completed path to an active or physically connected
938 * input widget. Returns number of complete paths.
939 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100940static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
941 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200942{
943 struct snd_soc_dapm_path *path;
944 int con = 0;
945
Mark Brown024dc072011-10-09 11:52:05 +0100946 if (widget->inputs >= 0)
947 return widget->inputs;
948
Mark Brownde02d072011-09-20 21:43:24 +0100949 DAPM_UPDATE_STAT(widget, path_checks);
950
Mark Brown62ea8742012-01-21 21:14:48 +0000951 switch (widget->id) {
952 case snd_soc_dapm_supply:
953 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200954 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200955 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100956 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000957 default:
958 break;
959 }
Mark Brown246d0a12009-04-22 18:24:55 +0100960
Richard Purdie2b97eab2006-10-06 18:32:18 +0200961 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100962 switch (widget->id) {
963 case snd_soc_dapm_dac:
964 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +0100965 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +0100966 if (widget->active) {
967 widget->inputs = snd_soc_dapm_suspend_check(widget);
968 return widget->inputs;
969 }
Mark Brown010ff262009-08-17 17:39:22 +0100970 default:
971 break;
972 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200973
974 if (widget->connected) {
975 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100976 if (widget->id == snd_soc_dapm_input && !widget->ext) {
977 widget->inputs = snd_soc_dapm_suspend_check(widget);
978 return widget->inputs;
979 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200980
981 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100982 if (widget->id == snd_soc_dapm_vmid) {
983 widget->inputs = snd_soc_dapm_suspend_check(widget);
984 return widget->inputs;
985 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200986
987 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300988 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100989 (widget->id == snd_soc_dapm_line &&
990 !list_empty(&widget->sinks))) {
991 widget->inputs = snd_soc_dapm_suspend_check(widget);
992 return widget->inputs;
993 }
994
Mark Brown1ab97c82011-11-27 16:21:51 +0000995 /* signal generator */
996 if (widget->id == snd_soc_dapm_siggen) {
997 widget->inputs = snd_soc_dapm_suspend_check(widget);
998 return widget->inputs;
999 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001000 }
1001
1002 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +01001003 DAPM_UPDATE_STAT(widget, neighbour_checks);
1004
Mark Brownbf3a9e12011-06-13 16:42:29 +01001005 if (path->weak)
1006 continue;
1007
Mark Brown8af294b2013-02-22 17:48:15 +00001008 if (path->walking)
1009 return 1;
1010
Richard Purdie2b97eab2006-10-06 18:32:18 +02001011 if (path->walked)
1012 continue;
1013
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001014 trace_snd_soc_dapm_input_path(widget, path);
1015
Richard Purdie2b97eab2006-10-06 18:32:18 +02001016 if (path->source && path->connect) {
1017 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001018 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001019
1020 /* do we need to add this widget to the list ? */
1021 if (list) {
1022 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001023 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001024 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001025 dev_err(widget->dapm->dev,
1026 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001027 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001028 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001029 return con;
1030 }
1031 }
1032
1033 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001034
1035 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001036 }
1037 }
1038
Mark Brown024dc072011-10-09 11:52:05 +01001039 widget->inputs = con;
1040
Richard Purdie2b97eab2006-10-06 18:32:18 +02001041 return con;
1042}
1043
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001044/**
1045 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1046 * @dai: the soc DAI.
1047 * @stream: stream direction.
1048 * @list: list of active widgets for this stream.
1049 *
1050 * Queries DAPM graph as to whether an valid audio stream path exists for
1051 * the initial stream specified by name. This takes into account
1052 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1053 *
1054 * Returns the number of valid paths or negative error.
1055 */
1056int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1057 struct snd_soc_dapm_widget_list **list)
1058{
1059 struct snd_soc_card *card = dai->card;
1060 int paths;
1061
1062 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1063 dapm_reset(card);
1064
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001065 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001066 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001067 dapm_clear_walk_output(&card->dapm,
1068 &dai->playback_widget->sinks);
1069 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001070 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001071 dapm_clear_walk_input(&card->dapm,
1072 &dai->capture_widget->sources);
1073 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001074
1075 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001076 mutex_unlock(&card->dapm_mutex);
1077
1078 return paths;
1079}
1080
Richard Purdie2b97eab2006-10-06 18:32:18 +02001081/*
Mark Brown62ea8742012-01-21 21:14:48 +00001082 * Handler for regulator supply widget.
1083 */
1084int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1085 struct snd_kcontrol *kcontrol, int event)
1086{
Mark Brownc05b84d2012-09-07 12:57:11 +08001087 int ret;
1088
Mark Browneb270e92013-10-09 13:52:52 +01001089 soc_dapm_async_complete(w->dapm);
1090
Mark Brownc05b84d2012-09-07 12:57:11 +08001091 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001092 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001093 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001094 if (ret != 0)
1095 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001096 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001097 w->name, ret);
1098 }
1099
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001100 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001101 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001102 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001103 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001104 if (ret != 0)
1105 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001106 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001107 w->name, ret);
1108 }
1109
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001110 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001111 }
Mark Brown62ea8742012-01-21 21:14:48 +00001112}
1113EXPORT_SYMBOL_GPL(dapm_regulator_event);
1114
Ola Liljad7e7eb92012-05-24 15:26:25 +02001115/*
1116 * Handler for clock supply widget.
1117 */
1118int dapm_clock_event(struct snd_soc_dapm_widget *w,
1119 struct snd_kcontrol *kcontrol, int event)
1120{
1121 if (!w->clk)
1122 return -EIO;
1123
Mark Browneb270e92013-10-09 13:52:52 +01001124 soc_dapm_async_complete(w->dapm);
1125
Mark Brownec029952012-06-04 08:16:20 +01001126#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001127 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001128 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001129 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001130 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001131 return 0;
1132 }
Mark Brownec029952012-06-04 08:16:20 +01001133#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001134 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001135}
1136EXPORT_SYMBOL_GPL(dapm_clock_event);
1137
Mark Brownd8050022011-09-28 18:28:23 +01001138static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1139{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001140 if (w->power_checked)
1141 return w->new_power;
1142
Mark Brownd8050022011-09-28 18:28:23 +01001143 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001144 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001145 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001146 w->new_power = w->power_check(w);
1147
1148 w->power_checked = true;
1149
1150 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001151}
1152
Mark Browncd0f2d42009-04-20 16:56:59 +01001153/* Generic check to see if a widget should be powered.
1154 */
1155static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1156{
1157 int in, out;
1158
Mark Brownde02d072011-09-20 21:43:24 +01001159 DAPM_UPDATE_STAT(w, power_checks);
1160
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001161 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001162 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001163 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001164 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001165 return out != 0 && in != 0;
1166}
1167
Mark Brown6ea31b92009-04-20 17:15:41 +01001168/* Check to see if an ADC has power */
1169static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1170{
1171 int in;
1172
Mark Brownde02d072011-09-20 21:43:24 +01001173 DAPM_UPDATE_STAT(w, power_checks);
1174
Mark Brown6ea31b92009-04-20 17:15:41 +01001175 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001176 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001177 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001178 return in != 0;
1179 } else {
1180 return dapm_generic_check_power(w);
1181 }
1182}
1183
1184/* Check to see if a DAC has power */
1185static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1186{
1187 int out;
1188
Mark Brownde02d072011-09-20 21:43:24 +01001189 DAPM_UPDATE_STAT(w, power_checks);
1190
Mark Brown6ea31b92009-04-20 17:15:41 +01001191 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001192 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001193 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001194 return out != 0;
1195 } else {
1196 return dapm_generic_check_power(w);
1197 }
1198}
1199
Mark Brown246d0a12009-04-22 18:24:55 +01001200/* Check to see if a power supply is needed */
1201static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1202{
1203 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001204
Mark Brownde02d072011-09-20 21:43:24 +01001205 DAPM_UPDATE_STAT(w, power_checks);
1206
Mark Brown246d0a12009-04-22 18:24:55 +01001207 /* Check if one of our outputs is connected */
1208 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001209 DAPM_UPDATE_STAT(w, neighbour_checks);
1210
Mark Brownbf3a9e12011-06-13 16:42:29 +01001211 if (path->weak)
1212 continue;
1213
Mark Brown215edda2009-09-08 18:59:05 +01001214 if (path->connected &&
1215 !path->connected(path->source, path->sink))
1216 continue;
1217
Mark Brown30173582011-02-11 11:42:19 +00001218 if (!path->sink)
1219 continue;
1220
Mark Brownf68d7e12011-10-04 22:57:50 +01001221 if (dapm_widget_power_check(path->sink))
1222 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001223 }
1224
Mark Brownf68d7e12011-10-04 22:57:50 +01001225 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001226}
1227
Mark Brown35c64bc2011-09-28 18:23:53 +01001228static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1229{
1230 return 1;
1231}
1232
Mark Brown38357ab2009-06-06 19:03:23 +01001233static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1234 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001235 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001236{
Mark Brown828a8422011-01-15 13:14:30 +00001237 int *sort;
1238
1239 if (power_up)
1240 sort = dapm_up_seq;
1241 else
1242 sort = dapm_down_seq;
1243
Mark Brown38357ab2009-06-06 19:03:23 +01001244 if (sort[a->id] != sort[b->id])
1245 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001246 if (a->subseq != b->subseq) {
1247 if (power_up)
1248 return a->subseq - b->subseq;
1249 else
1250 return b->subseq - a->subseq;
1251 }
Mark Brownb22ead22009-06-07 12:51:26 +01001252 if (a->reg != b->reg)
1253 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001254 if (a->dapm != b->dapm)
1255 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001256
Mark Brown38357ab2009-06-06 19:03:23 +01001257 return 0;
1258}
Mark Brown42aa3412009-03-01 19:21:10 +00001259
Mark Brown38357ab2009-06-06 19:03:23 +01001260/* Insert a widget in order into a DAPM power sequence. */
1261static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1262 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001263 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001264{
1265 struct snd_soc_dapm_widget *w;
1266
1267 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001268 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001269 list_add_tail(&new_widget->power_list, &w->power_list);
1270 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001271 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001272
Mark Brown38357ab2009-06-06 19:03:23 +01001273 list_add_tail(&new_widget->power_list, list);
1274}
Mark Brown42aa3412009-03-01 19:21:10 +00001275
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001276static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001277 struct snd_soc_dapm_widget *w, int event)
1278{
Mark Brown68f89ad2010-11-03 23:51:49 -04001279 const char *ev_name;
1280 int power, ret;
1281
1282 switch (event) {
1283 case SND_SOC_DAPM_PRE_PMU:
1284 ev_name = "PRE_PMU";
1285 power = 1;
1286 break;
1287 case SND_SOC_DAPM_POST_PMU:
1288 ev_name = "POST_PMU";
1289 power = 1;
1290 break;
1291 case SND_SOC_DAPM_PRE_PMD:
1292 ev_name = "PRE_PMD";
1293 power = 0;
1294 break;
1295 case SND_SOC_DAPM_POST_PMD:
1296 ev_name = "POST_PMD";
1297 power = 0;
1298 break;
Mark Brown80114122013-02-25 15:14:19 +00001299 case SND_SOC_DAPM_WILL_PMU:
1300 ev_name = "WILL_PMU";
1301 power = 1;
1302 break;
1303 case SND_SOC_DAPM_WILL_PMD:
1304 ev_name = "WILL_PMD";
1305 power = 0;
1306 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001307 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001308 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001309 return;
1310 }
1311
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001312 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001313 return;
1314
1315 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001316 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001317 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001318 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001319 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001320 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001321 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001322 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001323 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001324 ev_name, w->name, ret);
1325 }
1326}
1327
Mark Brownb22ead22009-06-07 12:51:26 +01001328/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001329static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001330 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001331{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001332 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001333 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001334 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001335 unsigned int value = 0;
1336 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001337
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001338 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1339 reg = w->reg;
1340 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001341
1342 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001343 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001344 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001345
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001346 mask |= w->mask << w->shift;
1347 if (w->power)
1348 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001349 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001350 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001351
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001352 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001353 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1354 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001355
Mark Brown68f89ad2010-11-03 23:51:49 -04001356 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001357 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1358 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001359 }
1360
Mark Brown81628102009-06-07 13:21:24 +01001361 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001362 /* Any widget will do, they should all be updating the
1363 * same register.
1364 */
Mark Brown29376bc2011-06-19 13:49:28 +01001365
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001366 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001367 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001368 value, mask, reg, card->pop_time);
1369 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001370 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001371 }
1372
1373 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001374 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1375 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001376 }
Mark Brown42aa3412009-03-01 19:21:10 +00001377}
1378
Mark Brownb22ead22009-06-07 12:51:26 +01001379/* Apply a DAPM power sequence.
1380 *
1381 * We walk over a pre-sorted list of widgets to apply power to. In
1382 * order to minimise the number of writes to the device required
1383 * multiple widgets will be updated in a single write where possible.
1384 * Currently anything that requires more than a single write is not
1385 * handled.
1386 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001387static void dapm_seq_run(struct snd_soc_card *card,
1388 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001389{
1390 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001391 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001392 LIST_HEAD(pending);
1393 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001394 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001395 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001396 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001397 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001398 int *sort;
1399
1400 if (power_up)
1401 sort = dapm_up_seq;
1402 else
1403 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001404
Mark Brownb22ead22009-06-07 12:51:26 +01001405 list_for_each_entry_safe(w, n, list, power_list) {
1406 ret = 0;
1407
1408 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001409 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001410 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001411 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001412 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001413
Mark Brown474b62d2011-01-18 16:14:44 +00001414 if (cur_dapm && cur_dapm->seq_notifier) {
1415 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1416 if (sort[i] == cur_sort)
1417 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001418 i,
1419 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001420 }
1421
Mark Browneb270e92013-10-09 13:52:52 +01001422 if (cur_dapm && w->dapm != cur_dapm)
1423 soc_dapm_async_complete(cur_dapm);
1424
Mark Brownb22ead22009-06-07 12:51:26 +01001425 INIT_LIST_HEAD(&pending);
1426 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001427 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001428 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001429 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001430 }
1431
Mark Brown163cac02009-06-07 10:12:52 +01001432 switch (w->id) {
1433 case snd_soc_dapm_pre:
1434 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001435 list_for_each_entry_safe_continue(w, n, list,
1436 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001437
Mark Brownb22ead22009-06-07 12:51:26 +01001438 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001439 ret = w->event(w,
1440 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001441 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001442 ret = w->event(w,
1443 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001444 break;
1445
1446 case snd_soc_dapm_post:
1447 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001448 list_for_each_entry_safe_continue(w, n, list,
1449 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001450
Mark Brownb22ead22009-06-07 12:51:26 +01001451 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001452 ret = w->event(w,
1453 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001454 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001455 ret = w->event(w,
1456 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001457 break;
1458
Mark Brown163cac02009-06-07 10:12:52 +01001459 default:
Mark Brown81628102009-06-07 13:21:24 +01001460 /* Queue it up for application */
1461 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001462 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001463 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001464 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001465 list_move(&w->power_list, &pending);
1466 break;
Mark Brown163cac02009-06-07 10:12:52 +01001467 }
Mark Brownb22ead22009-06-07 12:51:26 +01001468
1469 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001470 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001471 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001472 }
Mark Brownb22ead22009-06-07 12:51:26 +01001473
1474 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001475 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001476
1477 if (cur_dapm && cur_dapm->seq_notifier) {
1478 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1479 if (sort[i] == cur_sort)
1480 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001481 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001482 }
Mark Browneb270e92013-10-09 13:52:52 +01001483
1484 list_for_each_entry(d, &card->dapm_list, list) {
1485 soc_dapm_async_complete(d);
1486 }
Mark Brown163cac02009-06-07 10:12:52 +01001487}
1488
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001489static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001490{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001491 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001492 struct snd_soc_dapm_widget_list *wlist;
1493 struct snd_soc_dapm_widget *w = NULL;
1494 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001495 int ret;
1496
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001497 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001498 return;
1499
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001500 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001501
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001502 for (wi = 0; wi < wlist->num_widgets; wi++) {
1503 w = wlist->widgets[wi];
1504
1505 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1506 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1507 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001508 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001509 w->name, ret);
1510 }
Mark Brown97404f22010-12-14 16:13:57 +00001511 }
1512
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001513 if (!w)
1514 return;
1515
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001516 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1517 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001518 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001519 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001520 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001521
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001522 for (wi = 0; wi < wlist->num_widgets; wi++) {
1523 w = wlist->widgets[wi];
1524
1525 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1526 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1527 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001528 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001529 w->name, ret);
1530 }
Mark Brown97404f22010-12-14 16:13:57 +00001531 }
1532}
1533
Mark Brown9d0624a2011-02-18 11:49:43 -08001534/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1535 * they're changing state.
1536 */
1537static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1538{
1539 struct snd_soc_dapm_context *d = data;
1540 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001541
Mark Brown56fba412011-06-04 11:25:10 +01001542 /* If we're off and we're not supposed to be go into STANDBY */
1543 if (d->bias_level == SND_SOC_BIAS_OFF &&
1544 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001545 if (d->dev)
1546 pm_runtime_get_sync(d->dev);
1547
Mark Brown9d0624a2011-02-18 11:49:43 -08001548 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1549 if (ret != 0)
1550 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001551 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001552 }
1553
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001554 /* Prepare for a transition to ON or away from ON */
1555 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1556 d->bias_level != SND_SOC_BIAS_ON) ||
1557 (d->target_bias_level != SND_SOC_BIAS_ON &&
1558 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001559 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1560 if (ret != 0)
1561 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001562 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001563 }
1564}
1565
1566/* Async callback run prior to DAPM sequences - brings to their final
1567 * state.
1568 */
1569static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1570{
1571 struct snd_soc_dapm_context *d = data;
1572 int ret;
1573
1574 /* If we just powered the last thing off drop to standby 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_STANDBY ||
1577 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001578 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1579 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001580 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001581 ret);
1582 }
1583
1584 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001585 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1586 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001587 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1588 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001589 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1590 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001591
1592 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001593 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001594 }
1595
1596 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001597 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1598 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001599 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1600 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001601 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001602 ret);
1603 }
1604}
Mark Brown97404f22010-12-14 16:13:57 +00001605
Mark Brownfe4fda52011-10-03 22:36:57 +01001606static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1607 bool power, bool connect)
1608{
1609 /* If a connection is being made or broken then that update
1610 * will have marked the peer dirty, otherwise the widgets are
1611 * not connected and this update has no impact. */
1612 if (!connect)
1613 return;
1614
1615 /* If the peer is already in the state we're moving to then we
1616 * won't have an impact on it. */
1617 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001618 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001619}
1620
Mark Brown05623c42011-09-28 17:02:31 +01001621static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1622 struct list_head *up_list,
1623 struct list_head *down_list)
1624{
Mark Browndb432b42011-10-03 21:06:40 +01001625 struct snd_soc_dapm_path *path;
1626
Mark Brown05623c42011-09-28 17:02:31 +01001627 if (w->power == power)
1628 return;
1629
1630 trace_snd_soc_dapm_widget_power(w, power);
1631
Mark Browndb432b42011-10-03 21:06:40 +01001632 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001633 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001634 */
1635 list_for_each_entry(path, &w->sources, list_sink) {
1636 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001637 dapm_widget_set_peer_power(path->source, power,
1638 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001639 }
1640 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001641 switch (w->id) {
1642 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001643 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001644 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001645 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001646 /* Supplies can't affect their outputs, only their inputs */
1647 break;
1648 default:
1649 list_for_each_entry(path, &w->sinks, list_source) {
1650 if (path->sink) {
1651 dapm_widget_set_peer_power(path->sink, power,
1652 path->connect);
1653 }
Mark Browndb432b42011-10-03 21:06:40 +01001654 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001655 break;
Mark Browndb432b42011-10-03 21:06:40 +01001656 }
1657
Mark Brown05623c42011-09-28 17:02:31 +01001658 if (power)
1659 dapm_seq_insert(w, up_list, true);
1660 else
1661 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001662}
1663
Mark Brown7c81beb2011-09-20 22:22:32 +01001664static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1665 struct list_head *up_list,
1666 struct list_head *down_list)
1667{
Mark Brown7c81beb2011-09-20 22:22:32 +01001668 int power;
1669
1670 switch (w->id) {
1671 case snd_soc_dapm_pre:
1672 dapm_seq_insert(w, down_list, false);
1673 break;
1674 case snd_soc_dapm_post:
1675 dapm_seq_insert(w, up_list, true);
1676 break;
1677
1678 default:
Mark Brownd8050022011-09-28 18:28:23 +01001679 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001680
Mark Brown05623c42011-09-28 17:02:31 +01001681 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001682 break;
1683 }
1684}
1685
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001686static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1687{
1688 if (dapm->idle_bias_off)
1689 return true;
1690
1691 switch (snd_power_get_state(dapm->card->snd_card)) {
1692 case SNDRV_CTL_POWER_D3hot:
1693 case SNDRV_CTL_POWER_D3cold:
1694 return dapm->suspend_bias_off;
1695 default:
1696 break;
1697 }
1698
1699 return false;
1700}
1701
Mark Brown42aa3412009-03-01 19:21:10 +00001702/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001703 * Scan each dapm widget for complete audio path.
1704 * A complete path is a route that has valid endpoints i.e.:-
1705 *
1706 * o DAC to output pin.
1707 * o Input Pin to ADC.
1708 * o Input pin to Output pin (bypass, sidetone)
1709 * o DAC to ADC (loopback).
1710 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001711static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001712{
1713 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001714 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001715 LIST_HEAD(up_list);
1716 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001717 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001718 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001719
Mark Brownf9fa2b12014-03-06 16:49:11 +08001720 lockdep_assert_held(&card->dapm_mutex);
1721
Mark Brown84e90932010-11-04 00:07:02 -04001722 trace_snd_soc_dapm_start(card);
1723
Mark Brown56fba412011-06-04 11:25:10 +01001724 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001725 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001726 d->target_bias_level = SND_SOC_BIAS_OFF;
1727 else
1728 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001729 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001730
Liam Girdwood6c120e12012-02-15 15:15:34 +00001731 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001732
Mark Brown6d3ddc82009-05-16 17:47:29 +01001733 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001734 * lists indicating if they should be powered up or down. We
1735 * only check widgets that have been flagged as dirty but note
1736 * that new widgets may be added to the dirty list while we
1737 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001738 */
Mark Browndb432b42011-10-03 21:06:40 +01001739 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001740 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001741 }
1742
Mark Brownf9de6d72011-09-28 17:19:47 +01001743 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001744 switch (w->id) {
1745 case snd_soc_dapm_pre:
1746 case snd_soc_dapm_post:
1747 /* These widgets always need to be powered */
1748 break;
1749 default:
1750 list_del_init(&w->dirty);
1751 break;
1752 }
Mark Browndb432b42011-10-03 21:06:40 +01001753
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001754 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001755 d = w->dapm;
1756
1757 /* Supplies and micbiases only bring the
1758 * context up to STANDBY as unless something
1759 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001760 * generally don't require full power. Signal
1761 * generators are virtual pins and have no
1762 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001763 */
1764 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001765 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001766 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001767 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001768 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001769 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001770 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001771 case snd_soc_dapm_micbias:
1772 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1773 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1774 break;
1775 default:
1776 d->target_bias_level = SND_SOC_BIAS_ON;
1777 break;
1778 }
1779 }
1780
1781 }
1782
Mark Brown85a843c2011-09-21 21:29:47 +01001783 /* Force all contexts in the card to the same bias state if
1784 * they're not ground referenced.
1785 */
Mark Brown56fba412011-06-04 11:25:10 +01001786 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001787 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001788 if (d->target_bias_level > bias)
1789 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001790 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001791 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01001792 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001793
Mark Brownde02d072011-09-20 21:43:24 +01001794 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001795
Xiang Xiao17282ba2014-03-02 00:04:03 +08001796 /* Run card bias changes at first */
1797 dapm_pre_sequence_async(&card->dapm, 0);
1798 /* Run other bias changes in parallel */
1799 list_for_each_entry(d, &card->dapm_list, list) {
1800 if (d != &card->dapm)
1801 async_schedule_domain(dapm_pre_sequence_async, d,
1802 &async_domain);
1803 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001804 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001805
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001806 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001807 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001808 }
1809
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001810 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001811 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001812 }
1813
Mark Brown6d3ddc82009-05-16 17:47:29 +01001814 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001815 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001816
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001817 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001818
Mark Brown6d3ddc82009-05-16 17:47:29 +01001819 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001820 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001821
Mark Brown9d0624a2011-02-18 11:49:43 -08001822 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001823 list_for_each_entry(d, &card->dapm_list, list) {
1824 if (d != &card->dapm)
1825 async_schedule_domain(dapm_post_sequence_async, d,
1826 &async_domain);
1827 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001828 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001829 /* Run card bias changes at last */
1830 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001831
Liam Girdwood8078d872012-02-15 15:15:35 +00001832 /* do we need to notify any clients that DAPM event is complete */
1833 list_for_each_entry(d, &card->dapm_list, list) {
1834 if (d->stream_event)
1835 d->stream_event(d, event);
1836 }
1837
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001838 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001839 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001840 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001841
Mark Brown84e90932010-11-04 00:07:02 -04001842 trace_snd_soc_dapm_done(card);
1843
Mark Brown42aa3412009-03-01 19:21:10 +00001844 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001845}
1846
Mark Brown79fb9382009-08-21 16:38:13 +01001847#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001848static ssize_t dapm_widget_power_read_file(struct file *file,
1849 char __user *user_buf,
1850 size_t count, loff_t *ppos)
1851{
1852 struct snd_soc_dapm_widget *w = file->private_data;
1853 char *buf;
1854 int in, out;
1855 ssize_t ret;
1856 struct snd_soc_dapm_path *p = NULL;
1857
1858 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1859 if (!buf)
1860 return -ENOMEM;
1861
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001862 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001863 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001864 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001865 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001866
Mark Brownf13ebad2012-03-03 18:01:01 +00001867 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1868 w->name, w->power ? "On" : "Off",
1869 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001870
Mark Brownd033c362009-12-04 15:25:56 +00001871 if (w->reg >= 0)
1872 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001873 " - R%d(0x%x) mask 0x%x",
1874 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001875
1876 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1877
Mark Brown3eef08b2009-09-14 16:49:00 +01001878 if (w->sname)
1879 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1880 w->sname,
1881 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001882
1883 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001884 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001885 continue;
1886
Mark Brown79fb9382009-08-21 16:38:13 +01001887 if (p->connect)
1888 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001889 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001890 p->name ? p->name : "static",
1891 p->source->name);
1892 }
1893 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001894 if (p->connected && !p->connected(w, p->sink))
1895 continue;
1896
Mark Brown79fb9382009-08-21 16:38:13 +01001897 if (p->connect)
1898 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001899 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001900 p->name ? p->name : "static",
1901 p->sink->name);
1902 }
1903
1904 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1905
1906 kfree(buf);
1907 return ret;
1908}
1909
1910static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001911 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001912 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001913 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001914};
1915
Mark Brownef49e4f2011-04-04 20:48:13 +09001916static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1917 size_t count, loff_t *ppos)
1918{
1919 struct snd_soc_dapm_context *dapm = file->private_data;
1920 char *level;
1921
1922 switch (dapm->bias_level) {
1923 case SND_SOC_BIAS_ON:
1924 level = "On\n";
1925 break;
1926 case SND_SOC_BIAS_PREPARE:
1927 level = "Prepare\n";
1928 break;
1929 case SND_SOC_BIAS_STANDBY:
1930 level = "Standby\n";
1931 break;
1932 case SND_SOC_BIAS_OFF:
1933 level = "Off\n";
1934 break;
1935 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001936 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09001937 level = "Unknown\n";
1938 break;
1939 }
1940
1941 return simple_read_from_buffer(user_buf, count, ppos, level,
1942 strlen(level));
1943}
1944
1945static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001946 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001947 .read = dapm_bias_read_file,
1948 .llseek = default_llseek,
1949};
1950
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001951void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1952 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001953{
Mark Brown79fb9382009-08-21 16:38:13 +01001954 struct dentry *d;
1955
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001956 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1957
1958 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001959 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001960 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001961 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001962 }
Mark Brown79fb9382009-08-21 16:38:13 +01001963
Mark Brownef49e4f2011-04-04 20:48:13 +09001964 d = debugfs_create_file("bias_level", 0444,
1965 dapm->debugfs_dapm, dapm,
1966 &dapm_bias_fops);
1967 if (!d)
1968 dev_warn(dapm->dev,
1969 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001970}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001971
1972static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1973{
1974 struct snd_soc_dapm_context *dapm = w->dapm;
1975 struct dentry *d;
1976
1977 if (!dapm->debugfs_dapm || !w->name)
1978 return;
1979
1980 d = debugfs_create_file(w->name, 0444,
1981 dapm->debugfs_dapm, w,
1982 &dapm_widget_power_fops);
1983 if (!d)
1984 dev_warn(w->dapm->dev,
1985 "ASoC: Failed to create %s debugfs file\n",
1986 w->name);
1987}
1988
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001989static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1990{
1991 debugfs_remove_recursive(dapm->debugfs_dapm);
1992}
1993
Mark Brown79fb9382009-08-21 16:38:13 +01001994#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001995void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1996 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001997{
1998}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001999
2000static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2001{
2002}
2003
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002004static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2005{
2006}
2007
Mark Brown79fb9382009-08-21 16:38:13 +01002008#endif
2009
Richard Purdie2b97eab2006-10-06 18:32:18 +02002010/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002011static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002012 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002013{
2014 struct snd_soc_dapm_path *path;
2015 int found = 0;
2016
Mark Brownf9fa2b12014-03-06 16:49:11 +08002017 lockdep_assert_held(&card->dapm_mutex);
2018
Richard Purdie2b97eab2006-10-06 18:32:18 +02002019 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002020 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002021 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002022 continue;
2023
2024 found = 1;
2025 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002026 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002027 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002028 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002029 } else {
2030 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002031 dapm_mark_dirty(path->source,
2032 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002033 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002034 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002035 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002036 }
2037
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002038 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002039 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002040
Liam Girdwood618dae12012-04-25 12:12:51 +01002041 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002042}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002043
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002044int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002045 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2046 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002047{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002048 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002049 int ret;
2050
Liam Girdwood3cd04342012-03-09 12:02:08 +00002051 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002052 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002053 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002054 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002055 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002056 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002057 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002058 return ret;
2059}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002060EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002061
Milan plzik1b075e32008-01-10 14:39:46 +01002062/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002063static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002064 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002065{
2066 struct snd_soc_dapm_path *path;
2067 int found = 0;
2068
Mark Brownf9fa2b12014-03-06 16:49:11 +08002069 lockdep_assert_held(&card->dapm_mutex);
2070
Richard Purdie2b97eab2006-10-06 18:32:18 +02002071 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002072 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002073 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002074 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002075 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002076 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002077 }
2078
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002079 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002080 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002081
Liam Girdwood618dae12012-04-25 12:12:51 +01002082 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002083}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002084
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002085int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002086 struct snd_kcontrol *kcontrol, int connect,
2087 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002088{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002089 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002090 int ret;
2091
Liam Girdwood3cd04342012-03-09 12:02:08 +00002092 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002093 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002094 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002095 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002096 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002097 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002098 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002099 return ret;
2100}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002101EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002102
Benoit Cousson44ba2642014-07-08 23:19:36 +02002103static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002104{
Richard Purdie2b97eab2006-10-06 18:32:18 +02002105 struct snd_soc_dapm_widget *w;
2106 int count = 0;
2107 char *state = "not set";
2108
Lars-Peter Clausen00200102014-07-17 22:01:07 +02002109 list_for_each_entry(w, &codec->component.card->widgets, list) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002110 if (w->dapm != &codec->dapm)
2111 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002112
2113 /* only display widgets that burnm power */
2114 switch (w->id) {
2115 case snd_soc_dapm_hp:
2116 case snd_soc_dapm_mic:
2117 case snd_soc_dapm_spk:
2118 case snd_soc_dapm_line:
2119 case snd_soc_dapm_micbias:
2120 case snd_soc_dapm_dac:
2121 case snd_soc_dapm_adc:
2122 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002123 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002124 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002125 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002126 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002127 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002128 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002129 if (w->name)
2130 count += sprintf(buf + count, "%s: %s\n",
2131 w->name, w->power ? "On":"Off");
2132 break;
2133 default:
2134 break;
2135 }
2136 }
2137
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002138 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002139 case SND_SOC_BIAS_ON:
2140 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002141 break;
Mark Brown0be98982008-05-19 12:31:28 +02002142 case SND_SOC_BIAS_PREPARE:
2143 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002144 break;
Mark Brown0be98982008-05-19 12:31:28 +02002145 case SND_SOC_BIAS_STANDBY:
2146 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002147 break;
Mark Brown0be98982008-05-19 12:31:28 +02002148 case SND_SOC_BIAS_OFF:
2149 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002150 break;
2151 }
2152 count += sprintf(buf + count, "PM State: %s\n", state);
2153
2154 return count;
2155}
2156
Benoit Cousson44ba2642014-07-08 23:19:36 +02002157/* show dapm widget status in sys fs */
2158static ssize_t dapm_widget_show(struct device *dev,
2159 struct device_attribute *attr, char *buf)
2160{
2161 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2162 int i, count = 0;
2163
2164 for (i = 0; i < rtd->num_codecs; i++) {
2165 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
2166 count += dapm_widget_show_codec(codec, buf + count);
2167 }
2168
2169 return count;
2170}
2171
Richard Purdie2b97eab2006-10-06 18:32:18 +02002172static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2173
2174int snd_soc_dapm_sys_add(struct device *dev)
2175{
Troy Kisky12ef1932008-10-13 17:42:14 -07002176 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002177}
2178
2179static void snd_soc_dapm_sys_remove(struct device *dev)
2180{
Mark Brownaef90842009-05-16 17:53:16 +01002181 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002182}
2183
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002184static void dapm_free_path(struct snd_soc_dapm_path *path)
2185{
2186 list_del(&path->list_sink);
2187 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002188 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002189 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002190 kfree(path);
2191}
2192
Richard Purdie2b97eab2006-10-06 18:32:18 +02002193/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002194static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002195{
2196 struct snd_soc_dapm_widget *w, *next_w;
2197 struct snd_soc_dapm_path *p, *next_p;
2198
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002199 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2200 if (w->dapm != dapm)
2201 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002202 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002203 /*
2204 * remove source and sink paths associated to this widget.
2205 * While removing the path, remove reference to it from both
2206 * source and sink widgets so that path is removed only once.
2207 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002208 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2209 dapm_free_path(p);
2210
2211 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2212 dapm_free_path(p);
2213
Stephen Warrenfad59882011-04-28 17:37:59 -06002214 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002215 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002216 kfree(w);
2217 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002218}
2219
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002220static struct snd_soc_dapm_widget *dapm_find_widget(
2221 struct snd_soc_dapm_context *dapm, const char *pin,
2222 bool search_other_contexts)
2223{
2224 struct snd_soc_dapm_widget *w;
2225 struct snd_soc_dapm_widget *fallback = NULL;
2226
2227 list_for_each_entry(w, &dapm->card->widgets, list) {
2228 if (!strcmp(w->name, pin)) {
2229 if (w->dapm == dapm)
2230 return w;
2231 else
2232 fallback = w;
2233 }
2234 }
2235
2236 if (search_other_contexts)
2237 return fallback;
2238
2239 return NULL;
2240}
2241
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002242static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002243 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002244{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002245 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002246
Mark Brownf9fa2b12014-03-06 16:49:11 +08002247 dapm_assert_locked(dapm);
2248
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002249 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002250 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002251 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002252 }
2253
Mark Brown1a8b2d92012-02-16 11:50:07 -08002254 if (w->connected != status)
2255 dapm_mark_dirty(w, "pin configuration");
2256
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002257 w->connected = status;
2258 if (status == 0)
2259 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002260
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002261 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002262}
2263
Richard Purdie2b97eab2006-10-06 18:32:18 +02002264/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002265 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2266 * @dapm: DAPM context
2267 *
2268 * Walks all dapm audio paths and powers widgets according to their
2269 * stream or path usage.
2270 *
2271 * Requires external locking.
2272 *
2273 * Returns 0 for success.
2274 */
2275int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2276{
2277 /*
2278 * Suppress early reports (eg, jacks syncing their state) to avoid
2279 * silly DAPM runs during card startup.
2280 */
2281 if (!dapm->card || !dapm->card->instantiated)
2282 return 0;
2283
2284 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2285}
2286EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2287
2288/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002289 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002290 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002291 *
2292 * Walks all dapm audio paths and powers widgets according to their
2293 * stream or path usage.
2294 *
2295 * Returns 0 for success.
2296 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002297int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002298{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002299 int ret;
2300
Liam Girdwood3cd04342012-03-09 12:02:08 +00002301 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002302 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002303 mutex_unlock(&dapm->card->dapm_mutex);
2304 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002305}
Liam Girdwooda5302182008-07-07 13:35:17 +01002306EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002307
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002308static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2309 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2310 const char *control,
2311 int (*connected)(struct snd_soc_dapm_widget *source,
2312 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002313{
2314 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002315 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002316
2317 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2318 if (!path)
2319 return -ENOMEM;
2320
2321 path->source = wsource;
2322 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002323 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002324 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002325 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002326 INIT_LIST_HEAD(&path->list_source);
2327 INIT_LIST_HEAD(&path->list_sink);
2328
2329 /* check for external widgets */
2330 if (wsink->id == snd_soc_dapm_input) {
2331 if (wsource->id == snd_soc_dapm_micbias ||
2332 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002333 wsource->id == snd_soc_dapm_line ||
2334 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002335 wsink->ext = 1;
2336 }
2337 if (wsource->id == snd_soc_dapm_output) {
2338 if (wsink->id == snd_soc_dapm_spk ||
2339 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002340 wsink->id == snd_soc_dapm_line ||
2341 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002342 wsource->ext = 1;
2343 }
2344
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002345 dapm_mark_dirty(wsource, "Route added");
2346 dapm_mark_dirty(wsink, "Route added");
2347
Richard Purdie2b97eab2006-10-06 18:32:18 +02002348 /* connect static paths */
2349 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002350 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002351 list_add(&path->list_sink, &wsink->sources);
2352 list_add(&path->list_source, &wsource->sinks);
2353 path->connect = 1;
2354 return 0;
2355 }
2356
2357 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002358 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002359 case snd_soc_dapm_adc:
2360 case snd_soc_dapm_dac:
2361 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002362 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002363 case snd_soc_dapm_input:
2364 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002365 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002366 case snd_soc_dapm_micbias:
2367 case snd_soc_dapm_vmid:
2368 case snd_soc_dapm_pre:
2369 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002370 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002371 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002372 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002373 case snd_soc_dapm_aif_in:
2374 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002375 case snd_soc_dapm_dai_in:
2376 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002377 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002378 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002379 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002380 list_add(&path->list_sink, &wsink->sources);
2381 list_add(&path->list_source, &wsource->sinks);
2382 path->connect = 1;
2383 return 0;
2384 case snd_soc_dapm_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002385 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002386 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002387 if (ret != 0)
2388 goto err;
2389 break;
2390 case snd_soc_dapm_switch:
2391 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002392 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002393 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002394 if (ret != 0)
2395 goto err;
2396 break;
2397 case snd_soc_dapm_hp:
2398 case snd_soc_dapm_mic:
2399 case snd_soc_dapm_line:
2400 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002401 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002402 list_add(&path->list_sink, &wsink->sources);
2403 list_add(&path->list_source, &wsource->sinks);
2404 path->connect = 0;
2405 return 0;
2406 }
Mark Brownfabd0382012-07-05 17:20:06 +01002407
Richard Purdie2b97eab2006-10-06 18:32:18 +02002408 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002409err:
2410 kfree(path);
2411 return ret;
2412}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002413
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002414static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002415 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002416{
2417 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2418 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2419 const char *sink;
2420 const char *source;
2421 char prefixed_sink[80];
2422 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002423 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002424 int ret;
2425
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002426 prefix = soc_dapm_prefix(dapm);
2427 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002428 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002429 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002430 sink = prefixed_sink;
2431 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002432 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002433 source = prefixed_source;
2434 } else {
2435 sink = route->sink;
2436 source = route->source;
2437 }
2438
2439 /*
2440 * find src and dest widgets over all widgets but favor a widget from
2441 * current DAPM context
2442 */
2443 list_for_each_entry(w, &dapm->card->widgets, list) {
2444 if (!wsink && !(strcmp(w->name, sink))) {
2445 wtsink = w;
2446 if (w->dapm == dapm)
2447 wsink = w;
2448 continue;
2449 }
2450 if (!wsource && !(strcmp(w->name, source))) {
2451 wtsource = w;
2452 if (w->dapm == dapm)
2453 wsource = w;
2454 }
2455 }
2456 /* use widget from another DAPM context if not found from this */
2457 if (!wsink)
2458 wsink = wtsink;
2459 if (!wsource)
2460 wsource = wtsource;
2461
2462 if (wsource == NULL) {
2463 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2464 route->source);
2465 return -ENODEV;
2466 }
2467 if (wsink == NULL) {
2468 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2469 route->sink);
2470 return -ENODEV;
2471 }
2472
2473 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2474 route->connected);
2475 if (ret)
2476 goto err;
2477
2478 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002479err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002480 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002481 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002482 return ret;
2483}
Mark Brown105f1c22008-05-13 14:52:19 +02002484
Mark Brownefcc3c62012-07-05 17:24:19 +01002485static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2486 const struct snd_soc_dapm_route *route)
2487{
2488 struct snd_soc_dapm_path *path, *p;
2489 const char *sink;
2490 const char *source;
2491 char prefixed_sink[80];
2492 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002493 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002494
2495 if (route->control) {
2496 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002497 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002498 return -EINVAL;
2499 }
2500
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002501 prefix = soc_dapm_prefix(dapm);
2502 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002503 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002504 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002505 sink = prefixed_sink;
2506 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002507 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002508 source = prefixed_source;
2509 } else {
2510 sink = route->sink;
2511 source = route->source;
2512 }
2513
2514 path = NULL;
2515 list_for_each_entry(p, &dapm->card->paths, list) {
2516 if (strcmp(p->source->name, source) != 0)
2517 continue;
2518 if (strcmp(p->sink->name, sink) != 0)
2519 continue;
2520 path = p;
2521 break;
2522 }
2523
2524 if (path) {
2525 dapm_mark_dirty(path->source, "Route removed");
2526 dapm_mark_dirty(path->sink, "Route removed");
2527
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002528 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002529 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002530 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002531 source, sink);
2532 }
2533
2534 return 0;
2535}
2536
Mark Brown105f1c22008-05-13 14:52:19 +02002537/**
Mark Brown105f1c22008-05-13 14:52:19 +02002538 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002539 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002540 * @route: audio routes
2541 * @num: number of routes
2542 *
2543 * Connects 2 dapm widgets together via a named audio path. The sink is
2544 * the widget receiving the audio signal, whilst the source is the sender
2545 * of the audio signal.
2546 *
2547 * Returns 0 for success else error. On error all resources can be freed
2548 * with a call to snd_soc_card_free().
2549 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002550int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002551 const struct snd_soc_dapm_route *route, int num)
2552{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002553 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002554
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002555 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002556 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002557 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002558 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002559 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2560 route->source,
2561 route->control ? route->control : "direct",
2562 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002563 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002564 }
2565 route++;
2566 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002567 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002568
Dan Carpenter60884c22012-04-13 22:25:43 +03002569 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002570}
2571EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2572
Mark Brownefcc3c62012-07-05 17:24:19 +01002573/**
2574 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2575 * @dapm: DAPM context
2576 * @route: audio routes
2577 * @num: number of routes
2578 *
2579 * Removes routes from the DAPM context.
2580 */
2581int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2582 const struct snd_soc_dapm_route *route, int num)
2583{
2584 int i, ret = 0;
2585
2586 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2587 for (i = 0; i < num; i++) {
2588 snd_soc_dapm_del_route(dapm, route);
2589 route++;
2590 }
2591 mutex_unlock(&dapm->card->dapm_mutex);
2592
2593 return ret;
2594}
2595EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2596
Mark Brownbf3a9e12011-06-13 16:42:29 +01002597static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2598 const struct snd_soc_dapm_route *route)
2599{
2600 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2601 route->source,
2602 true);
2603 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2604 route->sink,
2605 true);
2606 struct snd_soc_dapm_path *path;
2607 int count = 0;
2608
2609 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002610 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002611 route->source);
2612 return -ENODEV;
2613 }
2614
2615 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002616 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002617 route->sink);
2618 return -ENODEV;
2619 }
2620
2621 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002622 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002623 route->source, route->sink);
2624
2625 list_for_each_entry(path, &source->sinks, list_source) {
2626 if (path->sink == sink) {
2627 path->weak = 1;
2628 count++;
2629 }
2630 }
2631
2632 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002633 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002634 route->source, route->sink);
2635 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002636 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002637 count, route->source, route->sink);
2638
2639 return 0;
2640}
2641
2642/**
2643 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2644 * @dapm: DAPM context
2645 * @route: audio routes
2646 * @num: number of routes
2647 *
2648 * Mark existing routes matching those specified in the passed array
2649 * as being weak, meaning that they are ignored for the purpose of
2650 * power decisions. The main intended use case is for sidetone paths
2651 * which couple audio between other independent paths if they are both
2652 * active in order to make the combination work better at the user
2653 * level but which aren't intended to be "used".
2654 *
2655 * Note that CODEC drivers should not use this as sidetone type paths
2656 * can frequently also be used as bypass paths.
2657 */
2658int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2659 const struct snd_soc_dapm_route *route, int num)
2660{
2661 int i, err;
2662 int ret = 0;
2663
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002664 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002665 for (i = 0; i < num; i++) {
2666 err = snd_soc_dapm_weak_route(dapm, route);
2667 if (err)
2668 ret = err;
2669 route++;
2670 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002671 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002672
2673 return ret;
2674}
2675EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2676
Mark Brown105f1c22008-05-13 14:52:19 +02002677/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002678 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002679 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002680 *
2681 * Checks the codec for any new dapm widgets and creates them if found.
2682 *
2683 * Returns 0 for success.
2684 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002685int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002686{
2687 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002688 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002689
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002690 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002691
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002692 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002693 {
2694 if (w->new)
2695 continue;
2696
Stephen Warrenfad59882011-04-28 17:37:59 -06002697 if (w->num_kcontrols) {
2698 w->kcontrols = kzalloc(w->num_kcontrols *
2699 sizeof(struct snd_kcontrol *),
2700 GFP_KERNEL);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002701 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002702 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002703 return -ENOMEM;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002704 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002705 }
2706
Richard Purdie2b97eab2006-10-06 18:32:18 +02002707 switch(w->id) {
2708 case snd_soc_dapm_switch:
2709 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002710 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002711 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002712 break;
2713 case snd_soc_dapm_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002714 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002715 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002716 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002717 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002718 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002719 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002720 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002721 break;
2722 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002723
2724 /* Read the initial power state from the device */
2725 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002726 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002727 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002728 val &= w->mask;
2729 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002730 w->power = 1;
2731 }
2732
Richard Purdie2b97eab2006-10-06 18:32:18 +02002733 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002734
Mark Brown7508b122011-10-05 12:09:12 +01002735 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002736 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002737 }
2738
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002739 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2740 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002741 return 0;
2742}
2743EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2744
2745/**
2746 * snd_soc_dapm_get_volsw - dapm mixer get callback
2747 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002748 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002749 *
2750 * Callback to get the value of a dapm mixer control.
2751 *
2752 * Returns 0 for success.
2753 */
2754int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2755 struct snd_ctl_elem_value *ucontrol)
2756{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002757 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2758 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002759 struct soc_mixer_control *mc =
2760 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002761 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002762 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002763 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002764 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002765 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002766 unsigned int val;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002767 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002768
2769 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002770 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002771 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002772 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002773
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002774 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002775 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
2776 ret = soc_dapm_read(dapm, reg, &val);
2777 val = (val >> shift) & mask;
2778 } else {
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002779 val = dapm_kcontrol_get_value(kcontrol);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002780 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002781 mutex_unlock(&card->dapm_mutex);
2782
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002783 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002784 ucontrol->value.integer.value[0] = max - val;
2785 else
2786 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002787
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002788 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002789}
2790EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2791
2792/**
2793 * snd_soc_dapm_put_volsw - dapm mixer set callback
2794 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002795 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002796 *
2797 * Callback to set the value of a dapm mixer control.
2798 *
2799 * Returns 0 for success.
2800 */
2801int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2802 struct snd_ctl_elem_value *ucontrol)
2803{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002804 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2805 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002806 struct soc_mixer_control *mc =
2807 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002808 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002809 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002810 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002811 unsigned int mask = (1 << fls(max)) - 1;
2812 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002813 unsigned int val;
Jarkko Nikula18626c72014-06-09 14:20:29 +03002814 int connect, change, reg_change = 0;
Mark Brown97404f22010-12-14 16:13:57 +00002815 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002816 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002817
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002818 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002819 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002820 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002821 kcontrol->id.name);
2822
Richard Purdie2b97eab2006-10-06 18:32:18 +02002823 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002824 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002825
2826 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002827 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002828
Liam Girdwood3cd04342012-03-09 12:02:08 +00002829 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002830
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002831 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown283375c2009-12-07 18:09:03 +00002832
Jarkko Nikula18626c72014-06-09 14:20:29 +03002833 if (reg != SND_SOC_NOPM) {
2834 mask = mask << shift;
2835 val = val << shift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02002836
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002837 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
Jarkko Nikula18626c72014-06-09 14:20:29 +03002838 }
2839
2840 if (change || reg_change) {
2841 if (reg_change) {
2842 update.kcontrol = kcontrol;
2843 update.reg = reg;
2844 update.mask = mask;
2845 update.val = val;
2846 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002847 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03002848 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00002849
Nenghua Cao52765972013-12-13 20:13:49 +08002850 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002851
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002852 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002853 }
2854
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002855 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002856
2857 if (ret > 0)
2858 soc_dpcm_runtime_update(card);
2859
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002860 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002861}
2862EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2863
2864/**
2865 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2866 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002867 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002868 *
2869 * Callback to get the value of a dapm enumerated double mixer control.
2870 *
2871 * Returns 0 for success.
2872 */
2873int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2874 struct snd_ctl_elem_value *ucontrol)
2875{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002876 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002877 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002878 unsigned int reg_val, val;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002879 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002880
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002881 if (e->reg != SND_SOC_NOPM)
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002882 ret = soc_dapm_read(dapm, e->reg, &reg_val);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002883 else
2884 reg_val = dapm_kcontrol_get_value(kcontrol);
2885
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002886 val = (reg_val >> e->shift_l) & e->mask;
2887 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2888 if (e->shift_l != e->shift_r) {
2889 val = (reg_val >> e->shift_r) & e->mask;
2890 val = snd_soc_enum_val_to_item(e, val);
2891 ucontrol->value.enumerated.item[1] = val;
2892 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002893
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002894 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002895}
2896EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2897
2898/**
2899 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2900 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002901 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002902 *
2903 * Callback to set the value of a dapm enumerated double mixer control.
2904 *
2905 * Returns 0 for success.
2906 */
2907int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2908 struct snd_ctl_elem_value *ucontrol)
2909{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002910 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2911 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002912 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002913 unsigned int *item = ucontrol->value.enumerated.item;
2914 unsigned int val, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002915 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002916 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002917 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002918
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002919 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002920 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002921
2922 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002923 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002924 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002925 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002926 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002927 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002928 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002929 }
2930
Liam Girdwood3cd04342012-03-09 12:02:08 +00002931 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002932
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002933 if (e->reg != SND_SOC_NOPM)
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002934 change = soc_dapm_test_bits(dapm, e->reg, mask, val);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002935 else
2936 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002937
Richard Purdie2b97eab2006-10-06 18:32:18 +02002938 if (change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002939 if (e->reg != SND_SOC_NOPM) {
2940 update.kcontrol = kcontrol;
2941 update.reg = e->reg;
2942 update.mask = mask;
2943 update.val = val;
2944 card->update = &update;
2945 }
Mark Brown3a655772009-10-05 17:23:30 +01002946
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002947 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002948
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002949 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002950 }
2951
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002952 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002953
2954 if (ret > 0)
2955 soc_dpcm_runtime_update(card);
2956
Mark Brown97404f22010-12-14 16:13:57 +00002957 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002958}
2959EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2960
2961/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002962 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2963 *
2964 * @kcontrol: mixer control
2965 * @uinfo: control element information
2966 *
2967 * Callback to provide information about a pin switch control.
2968 */
2969int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2970 struct snd_ctl_elem_info *uinfo)
2971{
2972 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2973 uinfo->count = 1;
2974 uinfo->value.integer.min = 0;
2975 uinfo->value.integer.max = 1;
2976
2977 return 0;
2978}
2979EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2980
2981/**
2982 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2983 *
2984 * @kcontrol: mixer control
2985 * @ucontrol: Value
2986 */
2987int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2988 struct snd_ctl_elem_value *ucontrol)
2989{
Mark Brown48a8c392012-02-14 17:11:15 -08002990 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002991 const char *pin = (const char *)kcontrol->private_value;
2992
Liam Girdwood3cd04342012-03-09 12:02:08 +00002993 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002994
2995 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002996 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002997
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002998 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002999
3000 return 0;
3001}
3002EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3003
3004/**
3005 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3006 *
3007 * @kcontrol: mixer control
3008 * @ucontrol: Value
3009 */
3010int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3011 struct snd_ctl_elem_value *ucontrol)
3012{
Mark Brown48a8c392012-02-14 17:11:15 -08003013 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003014 const char *pin = (const char *)kcontrol->private_value;
3015
Mark Brown8b37dbd2009-02-28 21:14:20 +00003016 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003017 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003018 else
Mark Brown48a8c392012-02-14 17:11:15 -08003019 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003020
Mark Brown48a8c392012-02-14 17:11:15 -08003021 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003022 return 0;
3023}
3024EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3025
Mark Brown5ba06fc2012-02-16 11:07:13 -08003026static struct snd_soc_dapm_widget *
3027snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3028 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003029{
3030 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003031 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003032 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003033
3034 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003035 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003036
Mark Brown62ea8742012-01-21 21:14:48 +00003037 switch (w->id) {
3038 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003039 w->regulator = devm_regulator_get(dapm->dev, w->name);
3040 if (IS_ERR(w->regulator)) {
3041 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003042 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003043 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003044 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003045 }
Mark Brown8784c772013-01-10 19:33:47 +00003046
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003047 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003048 ret = regulator_allow_bypass(w->regulator, true);
3049 if (ret != 0)
3050 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003051 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003052 w->name, ret);
3053 }
Mark Brown62ea8742012-01-21 21:14:48 +00003054 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003055 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003056#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003057 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003058 if (IS_ERR(w->clk)) {
3059 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003060 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003061 w->name, ret);
3062 return NULL;
3063 }
Mark Brownec029952012-06-04 08:16:20 +01003064#else
3065 return NULL;
3066#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003067 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003068 default:
3069 break;
3070 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003071
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003072 prefix = soc_dapm_prefix(dapm);
3073 if (prefix)
3074 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003075 else
3076 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3077
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003078 if (w->name == NULL) {
3079 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003080 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003081 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003082
Mark Brown7ca3a182011-10-08 14:04:50 +01003083 switch (w->id) {
3084 case snd_soc_dapm_switch:
3085 case snd_soc_dapm_mixer:
3086 case snd_soc_dapm_mixer_named_ctl:
3087 w->power_check = dapm_generic_check_power;
3088 break;
3089 case snd_soc_dapm_mux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003090 w->power_check = dapm_generic_check_power;
3091 break;
Mark Brown46162742013-06-05 19:36:11 +01003092 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003093 w->power_check = dapm_adc_check_power;
3094 break;
Mark Brown46162742013-06-05 19:36:11 +01003095 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003096 w->power_check = dapm_dac_check_power;
3097 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003098 case snd_soc_dapm_adc:
3099 case snd_soc_dapm_aif_out:
3100 case snd_soc_dapm_dac:
3101 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003102 case snd_soc_dapm_pga:
3103 case snd_soc_dapm_out_drv:
3104 case snd_soc_dapm_input:
3105 case snd_soc_dapm_output:
3106 case snd_soc_dapm_micbias:
3107 case snd_soc_dapm_spk:
3108 case snd_soc_dapm_hp:
3109 case snd_soc_dapm_mic:
3110 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003111 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003112 w->power_check = dapm_generic_check_power;
3113 break;
3114 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003115 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003116 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003117 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003118 w->power_check = dapm_supply_check_power;
3119 break;
3120 default:
3121 w->power_check = dapm_always_on_check_power;
3122 break;
3123 }
3124
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003125 w->dapm = dapm;
3126 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003127 INIT_LIST_HEAD(&w->sources);
3128 INIT_LIST_HEAD(&w->sinks);
3129 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003130 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003131 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003132
3133 /* machine layer set ups unconnected pins and insertions */
3134 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003135 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003136}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003137
3138/**
Mark Brown4ba13272008-05-13 14:51:19 +02003139 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003140 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003141 * @widget: widget array
3142 * @num: number of widgets
3143 *
3144 * Creates new DAPM controls based upon the templates.
3145 *
3146 * Returns 0 for success else error.
3147 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003148int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003149 const struct snd_soc_dapm_widget *widget,
3150 int num)
3151{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003152 struct snd_soc_dapm_widget *w;
3153 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003154 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003155
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003156 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003157 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003158 w = snd_soc_dapm_new_control(dapm, widget);
3159 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003160 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003161 "ASoC: Failed to create DAPM control %s\n",
3162 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003163 ret = -ENOMEM;
3164 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003165 }
Mark Brown4ba13272008-05-13 14:51:19 +02003166 widget++;
3167 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003168 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003169 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003170}
3171EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3172
Mark Brownc74184e2012-04-04 22:12:09 +01003173static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3174 struct snd_kcontrol *kcontrol, int event)
3175{
3176 struct snd_soc_dapm_path *source_p, *sink_p;
3177 struct snd_soc_dai *source, *sink;
3178 const struct snd_soc_pcm_stream *config = w->params;
3179 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003180 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003181 u64 fmt;
3182 int ret;
3183
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003184 if (WARN_ON(!config) ||
3185 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3186 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003187
3188 /* We only support a single source and sink, pick the first */
3189 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3190 list_sink);
3191 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3192 list_source);
3193
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003194 if (WARN_ON(!source_p || !sink_p) ||
3195 WARN_ON(!sink_p->source || !source_p->sink) ||
3196 WARN_ON(!source_p->source || !sink_p->sink))
3197 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003198
3199 source = source_p->source->priv;
3200 sink = sink_p->sink->priv;
3201
3202 /* Be a little careful as we don't want to overflow the mask array */
3203 if (config->formats) {
3204 fmt = ffs(config->formats) - 1;
3205 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003206 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003207 config->formats);
3208 fmt = 0;
3209 }
3210
3211 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003212 params = kzalloc(sizeof(*params), GFP_KERNEL);
3213 if (!params) {
3214 ret = -ENOMEM;
3215 goto out;
3216 }
3217 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003218
Mark Brown9747cec2012-04-26 19:12:21 +01003219 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003220 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003221 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003222 config->rate_max;
3223
Mark Brown9747cec2012-04-26 19:12:21 +01003224 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003225 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003226 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003227 = config->channels_max;
3228
3229 memset(&substream, 0, sizeof(substream));
3230
3231 switch (event) {
3232 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003233 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3234 ret = soc_dai_hw_params(&substream, params, source);
3235 if (ret < 0)
3236 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003237
Benoit Cousson93e69582014-07-08 23:19:38 +02003238 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3239 ret = soc_dai_hw_params(&substream, params, sink);
3240 if (ret < 0)
3241 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003242 break;
3243
3244 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003245 ret = snd_soc_dai_digital_mute(sink, 0,
3246 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003247 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003248 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003249 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003250 break;
3251
3252 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003253 ret = snd_soc_dai_digital_mute(sink, 1,
3254 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003255 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003256 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003257 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003258 break;
3259
3260 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003261 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003262 return -EINVAL;
3263 }
3264
Mark Brown9747cec2012-04-26 19:12:21 +01003265out:
3266 kfree(params);
3267 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003268}
3269
3270int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3271 const struct snd_soc_pcm_stream *params,
3272 struct snd_soc_dapm_widget *source,
3273 struct snd_soc_dapm_widget *sink)
3274{
Mark Brownc74184e2012-04-04 22:12:09 +01003275 struct snd_soc_dapm_widget template;
3276 struct snd_soc_dapm_widget *w;
3277 size_t len;
3278 char *link_name;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003279 int ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003280
3281 len = strlen(source->name) + strlen(sink->name) + 2;
3282 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3283 if (!link_name)
3284 return -ENOMEM;
3285 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3286
3287 memset(&template, 0, sizeof(template));
3288 template.reg = SND_SOC_NOPM;
3289 template.id = snd_soc_dapm_dai_link;
3290 template.name = link_name;
3291 template.event = snd_soc_dai_link_event;
3292 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3293 SND_SOC_DAPM_PRE_PMD;
3294
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003295 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003296
3297 w = snd_soc_dapm_new_control(&card->dapm, &template);
3298 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003299 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003300 link_name);
3301 return -ENOMEM;
3302 }
3303
3304 w->params = params;
3305
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003306 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3307 if (ret)
3308 return ret;
3309 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Mark Brownc74184e2012-04-04 22:12:09 +01003310}
3311
Mark Brown888df392012-02-16 19:37:51 -08003312int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3313 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003314{
Mark Brown888df392012-02-16 19:37:51 -08003315 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003316 struct snd_soc_dapm_widget *w;
3317
Mark Brown888df392012-02-16 19:37:51 -08003318 WARN_ON(dapm->dev != dai->dev);
3319
3320 memset(&template, 0, sizeof(template));
3321 template.reg = SND_SOC_NOPM;
3322
3323 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003324 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003325 template.name = dai->driver->playback.stream_name;
3326 template.sname = dai->driver->playback.stream_name;
3327
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003328 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003329 template.name);
3330
3331 w = snd_soc_dapm_new_control(dapm, &template);
3332 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003333 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003334 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003335 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003336 }
3337
3338 w->priv = dai;
3339 dai->playback_widget = w;
3340 }
3341
3342 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003343 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003344 template.name = dai->driver->capture.stream_name;
3345 template.sname = dai->driver->capture.stream_name;
3346
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003347 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003348 template.name);
3349
3350 w = snd_soc_dapm_new_control(dapm, &template);
3351 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003352 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003353 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003354 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003355 }
3356
3357 w->priv = dai;
3358 dai->capture_widget = w;
3359 }
3360
3361 return 0;
3362}
3363
3364int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3365{
3366 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003367 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003368 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003369
3370 /* For each DAI widget... */
3371 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003372 switch (dai_w->id) {
3373 case snd_soc_dapm_dai_in:
3374 case snd_soc_dapm_dai_out:
3375 break;
3376 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003377 continue;
Mark Brown46162742013-06-05 19:36:11 +01003378 }
Mark Brown888df392012-02-16 19:37:51 -08003379
3380 dai = dai_w->priv;
3381
3382 /* ...find all widgets with the same stream and link them */
3383 list_for_each_entry(w, &card->widgets, list) {
3384 if (w->dapm != dai_w->dapm)
3385 continue;
3386
Mark Brown46162742013-06-05 19:36:11 +01003387 switch (w->id) {
3388 case snd_soc_dapm_dai_in:
3389 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003390 continue;
Mark Brown46162742013-06-05 19:36:11 +01003391 default:
3392 break;
3393 }
Mark Brown888df392012-02-16 19:37:51 -08003394
Russell King19c2c5f2013-08-04 20:24:03 +01003395 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003396 continue;
3397
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003398 if (dai_w->id == snd_soc_dapm_dai_in) {
3399 src = dai_w;
3400 sink = w;
3401 } else {
3402 src = w;
3403 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003404 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003405 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3406 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003407 }
3408 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003409
Mark Brown888df392012-02-16 19:37:51 -08003410 return 0;
3411}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003412
Benoit Cousson44ba2642014-07-08 23:19:36 +02003413static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3414 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003415{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003416 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003417 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003418 int i;
3419
Benoit Cousson44ba2642014-07-08 23:19:36 +02003420 for (i = 0; i < rtd->num_codecs; i++) {
3421 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003422
3423 /* there is no point in connecting BE DAI links with dummies */
3424 if (snd_soc_dai_is_dummy(codec_dai) ||
3425 snd_soc_dai_is_dummy(cpu_dai))
3426 continue;
3427
3428 /* connect BE DAI playback if widgets are valid */
3429 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003430 source = cpu_dai->playback_widget;
3431 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003432 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003433 cpu_dai->component->name, source->name,
3434 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003435
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003436 snd_soc_dapm_add_path(&card->dapm, source, sink,
3437 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003438 }
3439
3440 /* connect BE DAI capture if widgets are valid */
3441 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003442 source = codec_dai->capture_widget;
3443 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003444 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003445 codec_dai->component->name, source->name,
3446 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003447
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003448 snd_soc_dapm_add_path(&card->dapm, source, sink,
3449 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003450 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003451 }
3452}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003453
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003454static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3455 int event)
3456{
3457 struct snd_soc_dapm_widget *w;
3458
3459 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3460 w = dai->playback_widget;
3461 else
3462 w = dai->capture_widget;
3463
3464 if (w) {
3465 dapm_mark_dirty(w, "stream event");
3466
3467 switch (event) {
3468 case SND_SOC_DAPM_STREAM_START:
3469 w->active = 1;
3470 break;
3471 case SND_SOC_DAPM_STREAM_STOP:
3472 w->active = 0;
3473 break;
3474 case SND_SOC_DAPM_STREAM_SUSPEND:
3475 case SND_SOC_DAPM_STREAM_RESUME:
3476 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3477 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3478 break;
3479 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003480 }
3481}
3482
Benoit Cousson44ba2642014-07-08 23:19:36 +02003483void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3484{
3485 struct snd_soc_pcm_runtime *rtd = card->rtd;
3486 int i;
3487
3488 /* for each BE DAI link... */
3489 for (i = 0; i < card->num_rtd; i++) {
3490 rtd = &card->rtd[i];
3491
3492 /*
3493 * dynamic FE links have no fixed DAI mapping.
3494 * CODEC<->CODEC links have no direct connection.
3495 */
3496 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3497 continue;
3498
3499 dapm_connect_dai_link_widgets(card, rtd);
3500 }
3501}
3502
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003503static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3504 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003505{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003506 int i;
3507
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003508 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Benoit Cousson44ba2642014-07-08 23:19:36 +02003509 for (i = 0; i < rtd->num_codecs; i++)
3510 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003511
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003512 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003513}
3514
3515/**
3516 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3517 * @rtd: PCM runtime data
3518 * @stream: stream name
3519 * @event: stream event
3520 *
3521 * Sends a stream event to the dapm core. The core then makes any
3522 * necessary widget power changes.
3523 *
3524 * Returns 0 for success else error.
3525 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003526void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3527 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003528{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003529 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003530
Liam Girdwood3cd04342012-03-09 12:02:08 +00003531 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003532 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003533 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003534}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003535
3536/**
Charles Keepax11391102014-02-18 15:22:14 +00003537 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3538 * @dapm: DAPM context
3539 * @pin: pin name
3540 *
3541 * Enables input/output pin and its parents or children widgets iff there is
3542 * a valid audio route and active audio stream.
3543 *
3544 * Requires external locking.
3545 *
3546 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3547 * do any widget power switching.
3548 */
3549int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3550 const char *pin)
3551{
3552 return snd_soc_dapm_set_pin(dapm, pin, 1);
3553}
3554EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3555
3556/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003557 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003558 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003559 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003560 *
Mark Brown74b8f952009-06-06 11:26:15 +01003561 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003562 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003563 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003564 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3565 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003566 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003567int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003568{
Charles Keepax11391102014-02-18 15:22:14 +00003569 int ret;
3570
3571 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3572
3573 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3574
3575 mutex_unlock(&dapm->card->dapm_mutex);
3576
3577 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003578}
Liam Girdwooda5302182008-07-07 13:35:17 +01003579EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003580
3581/**
Charles Keepax11391102014-02-18 15:22:14 +00003582 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3583 * @dapm: DAPM context
3584 * @pin: pin name
3585 *
3586 * Enables input/output pin regardless of any other state. This is
3587 * intended for use with microphone bias supplies used in microphone
3588 * jack detection.
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_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3596 const char *pin)
3597{
3598 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3599
3600 if (!w) {
3601 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3602 return -EINVAL;
3603 }
3604
3605 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3606 w->connected = 1;
3607 w->force = 1;
3608 dapm_mark_dirty(w, "force enable");
3609
3610 return 0;
3611}
3612EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3613
3614/**
Mark Brownda341832010-03-15 19:23:37 +00003615 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003616 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003617 * @pin: pin name
3618 *
3619 * Enables input/output pin regardless of any other state. This is
3620 * intended for use with microphone bias supplies used in microphone
3621 * jack detection.
3622 *
3623 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3624 * do any widget power switching.
3625 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003626int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3627 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003628{
Charles Keepax11391102014-02-18 15:22:14 +00003629 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003630
Charles Keepax11391102014-02-18 15:22:14 +00003631 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003632
Charles Keepax11391102014-02-18 15:22:14 +00003633 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003634
Charles Keepax11391102014-02-18 15:22:14 +00003635 mutex_unlock(&dapm->card->dapm_mutex);
3636
3637 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003638}
3639EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3640
3641/**
Charles Keepax11391102014-02-18 15:22:14 +00003642 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3643 * @dapm: DAPM context
3644 * @pin: pin name
3645 *
3646 * Disables input/output pin and its parents or children widgets.
3647 *
3648 * Requires external locking.
3649 *
3650 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3651 * do any widget power switching.
3652 */
3653int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3654 const char *pin)
3655{
3656 return snd_soc_dapm_set_pin(dapm, pin, 0);
3657}
3658EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3659
3660/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003661 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003662 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003663 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003664 *
Mark Brown74b8f952009-06-06 11:26:15 +01003665 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003666 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003667 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3668 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003669 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003670int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3671 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003672{
Charles Keepax11391102014-02-18 15:22:14 +00003673 int ret;
3674
3675 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3676
3677 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3678
3679 mutex_unlock(&dapm->card->dapm_mutex);
3680
3681 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003682}
3683EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3684
3685/**
Charles Keepax11391102014-02-18 15:22:14 +00003686 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3687 * @dapm: DAPM context
3688 * @pin: pin name
3689 *
3690 * Marks the specified pin as being not connected, disabling it along
3691 * any parent or child widgets. At present this is identical to
3692 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3693 * additional things such as disabling controls which only affect
3694 * paths through the pin.
3695 *
3696 * Requires external locking.
3697 *
3698 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3699 * do any widget power switching.
3700 */
3701int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3702 const char *pin)
3703{
3704 return snd_soc_dapm_set_pin(dapm, pin, 0);
3705}
3706EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3707
3708/**
Mark Brown5817b522008-09-24 11:23:11 +01003709 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003710 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003711 * @pin: pin name
3712 *
3713 * Marks the specified pin as being not connected, disabling it along
3714 * any parent or child widgets. At present this is identical to
3715 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3716 * additional things such as disabling controls which only affect
3717 * paths through the pin.
3718 *
3719 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3720 * do any widget power switching.
3721 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003722int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003723{
Charles Keepax11391102014-02-18 15:22:14 +00003724 int ret;
3725
3726 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3727
3728 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3729
3730 mutex_unlock(&dapm->card->dapm_mutex);
3731
3732 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003733}
3734EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3735
3736/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003737 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003738 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003739 * @pin: audio signal pin endpoint (or start point)
3740 *
3741 * Get audio pin status - connected or disconnected.
3742 *
3743 * Returns 1 for connected otherwise 0.
3744 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003745int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3746 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003747{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003748 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003749
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003750 if (w)
3751 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003752
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003753 return 0;
3754}
Liam Girdwooda5302182008-07-07 13:35:17 +01003755EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003756
3757/**
Mark Brown1547aba2010-05-07 21:11:40 +01003758 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003759 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003760 * @pin: audio signal pin endpoint (or start point)
3761 *
3762 * Mark the given endpoint or pin as ignoring suspend. When the
3763 * system is disabled a path between two endpoints flagged as ignoring
3764 * suspend will not be disabled. The path must already be enabled via
3765 * normal means at suspend time, it will not be turned on if it was not
3766 * already enabled.
3767 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003768int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3769 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003770{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003771 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003772
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003773 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003774 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003775 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003776 }
3777
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003778 w->ignore_suspend = 1;
3779
3780 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003781}
3782EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3783
Stephen Warren16332812011-11-23 12:42:04 -07003784static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3785 struct snd_soc_dapm_widget *w)
3786{
3787 struct snd_soc_dapm_path *p;
3788
3789 list_for_each_entry(p, &card->paths, list) {
3790 if ((p->source == w) || (p->sink == w)) {
3791 dev_dbg(card->dev,
3792 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3793 p->source->name, p->source->id, p->source->dapm,
3794 p->sink->name, p->sink->id, p->sink->dapm);
3795
3796 /* Connected to something other than the codec */
3797 if (p->source->dapm != p->sink->dapm)
3798 return true;
3799 /*
3800 * Loopback connection from codec external pin to
3801 * codec external pin
3802 */
3803 if (p->sink->id == snd_soc_dapm_input) {
3804 switch (p->source->id) {
3805 case snd_soc_dapm_output:
3806 case snd_soc_dapm_micbias:
3807 return true;
3808 default:
3809 break;
3810 }
3811 }
3812 }
3813 }
3814
3815 return false;
3816}
3817
3818/**
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003819 * snd_soc_dapm_auto_nc_pins - call snd_soc_dapm_nc_pin for unused pins
3820 * @card: The card whose pins should be processed
Stephen Warren16332812011-11-23 12:42:04 -07003821 *
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003822 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the card
3823 * which are unused. Pins are used if they are connected externally to a
3824 * component, whether that be to some other device, or a loop-back connection to
3825 * the component itself.
Stephen Warren16332812011-11-23 12:42:04 -07003826 */
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003827void snd_soc_dapm_auto_nc_pins(struct snd_soc_card *card)
Stephen Warren16332812011-11-23 12:42:04 -07003828{
Stephen Warren16332812011-11-23 12:42:04 -07003829 struct snd_soc_dapm_widget *w;
3830
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003831 dev_dbg(card->dev, "ASoC: Auto NC: DAPMs: card:%p\n", &card->dapm);
Stephen Warren16332812011-11-23 12:42:04 -07003832
3833 list_for_each_entry(w, &card->widgets, list) {
Stephen Warren16332812011-11-23 12:42:04 -07003834 switch (w->id) {
3835 case snd_soc_dapm_input:
3836 case snd_soc_dapm_output:
3837 case snd_soc_dapm_micbias:
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003838 dev_dbg(card->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003839 w->name);
3840 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003841 dev_dbg(card->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003842 "... Not in map; disabling\n");
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003843 snd_soc_dapm_nc_pin(w->dapm, w->name);
Stephen Warren16332812011-11-23 12:42:04 -07003844 }
3845 break;
3846 default:
3847 break;
3848 }
3849 }
3850}
3851
Mark Brown1547aba2010-05-07 21:11:40 +01003852/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003853 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003854 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003855 *
3856 * Free all dapm widgets and resources.
3857 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003858void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003859{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003860 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003861 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003862 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003863 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003864}
3865EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3866
Xiang Xiao57996352014-03-02 00:04:02 +08003867static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003868{
Liam Girdwood01005a72012-07-06 16:57:05 +01003869 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003870 struct snd_soc_dapm_widget *w;
3871 LIST_HEAD(down_list);
3872 int powerdown = 0;
3873
Liam Girdwood01005a72012-07-06 16:57:05 +01003874 mutex_lock(&card->dapm_mutex);
3875
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003876 list_for_each_entry(w, &dapm->card->widgets, list) {
3877 if (w->dapm != dapm)
3878 continue;
Mark Brown51737472009-06-22 13:16:51 +01003879 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003880 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003881 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003882 powerdown = 1;
3883 }
3884 }
3885
3886 /* If there were no widgets to power down we're already in
3887 * standby.
3888 */
3889 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003890 if (dapm->bias_level == SND_SOC_BIAS_ON)
3891 snd_soc_dapm_set_bias_level(dapm,
3892 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003893 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003894 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3895 snd_soc_dapm_set_bias_level(dapm,
3896 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003897 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003898
3899 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003900}
Mark Brown51737472009-06-22 13:16:51 +01003901
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003902/*
3903 * snd_soc_dapm_shutdown - callback for system shutdown
3904 */
3905void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3906{
Xiang Xiao57996352014-03-02 00:04:02 +08003907 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003908
Xiang Xiao57996352014-03-02 00:04:02 +08003909 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08003910 if (dapm != &card->dapm) {
3911 soc_dapm_shutdown_dapm(dapm);
3912 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3913 snd_soc_dapm_set_bias_level(dapm,
3914 SND_SOC_BIAS_OFF);
3915 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003916 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08003917
3918 soc_dapm_shutdown_dapm(&card->dapm);
3919 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3920 snd_soc_dapm_set_bias_level(&card->dapm,
3921 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01003922}
3923
Richard Purdie2b97eab2006-10-06 18:32:18 +02003924/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003925MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003926MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3927MODULE_LICENSE("GPL");