blob: 10ed22f457b87bb556da69aaa1b6c9a0c0292ee2 [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 Clausen57295072013-08-05 11:27:31 +0200257 kfree(data->widget);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200258 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200259 kfree(data);
260}
261
262static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
263 const struct snd_kcontrol *kcontrol)
264{
265 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
266
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200267 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200268}
269
270static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
271 struct snd_soc_dapm_widget *widget)
272{
273 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200274 struct snd_soc_dapm_widget_list *new_wlist;
275 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200276
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200277 if (data->wlist)
278 n = data->wlist->num_widgets + 1;
279 else
280 n = 1;
281
282 new_wlist = krealloc(data->wlist,
283 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
284 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200285 return -ENOMEM;
286
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200287 new_wlist->widgets[n - 1] = widget;
288 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200289
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200290 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200291
292 return 0;
293}
294
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200295static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
296 struct snd_soc_dapm_path *path)
297{
298 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
299
300 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200301
302 if (data->widget) {
303 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
304 path->source, NULL, NULL);
305 }
306}
307
308static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
309{
310 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
311
312 if (!data->widget)
313 return true;
314
315 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200316}
317
318static struct list_head *dapm_kcontrol_get_path_list(
319 const struct snd_kcontrol *kcontrol)
320{
321 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
322
323 return &data->paths;
324}
325
326#define dapm_kcontrol_for_each_path(path, kcontrol) \
327 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
328 list_kcontrol)
329
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200330static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
331{
332 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
333
334 return data->value;
335}
336
337static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
338 unsigned int value)
339{
340 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
341
342 if (data->value == value)
343 return false;
344
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200345 if (data->widget)
346 data->widget->on_val = value;
347
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200348 data->value = value;
349
350 return true;
351}
352
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200353/**
354 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
355 * @kcontrol: The kcontrol
356 */
357struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
358{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200359 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200360}
361EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
362
Liam Girdwood6c120e12012-02-15 15:15:34 +0000363static void dapm_reset(struct snd_soc_card *card)
364{
365 struct snd_soc_dapm_widget *w;
366
Mark Brownf9fa2b12014-03-06 16:49:11 +0800367 lockdep_assert_held(&card->dapm_mutex);
368
Liam Girdwood6c120e12012-02-15 15:15:34 +0000369 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
370
371 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200372 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000373 w->power_checked = false;
374 w->inputs = -1;
375 w->outputs = -1;
376 }
377}
378
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800379static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg,
380 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100381{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200382 if (!w->dapm->component)
383 return -EIO;
384 return snd_soc_component_read(w->dapm->component, reg, value);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100385}
386
Lars-Peter Clausen23d54422014-04-22 13:23:16 +0200387static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
Bard Liao34775012014-04-17 20:12:56 +0800388 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100389{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200390 if (!w->dapm->component)
391 return -EIO;
392 return snd_soc_component_update_bits_async(w->dapm->component, reg,
393 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000394}
395
Mark Browneb270e92013-10-09 13:52:52 +0100396static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
397{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200398 if (dapm->component)
399 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100400}
401
Mark Brown452c5ea2009-05-17 21:41:23 +0100402/**
403 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800404 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100405 * @level: level to configure
406 *
407 * Configure the bias (power) levels for the SoC audio device.
408 *
409 * Returns 0 for success else error.
410 */
Mark Browned5a4c42011-02-18 11:12:42 -0800411static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200412 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100413{
Mark Browned5a4c42011-02-18 11:12:42 -0800414 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100415 int ret = 0;
416
Mark Brown84e90932010-11-04 00:07:02 -0400417 trace_snd_soc_bias_level_start(card, level);
418
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000419 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100420 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100421 if (ret != 0)
422 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100423
Mark Browncc4c6702011-06-06 19:03:34 +0100424 if (dapm->codec) {
425 if (dapm->codec->driver->set_bias_level)
426 ret = dapm->codec->driver->set_bias_level(dapm->codec,
427 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100428 else
429 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100430 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100431 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100432 }
Liam Girdwood41231282012-07-06 16:56:16 +0100433
Mark Brown171ec6b2011-06-06 18:15:19 +0100434 if (ret != 0)
435 goto out;
436
437 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100438 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100439out:
Mark Brown84e90932010-11-04 00:07:02 -0400440 trace_snd_soc_bias_level_done(card, level);
441
Mark Brown452c5ea2009-05-17 21:41:23 +0100442 return ret;
443}
444
Mark Brown74b8f952009-06-06 11:26:15 +0100445/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200446static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200447 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
448 struct snd_soc_dapm_path *path, const char *control_name,
449 const struct snd_kcontrol_new *kcontrol)
450{
451 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100452 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200453 int i;
454
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100455 if (e->reg != SND_SOC_NOPM) {
456 soc_widget_read(dest, e->reg, &val);
457 val = (val >> e->shift_l) & e->mask;
458 item = snd_soc_enum_val_to_item(e, val);
459 } else {
460 /* since a virtual mux has no backing registers to
461 * decide which path to connect, it will try to match
462 * with the first enumeration. This is to ensure
463 * that the default mux choice (the first) will be
464 * correctly powered up during initialization.
465 */
466 item = 0;
467 }
468
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100469 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200470 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200471 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200472 list_add(&path->list_sink, &dest->sources);
473 list_add(&path->list_source, &src->sinks);
474 path->name = (char*)e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100475 if (i == item)
476 path->connect = 1;
477 else
478 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200479 return 0;
480 }
481 }
482
483 return -ENODEV;
484}
485
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100486/* set up initial codec paths */
487static void dapm_set_mixer_path_status(struct snd_soc_dapm_widget *w,
488 struct snd_soc_dapm_path *p, int i)
489{
490 struct soc_mixer_control *mc = (struct soc_mixer_control *)
491 w->kcontrol_news[i].private_value;
492 unsigned int reg = mc->reg;
493 unsigned int shift = mc->shift;
494 unsigned int max = mc->max;
495 unsigned int mask = (1 << fls(max)) - 1;
496 unsigned int invert = mc->invert;
497 unsigned int val;
498
499 if (reg != SND_SOC_NOPM) {
500 soc_widget_read(w, reg, &val);
501 val = (val >> shift) & mask;
502 if (invert)
503 val = max - val;
504 p->connect = !!val;
505 } else {
506 p->connect = 0;
507 }
508}
509
Mark Brown74b8f952009-06-06 11:26:15 +0100510/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200511static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200512 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
513 struct snd_soc_dapm_path *path, const char *control_name)
514{
515 int i;
516
517 /* search for mixer kcontrol */
518 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600519 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200520 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200521 list_add(&path->list_sink, &dest->sources);
522 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600523 path->name = dest->kcontrol_news[i].name;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100524 dapm_set_mixer_path_status(dest, path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200525 return 0;
526 }
527 }
528 return -ENODEV;
529}
530
Stephen Warrenaf468002011-04-28 17:38:01 -0600531static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600532 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600533 const struct snd_kcontrol_new *kcontrol_new,
534 struct snd_kcontrol **kcontrol)
535{
536 struct snd_soc_dapm_widget *w;
537 int i;
538
539 *kcontrol = NULL;
540
541 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600542 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
543 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600544 for (i = 0; i < w->num_kcontrols; i++) {
545 if (&w->kcontrol_news[i] == kcontrol_new) {
546 if (w->kcontrols)
547 *kcontrol = w->kcontrols[i];
548 return 1;
549 }
550 }
551 }
552
553 return 0;
554}
555
Stephen Warren85762e72013-03-29 15:40:10 -0600556/*
557 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
558 * create it. Either way, add the widget into the control's widget list
559 */
560static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100561 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200562{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200563 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000564 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000565 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600566 size_t prefix_len;
567 int shared;
568 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600569 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600570 char *long_name;
571 const char *name;
572 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000573
574 if (dapm->codec)
575 prefix = dapm->codec->name_prefix;
576 else
577 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200578
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000579 if (prefix)
580 prefix_len = strlen(prefix) + 1;
581 else
582 prefix_len = 0;
583
Stephen Warren85762e72013-03-29 15:40:10 -0600584 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
585 &kcontrol);
586
Stephen Warren85762e72013-03-29 15:40:10 -0600587 if (!kcontrol) {
588 if (shared) {
589 wname_in_long_name = false;
590 kcname_in_long_name = true;
591 } else {
592 switch (w->id) {
593 case snd_soc_dapm_switch:
594 case snd_soc_dapm_mixer:
595 wname_in_long_name = true;
596 kcname_in_long_name = true;
597 break;
598 case snd_soc_dapm_mixer_named_ctl:
599 wname_in_long_name = false;
600 kcname_in_long_name = true;
601 break;
602 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600603 wname_in_long_name = true;
604 kcname_in_long_name = false;
605 break;
606 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600607 return -EINVAL;
608 }
609 }
610
611 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600612 /*
613 * The control will get a prefix from the control
614 * creation process but we're also using the same
615 * prefix for widgets so cut the prefix off the
616 * front of the widget name.
617 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200618 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600619 w->name + prefix_len,
620 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200621 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200622 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600623
624 name = long_name;
625 } else if (wname_in_long_name) {
626 long_name = NULL;
627 name = w->name + prefix_len;
628 } else {
629 long_name = NULL;
630 name = w->kcontrol_news[kci].name;
631 }
632
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200633 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600634 prefix);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200635 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200636 if (!kcontrol)
637 return -ENOMEM;
638 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200639
640 ret = dapm_kcontrol_data_alloc(w, kcontrol);
641 if (ret) {
642 snd_ctl_free_one(kcontrol);
643 return ret;
644 }
645
Stephen Warren85762e72013-03-29 15:40:10 -0600646 ret = snd_ctl_add(card, kcontrol);
647 if (ret < 0) {
648 dev_err(dapm->dev,
649 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
650 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600651 return ret;
652 }
Stephen Warren85762e72013-03-29 15:40:10 -0600653 }
654
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200655 ret = dapm_kcontrol_add_widget(kcontrol, w);
656 if (ret)
657 return ret;
658
Stephen Warren85762e72013-03-29 15:40:10 -0600659 w->kcontrols[kci] = kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600660
661 return 0;
662}
663
664/* create new dapm mixer control */
665static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
666{
667 int i, ret;
668 struct snd_soc_dapm_path *path;
669
Richard Purdie2b97eab2006-10-06 18:32:18 +0200670 /* add kcontrol */
671 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200672 /* match name */
673 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200674 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600675 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200676 continue;
677
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200678 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200679 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200680 continue;
681 }
682
Mark Brown946d92a2013-08-12 23:28:42 +0100683 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600684 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200685 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100686
687 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200688 }
689 }
Stephen Warren85762e72013-03-29 15:40:10 -0600690
691 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200692}
693
694/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200695static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200696{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200697 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600698 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600699 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200700
Stephen Warrenaf468002011-04-28 17:38:01 -0600701 if (w->num_kcontrols != 1) {
702 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000703 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600704 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200705 return -EINVAL;
706 }
707
Lars-Peter Clausenfe581392013-08-01 18:30:38 +0200708 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600709 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
710 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600711 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200712
Mark Brown946d92a2013-08-12 23:28:42 +0100713 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600714 if (ret < 0)
715 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600716
Richard Purdie2b97eab2006-10-06 18:32:18 +0200717 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200718 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200719
Stephen Warrenaf468002011-04-28 17:38:01 -0600720 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200721}
722
723/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200724static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200725{
Mark Browna6c65732010-03-03 17:45:21 +0000726 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200727 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000728 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200729
Mark Browna6c65732010-03-03 17:45:21 +0000730 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200731}
732
733/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100734static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
735 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200736{
737 struct snd_soc_dapm_path *p;
738
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100739 list_for_each_entry(p, sink, list_source) {
740 if (p->walked) {
741 p->walked = 0;
742 dapm_clear_walk_output(dapm, &p->sink->sinks);
743 }
744 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200745}
746
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100747static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
748 struct list_head *source)
749{
750 struct snd_soc_dapm_path *p;
751
752 list_for_each_entry(p, source, list_sink) {
753 if (p->walked) {
754 p->walked = 0;
755 dapm_clear_walk_input(dapm, &p->source->sources);
756 }
757 }
758}
759
760
Mark Brown99497882010-05-07 20:24:05 +0100761/* We implement power down on suspend by checking the power state of
762 * the ALSA card - when we are suspending the ALSA state for the card
763 * is set to D3.
764 */
765static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
766{
Mark Brown12ea2c72011-03-02 18:17:32 +0000767 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100768
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000769 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100770 case SNDRV_CTL_POWER_D3hot:
771 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100772 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000773 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200774 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100775 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100776 default:
777 return 1;
778 }
779}
780
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100781/* add widget to list if it's not already in the list */
782static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
783 struct snd_soc_dapm_widget *w)
784{
785 struct snd_soc_dapm_widget_list *wlist;
786 int wlistsize, wlistentries, i;
787
788 if (*list == NULL)
789 return -EINVAL;
790
791 wlist = *list;
792
793 /* is this widget already in the list */
794 for (i = 0; i < wlist->num_widgets; i++) {
795 if (wlist->widgets[i] == w)
796 return 0;
797 }
798
799 /* allocate some new space */
800 wlistentries = wlist->num_widgets + 1;
801 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
802 wlistentries * sizeof(struct snd_soc_dapm_widget *);
803 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
804 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000805 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100806 w->name);
807 return -ENOMEM;
808 }
809 wlist = *list;
810
811 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000812 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100813 w->name, wlist->num_widgets);
814
815 wlist->widgets[wlist->num_widgets] = w;
816 wlist->num_widgets++;
817 return 1;
818}
819
Richard Purdie2b97eab2006-10-06 18:32:18 +0200820/*
821 * Recursively check for a completed path to an active or physically connected
822 * output widget. Returns number of complete paths.
823 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100824static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
825 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200826{
827 struct snd_soc_dapm_path *path;
828 int con = 0;
829
Mark Brown024dc072011-10-09 11:52:05 +0100830 if (widget->outputs >= 0)
831 return widget->outputs;
832
Mark Brownde02d072011-09-20 21:43:24 +0100833 DAPM_UPDATE_STAT(widget, path_checks);
834
Mark Brown62ea8742012-01-21 21:14:48 +0000835 switch (widget->id) {
836 case snd_soc_dapm_supply:
837 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200838 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200839 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100840 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000841 default:
842 break;
843 }
Mark Brown246d0a12009-04-22 18:24:55 +0100844
Mark Brown010ff262009-08-17 17:39:22 +0100845 switch (widget->id) {
846 case snd_soc_dapm_adc:
847 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100848 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100849 if (widget->active) {
850 widget->outputs = snd_soc_dapm_suspend_check(widget);
851 return widget->outputs;
852 }
Mark Brown010ff262009-08-17 17:39:22 +0100853 default:
854 break;
855 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200856
857 if (widget->connected) {
858 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100859 if (widget->id == snd_soc_dapm_output && !widget->ext) {
860 widget->outputs = snd_soc_dapm_suspend_check(widget);
861 return widget->outputs;
862 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200863
864 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100865 if (widget->id == snd_soc_dapm_hp ||
866 widget->id == snd_soc_dapm_spk ||
867 (widget->id == snd_soc_dapm_line &&
868 !list_empty(&widget->sources))) {
869 widget->outputs = snd_soc_dapm_suspend_check(widget);
870 return widget->outputs;
871 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200872 }
873
874 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100875 DAPM_UPDATE_STAT(widget, neighbour_checks);
876
Mark Brownbf3a9e12011-06-13 16:42:29 +0100877 if (path->weak)
878 continue;
879
Mark Brown8af294b2013-02-22 17:48:15 +0000880 if (path->walking)
881 return 1;
882
Richard Purdie2b97eab2006-10-06 18:32:18 +0200883 if (path->walked)
884 continue;
885
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100886 trace_snd_soc_dapm_output_path(widget, path);
887
Richard Purdie2b97eab2006-10-06 18:32:18 +0200888 if (path->sink && path->connect) {
889 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000890 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100891
892 /* do we need to add this widget to the list ? */
893 if (list) {
894 int err;
895 err = dapm_list_add_widget(list, path->sink);
896 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000897 dev_err(widget->dapm->dev,
898 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100899 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000900 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100901 return con;
902 }
903 }
904
905 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000906
907 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200908 }
909 }
910
Mark Brown024dc072011-10-09 11:52:05 +0100911 widget->outputs = con;
912
Richard Purdie2b97eab2006-10-06 18:32:18 +0200913 return con;
914}
915
916/*
917 * Recursively check for a completed path to an active or physically connected
918 * input widget. Returns number of complete paths.
919 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100920static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
921 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200922{
923 struct snd_soc_dapm_path *path;
924 int con = 0;
925
Mark Brown024dc072011-10-09 11:52:05 +0100926 if (widget->inputs >= 0)
927 return widget->inputs;
928
Mark Brownde02d072011-09-20 21:43:24 +0100929 DAPM_UPDATE_STAT(widget, path_checks);
930
Mark Brown62ea8742012-01-21 21:14:48 +0000931 switch (widget->id) {
932 case snd_soc_dapm_supply:
933 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200934 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200935 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100936 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000937 default:
938 break;
939 }
Mark Brown246d0a12009-04-22 18:24:55 +0100940
Richard Purdie2b97eab2006-10-06 18:32:18 +0200941 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100942 switch (widget->id) {
943 case snd_soc_dapm_dac:
944 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +0100945 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +0100946 if (widget->active) {
947 widget->inputs = snd_soc_dapm_suspend_check(widget);
948 return widget->inputs;
949 }
Mark Brown010ff262009-08-17 17:39:22 +0100950 default:
951 break;
952 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200953
954 if (widget->connected) {
955 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100956 if (widget->id == snd_soc_dapm_input && !widget->ext) {
957 widget->inputs = snd_soc_dapm_suspend_check(widget);
958 return widget->inputs;
959 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200960
961 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100962 if (widget->id == snd_soc_dapm_vmid) {
963 widget->inputs = snd_soc_dapm_suspend_check(widget);
964 return widget->inputs;
965 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200966
967 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300968 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100969 (widget->id == snd_soc_dapm_line &&
970 !list_empty(&widget->sinks))) {
971 widget->inputs = snd_soc_dapm_suspend_check(widget);
972 return widget->inputs;
973 }
974
Mark Brown1ab97c82011-11-27 16:21:51 +0000975 /* signal generator */
976 if (widget->id == snd_soc_dapm_siggen) {
977 widget->inputs = snd_soc_dapm_suspend_check(widget);
978 return widget->inputs;
979 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200980 }
981
982 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +0100983 DAPM_UPDATE_STAT(widget, neighbour_checks);
984
Mark Brownbf3a9e12011-06-13 16:42:29 +0100985 if (path->weak)
986 continue;
987
Mark Brown8af294b2013-02-22 17:48:15 +0000988 if (path->walking)
989 return 1;
990
Richard Purdie2b97eab2006-10-06 18:32:18 +0200991 if (path->walked)
992 continue;
993
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100994 trace_snd_soc_dapm_input_path(widget, path);
995
Richard Purdie2b97eab2006-10-06 18:32:18 +0200996 if (path->source && path->connect) {
997 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000998 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100999
1000 /* do we need to add this widget to the list ? */
1001 if (list) {
1002 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001003 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001004 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001005 dev_err(widget->dapm->dev,
1006 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001007 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001008 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001009 return con;
1010 }
1011 }
1012
1013 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001014
1015 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001016 }
1017 }
1018
Mark Brown024dc072011-10-09 11:52:05 +01001019 widget->inputs = con;
1020
Richard Purdie2b97eab2006-10-06 18:32:18 +02001021 return con;
1022}
1023
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001024/**
1025 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1026 * @dai: the soc DAI.
1027 * @stream: stream direction.
1028 * @list: list of active widgets for this stream.
1029 *
1030 * Queries DAPM graph as to whether an valid audio stream path exists for
1031 * the initial stream specified by name. This takes into account
1032 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1033 *
1034 * Returns the number of valid paths or negative error.
1035 */
1036int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1037 struct snd_soc_dapm_widget_list **list)
1038{
1039 struct snd_soc_card *card = dai->card;
1040 int paths;
1041
1042 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1043 dapm_reset(card);
1044
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001045 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001046 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001047 dapm_clear_walk_output(&card->dapm,
1048 &dai->playback_widget->sinks);
1049 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001050 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001051 dapm_clear_walk_input(&card->dapm,
1052 &dai->capture_widget->sources);
1053 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001054
1055 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001056 mutex_unlock(&card->dapm_mutex);
1057
1058 return paths;
1059}
1060
Richard Purdie2b97eab2006-10-06 18:32:18 +02001061/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001062 * Handler for generic register modifier widget.
1063 */
1064int dapm_reg_event(struct snd_soc_dapm_widget *w,
1065 struct snd_kcontrol *kcontrol, int event)
1066{
1067 unsigned int val;
1068
1069 if (SND_SOC_DAPM_EVENT_ON(event))
1070 val = w->on_val;
1071 else
1072 val = w->off_val;
1073
Lars-Peter Clausen23d54422014-04-22 13:23:16 +02001074 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001075 w->mask << w->shift, val << w->shift);
1076
1077 return 0;
1078}
Mark Brown11589412008-07-29 11:42:23 +01001079EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001080
Mark Brown62ea8742012-01-21 21:14:48 +00001081/*
1082 * 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{
Mark Brown68f89ad2010-11-03 23:51:49 -04001332 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001333 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001334 unsigned int value = 0;
1335 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001336
1337 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1338 power_list)->reg;
1339
1340 list_for_each_entry(w, pending, power_list) {
Takashi Iwaibf4edea2013-11-07 18:38:47 +01001341 WARN_ON(reg != w->reg);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001342 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001343
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001344 mask |= w->mask << w->shift;
1345 if (w->power)
1346 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001347 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001348 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001349
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001350 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001351 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1352 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001353
Mark Brown68f89ad2010-11-03 23:51:49 -04001354 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001355 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1356 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001357 }
1358
Mark Brown81628102009-06-07 13:21:24 +01001359 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001360 /* Any widget will do, they should all be updating the
1361 * same register.
1362 */
1363 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1364 power_list);
1365
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001366 pop_dbg(w->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 Clausen23d54422014-04-22 13:23:16 +02001370 soc_widget_update_bits(w, 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 Clausen23d54422014-04-22 13:23:16 +02001516 ret = soc_widget_update_bits(w, update->reg, update->mask, update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001517 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001518 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001519 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001520
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001521 for (wi = 0; wi < wlist->num_widgets; wi++) {
1522 w = wlist->widgets[wi];
1523
1524 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1525 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1526 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001527 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001528 w->name, ret);
1529 }
Mark Brown97404f22010-12-14 16:13:57 +00001530 }
1531}
1532
Mark Brown9d0624a2011-02-18 11:49:43 -08001533/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1534 * they're changing state.
1535 */
1536static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1537{
1538 struct snd_soc_dapm_context *d = data;
1539 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001540
Mark Brown56fba412011-06-04 11:25:10 +01001541 /* If we're off and we're not supposed to be go into STANDBY */
1542 if (d->bias_level == SND_SOC_BIAS_OFF &&
1543 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001544 if (d->dev)
1545 pm_runtime_get_sync(d->dev);
1546
Mark Brown9d0624a2011-02-18 11:49:43 -08001547 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1548 if (ret != 0)
1549 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001550 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001551 }
1552
Mark Brown56fba412011-06-04 11:25:10 +01001553 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1554 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001555 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1556 if (ret != 0)
1557 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001558 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001559 }
1560}
1561
1562/* Async callback run prior to DAPM sequences - brings to their final
1563 * state.
1564 */
1565static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1566{
1567 struct snd_soc_dapm_context *d = data;
1568 int ret;
1569
1570 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001571 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1572 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1573 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001574 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1575 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001576 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001577 ret);
1578 }
1579
1580 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001581 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1582 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001583 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1584 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001585 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1586 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001587
1588 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001589 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001590 }
1591
1592 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001593 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1594 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001595 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1596 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001597 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001598 ret);
1599 }
1600}
Mark Brown97404f22010-12-14 16:13:57 +00001601
Mark Brownfe4fda52011-10-03 22:36:57 +01001602static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1603 bool power, bool connect)
1604{
1605 /* If a connection is being made or broken then that update
1606 * will have marked the peer dirty, otherwise the widgets are
1607 * not connected and this update has no impact. */
1608 if (!connect)
1609 return;
1610
1611 /* If the peer is already in the state we're moving to then we
1612 * won't have an impact on it. */
1613 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001614 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001615}
1616
Mark Brown05623c42011-09-28 17:02:31 +01001617static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1618 struct list_head *up_list,
1619 struct list_head *down_list)
1620{
Mark Browndb432b42011-10-03 21:06:40 +01001621 struct snd_soc_dapm_path *path;
1622
Mark Brown05623c42011-09-28 17:02:31 +01001623 if (w->power == power)
1624 return;
1625
1626 trace_snd_soc_dapm_widget_power(w, power);
1627
Mark Browndb432b42011-10-03 21:06:40 +01001628 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001629 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001630 */
1631 list_for_each_entry(path, &w->sources, list_sink) {
1632 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001633 dapm_widget_set_peer_power(path->source, power,
1634 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001635 }
1636 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001637 switch (w->id) {
1638 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001639 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001640 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001641 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001642 /* Supplies can't affect their outputs, only their inputs */
1643 break;
1644 default:
1645 list_for_each_entry(path, &w->sinks, list_source) {
1646 if (path->sink) {
1647 dapm_widget_set_peer_power(path->sink, power,
1648 path->connect);
1649 }
Mark Browndb432b42011-10-03 21:06:40 +01001650 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001651 break;
Mark Browndb432b42011-10-03 21:06:40 +01001652 }
1653
Mark Brown05623c42011-09-28 17:02:31 +01001654 if (power)
1655 dapm_seq_insert(w, up_list, true);
1656 else
1657 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001658}
1659
Mark Brown7c81beb2011-09-20 22:22:32 +01001660static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1661 struct list_head *up_list,
1662 struct list_head *down_list)
1663{
Mark Brown7c81beb2011-09-20 22:22:32 +01001664 int power;
1665
1666 switch (w->id) {
1667 case snd_soc_dapm_pre:
1668 dapm_seq_insert(w, down_list, false);
1669 break;
1670 case snd_soc_dapm_post:
1671 dapm_seq_insert(w, up_list, true);
1672 break;
1673
1674 default:
Mark Brownd8050022011-09-28 18:28:23 +01001675 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001676
Mark Brown05623c42011-09-28 17:02:31 +01001677 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001678 break;
1679 }
1680}
1681
Mark Brown42aa3412009-03-01 19:21:10 +00001682/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001683 * Scan each dapm widget for complete audio path.
1684 * A complete path is a route that has valid endpoints i.e.:-
1685 *
1686 * o DAC to output pin.
1687 * o Input Pin to ADC.
1688 * o Input pin to Output pin (bypass, sidetone)
1689 * o DAC to ADC (loopback).
1690 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001691static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001692{
1693 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001694 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001695 LIST_HEAD(up_list);
1696 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001697 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001698 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001699
Mark Brownf9fa2b12014-03-06 16:49:11 +08001700 lockdep_assert_held(&card->dapm_mutex);
1701
Mark Brown84e90932010-11-04 00:07:02 -04001702 trace_snd_soc_dapm_start(card);
1703
Mark Brown56fba412011-06-04 11:25:10 +01001704 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001705 if (d->idle_bias_off)
1706 d->target_bias_level = SND_SOC_BIAS_OFF;
1707 else
1708 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001709 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001710
Liam Girdwood6c120e12012-02-15 15:15:34 +00001711 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001712
Mark Brown6d3ddc82009-05-16 17:47:29 +01001713 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001714 * lists indicating if they should be powered up or down. We
1715 * only check widgets that have been flagged as dirty but note
1716 * that new widgets may be added to the dirty list while we
1717 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001718 */
Mark Browndb432b42011-10-03 21:06:40 +01001719 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001720 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001721 }
1722
Mark Brownf9de6d72011-09-28 17:19:47 +01001723 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001724 switch (w->id) {
1725 case snd_soc_dapm_pre:
1726 case snd_soc_dapm_post:
1727 /* These widgets always need to be powered */
1728 break;
1729 default:
1730 list_del_init(&w->dirty);
1731 break;
1732 }
Mark Browndb432b42011-10-03 21:06:40 +01001733
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001734 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001735 d = w->dapm;
1736
1737 /* Supplies and micbiases only bring the
1738 * context up to STANDBY as unless something
1739 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001740 * generally don't require full power. Signal
1741 * generators are virtual pins and have no
1742 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001743 */
1744 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001745 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001746 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001747 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001748 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001749 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001750 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001751 case snd_soc_dapm_micbias:
1752 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1753 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1754 break;
1755 default:
1756 d->target_bias_level = SND_SOC_BIAS_ON;
1757 break;
1758 }
1759 }
1760
1761 }
1762
Mark Brown85a843c2011-09-21 21:29:47 +01001763 /* Force all contexts in the card to the same bias state if
1764 * they're not ground referenced.
1765 */
Mark Brown56fba412011-06-04 11:25:10 +01001766 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001767 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001768 if (d->target_bias_level > bias)
1769 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001770 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001771 if (!d->idle_bias_off)
1772 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001773
Mark Brownde02d072011-09-20 21:43:24 +01001774 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001775
Xiang Xiao17282ba2014-03-02 00:04:03 +08001776 /* Run card bias changes at first */
1777 dapm_pre_sequence_async(&card->dapm, 0);
1778 /* Run other bias changes in parallel */
1779 list_for_each_entry(d, &card->dapm_list, list) {
1780 if (d != &card->dapm)
1781 async_schedule_domain(dapm_pre_sequence_async, d,
1782 &async_domain);
1783 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001784 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001785
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001786 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001787 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001788 }
1789
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001790 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001791 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001792 }
1793
Mark Brown6d3ddc82009-05-16 17:47:29 +01001794 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001795 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001796
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001797 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001798
Mark Brown6d3ddc82009-05-16 17:47:29 +01001799 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001800 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001801
Mark Brown9d0624a2011-02-18 11:49:43 -08001802 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001803 list_for_each_entry(d, &card->dapm_list, list) {
1804 if (d != &card->dapm)
1805 async_schedule_domain(dapm_post_sequence_async, d,
1806 &async_domain);
1807 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001808 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001809 /* Run card bias changes at last */
1810 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001811
Liam Girdwood8078d872012-02-15 15:15:35 +00001812 /* do we need to notify any clients that DAPM event is complete */
1813 list_for_each_entry(d, &card->dapm_list, list) {
1814 if (d->stream_event)
1815 d->stream_event(d, event);
1816 }
1817
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001818 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001819 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001820 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001821
Mark Brown84e90932010-11-04 00:07:02 -04001822 trace_snd_soc_dapm_done(card);
1823
Mark Brown42aa3412009-03-01 19:21:10 +00001824 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001825}
1826
Mark Brown79fb9382009-08-21 16:38:13 +01001827#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001828static ssize_t dapm_widget_power_read_file(struct file *file,
1829 char __user *user_buf,
1830 size_t count, loff_t *ppos)
1831{
1832 struct snd_soc_dapm_widget *w = file->private_data;
1833 char *buf;
1834 int in, out;
1835 ssize_t ret;
1836 struct snd_soc_dapm_path *p = NULL;
1837
1838 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1839 if (!buf)
1840 return -ENOMEM;
1841
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001842 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001843 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001844 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001845 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001846
Mark Brownf13ebad2012-03-03 18:01:01 +00001847 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1848 w->name, w->power ? "On" : "Off",
1849 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001850
Mark Brownd033c362009-12-04 15:25:56 +00001851 if (w->reg >= 0)
1852 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001853 " - R%d(0x%x) mask 0x%x",
1854 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001855
1856 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1857
Mark Brown3eef08b2009-09-14 16:49:00 +01001858 if (w->sname)
1859 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1860 w->sname,
1861 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001862
1863 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001864 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001865 continue;
1866
Mark Brown79fb9382009-08-21 16:38:13 +01001867 if (p->connect)
1868 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001869 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001870 p->name ? p->name : "static",
1871 p->source->name);
1872 }
1873 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001874 if (p->connected && !p->connected(w, p->sink))
1875 continue;
1876
Mark Brown79fb9382009-08-21 16:38:13 +01001877 if (p->connect)
1878 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001879 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001880 p->name ? p->name : "static",
1881 p->sink->name);
1882 }
1883
1884 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1885
1886 kfree(buf);
1887 return ret;
1888}
1889
1890static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001891 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001892 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001893 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001894};
1895
Mark Brownef49e4f2011-04-04 20:48:13 +09001896static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1897 size_t count, loff_t *ppos)
1898{
1899 struct snd_soc_dapm_context *dapm = file->private_data;
1900 char *level;
1901
1902 switch (dapm->bias_level) {
1903 case SND_SOC_BIAS_ON:
1904 level = "On\n";
1905 break;
1906 case SND_SOC_BIAS_PREPARE:
1907 level = "Prepare\n";
1908 break;
1909 case SND_SOC_BIAS_STANDBY:
1910 level = "Standby\n";
1911 break;
1912 case SND_SOC_BIAS_OFF:
1913 level = "Off\n";
1914 break;
1915 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001916 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09001917 level = "Unknown\n";
1918 break;
1919 }
1920
1921 return simple_read_from_buffer(user_buf, count, ppos, level,
1922 strlen(level));
1923}
1924
1925static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001926 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001927 .read = dapm_bias_read_file,
1928 .llseek = default_llseek,
1929};
1930
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001931void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1932 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001933{
Mark Brown79fb9382009-08-21 16:38:13 +01001934 struct dentry *d;
1935
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001936 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1937
1938 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001939 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001940 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001941 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001942 }
Mark Brown79fb9382009-08-21 16:38:13 +01001943
Mark Brownef49e4f2011-04-04 20:48:13 +09001944 d = debugfs_create_file("bias_level", 0444,
1945 dapm->debugfs_dapm, dapm,
1946 &dapm_bias_fops);
1947 if (!d)
1948 dev_warn(dapm->dev,
1949 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001950}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001951
1952static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1953{
1954 struct snd_soc_dapm_context *dapm = w->dapm;
1955 struct dentry *d;
1956
1957 if (!dapm->debugfs_dapm || !w->name)
1958 return;
1959
1960 d = debugfs_create_file(w->name, 0444,
1961 dapm->debugfs_dapm, w,
1962 &dapm_widget_power_fops);
1963 if (!d)
1964 dev_warn(w->dapm->dev,
1965 "ASoC: Failed to create %s debugfs file\n",
1966 w->name);
1967}
1968
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001969static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1970{
1971 debugfs_remove_recursive(dapm->debugfs_dapm);
1972}
1973
Mark Brown79fb9382009-08-21 16:38:13 +01001974#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001975void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1976 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001977{
1978}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001979
1980static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1981{
1982}
1983
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001984static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1985{
1986}
1987
Mark Brown79fb9382009-08-21 16:38:13 +01001988#endif
1989
Richard Purdie2b97eab2006-10-06 18:32:18 +02001990/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001991static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001992 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001993{
1994 struct snd_soc_dapm_path *path;
1995 int found = 0;
1996
Mark Brownf9fa2b12014-03-06 16:49:11 +08001997 lockdep_assert_held(&card->dapm_mutex);
1998
Richard Purdie2b97eab2006-10-06 18:32:18 +02001999 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002000 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002001 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002002 continue;
2003
2004 found = 1;
2005 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002006 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002007 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002008 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002009 } else {
2010 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002011 dapm_mark_dirty(path->source,
2012 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002013 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002014 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002015 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002016 }
2017
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002018 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002019 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002020
Liam Girdwood618dae12012-04-25 12:12:51 +01002021 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002022}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002023
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002024int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002025 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2026 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002027{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002028 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002029 int ret;
2030
Liam Girdwood3cd04342012-03-09 12:02:08 +00002031 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002032 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002033 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002034 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002035 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002036 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002037 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002038 return ret;
2039}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002040EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002041
Milan plzik1b075e32008-01-10 14:39:46 +01002042/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002043static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002044 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002045{
2046 struct snd_soc_dapm_path *path;
2047 int found = 0;
2048
Mark Brownf9fa2b12014-03-06 16:49:11 +08002049 lockdep_assert_held(&card->dapm_mutex);
2050
Richard Purdie2b97eab2006-10-06 18:32:18 +02002051 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002052 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002053 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002054 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002055 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002056 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002057 }
2058
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002059 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002060 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002061
Liam Girdwood618dae12012-04-25 12:12:51 +01002062 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002063}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002064
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002065int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002066 struct snd_kcontrol *kcontrol, int connect,
2067 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002068{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002069 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002070 int ret;
2071
Liam Girdwood3cd04342012-03-09 12:02:08 +00002072 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002073 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002074 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002075 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002076 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002077 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002078 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002079 return ret;
2080}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002081EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002082
2083/* show dapm widget status in sys fs */
2084static ssize_t dapm_widget_show(struct device *dev,
2085 struct device_attribute *attr, char *buf)
2086{
Mark Brown36ae1a92012-01-06 17:12:45 -08002087 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002088 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002089 struct snd_soc_dapm_widget *w;
2090 int count = 0;
2091 char *state = "not set";
2092
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002093 list_for_each_entry(w, &codec->card->widgets, list) {
2094 if (w->dapm != &codec->dapm)
2095 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002096
2097 /* only display widgets that burnm power */
2098 switch (w->id) {
2099 case snd_soc_dapm_hp:
2100 case snd_soc_dapm_mic:
2101 case snd_soc_dapm_spk:
2102 case snd_soc_dapm_line:
2103 case snd_soc_dapm_micbias:
2104 case snd_soc_dapm_dac:
2105 case snd_soc_dapm_adc:
2106 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002107 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002108 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002109 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002110 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002111 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002112 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002113 if (w->name)
2114 count += sprintf(buf + count, "%s: %s\n",
2115 w->name, w->power ? "On":"Off");
2116 break;
2117 default:
2118 break;
2119 }
2120 }
2121
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002122 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002123 case SND_SOC_BIAS_ON:
2124 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002125 break;
Mark Brown0be98982008-05-19 12:31:28 +02002126 case SND_SOC_BIAS_PREPARE:
2127 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002128 break;
Mark Brown0be98982008-05-19 12:31:28 +02002129 case SND_SOC_BIAS_STANDBY:
2130 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002131 break;
Mark Brown0be98982008-05-19 12:31:28 +02002132 case SND_SOC_BIAS_OFF:
2133 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002134 break;
2135 }
2136 count += sprintf(buf + count, "PM State: %s\n", state);
2137
2138 return count;
2139}
2140
2141static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2142
2143int snd_soc_dapm_sys_add(struct device *dev)
2144{
Troy Kisky12ef1932008-10-13 17:42:14 -07002145 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002146}
2147
2148static void snd_soc_dapm_sys_remove(struct device *dev)
2149{
Mark Brownaef90842009-05-16 17:53:16 +01002150 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002151}
2152
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002153static void dapm_free_path(struct snd_soc_dapm_path *path)
2154{
2155 list_del(&path->list_sink);
2156 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002157 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002158 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002159 kfree(path);
2160}
2161
Richard Purdie2b97eab2006-10-06 18:32:18 +02002162/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002163static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002164{
2165 struct snd_soc_dapm_widget *w, *next_w;
2166 struct snd_soc_dapm_path *p, *next_p;
2167
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002168 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2169 if (w->dapm != dapm)
2170 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002171 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002172 /*
2173 * remove source and sink paths associated to this widget.
2174 * While removing the path, remove reference to it from both
2175 * source and sink widgets so that path is removed only once.
2176 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002177 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2178 dapm_free_path(p);
2179
2180 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2181 dapm_free_path(p);
2182
Stephen Warrenfad59882011-04-28 17:37:59 -06002183 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002184 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002185 kfree(w);
2186 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002187}
2188
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002189static struct snd_soc_dapm_widget *dapm_find_widget(
2190 struct snd_soc_dapm_context *dapm, const char *pin,
2191 bool search_other_contexts)
2192{
2193 struct snd_soc_dapm_widget *w;
2194 struct snd_soc_dapm_widget *fallback = NULL;
2195
2196 list_for_each_entry(w, &dapm->card->widgets, list) {
2197 if (!strcmp(w->name, pin)) {
2198 if (w->dapm == dapm)
2199 return w;
2200 else
2201 fallback = w;
2202 }
2203 }
2204
2205 if (search_other_contexts)
2206 return fallback;
2207
2208 return NULL;
2209}
2210
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002211static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002212 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002213{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002214 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002215
Mark Brownf9fa2b12014-03-06 16:49:11 +08002216 dapm_assert_locked(dapm);
2217
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002218 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002219 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002220 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002221 }
2222
Mark Brown1a8b2d92012-02-16 11:50:07 -08002223 if (w->connected != status)
2224 dapm_mark_dirty(w, "pin configuration");
2225
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002226 w->connected = status;
2227 if (status == 0)
2228 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002229
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002230 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002231}
2232
Richard Purdie2b97eab2006-10-06 18:32:18 +02002233/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002234 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2235 * @dapm: DAPM context
2236 *
2237 * Walks all dapm audio paths and powers widgets according to their
2238 * stream or path usage.
2239 *
2240 * Requires external locking.
2241 *
2242 * Returns 0 for success.
2243 */
2244int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2245{
2246 /*
2247 * Suppress early reports (eg, jacks syncing their state) to avoid
2248 * silly DAPM runs during card startup.
2249 */
2250 if (!dapm->card || !dapm->card->instantiated)
2251 return 0;
2252
2253 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2254}
2255EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2256
2257/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002258 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002259 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002260 *
2261 * Walks all dapm audio paths and powers widgets according to their
2262 * stream or path usage.
2263 *
2264 * Returns 0 for success.
2265 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002266int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002267{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002268 int ret;
2269
Liam Girdwood3cd04342012-03-09 12:02:08 +00002270 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002271 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002272 mutex_unlock(&dapm->card->dapm_mutex);
2273 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002274}
Liam Girdwooda5302182008-07-07 13:35:17 +01002275EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002276
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002277static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2278 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2279 const char *control,
2280 int (*connected)(struct snd_soc_dapm_widget *source,
2281 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002282{
2283 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002284 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002285
2286 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2287 if (!path)
2288 return -ENOMEM;
2289
2290 path->source = wsource;
2291 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002292 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002293 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002294 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002295 INIT_LIST_HEAD(&path->list_source);
2296 INIT_LIST_HEAD(&path->list_sink);
2297
2298 /* check for external widgets */
2299 if (wsink->id == snd_soc_dapm_input) {
2300 if (wsource->id == snd_soc_dapm_micbias ||
2301 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002302 wsource->id == snd_soc_dapm_line ||
2303 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002304 wsink->ext = 1;
2305 }
2306 if (wsource->id == snd_soc_dapm_output) {
2307 if (wsink->id == snd_soc_dapm_spk ||
2308 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002309 wsink->id == snd_soc_dapm_line ||
2310 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002311 wsource->ext = 1;
2312 }
2313
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002314 dapm_mark_dirty(wsource, "Route added");
2315 dapm_mark_dirty(wsink, "Route added");
2316
Richard Purdie2b97eab2006-10-06 18:32:18 +02002317 /* connect static paths */
2318 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002319 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002320 list_add(&path->list_sink, &wsink->sources);
2321 list_add(&path->list_source, &wsource->sinks);
2322 path->connect = 1;
2323 return 0;
2324 }
2325
2326 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002327 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002328 case snd_soc_dapm_adc:
2329 case snd_soc_dapm_dac:
2330 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002331 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002332 case snd_soc_dapm_input:
2333 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002334 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002335 case snd_soc_dapm_micbias:
2336 case snd_soc_dapm_vmid:
2337 case snd_soc_dapm_pre:
2338 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002339 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002340 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002341 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002342 case snd_soc_dapm_aif_in:
2343 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002344 case snd_soc_dapm_dai_in:
2345 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002346 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002347 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002348 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002349 list_add(&path->list_sink, &wsink->sources);
2350 list_add(&path->list_source, &wsource->sinks);
2351 path->connect = 1;
2352 return 0;
2353 case snd_soc_dapm_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002354 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002355 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002356 if (ret != 0)
2357 goto err;
2358 break;
2359 case snd_soc_dapm_switch:
2360 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002361 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002362 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002363 if (ret != 0)
2364 goto err;
2365 break;
2366 case snd_soc_dapm_hp:
2367 case snd_soc_dapm_mic:
2368 case snd_soc_dapm_line:
2369 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002370 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002371 list_add(&path->list_sink, &wsink->sources);
2372 list_add(&path->list_source, &wsource->sinks);
2373 path->connect = 0;
2374 return 0;
2375 }
Mark Brownfabd0382012-07-05 17:20:06 +01002376
Richard Purdie2b97eab2006-10-06 18:32:18 +02002377 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002378err:
2379 kfree(path);
2380 return ret;
2381}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002382
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002383static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002384 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002385{
2386 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2387 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2388 const char *sink;
2389 const char *source;
2390 char prefixed_sink[80];
2391 char prefixed_source[80];
2392 int ret;
2393
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002394 if (dapm->codec && dapm->codec->name_prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002395 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2396 dapm->codec->name_prefix, route->sink);
2397 sink = prefixed_sink;
2398 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2399 dapm->codec->name_prefix, route->source);
2400 source = prefixed_source;
2401 } else {
2402 sink = route->sink;
2403 source = route->source;
2404 }
2405
2406 /*
2407 * find src and dest widgets over all widgets but favor a widget from
2408 * current DAPM context
2409 */
2410 list_for_each_entry(w, &dapm->card->widgets, list) {
2411 if (!wsink && !(strcmp(w->name, sink))) {
2412 wtsink = w;
2413 if (w->dapm == dapm)
2414 wsink = w;
2415 continue;
2416 }
2417 if (!wsource && !(strcmp(w->name, source))) {
2418 wtsource = w;
2419 if (w->dapm == dapm)
2420 wsource = w;
2421 }
2422 }
2423 /* use widget from another DAPM context if not found from this */
2424 if (!wsink)
2425 wsink = wtsink;
2426 if (!wsource)
2427 wsource = wtsource;
2428
2429 if (wsource == NULL) {
2430 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2431 route->source);
2432 return -ENODEV;
2433 }
2434 if (wsink == NULL) {
2435 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2436 route->sink);
2437 return -ENODEV;
2438 }
2439
2440 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2441 route->connected);
2442 if (ret)
2443 goto err;
2444
2445 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002446err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002447 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002448 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002449 return ret;
2450}
Mark Brown105f1c22008-05-13 14:52:19 +02002451
Mark Brownefcc3c62012-07-05 17:24:19 +01002452static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2453 const struct snd_soc_dapm_route *route)
2454{
2455 struct snd_soc_dapm_path *path, *p;
2456 const char *sink;
2457 const char *source;
2458 char prefixed_sink[80];
2459 char prefixed_source[80];
2460
2461 if (route->control) {
2462 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002463 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002464 return -EINVAL;
2465 }
2466
2467 if (dapm->codec && dapm->codec->name_prefix) {
2468 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2469 dapm->codec->name_prefix, route->sink);
2470 sink = prefixed_sink;
2471 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2472 dapm->codec->name_prefix, route->source);
2473 source = prefixed_source;
2474 } else {
2475 sink = route->sink;
2476 source = route->source;
2477 }
2478
2479 path = NULL;
2480 list_for_each_entry(p, &dapm->card->paths, list) {
2481 if (strcmp(p->source->name, source) != 0)
2482 continue;
2483 if (strcmp(p->sink->name, sink) != 0)
2484 continue;
2485 path = p;
2486 break;
2487 }
2488
2489 if (path) {
2490 dapm_mark_dirty(path->source, "Route removed");
2491 dapm_mark_dirty(path->sink, "Route removed");
2492
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002493 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002494 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002495 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002496 source, sink);
2497 }
2498
2499 return 0;
2500}
2501
Mark Brown105f1c22008-05-13 14:52:19 +02002502/**
Mark Brown105f1c22008-05-13 14:52:19 +02002503 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002504 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002505 * @route: audio routes
2506 * @num: number of routes
2507 *
2508 * Connects 2 dapm widgets together via a named audio path. The sink is
2509 * the widget receiving the audio signal, whilst the source is the sender
2510 * of the audio signal.
2511 *
2512 * Returns 0 for success else error. On error all resources can be freed
2513 * with a call to snd_soc_card_free().
2514 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002515int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002516 const struct snd_soc_dapm_route *route, int num)
2517{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002518 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002519
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002520 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002521 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002522 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002523 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002524 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2525 route->source,
2526 route->control ? route->control : "direct",
2527 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002528 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002529 }
2530 route++;
2531 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002532 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002533
Dan Carpenter60884c22012-04-13 22:25:43 +03002534 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002535}
2536EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2537
Mark Brownefcc3c62012-07-05 17:24:19 +01002538/**
2539 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2540 * @dapm: DAPM context
2541 * @route: audio routes
2542 * @num: number of routes
2543 *
2544 * Removes routes from the DAPM context.
2545 */
2546int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2547 const struct snd_soc_dapm_route *route, int num)
2548{
2549 int i, ret = 0;
2550
2551 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2552 for (i = 0; i < num; i++) {
2553 snd_soc_dapm_del_route(dapm, route);
2554 route++;
2555 }
2556 mutex_unlock(&dapm->card->dapm_mutex);
2557
2558 return ret;
2559}
2560EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2561
Mark Brownbf3a9e12011-06-13 16:42:29 +01002562static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2563 const struct snd_soc_dapm_route *route)
2564{
2565 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2566 route->source,
2567 true);
2568 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2569 route->sink,
2570 true);
2571 struct snd_soc_dapm_path *path;
2572 int count = 0;
2573
2574 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002575 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002576 route->source);
2577 return -ENODEV;
2578 }
2579
2580 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002581 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002582 route->sink);
2583 return -ENODEV;
2584 }
2585
2586 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002587 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002588 route->source, route->sink);
2589
2590 list_for_each_entry(path, &source->sinks, list_source) {
2591 if (path->sink == sink) {
2592 path->weak = 1;
2593 count++;
2594 }
2595 }
2596
2597 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002598 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002599 route->source, route->sink);
2600 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002601 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002602 count, route->source, route->sink);
2603
2604 return 0;
2605}
2606
2607/**
2608 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2609 * @dapm: DAPM context
2610 * @route: audio routes
2611 * @num: number of routes
2612 *
2613 * Mark existing routes matching those specified in the passed array
2614 * as being weak, meaning that they are ignored for the purpose of
2615 * power decisions. The main intended use case is for sidetone paths
2616 * which couple audio between other independent paths if they are both
2617 * active in order to make the combination work better at the user
2618 * level but which aren't intended to be "used".
2619 *
2620 * Note that CODEC drivers should not use this as sidetone type paths
2621 * can frequently also be used as bypass paths.
2622 */
2623int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2624 const struct snd_soc_dapm_route *route, int num)
2625{
2626 int i, err;
2627 int ret = 0;
2628
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002629 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002630 for (i = 0; i < num; i++) {
2631 err = snd_soc_dapm_weak_route(dapm, route);
2632 if (err)
2633 ret = err;
2634 route++;
2635 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002636 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002637
2638 return ret;
2639}
2640EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2641
Mark Brown105f1c22008-05-13 14:52:19 +02002642/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002643 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002644 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002645 *
2646 * Checks the codec for any new dapm widgets and creates them if found.
2647 *
2648 * Returns 0 for success.
2649 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002650int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002651{
2652 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002653 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002654
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002655 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002656
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002657 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002658 {
2659 if (w->new)
2660 continue;
2661
Stephen Warrenfad59882011-04-28 17:37:59 -06002662 if (w->num_kcontrols) {
2663 w->kcontrols = kzalloc(w->num_kcontrols *
2664 sizeof(struct snd_kcontrol *),
2665 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002666 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002667 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002668 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002669 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002670 }
2671
Richard Purdie2b97eab2006-10-06 18:32:18 +02002672 switch(w->id) {
2673 case snd_soc_dapm_switch:
2674 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002675 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002676 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002677 break;
2678 case snd_soc_dapm_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002679 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002680 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002681 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002682 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002683 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002684 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002685 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002686 break;
2687 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002688
2689 /* Read the initial power state from the device */
2690 if (w->reg >= 0) {
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002691 soc_widget_read(w, w->reg, &val);
2692 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002693 val &= w->mask;
2694 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002695 w->power = 1;
2696 }
2697
Richard Purdie2b97eab2006-10-06 18:32:18 +02002698 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002699
Mark Brown7508b122011-10-05 12:09:12 +01002700 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002701 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002702 }
2703
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002704 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2705 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002706 return 0;
2707}
2708EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2709
2710/**
2711 * snd_soc_dapm_get_volsw - dapm mixer get callback
2712 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002713 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002714 *
2715 * Callback to get the value of a dapm mixer control.
2716 *
2717 * Returns 0 for success.
2718 */
2719int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2720 struct snd_ctl_elem_value *ucontrol)
2721{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002722 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002723 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002724 struct soc_mixer_control *mc =
2725 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002726 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002727 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002728 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002729 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002730 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002731 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002732
2733 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002734 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002735 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002736 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002737
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002738 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002739 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002740 val = (snd_soc_read(codec, reg) >> shift) & mask;
2741 else
2742 val = dapm_kcontrol_get_value(kcontrol);
2743 mutex_unlock(&card->dapm_mutex);
2744
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002745 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002746 ucontrol->value.integer.value[0] = max - val;
2747 else
2748 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002749
2750 return 0;
2751}
2752EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2753
2754/**
2755 * snd_soc_dapm_put_volsw - dapm mixer set callback
2756 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002757 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002758 *
2759 * Callback to set the value of a dapm mixer control.
2760 *
2761 * Returns 0 for success.
2762 */
2763int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2764 struct snd_ctl_elem_value *ucontrol)
2765{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002766 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002767 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002768 struct soc_mixer_control *mc =
2769 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002770 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002771 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002772 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002773 unsigned int mask = (1 << fls(max)) - 1;
2774 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002775 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002776 int connect, change;
2777 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002778 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002779
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002780 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002781 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002782 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002783 kcontrol->id.name);
2784
Richard Purdie2b97eab2006-10-06 18:32:18 +02002785 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002786 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002787
2788 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002789 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002790
Liam Girdwood3cd04342012-03-09 12:02:08 +00002791 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002792
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002793 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002794 if (change) {
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002795 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02002796 mask = mask << shift;
2797 val = val << shift;
Mark Brown283375c2009-12-07 18:09:03 +00002798
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02002799 if (snd_soc_test_bits(codec, reg, mask, val)) {
2800 update.kcontrol = kcontrol;
2801 update.reg = reg;
2802 update.mask = mask;
2803 update.val = val;
2804 card->update = &update;
2805 }
2806
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002807 }
Mark Brown97404f22010-12-14 16:13:57 +00002808
Nenghua Cao52765972013-12-13 20:13:49 +08002809 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002810
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002811 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002812 }
2813
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002814 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002815
2816 if (ret > 0)
2817 soc_dpcm_runtime_update(card);
2818
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002819 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002820}
2821EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2822
2823/**
2824 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2825 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002826 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002827 *
2828 * Callback to get the value of a dapm enumerated double mixer control.
2829 *
2830 * Returns 0 for success.
2831 */
2832int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2833 struct snd_ctl_elem_value *ucontrol)
2834{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002835 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002836 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002837 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002838
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002839 if (e->reg != SND_SOC_NOPM)
2840 reg_val = snd_soc_read(codec, e->reg);
2841 else
2842 reg_val = dapm_kcontrol_get_value(kcontrol);
2843
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002844 val = (reg_val >> e->shift_l) & e->mask;
2845 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2846 if (e->shift_l != e->shift_r) {
2847 val = (reg_val >> e->shift_r) & e->mask;
2848 val = snd_soc_enum_val_to_item(e, val);
2849 ucontrol->value.enumerated.item[1] = val;
2850 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002851
2852 return 0;
2853}
2854EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2855
2856/**
2857 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2858 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002859 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002860 *
2861 * Callback to set the value of a dapm enumerated double mixer control.
2862 *
2863 * Returns 0 for success.
2864 */
2865int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2866 struct snd_ctl_elem_value *ucontrol)
2867{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002868 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002869 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002870 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002871 unsigned int *item = ucontrol->value.enumerated.item;
2872 unsigned int val, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002873 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002874 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002875 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002876
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002877 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002878 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002879
2880 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002881 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002882 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002883 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002884 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002885 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002886 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002887 }
2888
Liam Girdwood3cd04342012-03-09 12:02:08 +00002889 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002890
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002891 if (e->reg != SND_SOC_NOPM)
2892 change = snd_soc_test_bits(codec, e->reg, mask, val);
2893 else
2894 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002895
Richard Purdie2b97eab2006-10-06 18:32:18 +02002896 if (change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002897 if (e->reg != SND_SOC_NOPM) {
2898 update.kcontrol = kcontrol;
2899 update.reg = e->reg;
2900 update.mask = mask;
2901 update.val = val;
2902 card->update = &update;
2903 }
Mark Brown3a655772009-10-05 17:23:30 +01002904
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002905 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002906
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002907 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002908 }
2909
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002910 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002911
2912 if (ret > 0)
2913 soc_dpcm_runtime_update(card);
2914
Mark Brown97404f22010-12-14 16:13:57 +00002915 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002916}
2917EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2918
2919/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002920 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2921 *
2922 * @kcontrol: mixer control
2923 * @uinfo: control element information
2924 *
2925 * Callback to provide information about a pin switch control.
2926 */
2927int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2928 struct snd_ctl_elem_info *uinfo)
2929{
2930 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2931 uinfo->count = 1;
2932 uinfo->value.integer.min = 0;
2933 uinfo->value.integer.max = 1;
2934
2935 return 0;
2936}
2937EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2938
2939/**
2940 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2941 *
2942 * @kcontrol: mixer control
2943 * @ucontrol: Value
2944 */
2945int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2946 struct snd_ctl_elem_value *ucontrol)
2947{
Mark Brown48a8c392012-02-14 17:11:15 -08002948 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002949 const char *pin = (const char *)kcontrol->private_value;
2950
Liam Girdwood3cd04342012-03-09 12:02:08 +00002951 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002952
2953 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002954 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002955
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002956 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002957
2958 return 0;
2959}
2960EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2961
2962/**
2963 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2964 *
2965 * @kcontrol: mixer control
2966 * @ucontrol: Value
2967 */
2968int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2969 struct snd_ctl_elem_value *ucontrol)
2970{
Mark Brown48a8c392012-02-14 17:11:15 -08002971 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002972 const char *pin = (const char *)kcontrol->private_value;
2973
Mark Brown8b37dbd2009-02-28 21:14:20 +00002974 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002975 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002976 else
Mark Brown48a8c392012-02-14 17:11:15 -08002977 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002978
Mark Brown48a8c392012-02-14 17:11:15 -08002979 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002980 return 0;
2981}
2982EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2983
Mark Brown5ba06fc2012-02-16 11:07:13 -08002984static struct snd_soc_dapm_widget *
2985snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2986 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002987{
2988 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00002989 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002990
2991 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08002992 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002993
Mark Brown62ea8742012-01-21 21:14:48 +00002994 switch (w->id) {
2995 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00002996 w->regulator = devm_regulator_get(dapm->dev, w->name);
2997 if (IS_ERR(w->regulator)) {
2998 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002999 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003000 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003001 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003002 }
Mark Brown8784c772013-01-10 19:33:47 +00003003
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003004 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003005 ret = regulator_allow_bypass(w->regulator, true);
3006 if (ret != 0)
3007 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003008 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003009 w->name, ret);
3010 }
Mark Brown62ea8742012-01-21 21:14:48 +00003011 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003012 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003013#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003014 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003015 if (IS_ERR(w->clk)) {
3016 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003017 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003018 w->name, ret);
3019 return NULL;
3020 }
Mark Brownec029952012-06-04 08:16:20 +01003021#else
3022 return NULL;
3023#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003024 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003025 default:
3026 break;
3027 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003028
Mark Brown88e8b9a2011-03-02 18:18:24 +00003029 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003030 w->name = kasprintf(GFP_KERNEL, "%s %s",
3031 dapm->codec->name_prefix, widget->name);
3032 else
3033 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3034
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003035 if (w->name == NULL) {
3036 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003037 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003038 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003039
Mark Brown7ca3a182011-10-08 14:04:50 +01003040 switch (w->id) {
3041 case snd_soc_dapm_switch:
3042 case snd_soc_dapm_mixer:
3043 case snd_soc_dapm_mixer_named_ctl:
3044 w->power_check = dapm_generic_check_power;
3045 break;
3046 case snd_soc_dapm_mux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003047 w->power_check = dapm_generic_check_power;
3048 break;
Mark Brown46162742013-06-05 19:36:11 +01003049 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003050 w->power_check = dapm_adc_check_power;
3051 break;
Mark Brown46162742013-06-05 19:36:11 +01003052 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003053 w->power_check = dapm_dac_check_power;
3054 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003055 case snd_soc_dapm_adc:
3056 case snd_soc_dapm_aif_out:
3057 case snd_soc_dapm_dac:
3058 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003059 case snd_soc_dapm_pga:
3060 case snd_soc_dapm_out_drv:
3061 case snd_soc_dapm_input:
3062 case snd_soc_dapm_output:
3063 case snd_soc_dapm_micbias:
3064 case snd_soc_dapm_spk:
3065 case snd_soc_dapm_hp:
3066 case snd_soc_dapm_mic:
3067 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003068 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003069 w->power_check = dapm_generic_check_power;
3070 break;
3071 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003072 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003073 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003074 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003075 w->power_check = dapm_supply_check_power;
3076 break;
3077 default:
3078 w->power_check = dapm_always_on_check_power;
3079 break;
3080 }
3081
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003082 w->dapm = dapm;
3083 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003084 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003085 INIT_LIST_HEAD(&w->sources);
3086 INIT_LIST_HEAD(&w->sinks);
3087 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003088 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003089 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003090
3091 /* machine layer set ups unconnected pins and insertions */
3092 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003093 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003094}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003095
3096/**
Mark Brown4ba13272008-05-13 14:51:19 +02003097 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003098 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003099 * @widget: widget array
3100 * @num: number of widgets
3101 *
3102 * Creates new DAPM controls based upon the templates.
3103 *
3104 * Returns 0 for success else error.
3105 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003106int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003107 const struct snd_soc_dapm_widget *widget,
3108 int num)
3109{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003110 struct snd_soc_dapm_widget *w;
3111 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003112 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003113
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003114 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003115 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003116 w = snd_soc_dapm_new_control(dapm, widget);
3117 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003118 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003119 "ASoC: Failed to create DAPM control %s\n",
3120 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003121 ret = -ENOMEM;
3122 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003123 }
Mark Brown4ba13272008-05-13 14:51:19 +02003124 widget++;
3125 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003126 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003127 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003128}
3129EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3130
Mark Brownc74184e2012-04-04 22:12:09 +01003131static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3132 struct snd_kcontrol *kcontrol, int event)
3133{
3134 struct snd_soc_dapm_path *source_p, *sink_p;
3135 struct snd_soc_dai *source, *sink;
3136 const struct snd_soc_pcm_stream *config = w->params;
3137 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003138 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003139 u64 fmt;
3140 int ret;
3141
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003142 if (WARN_ON(!config) ||
3143 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3144 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003145
3146 /* We only support a single source and sink, pick the first */
3147 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3148 list_sink);
3149 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3150 list_source);
3151
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003152 if (WARN_ON(!source_p || !sink_p) ||
3153 WARN_ON(!sink_p->source || !source_p->sink) ||
3154 WARN_ON(!source_p->source || !sink_p->sink))
3155 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003156
3157 source = source_p->source->priv;
3158 sink = sink_p->sink->priv;
3159
3160 /* Be a little careful as we don't want to overflow the mask array */
3161 if (config->formats) {
3162 fmt = ffs(config->formats) - 1;
3163 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003164 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003165 config->formats);
3166 fmt = 0;
3167 }
3168
3169 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003170 params = kzalloc(sizeof(*params), GFP_KERNEL);
3171 if (!params) {
3172 ret = -ENOMEM;
3173 goto out;
3174 }
3175 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003176
Mark Brown9747cec2012-04-26 19:12:21 +01003177 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003178 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003179 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003180 config->rate_max;
3181
Mark Brown9747cec2012-04-26 19:12:21 +01003182 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003183 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003184 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003185 = config->channels_max;
3186
3187 memset(&substream, 0, sizeof(substream));
3188
3189 switch (event) {
3190 case SND_SOC_DAPM_PRE_PMU:
3191 if (source->driver->ops && source->driver->ops->hw_params) {
3192 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3193 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003194 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003195 if (ret != 0) {
3196 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003197 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003198 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003199 }
3200 }
3201
3202 if (sink->driver->ops && sink->driver->ops->hw_params) {
3203 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003204 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003205 sink);
3206 if (ret != 0) {
3207 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003208 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003209 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003210 }
3211 }
3212 break;
3213
3214 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003215 ret = snd_soc_dai_digital_mute(sink, 0,
3216 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003217 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003218 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003219 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003220 break;
3221
3222 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003223 ret = snd_soc_dai_digital_mute(sink, 1,
3224 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003225 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003226 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003227 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003228 break;
3229
3230 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003231 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003232 return -EINVAL;
3233 }
3234
Mark Brown9747cec2012-04-26 19:12:21 +01003235out:
3236 kfree(params);
3237 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003238}
3239
3240int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3241 const struct snd_soc_pcm_stream *params,
3242 struct snd_soc_dapm_widget *source,
3243 struct snd_soc_dapm_widget *sink)
3244{
3245 struct snd_soc_dapm_route routes[2];
3246 struct snd_soc_dapm_widget template;
3247 struct snd_soc_dapm_widget *w;
3248 size_t len;
3249 char *link_name;
3250
3251 len = strlen(source->name) + strlen(sink->name) + 2;
3252 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3253 if (!link_name)
3254 return -ENOMEM;
3255 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3256
3257 memset(&template, 0, sizeof(template));
3258 template.reg = SND_SOC_NOPM;
3259 template.id = snd_soc_dapm_dai_link;
3260 template.name = link_name;
3261 template.event = snd_soc_dai_link_event;
3262 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3263 SND_SOC_DAPM_PRE_PMD;
3264
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003265 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003266
3267 w = snd_soc_dapm_new_control(&card->dapm, &template);
3268 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003269 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003270 link_name);
3271 return -ENOMEM;
3272 }
3273
3274 w->params = params;
3275
3276 memset(&routes, 0, sizeof(routes));
3277
3278 routes[0].source = source->name;
3279 routes[0].sink = link_name;
3280 routes[1].source = link_name;
3281 routes[1].sink = sink->name;
3282
3283 return snd_soc_dapm_add_routes(&card->dapm, routes,
3284 ARRAY_SIZE(routes));
3285}
3286
Mark Brown888df392012-02-16 19:37:51 -08003287int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3288 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003289{
Mark Brown888df392012-02-16 19:37:51 -08003290 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003291 struct snd_soc_dapm_widget *w;
3292
Mark Brown888df392012-02-16 19:37:51 -08003293 WARN_ON(dapm->dev != dai->dev);
3294
3295 memset(&template, 0, sizeof(template));
3296 template.reg = SND_SOC_NOPM;
3297
3298 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003299 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003300 template.name = dai->driver->playback.stream_name;
3301 template.sname = dai->driver->playback.stream_name;
3302
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003303 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003304 template.name);
3305
3306 w = snd_soc_dapm_new_control(dapm, &template);
3307 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003308 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003309 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003310 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003311 }
3312
3313 w->priv = dai;
3314 dai->playback_widget = w;
3315 }
3316
3317 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003318 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003319 template.name = dai->driver->capture.stream_name;
3320 template.sname = dai->driver->capture.stream_name;
3321
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003322 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003323 template.name);
3324
3325 w = snd_soc_dapm_new_control(dapm, &template);
3326 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003327 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003328 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003329 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003330 }
3331
3332 w->priv = dai;
3333 dai->capture_widget = w;
3334 }
3335
3336 return 0;
3337}
3338
3339int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3340{
3341 struct snd_soc_dapm_widget *dai_w, *w;
3342 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003343
3344 /* For each DAI widget... */
3345 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003346 switch (dai_w->id) {
3347 case snd_soc_dapm_dai_in:
3348 case snd_soc_dapm_dai_out:
3349 break;
3350 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003351 continue;
Mark Brown46162742013-06-05 19:36:11 +01003352 }
Mark Brown888df392012-02-16 19:37:51 -08003353
3354 dai = dai_w->priv;
3355
3356 /* ...find all widgets with the same stream and link them */
3357 list_for_each_entry(w, &card->widgets, list) {
3358 if (w->dapm != dai_w->dapm)
3359 continue;
3360
Mark Brown46162742013-06-05 19:36:11 +01003361 switch (w->id) {
3362 case snd_soc_dapm_dai_in:
3363 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003364 continue;
Mark Brown46162742013-06-05 19:36:11 +01003365 default:
3366 break;
3367 }
Mark Brown888df392012-02-16 19:37:51 -08003368
Russell King19c2c5f2013-08-04 20:24:03 +01003369 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003370 continue;
3371
3372 if (dai->driver->playback.stream_name &&
3373 strstr(w->sname,
3374 dai->driver->playback.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003375 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003376 dai->playback_widget->name, w->name);
Mark Brown888df392012-02-16 19:37:51 -08003377
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003378 snd_soc_dapm_add_path(w->dapm,
3379 dai->playback_widget, w, NULL, NULL);
Mark Brown888df392012-02-16 19:37:51 -08003380 }
3381
3382 if (dai->driver->capture.stream_name &&
3383 strstr(w->sname,
3384 dai->driver->capture.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003385 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003386 w->name, dai->capture_widget->name);
Mark Brown888df392012-02-16 19:37:51 -08003387
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003388 snd_soc_dapm_add_path(w->dapm, w,
3389 dai->capture_widget, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003390 }
3391 }
3392 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003393
Mark Brown888df392012-02-16 19:37:51 -08003394 return 0;
3395}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003396
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003397void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3398{
3399 struct snd_soc_pcm_runtime *rtd = card->rtd;
3400 struct snd_soc_dai *cpu_dai, *codec_dai;
3401 struct snd_soc_dapm_route r;
3402 int i;
3403
3404 memset(&r, 0, sizeof(r));
3405
3406 /* for each BE DAI link... */
3407 for (i = 0; i < card->num_rtd; i++) {
3408 rtd = &card->rtd[i];
3409 cpu_dai = rtd->cpu_dai;
3410 codec_dai = rtd->codec_dai;
3411
3412 /* dynamic FE links have no fixed DAI mapping */
3413 if (rtd->dai_link->dynamic)
3414 continue;
3415
3416 /* there is no point in connecting BE DAI links with dummies */
3417 if (snd_soc_dai_is_dummy(codec_dai) ||
3418 snd_soc_dai_is_dummy(cpu_dai))
3419 continue;
3420
3421 /* connect BE DAI playback if widgets are valid */
3422 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3423 r.source = cpu_dai->playback_widget->name;
3424 r.sink = codec_dai->playback_widget->name;
3425 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3426 cpu_dai->codec->name, r.source,
3427 codec_dai->platform->name, r.sink);
3428
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02003429 snd_soc_dapm_add_route(&card->dapm, &r);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003430 }
3431
3432 /* connect BE DAI capture if widgets are valid */
3433 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3434 r.source = codec_dai->capture_widget->name;
3435 r.sink = cpu_dai->capture_widget->name;
3436 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3437 codec_dai->codec->name, r.source,
3438 cpu_dai->platform->name, r.sink);
3439
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02003440 snd_soc_dapm_add_route(&card->dapm, &r);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003441 }
3442
3443 }
3444}
3445
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003446static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3447 int event)
3448{
3449 struct snd_soc_dapm_widget *w;
3450
3451 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3452 w = dai->playback_widget;
3453 else
3454 w = dai->capture_widget;
3455
3456 if (w) {
3457 dapm_mark_dirty(w, "stream event");
3458
3459 switch (event) {
3460 case SND_SOC_DAPM_STREAM_START:
3461 w->active = 1;
3462 break;
3463 case SND_SOC_DAPM_STREAM_STOP:
3464 w->active = 0;
3465 break;
3466 case SND_SOC_DAPM_STREAM_SUSPEND:
3467 case SND_SOC_DAPM_STREAM_RESUME:
3468 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3469 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3470 break;
3471 }
3472 }
3473}
3474
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003475static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3476 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003477{
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003478 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
3479 soc_dapm_dai_stream_event(rtd->codec_dai, stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003480
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003481 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003482}
3483
3484/**
3485 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3486 * @rtd: PCM runtime data
3487 * @stream: stream name
3488 * @event: stream event
3489 *
3490 * Sends a stream event to the dapm core. The core then makes any
3491 * necessary widget power changes.
3492 *
3493 * Returns 0 for success else error.
3494 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003495void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3496 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003497{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003498 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003499
Liam Girdwood3cd04342012-03-09 12:02:08 +00003500 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003501 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003502 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003503}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003504
3505/**
Charles Keepax11391102014-02-18 15:22:14 +00003506 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3507 * @dapm: DAPM context
3508 * @pin: pin name
3509 *
3510 * Enables input/output pin and its parents or children widgets iff there is
3511 * a valid audio route and active audio stream.
3512 *
3513 * Requires external locking.
3514 *
3515 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3516 * do any widget power switching.
3517 */
3518int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3519 const char *pin)
3520{
3521 return snd_soc_dapm_set_pin(dapm, pin, 1);
3522}
3523EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3524
3525/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003526 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003527 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003528 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003529 *
Mark Brown74b8f952009-06-06 11:26:15 +01003530 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003531 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003532 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003533 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3534 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003535 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003536int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003537{
Charles Keepax11391102014-02-18 15:22:14 +00003538 int ret;
3539
3540 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3541
3542 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3543
3544 mutex_unlock(&dapm->card->dapm_mutex);
3545
3546 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003547}
Liam Girdwooda5302182008-07-07 13:35:17 +01003548EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003549
3550/**
Charles Keepax11391102014-02-18 15:22:14 +00003551 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3552 * @dapm: DAPM context
3553 * @pin: pin name
3554 *
3555 * Enables input/output pin regardless of any other state. This is
3556 * intended for use with microphone bias supplies used in microphone
3557 * jack detection.
3558 *
3559 * Requires external locking.
3560 *
3561 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3562 * do any widget power switching.
3563 */
3564int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3565 const char *pin)
3566{
3567 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3568
3569 if (!w) {
3570 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3571 return -EINVAL;
3572 }
3573
3574 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3575 w->connected = 1;
3576 w->force = 1;
3577 dapm_mark_dirty(w, "force enable");
3578
3579 return 0;
3580}
3581EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3582
3583/**
Mark Brownda341832010-03-15 19:23:37 +00003584 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003585 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003586 * @pin: pin name
3587 *
3588 * Enables input/output pin regardless of any other state. This is
3589 * intended for use with microphone bias supplies used in microphone
3590 * jack detection.
3591 *
3592 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3593 * do any widget power switching.
3594 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003595int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3596 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003597{
Charles Keepax11391102014-02-18 15:22:14 +00003598 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003599
Charles Keepax11391102014-02-18 15:22:14 +00003600 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003601
Charles Keepax11391102014-02-18 15:22:14 +00003602 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003603
Charles Keepax11391102014-02-18 15:22:14 +00003604 mutex_unlock(&dapm->card->dapm_mutex);
3605
3606 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003607}
3608EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3609
3610/**
Charles Keepax11391102014-02-18 15:22:14 +00003611 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3612 * @dapm: DAPM context
3613 * @pin: pin name
3614 *
3615 * Disables input/output pin and its parents or children widgets.
3616 *
3617 * Requires external locking.
3618 *
3619 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3620 * do any widget power switching.
3621 */
3622int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3623 const char *pin)
3624{
3625 return snd_soc_dapm_set_pin(dapm, pin, 0);
3626}
3627EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3628
3629/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003630 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003631 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003632 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003633 *
Mark Brown74b8f952009-06-06 11:26:15 +01003634 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003635 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003636 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3637 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003638 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003639int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3640 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003641{
Charles Keepax11391102014-02-18 15:22:14 +00003642 int ret;
3643
3644 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3645
3646 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3647
3648 mutex_unlock(&dapm->card->dapm_mutex);
3649
3650 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003651}
3652EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3653
3654/**
Charles Keepax11391102014-02-18 15:22:14 +00003655 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3656 * @dapm: DAPM context
3657 * @pin: pin name
3658 *
3659 * Marks the specified pin as being not connected, disabling it along
3660 * any parent or child widgets. At present this is identical to
3661 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3662 * additional things such as disabling controls which only affect
3663 * paths through the pin.
3664 *
3665 * Requires external locking.
3666 *
3667 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3668 * do any widget power switching.
3669 */
3670int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3671 const char *pin)
3672{
3673 return snd_soc_dapm_set_pin(dapm, pin, 0);
3674}
3675EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3676
3677/**
Mark Brown5817b522008-09-24 11:23:11 +01003678 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003679 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003680 * @pin: pin name
3681 *
3682 * Marks the specified pin as being not connected, disabling it along
3683 * any parent or child widgets. At present this is identical to
3684 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3685 * additional things such as disabling controls which only affect
3686 * paths through the pin.
3687 *
3688 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3689 * do any widget power switching.
3690 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003691int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003692{
Charles Keepax11391102014-02-18 15:22:14 +00003693 int ret;
3694
3695 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3696
3697 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3698
3699 mutex_unlock(&dapm->card->dapm_mutex);
3700
3701 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003702}
3703EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3704
3705/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003706 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003707 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003708 * @pin: audio signal pin endpoint (or start point)
3709 *
3710 * Get audio pin status - connected or disconnected.
3711 *
3712 * Returns 1 for connected otherwise 0.
3713 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003714int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3715 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003716{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003717 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003718
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003719 if (w)
3720 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003721
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003722 return 0;
3723}
Liam Girdwooda5302182008-07-07 13:35:17 +01003724EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003725
3726/**
Mark Brown1547aba2010-05-07 21:11:40 +01003727 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003728 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003729 * @pin: audio signal pin endpoint (or start point)
3730 *
3731 * Mark the given endpoint or pin as ignoring suspend. When the
3732 * system is disabled a path between two endpoints flagged as ignoring
3733 * suspend will not be disabled. The path must already be enabled via
3734 * normal means at suspend time, it will not be turned on if it was not
3735 * already enabled.
3736 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003737int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3738 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003739{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003740 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003741
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003742 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003743 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003744 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003745 }
3746
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003747 w->ignore_suspend = 1;
3748
3749 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003750}
3751EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3752
Stephen Warren16332812011-11-23 12:42:04 -07003753static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3754 struct snd_soc_dapm_widget *w)
3755{
3756 struct snd_soc_dapm_path *p;
3757
3758 list_for_each_entry(p, &card->paths, list) {
3759 if ((p->source == w) || (p->sink == w)) {
3760 dev_dbg(card->dev,
3761 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3762 p->source->name, p->source->id, p->source->dapm,
3763 p->sink->name, p->sink->id, p->sink->dapm);
3764
3765 /* Connected to something other than the codec */
3766 if (p->source->dapm != p->sink->dapm)
3767 return true;
3768 /*
3769 * Loopback connection from codec external pin to
3770 * codec external pin
3771 */
3772 if (p->sink->id == snd_soc_dapm_input) {
3773 switch (p->source->id) {
3774 case snd_soc_dapm_output:
3775 case snd_soc_dapm_micbias:
3776 return true;
3777 default:
3778 break;
3779 }
3780 }
3781 }
3782 }
3783
3784 return false;
3785}
3786
3787/**
3788 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3789 * @codec: The codec whose pins should be processed
3790 *
3791 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3792 * which are unused. Pins are used if they are connected externally to the
3793 * codec, whether that be to some other device, or a loop-back connection to
3794 * the codec itself.
3795 */
3796void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3797{
3798 struct snd_soc_card *card = codec->card;
3799 struct snd_soc_dapm_context *dapm = &codec->dapm;
3800 struct snd_soc_dapm_widget *w;
3801
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003802 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003803 &card->dapm, &codec->dapm);
3804
3805 list_for_each_entry(w, &card->widgets, list) {
3806 if (w->dapm != dapm)
3807 continue;
3808 switch (w->id) {
3809 case snd_soc_dapm_input:
3810 case snd_soc_dapm_output:
3811 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003812 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003813 w->name);
3814 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003815 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003816 "... Not in map; disabling\n");
3817 snd_soc_dapm_nc_pin(dapm, w->name);
3818 }
3819 break;
3820 default:
3821 break;
3822 }
3823 }
3824}
3825
Mark Brown1547aba2010-05-07 21:11:40 +01003826/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003827 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003828 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003829 *
3830 * Free all dapm widgets and resources.
3831 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003832void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003833{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003834 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003835 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003836 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003837 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003838}
3839EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3840
Xiang Xiao57996352014-03-02 00:04:02 +08003841static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003842{
Liam Girdwood01005a72012-07-06 16:57:05 +01003843 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003844 struct snd_soc_dapm_widget *w;
3845 LIST_HEAD(down_list);
3846 int powerdown = 0;
3847
Liam Girdwood01005a72012-07-06 16:57:05 +01003848 mutex_lock(&card->dapm_mutex);
3849
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003850 list_for_each_entry(w, &dapm->card->widgets, list) {
3851 if (w->dapm != dapm)
3852 continue;
Mark Brown51737472009-06-22 13:16:51 +01003853 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003854 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003855 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003856 powerdown = 1;
3857 }
3858 }
3859
3860 /* If there were no widgets to power down we're already in
3861 * standby.
3862 */
3863 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003864 if (dapm->bias_level == SND_SOC_BIAS_ON)
3865 snd_soc_dapm_set_bias_level(dapm,
3866 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003867 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003868 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3869 snd_soc_dapm_set_bias_level(dapm,
3870 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003871 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003872
3873 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003874}
Mark Brown51737472009-06-22 13:16:51 +01003875
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003876/*
3877 * snd_soc_dapm_shutdown - callback for system shutdown
3878 */
3879void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3880{
Xiang Xiao57996352014-03-02 00:04:02 +08003881 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003882
Xiang Xiao57996352014-03-02 00:04:02 +08003883 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08003884 if (dapm != &card->dapm) {
3885 soc_dapm_shutdown_dapm(dapm);
3886 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3887 snd_soc_dapm_set_bias_level(dapm,
3888 SND_SOC_BIAS_OFF);
3889 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003890 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08003891
3892 soc_dapm_shutdown_dapm(&card->dapm);
3893 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3894 snd_soc_dapm_set_bias_level(&card->dapm,
3895 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01003896}
3897
Richard Purdie2b97eab2006-10-06 18:32:18 +02003898/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003899MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003900MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3901MODULE_LICENSE("GPL");