blob: fb6c7b793459f6a9cc5a520c85036dd2e8e90696 [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 Clausenfe5813912013-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 Browne56235e02011-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 Browne56235e02011-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 Girdwooda73fb2df2012-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 Girdwooda73fb2df2012-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,
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002384 const struct snd_soc_dapm_route *route,
2385 unsigned int is_prefixed)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002386{
2387 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2388 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2389 const char *sink;
2390 const char *source;
2391 char prefixed_sink[80];
2392 char prefixed_source[80];
2393 int ret;
2394
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002395 if (dapm->codec && dapm->codec->name_prefix && !is_prefixed) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002396 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2397 dapm->codec->name_prefix, route->sink);
2398 sink = prefixed_sink;
2399 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2400 dapm->codec->name_prefix, route->source);
2401 source = prefixed_source;
2402 } else {
2403 sink = route->sink;
2404 source = route->source;
2405 }
2406
2407 /*
2408 * find src and dest widgets over all widgets but favor a widget from
2409 * current DAPM context
2410 */
2411 list_for_each_entry(w, &dapm->card->widgets, list) {
2412 if (!wsink && !(strcmp(w->name, sink))) {
2413 wtsink = w;
2414 if (w->dapm == dapm)
2415 wsink = w;
2416 continue;
2417 }
2418 if (!wsource && !(strcmp(w->name, source))) {
2419 wtsource = w;
2420 if (w->dapm == dapm)
2421 wsource = w;
2422 }
2423 }
2424 /* use widget from another DAPM context if not found from this */
2425 if (!wsink)
2426 wsink = wtsink;
2427 if (!wsource)
2428 wsource = wtsource;
2429
2430 if (wsource == NULL) {
2431 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2432 route->source);
2433 return -ENODEV;
2434 }
2435 if (wsink == NULL) {
2436 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2437 route->sink);
2438 return -ENODEV;
2439 }
2440
2441 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2442 route->connected);
2443 if (ret)
2444 goto err;
2445
2446 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002447err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002448 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002449 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002450 return ret;
2451}
Mark Brown105f1c22008-05-13 14:52:19 +02002452
Mark Brownefcc3c62012-07-05 17:24:19 +01002453static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2454 const struct snd_soc_dapm_route *route)
2455{
2456 struct snd_soc_dapm_path *path, *p;
2457 const char *sink;
2458 const char *source;
2459 char prefixed_sink[80];
2460 char prefixed_source[80];
2461
2462 if (route->control) {
2463 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002464 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002465 return -EINVAL;
2466 }
2467
2468 if (dapm->codec && dapm->codec->name_prefix) {
2469 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2470 dapm->codec->name_prefix, route->sink);
2471 sink = prefixed_sink;
2472 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2473 dapm->codec->name_prefix, route->source);
2474 source = prefixed_source;
2475 } else {
2476 sink = route->sink;
2477 source = route->source;
2478 }
2479
2480 path = NULL;
2481 list_for_each_entry(p, &dapm->card->paths, list) {
2482 if (strcmp(p->source->name, source) != 0)
2483 continue;
2484 if (strcmp(p->sink->name, sink) != 0)
2485 continue;
2486 path = p;
2487 break;
2488 }
2489
2490 if (path) {
2491 dapm_mark_dirty(path->source, "Route removed");
2492 dapm_mark_dirty(path->sink, "Route removed");
2493
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002494 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002495 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002496 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002497 source, sink);
2498 }
2499
2500 return 0;
2501}
2502
Mark Brown105f1c22008-05-13 14:52:19 +02002503/**
Mark Brown105f1c22008-05-13 14:52:19 +02002504 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002505 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002506 * @route: audio routes
2507 * @num: number of routes
2508 *
2509 * Connects 2 dapm widgets together via a named audio path. The sink is
2510 * the widget receiving the audio signal, whilst the source is the sender
2511 * of the audio signal.
2512 *
2513 * Returns 0 for success else error. On error all resources can be freed
2514 * with a call to snd_soc_card_free().
2515 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002516int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002517 const struct snd_soc_dapm_route *route, int num)
2518{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002519 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002520
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002521 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002522 for (i = 0; i < num; i++) {
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002523 r = snd_soc_dapm_add_route(dapm, route, false);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002524 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002525 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2526 route->source,
2527 route->control ? route->control : "direct",
2528 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002529 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002530 }
2531 route++;
2532 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002533 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002534
Dan Carpenter60884c22012-04-13 22:25:43 +03002535 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002536}
2537EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2538
Mark Brownefcc3c62012-07-05 17:24:19 +01002539/**
2540 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2541 * @dapm: DAPM context
2542 * @route: audio routes
2543 * @num: number of routes
2544 *
2545 * Removes routes from the DAPM context.
2546 */
2547int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2548 const struct snd_soc_dapm_route *route, int num)
2549{
2550 int i, ret = 0;
2551
2552 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2553 for (i = 0; i < num; i++) {
2554 snd_soc_dapm_del_route(dapm, route);
2555 route++;
2556 }
2557 mutex_unlock(&dapm->card->dapm_mutex);
2558
2559 return ret;
2560}
2561EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2562
Mark Brownbf3a9e12011-06-13 16:42:29 +01002563static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2564 const struct snd_soc_dapm_route *route)
2565{
2566 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2567 route->source,
2568 true);
2569 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2570 route->sink,
2571 true);
2572 struct snd_soc_dapm_path *path;
2573 int count = 0;
2574
2575 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002576 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002577 route->source);
2578 return -ENODEV;
2579 }
2580
2581 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002582 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002583 route->sink);
2584 return -ENODEV;
2585 }
2586
2587 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002588 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002589 route->source, route->sink);
2590
2591 list_for_each_entry(path, &source->sinks, list_source) {
2592 if (path->sink == sink) {
2593 path->weak = 1;
2594 count++;
2595 }
2596 }
2597
2598 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002599 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002600 route->source, route->sink);
2601 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002602 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002603 count, route->source, route->sink);
2604
2605 return 0;
2606}
2607
2608/**
2609 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2610 * @dapm: DAPM context
2611 * @route: audio routes
2612 * @num: number of routes
2613 *
2614 * Mark existing routes matching those specified in the passed array
2615 * as being weak, meaning that they are ignored for the purpose of
2616 * power decisions. The main intended use case is for sidetone paths
2617 * which couple audio between other independent paths if they are both
2618 * active in order to make the combination work better at the user
2619 * level but which aren't intended to be "used".
2620 *
2621 * Note that CODEC drivers should not use this as sidetone type paths
2622 * can frequently also be used as bypass paths.
2623 */
2624int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2625 const struct snd_soc_dapm_route *route, int num)
2626{
2627 int i, err;
2628 int ret = 0;
2629
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002630 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002631 for (i = 0; i < num; i++) {
2632 err = snd_soc_dapm_weak_route(dapm, route);
2633 if (err)
2634 ret = err;
2635 route++;
2636 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002637 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002638
2639 return ret;
2640}
2641EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2642
Mark Brown105f1c22008-05-13 14:52:19 +02002643/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002644 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002645 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002646 *
2647 * Checks the codec for any new dapm widgets and creates them if found.
2648 *
2649 * Returns 0 for success.
2650 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002651int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002652{
2653 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002654 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002655
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002656 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002657
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002658 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002659 {
2660 if (w->new)
2661 continue;
2662
Stephen Warrenfad59882011-04-28 17:37:59 -06002663 if (w->num_kcontrols) {
2664 w->kcontrols = kzalloc(w->num_kcontrols *
2665 sizeof(struct snd_kcontrol *),
2666 GFP_KERNEL);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002667 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002668 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002669 return -ENOMEM;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002670 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002671 }
2672
Richard Purdie2b97eab2006-10-06 18:32:18 +02002673 switch(w->id) {
2674 case snd_soc_dapm_switch:
2675 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002676 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002677 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002678 break;
2679 case snd_soc_dapm_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002680 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002681 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002682 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002683 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002684 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002685 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002686 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002687 break;
2688 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002689
2690 /* Read the initial power state from the device */
2691 if (w->reg >= 0) {
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002692 soc_widget_read(w, w->reg, &val);
2693 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002694 val &= w->mask;
2695 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002696 w->power = 1;
2697 }
2698
Richard Purdie2b97eab2006-10-06 18:32:18 +02002699 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002700
Mark Brown7508b122011-10-05 12:09:12 +01002701 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002702 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002703 }
2704
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002705 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2706 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002707 return 0;
2708}
2709EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2710
2711/**
2712 * snd_soc_dapm_get_volsw - dapm mixer get callback
2713 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002714 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002715 *
2716 * Callback to get the value of a dapm mixer control.
2717 *
2718 * Returns 0 for success.
2719 */
2720int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2721 struct snd_ctl_elem_value *ucontrol)
2722{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002723 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002724 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002725 struct soc_mixer_control *mc =
2726 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002727 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002728 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002729 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002730 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002731 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002732 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002733
2734 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002735 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002736 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002737 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002738
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002739 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002740 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002741 val = (snd_soc_read(codec, reg) >> shift) & mask;
2742 else
2743 val = dapm_kcontrol_get_value(kcontrol);
2744 mutex_unlock(&card->dapm_mutex);
2745
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002746 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002747 ucontrol->value.integer.value[0] = max - val;
2748 else
2749 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002750
2751 return 0;
2752}
2753EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2754
2755/**
2756 * snd_soc_dapm_put_volsw - dapm mixer set callback
2757 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002758 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002759 *
2760 * Callback to set the value of a dapm mixer control.
2761 *
2762 * Returns 0 for success.
2763 */
2764int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2765 struct snd_ctl_elem_value *ucontrol)
2766{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002767 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002768 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002769 struct soc_mixer_control *mc =
2770 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002771 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002772 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002773 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002774 unsigned int mask = (1 << fls(max)) - 1;
2775 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002776 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002777 int connect, change;
2778 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002779 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002780
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002781 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002782 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002783 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002784 kcontrol->id.name);
2785
Richard Purdie2b97eab2006-10-06 18:32:18 +02002786 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002787 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002788
2789 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002790 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002791
Liam Girdwood3cd04342012-03-09 12:02:08 +00002792 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002793
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002794 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002795 if (change) {
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002796 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02002797 mask = mask << shift;
2798 val = val << shift;
Mark Brown283375c2009-12-07 18:09:03 +00002799
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02002800 if (snd_soc_test_bits(codec, reg, mask, val)) {
2801 update.kcontrol = kcontrol;
2802 update.reg = reg;
2803 update.mask = mask;
2804 update.val = val;
2805 card->update = &update;
2806 }
2807
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002808 }
Mark Brown97404f22010-12-14 16:13:57 +00002809
Nenghua Cao52765972013-12-13 20:13:49 +08002810 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002811
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002812 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002813 }
2814
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002815 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002816
2817 if (ret > 0)
2818 soc_dpcm_runtime_update(card);
2819
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002820 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002821}
2822EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2823
2824/**
2825 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2826 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002827 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002828 *
2829 * Callback to get the value of a dapm enumerated double mixer control.
2830 *
2831 * Returns 0 for success.
2832 */
2833int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2834 struct snd_ctl_elem_value *ucontrol)
2835{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002836 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002837 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002838 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002839
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002840 if (e->reg != SND_SOC_NOPM)
2841 reg_val = snd_soc_read(codec, e->reg);
2842 else
2843 reg_val = dapm_kcontrol_get_value(kcontrol);
2844
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002845 val = (reg_val >> e->shift_l) & e->mask;
2846 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2847 if (e->shift_l != e->shift_r) {
2848 val = (reg_val >> e->shift_r) & e->mask;
2849 val = snd_soc_enum_val_to_item(e, val);
2850 ucontrol->value.enumerated.item[1] = val;
2851 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002852
2853 return 0;
2854}
2855EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2856
2857/**
2858 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2859 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002860 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002861 *
2862 * Callback to set the value of a dapm enumerated double mixer control.
2863 *
2864 * Returns 0 for success.
2865 */
2866int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2867 struct snd_ctl_elem_value *ucontrol)
2868{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002869 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002870 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002871 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002872 unsigned int *item = ucontrol->value.enumerated.item;
2873 unsigned int val, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002874 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002875 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002876 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002877
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002878 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002879 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002880
2881 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002882 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002883 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002884 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002885 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002886 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002887 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002888 }
2889
Liam Girdwood3cd04342012-03-09 12:02:08 +00002890 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002891
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002892 if (e->reg != SND_SOC_NOPM)
2893 change = snd_soc_test_bits(codec, e->reg, mask, val);
2894 else
2895 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002896
Richard Purdie2b97eab2006-10-06 18:32:18 +02002897 if (change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002898 if (e->reg != SND_SOC_NOPM) {
2899 update.kcontrol = kcontrol;
2900 update.reg = e->reg;
2901 update.mask = mask;
2902 update.val = val;
2903 card->update = &update;
2904 }
Mark Brown3a655772009-10-05 17:23:30 +01002905
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002906 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002907
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002908 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002909 }
2910
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002911 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002912
2913 if (ret > 0)
2914 soc_dpcm_runtime_update(card);
2915
Mark Brown97404f22010-12-14 16:13:57 +00002916 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002917}
2918EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2919
2920/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002921 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2922 *
2923 * @kcontrol: mixer control
2924 * @uinfo: control element information
2925 *
2926 * Callback to provide information about a pin switch control.
2927 */
2928int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2929 struct snd_ctl_elem_info *uinfo)
2930{
2931 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2932 uinfo->count = 1;
2933 uinfo->value.integer.min = 0;
2934 uinfo->value.integer.max = 1;
2935
2936 return 0;
2937}
2938EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2939
2940/**
2941 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2942 *
2943 * @kcontrol: mixer control
2944 * @ucontrol: Value
2945 */
2946int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2947 struct snd_ctl_elem_value *ucontrol)
2948{
Mark Brown48a8c392012-02-14 17:11:15 -08002949 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002950 const char *pin = (const char *)kcontrol->private_value;
2951
Liam Girdwood3cd04342012-03-09 12:02:08 +00002952 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002953
2954 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002955 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002956
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002957 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002958
2959 return 0;
2960}
2961EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2962
2963/**
2964 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2965 *
2966 * @kcontrol: mixer control
2967 * @ucontrol: Value
2968 */
2969int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2970 struct snd_ctl_elem_value *ucontrol)
2971{
Mark Brown48a8c392012-02-14 17:11:15 -08002972 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002973 const char *pin = (const char *)kcontrol->private_value;
2974
Mark Brown8b37dbd2009-02-28 21:14:20 +00002975 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002976 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002977 else
Mark Brown48a8c392012-02-14 17:11:15 -08002978 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002979
Mark Brown48a8c392012-02-14 17:11:15 -08002980 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002981 return 0;
2982}
2983EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2984
Mark Brown5ba06fc2012-02-16 11:07:13 -08002985static struct snd_soc_dapm_widget *
2986snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2987 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002988{
2989 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00002990 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002991
2992 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08002993 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002994
Mark Brown62ea8742012-01-21 21:14:48 +00002995 switch (w->id) {
2996 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00002997 w->regulator = devm_regulator_get(dapm->dev, w->name);
2998 if (IS_ERR(w->regulator)) {
2999 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003000 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003001 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003002 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003003 }
Mark Brown8784c772013-01-10 19:33:47 +00003004
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003005 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003006 ret = regulator_allow_bypass(w->regulator, true);
3007 if (ret != 0)
3008 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003009 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003010 w->name, ret);
3011 }
Mark Brown62ea8742012-01-21 21:14:48 +00003012 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003013 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003014#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003015 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003016 if (IS_ERR(w->clk)) {
3017 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003018 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003019 w->name, ret);
3020 return NULL;
3021 }
Mark Brownec029952012-06-04 08:16:20 +01003022#else
3023 return NULL;
3024#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003025 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003026 default:
3027 break;
3028 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003029
Mark Brown88e8b9a2011-03-02 18:18:24 +00003030 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003031 w->name = kasprintf(GFP_KERNEL, "%s %s",
3032 dapm->codec->name_prefix, widget->name);
3033 else
3034 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3035
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003036 if (w->name == NULL) {
3037 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003038 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003039 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003040
Mark Brown7ca3a182011-10-08 14:04:50 +01003041 switch (w->id) {
3042 case snd_soc_dapm_switch:
3043 case snd_soc_dapm_mixer:
3044 case snd_soc_dapm_mixer_named_ctl:
3045 w->power_check = dapm_generic_check_power;
3046 break;
3047 case snd_soc_dapm_mux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003048 w->power_check = dapm_generic_check_power;
3049 break;
Mark Brown46162742013-06-05 19:36:11 +01003050 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003051 w->power_check = dapm_adc_check_power;
3052 break;
Mark Brown46162742013-06-05 19:36:11 +01003053 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003054 w->power_check = dapm_dac_check_power;
3055 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003056 case snd_soc_dapm_adc:
3057 case snd_soc_dapm_aif_out:
3058 case snd_soc_dapm_dac:
3059 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003060 case snd_soc_dapm_pga:
3061 case snd_soc_dapm_out_drv:
3062 case snd_soc_dapm_input:
3063 case snd_soc_dapm_output:
3064 case snd_soc_dapm_micbias:
3065 case snd_soc_dapm_spk:
3066 case snd_soc_dapm_hp:
3067 case snd_soc_dapm_mic:
3068 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003069 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003070 w->power_check = dapm_generic_check_power;
3071 break;
3072 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003073 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003074 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003075 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003076 w->power_check = dapm_supply_check_power;
3077 break;
3078 default:
3079 w->power_check = dapm_always_on_check_power;
3080 break;
3081 }
3082
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003083 w->dapm = dapm;
3084 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003085 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003086 INIT_LIST_HEAD(&w->sources);
3087 INIT_LIST_HEAD(&w->sinks);
3088 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003089 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003090 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003091
3092 /* machine layer set ups unconnected pins and insertions */
3093 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003094 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003095}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003096
3097/**
Mark Brown4ba13272008-05-13 14:51:19 +02003098 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003099 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003100 * @widget: widget array
3101 * @num: number of widgets
3102 *
3103 * Creates new DAPM controls based upon the templates.
3104 *
3105 * Returns 0 for success else error.
3106 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003107int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003108 const struct snd_soc_dapm_widget *widget,
3109 int num)
3110{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003111 struct snd_soc_dapm_widget *w;
3112 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003113 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003114
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003115 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003116 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003117 w = snd_soc_dapm_new_control(dapm, widget);
3118 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003119 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003120 "ASoC: Failed to create DAPM control %s\n",
3121 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003122 ret = -ENOMEM;
3123 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003124 }
Mark Brown4ba13272008-05-13 14:51:19 +02003125 widget++;
3126 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003127 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003128 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003129}
3130EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3131
Mark Brownc74184e2012-04-04 22:12:09 +01003132static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3133 struct snd_kcontrol *kcontrol, int event)
3134{
3135 struct snd_soc_dapm_path *source_p, *sink_p;
3136 struct snd_soc_dai *source, *sink;
3137 const struct snd_soc_pcm_stream *config = w->params;
3138 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003139 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003140 u64 fmt;
3141 int ret;
3142
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003143 if (WARN_ON(!config) ||
3144 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3145 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003146
3147 /* We only support a single source and sink, pick the first */
3148 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3149 list_sink);
3150 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3151 list_source);
3152
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003153 if (WARN_ON(!source_p || !sink_p) ||
3154 WARN_ON(!sink_p->source || !source_p->sink) ||
3155 WARN_ON(!source_p->source || !sink_p->sink))
3156 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003157
3158 source = source_p->source->priv;
3159 sink = sink_p->sink->priv;
3160
3161 /* Be a little careful as we don't want to overflow the mask array */
3162 if (config->formats) {
3163 fmt = ffs(config->formats) - 1;
3164 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003165 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003166 config->formats);
3167 fmt = 0;
3168 }
3169
3170 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003171 params = kzalloc(sizeof(*params), GFP_KERNEL);
3172 if (!params) {
3173 ret = -ENOMEM;
3174 goto out;
3175 }
3176 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003177
Mark Brown9747cec2012-04-26 19:12:21 +01003178 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003179 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003180 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003181 config->rate_max;
3182
Mark Brown9747cec2012-04-26 19:12:21 +01003183 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003184 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003185 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003186 = config->channels_max;
3187
3188 memset(&substream, 0, sizeof(substream));
3189
3190 switch (event) {
3191 case SND_SOC_DAPM_PRE_PMU:
3192 if (source->driver->ops && source->driver->ops->hw_params) {
3193 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3194 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003195 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003196 if (ret != 0) {
3197 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003198 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003199 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003200 }
3201 }
3202
3203 if (sink->driver->ops && sink->driver->ops->hw_params) {
3204 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003205 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003206 sink);
3207 if (ret != 0) {
3208 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003209 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003210 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003211 }
3212 }
3213 break;
3214
3215 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003216 ret = snd_soc_dai_digital_mute(sink, 0,
3217 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003218 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003219 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003220 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003221 break;
3222
3223 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003224 ret = snd_soc_dai_digital_mute(sink, 1,
3225 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003226 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003227 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003228 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003229 break;
3230
3231 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003232 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003233 return -EINVAL;
3234 }
3235
Mark Brown9747cec2012-04-26 19:12:21 +01003236out:
3237 kfree(params);
3238 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003239}
3240
3241int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3242 const struct snd_soc_pcm_stream *params,
3243 struct snd_soc_dapm_widget *source,
3244 struct snd_soc_dapm_widget *sink)
3245{
3246 struct snd_soc_dapm_route routes[2];
3247 struct snd_soc_dapm_widget template;
3248 struct snd_soc_dapm_widget *w;
3249 size_t len;
3250 char *link_name;
3251
3252 len = strlen(source->name) + strlen(sink->name) + 2;
3253 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3254 if (!link_name)
3255 return -ENOMEM;
3256 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3257
3258 memset(&template, 0, sizeof(template));
3259 template.reg = SND_SOC_NOPM;
3260 template.id = snd_soc_dapm_dai_link;
3261 template.name = link_name;
3262 template.event = snd_soc_dai_link_event;
3263 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3264 SND_SOC_DAPM_PRE_PMD;
3265
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003266 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003267
3268 w = snd_soc_dapm_new_control(&card->dapm, &template);
3269 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003270 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003271 link_name);
3272 return -ENOMEM;
3273 }
3274
3275 w->params = params;
3276
3277 memset(&routes, 0, sizeof(routes));
3278
3279 routes[0].source = source->name;
3280 routes[0].sink = link_name;
3281 routes[1].source = link_name;
3282 routes[1].sink = sink->name;
3283
3284 return snd_soc_dapm_add_routes(&card->dapm, routes,
3285 ARRAY_SIZE(routes));
3286}
3287
Mark Brown888df392012-02-16 19:37:51 -08003288int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3289 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003290{
Mark Brown888df392012-02-16 19:37:51 -08003291 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003292 struct snd_soc_dapm_widget *w;
3293
Mark Brown888df392012-02-16 19:37:51 -08003294 WARN_ON(dapm->dev != dai->dev);
3295
3296 memset(&template, 0, sizeof(template));
3297 template.reg = SND_SOC_NOPM;
3298
3299 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003300 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003301 template.name = dai->driver->playback.stream_name;
3302 template.sname = dai->driver->playback.stream_name;
3303
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003304 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003305 template.name);
3306
3307 w = snd_soc_dapm_new_control(dapm, &template);
3308 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003309 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003310 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003311 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003312 }
3313
3314 w->priv = dai;
3315 dai->playback_widget = w;
3316 }
3317
3318 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003319 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003320 template.name = dai->driver->capture.stream_name;
3321 template.sname = dai->driver->capture.stream_name;
3322
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003323 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003324 template.name);
3325
3326 w = snd_soc_dapm_new_control(dapm, &template);
3327 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003328 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003329 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003330 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003331 }
3332
3333 w->priv = dai;
3334 dai->capture_widget = w;
3335 }
3336
3337 return 0;
3338}
3339
3340int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3341{
3342 struct snd_soc_dapm_widget *dai_w, *w;
3343 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003344
3345 /* For each DAI widget... */
3346 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003347 switch (dai_w->id) {
3348 case snd_soc_dapm_dai_in:
3349 case snd_soc_dapm_dai_out:
3350 break;
3351 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003352 continue;
Mark Brown46162742013-06-05 19:36:11 +01003353 }
Mark Brown888df392012-02-16 19:37:51 -08003354
3355 dai = dai_w->priv;
3356
3357 /* ...find all widgets with the same stream and link them */
3358 list_for_each_entry(w, &card->widgets, list) {
3359 if (w->dapm != dai_w->dapm)
3360 continue;
3361
Mark Brown46162742013-06-05 19:36:11 +01003362 switch (w->id) {
3363 case snd_soc_dapm_dai_in:
3364 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003365 continue;
Mark Brown46162742013-06-05 19:36:11 +01003366 default:
3367 break;
3368 }
Mark Brown888df392012-02-16 19:37:51 -08003369
Russell King19c2c5f2013-08-04 20:24:03 +01003370 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003371 continue;
3372
3373 if (dai->driver->playback.stream_name &&
3374 strstr(w->sname,
3375 dai->driver->playback.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003376 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003377 dai->playback_widget->name, w->name);
Mark Brown888df392012-02-16 19:37:51 -08003378
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003379 snd_soc_dapm_add_path(w->dapm,
3380 dai->playback_widget, w, NULL, NULL);
Mark Brown888df392012-02-16 19:37:51 -08003381 }
3382
3383 if (dai->driver->capture.stream_name &&
3384 strstr(w->sname,
3385 dai->driver->capture.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003386 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003387 w->name, dai->capture_widget->name);
Mark Brown888df392012-02-16 19:37:51 -08003388
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003389 snd_soc_dapm_add_path(w->dapm, w,
3390 dai->capture_widget, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003391 }
3392 }
3393 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003394
Mark Brown888df392012-02-16 19:37:51 -08003395 return 0;
3396}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003397
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003398void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3399{
3400 struct snd_soc_pcm_runtime *rtd = card->rtd;
3401 struct snd_soc_dai *cpu_dai, *codec_dai;
3402 struct snd_soc_dapm_route r;
3403 int i;
3404
3405 memset(&r, 0, sizeof(r));
3406
3407 /* for each BE DAI link... */
3408 for (i = 0; i < card->num_rtd; i++) {
3409 rtd = &card->rtd[i];
3410 cpu_dai = rtd->cpu_dai;
3411 codec_dai = rtd->codec_dai;
3412
3413 /* dynamic FE links have no fixed DAI mapping */
3414 if (rtd->dai_link->dynamic)
3415 continue;
3416
3417 /* there is no point in connecting BE DAI links with dummies */
3418 if (snd_soc_dai_is_dummy(codec_dai) ||
3419 snd_soc_dai_is_dummy(cpu_dai))
3420 continue;
3421
3422 /* connect BE DAI playback if widgets are valid */
3423 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3424 r.source = cpu_dai->playback_widget->name;
3425 r.sink = codec_dai->playback_widget->name;
3426 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3427 cpu_dai->codec->name, r.source,
3428 codec_dai->platform->name, r.sink);
3429
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003430 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003431 }
3432
3433 /* connect BE DAI capture if widgets are valid */
3434 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3435 r.source = codec_dai->capture_widget->name;
3436 r.sink = cpu_dai->capture_widget->name;
3437 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3438 codec_dai->codec->name, r.source,
3439 cpu_dai->platform->name, r.sink);
3440
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003441 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003442 }
3443
3444 }
3445}
3446
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003447static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3448 int event)
3449{
3450 struct snd_soc_dapm_widget *w;
3451
3452 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3453 w = dai->playback_widget;
3454 else
3455 w = dai->capture_widget;
3456
3457 if (w) {
3458 dapm_mark_dirty(w, "stream event");
3459
3460 switch (event) {
3461 case SND_SOC_DAPM_STREAM_START:
3462 w->active = 1;
3463 break;
3464 case SND_SOC_DAPM_STREAM_STOP:
3465 w->active = 0;
3466 break;
3467 case SND_SOC_DAPM_STREAM_SUSPEND:
3468 case SND_SOC_DAPM_STREAM_RESUME:
3469 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3470 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3471 break;
3472 }
3473 }
3474}
3475
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003476static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3477 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003478{
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003479 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
3480 soc_dapm_dai_stream_event(rtd->codec_dai, stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003481
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003482 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003483}
3484
3485/**
3486 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3487 * @rtd: PCM runtime data
3488 * @stream: stream name
3489 * @event: stream event
3490 *
3491 * Sends a stream event to the dapm core. The core then makes any
3492 * necessary widget power changes.
3493 *
3494 * Returns 0 for success else error.
3495 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003496void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3497 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003498{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003499 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003500
Liam Girdwood3cd04342012-03-09 12:02:08 +00003501 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003502 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003503 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003504}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003505
3506/**
Charles Keepax11391102014-02-18 15:22:14 +00003507 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3508 * @dapm: DAPM context
3509 * @pin: pin name
3510 *
3511 * Enables input/output pin and its parents or children widgets iff there is
3512 * a valid audio route and active audio stream.
3513 *
3514 * Requires external locking.
3515 *
3516 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3517 * do any widget power switching.
3518 */
3519int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3520 const char *pin)
3521{
3522 return snd_soc_dapm_set_pin(dapm, pin, 1);
3523}
3524EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3525
3526/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003527 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003528 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003529 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003530 *
Mark Brown74b8f952009-06-06 11:26:15 +01003531 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003532 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003533 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003534 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3535 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003536 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003537int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003538{
Charles Keepax11391102014-02-18 15:22:14 +00003539 int ret;
3540
3541 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3542
3543 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3544
3545 mutex_unlock(&dapm->card->dapm_mutex);
3546
3547 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003548}
Liam Girdwooda5302182008-07-07 13:35:17 +01003549EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003550
3551/**
Charles Keepax11391102014-02-18 15:22:14 +00003552 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3553 * @dapm: DAPM context
3554 * @pin: pin name
3555 *
3556 * Enables input/output pin regardless of any other state. This is
3557 * intended for use with microphone bias supplies used in microphone
3558 * jack detection.
3559 *
3560 * Requires external locking.
3561 *
3562 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3563 * do any widget power switching.
3564 */
3565int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3566 const char *pin)
3567{
3568 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3569
3570 if (!w) {
3571 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3572 return -EINVAL;
3573 }
3574
3575 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3576 w->connected = 1;
3577 w->force = 1;
3578 dapm_mark_dirty(w, "force enable");
3579
3580 return 0;
3581}
3582EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3583
3584/**
Mark Brownda341832010-03-15 19:23:37 +00003585 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003586 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003587 * @pin: pin name
3588 *
3589 * Enables input/output pin regardless of any other state. This is
3590 * intended for use with microphone bias supplies used in microphone
3591 * jack detection.
3592 *
3593 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3594 * do any widget power switching.
3595 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003596int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3597 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003598{
Charles Keepax11391102014-02-18 15:22:14 +00003599 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003600
Charles Keepax11391102014-02-18 15:22:14 +00003601 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003602
Charles Keepax11391102014-02-18 15:22:14 +00003603 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003604
Charles Keepax11391102014-02-18 15:22:14 +00003605 mutex_unlock(&dapm->card->dapm_mutex);
3606
3607 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003608}
3609EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3610
3611/**
Charles Keepax11391102014-02-18 15:22:14 +00003612 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3613 * @dapm: DAPM context
3614 * @pin: pin name
3615 *
3616 * Disables input/output pin and its parents or children widgets.
3617 *
3618 * Requires external locking.
3619 *
3620 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3621 * do any widget power switching.
3622 */
3623int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3624 const char *pin)
3625{
3626 return snd_soc_dapm_set_pin(dapm, pin, 0);
3627}
3628EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3629
3630/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003631 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003632 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003633 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003634 *
Mark Brown74b8f952009-06-06 11:26:15 +01003635 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003636 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003637 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3638 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003639 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003640int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3641 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003642{
Charles Keepax11391102014-02-18 15:22:14 +00003643 int ret;
3644
3645 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3646
3647 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3648
3649 mutex_unlock(&dapm->card->dapm_mutex);
3650
3651 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003652}
3653EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3654
3655/**
Charles Keepax11391102014-02-18 15:22:14 +00003656 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3657 * @dapm: DAPM context
3658 * @pin: pin name
3659 *
3660 * Marks the specified pin as being not connected, disabling it along
3661 * any parent or child widgets. At present this is identical to
3662 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3663 * additional things such as disabling controls which only affect
3664 * paths through the pin.
3665 *
3666 * Requires external locking.
3667 *
3668 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3669 * do any widget power switching.
3670 */
3671int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3672 const char *pin)
3673{
3674 return snd_soc_dapm_set_pin(dapm, pin, 0);
3675}
3676EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3677
3678/**
Mark Brown5817b522008-09-24 11:23:11 +01003679 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003680 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003681 * @pin: pin name
3682 *
3683 * Marks the specified pin as being not connected, disabling it along
3684 * any parent or child widgets. At present this is identical to
3685 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3686 * additional things such as disabling controls which only affect
3687 * paths through the pin.
3688 *
3689 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3690 * do any widget power switching.
3691 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003692int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003693{
Charles Keepax11391102014-02-18 15:22:14 +00003694 int ret;
3695
3696 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3697
3698 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3699
3700 mutex_unlock(&dapm->card->dapm_mutex);
3701
3702 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003703}
3704EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3705
3706/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003707 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003708 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003709 * @pin: audio signal pin endpoint (or start point)
3710 *
3711 * Get audio pin status - connected or disconnected.
3712 *
3713 * Returns 1 for connected otherwise 0.
3714 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003715int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3716 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003717{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003718 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003719
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003720 if (w)
3721 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003722
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003723 return 0;
3724}
Liam Girdwooda5302182008-07-07 13:35:17 +01003725EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003726
3727/**
Mark Brown1547aba2010-05-07 21:11:40 +01003728 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003729 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003730 * @pin: audio signal pin endpoint (or start point)
3731 *
3732 * Mark the given endpoint or pin as ignoring suspend. When the
3733 * system is disabled a path between two endpoints flagged as ignoring
3734 * suspend will not be disabled. The path must already be enabled via
3735 * normal means at suspend time, it will not be turned on if it was not
3736 * already enabled.
3737 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003738int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3739 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003740{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003741 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003742
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003743 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003744 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003745 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003746 }
3747
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003748 w->ignore_suspend = 1;
3749
3750 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003751}
3752EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3753
Stephen Warren16332812011-11-23 12:42:04 -07003754static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3755 struct snd_soc_dapm_widget *w)
3756{
3757 struct snd_soc_dapm_path *p;
3758
3759 list_for_each_entry(p, &card->paths, list) {
3760 if ((p->source == w) || (p->sink == w)) {
3761 dev_dbg(card->dev,
3762 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3763 p->source->name, p->source->id, p->source->dapm,
3764 p->sink->name, p->sink->id, p->sink->dapm);
3765
3766 /* Connected to something other than the codec */
3767 if (p->source->dapm != p->sink->dapm)
3768 return true;
3769 /*
3770 * Loopback connection from codec external pin to
3771 * codec external pin
3772 */
3773 if (p->sink->id == snd_soc_dapm_input) {
3774 switch (p->source->id) {
3775 case snd_soc_dapm_output:
3776 case snd_soc_dapm_micbias:
3777 return true;
3778 default:
3779 break;
3780 }
3781 }
3782 }
3783 }
3784
3785 return false;
3786}
3787
3788/**
3789 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3790 * @codec: The codec whose pins should be processed
3791 *
3792 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3793 * which are unused. Pins are used if they are connected externally to the
3794 * codec, whether that be to some other device, or a loop-back connection to
3795 * the codec itself.
3796 */
3797void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3798{
3799 struct snd_soc_card *card = codec->card;
3800 struct snd_soc_dapm_context *dapm = &codec->dapm;
3801 struct snd_soc_dapm_widget *w;
3802
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003803 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003804 &card->dapm, &codec->dapm);
3805
3806 list_for_each_entry(w, &card->widgets, list) {
3807 if (w->dapm != dapm)
3808 continue;
3809 switch (w->id) {
3810 case snd_soc_dapm_input:
3811 case snd_soc_dapm_output:
3812 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003813 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003814 w->name);
3815 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003816 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003817 "... Not in map; disabling\n");
3818 snd_soc_dapm_nc_pin(dapm, w->name);
3819 }
3820 break;
3821 default:
3822 break;
3823 }
3824 }
3825}
3826
Mark Brown1547aba2010-05-07 21:11:40 +01003827/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003828 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003829 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003830 *
3831 * Free all dapm widgets and resources.
3832 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003833void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003834{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003835 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003836 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003837 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003838 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003839}
3840EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3841
Xiang Xiao57996352014-03-02 00:04:02 +08003842static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003843{
Liam Girdwood01005a72012-07-06 16:57:05 +01003844 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003845 struct snd_soc_dapm_widget *w;
3846 LIST_HEAD(down_list);
3847 int powerdown = 0;
3848
Liam Girdwood01005a72012-07-06 16:57:05 +01003849 mutex_lock(&card->dapm_mutex);
3850
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003851 list_for_each_entry(w, &dapm->card->widgets, list) {
3852 if (w->dapm != dapm)
3853 continue;
Mark Brown51737472009-06-22 13:16:51 +01003854 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003855 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003856 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003857 powerdown = 1;
3858 }
3859 }
3860
3861 /* If there were no widgets to power down we're already in
3862 * standby.
3863 */
3864 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003865 if (dapm->bias_level == SND_SOC_BIAS_ON)
3866 snd_soc_dapm_set_bias_level(dapm,
3867 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003868 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003869 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3870 snd_soc_dapm_set_bias_level(dapm,
3871 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003872 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003873
3874 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003875}
Mark Brown51737472009-06-22 13:16:51 +01003876
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003877/*
3878 * snd_soc_dapm_shutdown - callback for system shutdown
3879 */
3880void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3881{
Xiang Xiao57996352014-03-02 00:04:02 +08003882 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003883
Xiang Xiao57996352014-03-02 00:04:02 +08003884 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08003885 if (dapm != &card->dapm) {
3886 soc_dapm_shutdown_dapm(dapm);
3887 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3888 snd_soc_dapm_set_bias_level(dapm,
3889 SND_SOC_BIAS_OFF);
3890 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003891 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08003892
3893 soc_dapm_shutdown_dapm(&card->dapm);
3894 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3895 snd_soc_dapm_set_bias_level(&card->dapm,
3896 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01003897}
3898
Richard Purdie2b97eab2006-10-06 18:32:18 +02003899/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003900MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003901MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3902MODULE_LICENSE("GPL");