blob: 1fed2207b024359aa6d3b51bba25f38d6bf3ca57 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
Liam Girdwood612a3fe2012-02-06 16:05:29 +000017 * mic/headphone insertion events.
Richard Purdie2b97eab2006-10-06 18:32:18 +020018 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Liam Girdwood612a3fe2012-02-06 16:05:29 +000021 * o Delayed power down of audio subsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020024 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080029#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020030#include <linux/delay.h>
31#include <linux/pm.h>
32#include <linux/bitops.h>
33#include <linux/platform_device.h>
34#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020035#include <linux/debugfs.h>
Mark Brownf1aac482011-12-05 15:17:06 +000036#include <linux/pm_runtime.h>
Mark Brown62ea8742012-01-21 21:14:48 +000037#include <linux/regulator/consumer.h>
Ola Liljad7e7eb92012-05-24 15:26:25 +020038#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020040#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020043#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020044#include <sound/initval.h>
45
Mark Brown84e90932010-11-04 00:07:02 -040046#include <trace/events/asoc.h>
47
Mark Brownde02d072011-09-20 21:43:24 +010048#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49
Lars-Peter Clausen57295072013-08-05 11:27:31 +020050static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
51 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
52 const char *control,
53 int (*connected)(struct snd_soc_dapm_widget *source,
54 struct snd_soc_dapm_widget *sink));
55static struct snd_soc_dapm_widget *
56snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
57 const struct snd_soc_dapm_widget *widget);
58
Richard Purdie2b97eab2006-10-06 18:32:18 +020059/* dapm power sequences - make this per codec in the future */
60static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010061 [snd_soc_dapm_pre] = 0,
Mark Brown62ea8742012-01-21 21:14:48 +000062 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020063 [snd_soc_dapm_clock_supply] = 1,
Mark Brown1dd275b2013-10-09 13:56:37 +010064 [snd_soc_dapm_supply] = 2,
65 [snd_soc_dapm_micbias] = 3,
Mark Brownc74184e2012-04-04 22:12:09 +010066 [snd_soc_dapm_dai_link] = 2,
Mark Brown1dd275b2013-10-09 13:56:37 +010067 [snd_soc_dapm_dai_in] = 4,
68 [snd_soc_dapm_dai_out] = 4,
69 [snd_soc_dapm_aif_in] = 4,
70 [snd_soc_dapm_aif_out] = 4,
71 [snd_soc_dapm_mic] = 5,
72 [snd_soc_dapm_mux] = 6,
Mark Brown1dd275b2013-10-09 13:56:37 +010073 [snd_soc_dapm_dac] = 7,
74 [snd_soc_dapm_switch] = 8,
75 [snd_soc_dapm_mixer] = 8,
76 [snd_soc_dapm_mixer_named_ctl] = 8,
77 [snd_soc_dapm_pga] = 9,
78 [snd_soc_dapm_adc] = 10,
79 [snd_soc_dapm_out_drv] = 11,
80 [snd_soc_dapm_hp] = 11,
81 [snd_soc_dapm_spk] = 11,
82 [snd_soc_dapm_line] = 11,
83 [snd_soc_dapm_kcontrol] = 12,
84 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +020085};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000086
Richard Purdie2b97eab2006-10-06 18:32:18 +020087static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010088 [snd_soc_dapm_pre] = 0,
Lars-Peter Clausen57295072013-08-05 11:27:31 +020089 [snd_soc_dapm_kcontrol] = 1,
90 [snd_soc_dapm_adc] = 2,
91 [snd_soc_dapm_hp] = 3,
92 [snd_soc_dapm_spk] = 3,
93 [snd_soc_dapm_line] = 3,
94 [snd_soc_dapm_out_drv] = 3,
Mark Brown38357ab2009-06-06 19:03:23 +010095 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020096 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +010097 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010098 [snd_soc_dapm_mixer] = 5,
99 [snd_soc_dapm_dac] = 6,
100 [snd_soc_dapm_mic] = 7,
101 [snd_soc_dapm_micbias] = 8,
102 [snd_soc_dapm_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +0100103 [snd_soc_dapm_aif_in] = 10,
104 [snd_soc_dapm_aif_out] = 10,
Mark Brown46162742013-06-05 19:36:11 +0100105 [snd_soc_dapm_dai_in] = 10,
106 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100107 [snd_soc_dapm_dai_link] = 11,
Mark Brownc74184e2012-04-04 22:12:09 +0100108 [snd_soc_dapm_supply] = 12,
Mark Brown1dd275b2013-10-09 13:56:37 +0100109 [snd_soc_dapm_clock_supply] = 13,
110 [snd_soc_dapm_regulator_supply] = 13,
111 [snd_soc_dapm_post] = 14,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200112};
113
Mark Brownf9fa2b12014-03-06 16:49:11 +0800114static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
115{
116 if (dapm->card && dapm->card->instantiated)
117 lockdep_assert_held(&dapm->card->dapm_mutex);
118}
119
Troy Kisky12ef1932008-10-13 17:42:14 -0700120static void pop_wait(u32 pop_time)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100121{
122 if (pop_time)
123 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
124}
125
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200126static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100127{
128 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200129 char *buf;
130
131 if (!pop_time)
132 return;
133
134 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
135 if (buf == NULL)
136 return;
Mark Brown15e4c72f2008-07-02 11:51:20 +0100137
138 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200139 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100140 dev_info(dev, "%s", buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100141 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200142
143 kfree(buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100144}
145
Mark Browndb432b42011-10-03 21:06:40 +0100146static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
147{
148 return !list_empty(&w->dirty);
149}
150
Mark Brown492c0a12014-03-06 16:15:48 +0800151static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100152{
Mark Brownf9fa2b12014-03-06 16:49:11 +0800153 dapm_assert_locked(w->dapm);
154
Mark Brown75c1f892011-10-04 22:28:08 +0100155 if (!dapm_dirty_widget(w)) {
156 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
157 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100158 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100159 }
Mark Browndb432b42011-10-03 21:06:40 +0100160}
161
Mark Browne2d32ff2012-08-31 17:38:32 -0700162void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
163{
164 struct snd_soc_card *card = dapm->card;
165 struct snd_soc_dapm_widget *w;
166
167 mutex_lock(&card->dapm_mutex);
168
169 list_for_each_entry(w, &card->widgets, list) {
170 switch (w->id) {
171 case snd_soc_dapm_input:
172 case snd_soc_dapm_output:
173 dapm_mark_dirty(w, "Rechecking inputs and outputs");
174 break;
175 default:
176 break;
177 }
178 }
179
180 mutex_unlock(&card->dapm_mutex);
181}
182EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
183
Richard Purdie2b97eab2006-10-06 18:32:18 +0200184/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100185static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200186 const struct snd_soc_dapm_widget *_widget)
187{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100188 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200189}
190
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200191struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200192 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200193 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200194 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200195 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200196};
197
198static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
199 struct snd_kcontrol *kcontrol)
200{
201 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200202 struct soc_mixer_control *mc;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200203
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200204 data = kzalloc(sizeof(*data), GFP_KERNEL);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200205 if (!data) {
206 dev_err(widget->dapm->dev,
207 "ASoC: can't allocate kcontrol data for %s\n",
208 widget->name);
209 return -ENOMEM;
210 }
211
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200212 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200213
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200214 switch (widget->id) {
215 case snd_soc_dapm_switch:
216 case snd_soc_dapm_mixer:
217 case snd_soc_dapm_mixer_named_ctl:
218 mc = (struct soc_mixer_control *)kcontrol->private_value;
219
220 if (mc->autodisable) {
221 struct snd_soc_dapm_widget template;
222
223 memset(&template, 0, sizeof(template));
224 template.reg = mc->reg;
225 template.mask = (1 << fls(mc->max)) - 1;
226 template.shift = mc->shift;
227 if (mc->invert)
228 template.off_val = mc->max;
229 else
230 template.off_val = 0;
231 template.on_val = template.off_val;
232 template.id = snd_soc_dapm_kcontrol;
233 template.name = kcontrol->id.name;
234
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200235 data->value = template.on_val;
236
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200237 data->widget = snd_soc_dapm_new_control(widget->dapm,
238 &template);
239 if (!data->widget) {
240 kfree(data);
241 return -ENOMEM;
242 }
243 }
244 break;
245 default:
246 break;
247 }
248
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200249 kcontrol->private_data = data;
250
251 return 0;
252}
253
254static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
255{
256 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200257 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200258 kfree(data);
259}
260
261static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
262 const struct snd_kcontrol *kcontrol)
263{
264 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
265
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200266 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200267}
268
269static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
270 struct snd_soc_dapm_widget *widget)
271{
272 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200273 struct snd_soc_dapm_widget_list *new_wlist;
274 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200275
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200276 if (data->wlist)
277 n = data->wlist->num_widgets + 1;
278 else
279 n = 1;
280
281 new_wlist = krealloc(data->wlist,
282 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
283 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200284 return -ENOMEM;
285
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200286 new_wlist->widgets[n - 1] = widget;
287 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200288
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200289 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200290
291 return 0;
292}
293
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200294static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
295 struct snd_soc_dapm_path *path)
296{
297 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
298
299 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200300
301 if (data->widget) {
302 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
303 path->source, NULL, NULL);
304 }
305}
306
307static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
308{
309 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
310
311 if (!data->widget)
312 return true;
313
314 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200315}
316
317static struct list_head *dapm_kcontrol_get_path_list(
318 const struct snd_kcontrol *kcontrol)
319{
320 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
321
322 return &data->paths;
323}
324
325#define dapm_kcontrol_for_each_path(path, kcontrol) \
326 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
327 list_kcontrol)
328
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530329unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200330{
331 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
332
333 return data->value;
334}
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530335EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200336
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/**
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200354 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
355 * kcontrol
356 * @kcontrol: The kcontrol
357 *
358 * Note: This function must only be used on kcontrols that are known to have
359 * been registered for a CODEC. Otherwise the behaviour is undefined.
360 */
361struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
362 struct snd_kcontrol *kcontrol)
363{
364 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
365}
366EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
367
368/**
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200369 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
370 * @kcontrol: The kcontrol
371 */
372struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
373{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200374 return snd_soc_dapm_to_codec(snd_soc_dapm_kcontrol_dapm(kcontrol));
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200375}
376EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
377
Liam Girdwood6c120e12012-02-15 15:15:34 +0000378static void dapm_reset(struct snd_soc_card *card)
379{
380 struct snd_soc_dapm_widget *w;
381
Mark Brownf9fa2b12014-03-06 16:49:11 +0800382 lockdep_assert_held(&card->dapm_mutex);
383
Liam Girdwood6c120e12012-02-15 15:15:34 +0000384 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
385
386 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200387 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000388 w->power_checked = false;
389 w->inputs = -1;
390 w->outputs = -1;
391 }
392}
393
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200394static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
395{
396 if (!dapm->component)
397 return NULL;
398 return dapm->component->name_prefix;
399}
400
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200401static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800402 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100403{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200404 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200405 return -EIO;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200406 return snd_soc_component_read(dapm->component, reg, value);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100407}
408
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200409static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
Bard Liao34775012014-04-17 20:12:56 +0800410 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100411{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200412 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200413 return -EIO;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200414 return snd_soc_component_update_bits_async(dapm->component, reg,
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200415 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000416}
417
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200418static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
419 int reg, unsigned int mask, unsigned int value)
420{
421 if (!dapm->component)
422 return -EIO;
423 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
424}
425
Mark Browneb270e92013-10-09 13:52:52 +0100426static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
427{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200428 if (dapm->component)
429 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100430}
431
Mark Brown452c5ea2009-05-17 21:41:23 +0100432/**
433 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800434 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100435 * @level: level to configure
436 *
437 * Configure the bias (power) levels for the SoC audio device.
438 *
439 * Returns 0 for success else error.
440 */
Mark Browned5a4c42011-02-18 11:12:42 -0800441static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200442 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100443{
Mark Browned5a4c42011-02-18 11:12:42 -0800444 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100445 int ret = 0;
446
Mark Brown84e90932010-11-04 00:07:02 -0400447 trace_snd_soc_bias_level_start(card, level);
448
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000449 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100450 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100451 if (ret != 0)
452 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100453
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +0200454 if (dapm->set_bias_level)
455 ret = dapm->set_bias_level(dapm, level);
456 else if (!card || dapm != &card->dapm)
Liam Girdwood41231282012-07-06 16:56:16 +0100457 dapm->bias_level = level;
458
Mark Brown171ec6b2011-06-06 18:15:19 +0100459 if (ret != 0)
460 goto out;
461
462 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100463 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100464out:
Mark Brown84e90932010-11-04 00:07:02 -0400465 trace_snd_soc_bias_level_done(card, level);
466
Mark Brown452c5ea2009-05-17 21:41:23 +0100467 return ret;
468}
469
Mark Brown74b8f952009-06-06 11:26:15 +0100470/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200471static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200472 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
473 struct snd_soc_dapm_path *path, const char *control_name,
474 const struct snd_kcontrol_new *kcontrol)
475{
476 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100477 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200478 int i;
479
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100480 if (e->reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200481 soc_dapm_read(dapm, e->reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100482 val = (val >> e->shift_l) & e->mask;
483 item = snd_soc_enum_val_to_item(e, val);
484 } else {
485 /* since a virtual mux has no backing registers to
486 * decide which path to connect, it will try to match
487 * with the first enumeration. This is to ensure
488 * that the default mux choice (the first) will be
489 * correctly powered up during initialization.
490 */
491 item = 0;
492 }
493
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100494 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200495 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200496 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200497 list_add(&path->list_sink, &dest->sources);
498 list_add(&path->list_source, &src->sinks);
Rasmus Villemoes98ad73c2014-10-21 17:01:15 +0200499 path->name = e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100500 if (i == item)
501 path->connect = 1;
502 else
503 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200504 return 0;
505 }
506 }
507
508 return -ENODEV;
509}
510
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100511/* set up initial codec paths */
512static void dapm_set_mixer_path_status(struct snd_soc_dapm_widget *w,
513 struct snd_soc_dapm_path *p, int i)
514{
515 struct soc_mixer_control *mc = (struct soc_mixer_control *)
516 w->kcontrol_news[i].private_value;
517 unsigned int reg = mc->reg;
518 unsigned int shift = mc->shift;
519 unsigned int max = mc->max;
520 unsigned int mask = (1 << fls(max)) - 1;
521 unsigned int invert = mc->invert;
522 unsigned int val;
523
524 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200525 soc_dapm_read(w->dapm, reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100526 val = (val >> shift) & mask;
527 if (invert)
528 val = max - val;
529 p->connect = !!val;
530 } else {
531 p->connect = 0;
532 }
533}
534
Mark Brown74b8f952009-06-06 11:26:15 +0100535/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200536static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200537 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
538 struct snd_soc_dapm_path *path, const char *control_name)
539{
540 int i;
541
542 /* search for mixer kcontrol */
543 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600544 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200545 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200546 list_add(&path->list_sink, &dest->sources);
547 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600548 path->name = dest->kcontrol_news[i].name;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100549 dapm_set_mixer_path_status(dest, path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200550 return 0;
551 }
552 }
553 return -ENODEV;
554}
555
Stephen Warrenaf468002011-04-28 17:38:01 -0600556static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600557 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600558 const struct snd_kcontrol_new *kcontrol_new,
559 struct snd_kcontrol **kcontrol)
560{
561 struct snd_soc_dapm_widget *w;
562 int i;
563
564 *kcontrol = NULL;
565
566 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600567 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
568 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600569 for (i = 0; i < w->num_kcontrols; i++) {
570 if (&w->kcontrol_news[i] == kcontrol_new) {
571 if (w->kcontrols)
572 *kcontrol = w->kcontrols[i];
573 return 1;
574 }
575 }
576 }
577
578 return 0;
579}
580
Stephen Warren85762e72013-03-29 15:40:10 -0600581/*
582 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
583 * create it. Either way, add the widget into the control's widget list
584 */
585static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100586 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200587{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200588 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000589 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000590 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600591 size_t prefix_len;
592 int shared;
593 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600594 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200595 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600596 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200597 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000598
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200599 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000600 if (prefix)
601 prefix_len = strlen(prefix) + 1;
602 else
603 prefix_len = 0;
604
Stephen Warren85762e72013-03-29 15:40:10 -0600605 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
606 &kcontrol);
607
Stephen Warren85762e72013-03-29 15:40:10 -0600608 if (!kcontrol) {
609 if (shared) {
610 wname_in_long_name = false;
611 kcname_in_long_name = true;
612 } else {
613 switch (w->id) {
614 case snd_soc_dapm_switch:
615 case snd_soc_dapm_mixer:
616 wname_in_long_name = true;
617 kcname_in_long_name = true;
618 break;
619 case snd_soc_dapm_mixer_named_ctl:
620 wname_in_long_name = false;
621 kcname_in_long_name = true;
622 break;
623 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600624 wname_in_long_name = true;
625 kcname_in_long_name = false;
626 break;
627 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600628 return -EINVAL;
629 }
630 }
631
632 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600633 /*
634 * The control will get a prefix from the control
635 * creation process but we're also using the same
636 * prefix for widgets so cut the prefix off the
637 * front of the widget name.
638 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200639 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600640 w->name + prefix_len,
641 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200642 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200643 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600644
645 name = long_name;
646 } else if (wname_in_long_name) {
647 long_name = NULL;
648 name = w->name + prefix_len;
649 } else {
650 long_name = NULL;
651 name = w->kcontrol_news[kci].name;
652 }
653
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200654 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600655 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200656 if (!kcontrol) {
657 ret = -ENOMEM;
658 goto exit_free;
659 }
660
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200661 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200662
663 ret = dapm_kcontrol_data_alloc(w, kcontrol);
664 if (ret) {
665 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200666 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200667 }
668
Stephen Warren85762e72013-03-29 15:40:10 -0600669 ret = snd_ctl_add(card, kcontrol);
670 if (ret < 0) {
671 dev_err(dapm->dev,
672 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
673 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200674 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600675 }
Stephen Warren85762e72013-03-29 15:40:10 -0600676 }
677
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200678 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200679 if (ret == 0)
680 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200681
Daniel Macke5092c92014-10-07 13:41:24 +0200682exit_free:
683 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600684
Daniel Macke5092c92014-10-07 13:41:24 +0200685 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600686}
687
688/* create new dapm mixer control */
689static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
690{
691 int i, ret;
692 struct snd_soc_dapm_path *path;
693
Richard Purdie2b97eab2006-10-06 18:32:18 +0200694 /* add kcontrol */
695 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200696 /* match name */
697 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200698 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600699 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200700 continue;
701
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200702 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200703 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200704 continue;
705 }
706
Mark Brown946d92a2013-08-12 23:28:42 +0100707 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600708 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200709 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100710
711 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200712 }
713 }
Stephen Warren85762e72013-03-29 15:40:10 -0600714
715 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200716}
717
718/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200719static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200720{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200721 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600722 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600723 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200724
Stephen Warrenaf468002011-04-28 17:38:01 -0600725 if (w->num_kcontrols != 1) {
726 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000727 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600728 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200729 return -EINVAL;
730 }
731
Lars-Peter Clausenfe581392013-08-01 18:30:38 +0200732 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600733 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
734 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600735 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200736
Mark Brown946d92a2013-08-12 23:28:42 +0100737 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600738 if (ret < 0)
739 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600740
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +0200741 list_for_each_entry(path, &w->sources, list_sink) {
742 if (path->name)
743 dapm_kcontrol_add_path(w->kcontrols[0], path);
744 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200745
Stephen Warrenaf468002011-04-28 17:38:01 -0600746 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200747}
748
749/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200750static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200751{
Mark Browna6c65732010-03-03 17:45:21 +0000752 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200753 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000754 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200755
Mark Browna6c65732010-03-03 17:45:21 +0000756 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200757}
758
Mark Brown9949788b2010-05-07 20:24:05 +0100759/* We implement power down on suspend by checking the power state of
760 * the ALSA card - when we are suspending the ALSA state for the card
761 * is set to D3.
762 */
763static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
764{
Mark Brown12ea2c72011-03-02 18:17:32 +0000765 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100766
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000767 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100768 case SNDRV_CTL_POWER_D3hot:
769 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100770 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000771 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200772 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100773 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100774 default:
775 return 1;
776 }
777}
778
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100779/* add widget to list if it's not already in the list */
780static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
781 struct snd_soc_dapm_widget *w)
782{
783 struct snd_soc_dapm_widget_list *wlist;
784 int wlistsize, wlistentries, i;
785
786 if (*list == NULL)
787 return -EINVAL;
788
789 wlist = *list;
790
791 /* is this widget already in the list */
792 for (i = 0; i < wlist->num_widgets; i++) {
793 if (wlist->widgets[i] == w)
794 return 0;
795 }
796
797 /* allocate some new space */
798 wlistentries = wlist->num_widgets + 1;
799 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
800 wlistentries * sizeof(struct snd_soc_dapm_widget *);
801 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
802 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000803 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100804 w->name);
805 return -ENOMEM;
806 }
807 wlist = *list;
808
809 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000810 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100811 w->name, wlist->num_widgets);
812
813 wlist->widgets[wlist->num_widgets] = w;
814 wlist->num_widgets++;
815 return 1;
816}
817
Richard Purdie2b97eab2006-10-06 18:32:18 +0200818/*
819 * Recursively check for a completed path to an active or physically connected
820 * output widget. Returns number of complete paths.
821 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100822static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
823 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200824{
825 struct snd_soc_dapm_path *path;
826 int con = 0;
827
Mark Brown024dc072011-10-09 11:52:05 +0100828 if (widget->outputs >= 0)
829 return widget->outputs;
830
Mark Brownde02d072011-09-20 21:43:24 +0100831 DAPM_UPDATE_STAT(widget, path_checks);
832
Mark Brown62ea8742012-01-21 21:14:48 +0000833 switch (widget->id) {
834 case snd_soc_dapm_supply:
835 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200836 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200837 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100838 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000839 default:
840 break;
841 }
Mark Brown246d0a12009-04-22 18:24:55 +0100842
Mark Brown010ff262009-08-17 17:39:22 +0100843 switch (widget->id) {
844 case snd_soc_dapm_adc:
845 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100846 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100847 if (widget->active) {
848 widget->outputs = snd_soc_dapm_suspend_check(widget);
849 return widget->outputs;
850 }
Mark Brown010ff262009-08-17 17:39:22 +0100851 default:
852 break;
853 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200854
855 if (widget->connected) {
856 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100857 if (widget->id == snd_soc_dapm_output && !widget->ext) {
858 widget->outputs = snd_soc_dapm_suspend_check(widget);
859 return widget->outputs;
860 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200861
862 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100863 if (widget->id == snd_soc_dapm_hp ||
864 widget->id == snd_soc_dapm_spk ||
865 (widget->id == snd_soc_dapm_line &&
866 !list_empty(&widget->sources))) {
867 widget->outputs = snd_soc_dapm_suspend_check(widget);
868 return widget->outputs;
869 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200870 }
871
872 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100873 DAPM_UPDATE_STAT(widget, neighbour_checks);
874
Mark Brownbf3a9e12011-06-13 16:42:29 +0100875 if (path->weak)
876 continue;
877
Mark Brown8af294b2013-02-22 17:48:15 +0000878 if (path->walking)
879 return 1;
880
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100881 trace_snd_soc_dapm_output_path(widget, path);
882
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +0200883 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +0000884 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100885
886 /* do we need to add this widget to the list ? */
887 if (list) {
888 int err;
889 err = dapm_list_add_widget(list, path->sink);
890 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000891 dev_err(widget->dapm->dev,
892 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100893 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000894 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100895 return con;
896 }
897 }
898
899 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000900
901 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200902 }
903 }
904
Mark Brown024dc072011-10-09 11:52:05 +0100905 widget->outputs = con;
906
Richard Purdie2b97eab2006-10-06 18:32:18 +0200907 return con;
908}
909
910/*
911 * Recursively check for a completed path to an active or physically connected
912 * input widget. Returns number of complete paths.
913 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100914static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
915 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200916{
917 struct snd_soc_dapm_path *path;
918 int con = 0;
919
Mark Brown024dc072011-10-09 11:52:05 +0100920 if (widget->inputs >= 0)
921 return widget->inputs;
922
Mark Brownde02d072011-09-20 21:43:24 +0100923 DAPM_UPDATE_STAT(widget, path_checks);
924
Mark Brown62ea8742012-01-21 21:14:48 +0000925 switch (widget->id) {
926 case snd_soc_dapm_supply:
927 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200928 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200929 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100930 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000931 default:
932 break;
933 }
Mark Brown246d0a12009-04-22 18:24:55 +0100934
Richard Purdie2b97eab2006-10-06 18:32:18 +0200935 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100936 switch (widget->id) {
937 case snd_soc_dapm_dac:
938 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +0100939 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +0100940 if (widget->active) {
941 widget->inputs = snd_soc_dapm_suspend_check(widget);
942 return widget->inputs;
943 }
Mark Brown010ff262009-08-17 17:39:22 +0100944 default:
945 break;
946 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200947
948 if (widget->connected) {
949 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100950 if (widget->id == snd_soc_dapm_input && !widget->ext) {
951 widget->inputs = snd_soc_dapm_suspend_check(widget);
952 return widget->inputs;
953 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200954
955 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100956 if (widget->id == snd_soc_dapm_vmid) {
957 widget->inputs = snd_soc_dapm_suspend_check(widget);
958 return widget->inputs;
959 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200960
961 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300962 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100963 (widget->id == snd_soc_dapm_line &&
964 !list_empty(&widget->sinks))) {
965 widget->inputs = snd_soc_dapm_suspend_check(widget);
966 return widget->inputs;
967 }
968
Mark Brown1ab97c82011-11-27 16:21:51 +0000969 /* signal generator */
970 if (widget->id == snd_soc_dapm_siggen) {
971 widget->inputs = snd_soc_dapm_suspend_check(widget);
972 return widget->inputs;
973 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200974 }
975
976 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +0100977 DAPM_UPDATE_STAT(widget, neighbour_checks);
978
Mark Brownbf3a9e12011-06-13 16:42:29 +0100979 if (path->weak)
980 continue;
981
Mark Brown8af294b2013-02-22 17:48:15 +0000982 if (path->walking)
983 return 1;
984
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100985 trace_snd_soc_dapm_input_path(widget, path);
986
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +0200987 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +0000988 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100989
990 /* do we need to add this widget to the list ? */
991 if (list) {
992 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +0100993 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100994 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000995 dev_err(widget->dapm->dev,
996 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100997 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000998 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100999 return con;
1000 }
1001 }
1002
1003 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001004
1005 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001006 }
1007 }
1008
Mark Brown024dc072011-10-09 11:52:05 +01001009 widget->inputs = con;
1010
Richard Purdie2b97eab2006-10-06 18:32:18 +02001011 return con;
1012}
1013
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001014/**
1015 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1016 * @dai: the soc DAI.
1017 * @stream: stream direction.
1018 * @list: list of active widgets for this stream.
1019 *
1020 * Queries DAPM graph as to whether an valid audio stream path exists for
1021 * the initial stream specified by name. This takes into account
1022 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1023 *
1024 * Returns the number of valid paths or negative error.
1025 */
1026int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1027 struct snd_soc_dapm_widget_list **list)
1028{
1029 struct snd_soc_card *card = dai->card;
1030 int paths;
1031
1032 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1033 dapm_reset(card);
1034
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001035 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001036 paths = is_connected_output_ep(dai->playback_widget, list);
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001037 else
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001038 paths = is_connected_input_ep(dai->capture_widget, list);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001039
1040 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001041 mutex_unlock(&card->dapm_mutex);
1042
1043 return paths;
1044}
1045
Richard Purdie2b97eab2006-10-06 18:32:18 +02001046/*
Mark Brown62ea8742012-01-21 21:14:48 +00001047 * Handler for regulator supply widget.
1048 */
1049int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1050 struct snd_kcontrol *kcontrol, int event)
1051{
Mark Brownc05b84d2012-09-07 12:57:11 +08001052 int ret;
1053
Mark Browneb270e92013-10-09 13:52:52 +01001054 soc_dapm_async_complete(w->dapm);
1055
Mark Brownc05b84d2012-09-07 12:57:11 +08001056 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001057 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001058 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001059 if (ret != 0)
1060 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001061 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001062 w->name, ret);
1063 }
1064
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001065 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001066 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001067 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001068 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001069 if (ret != 0)
1070 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001071 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001072 w->name, ret);
1073 }
1074
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001075 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001076 }
Mark Brown62ea8742012-01-21 21:14:48 +00001077}
1078EXPORT_SYMBOL_GPL(dapm_regulator_event);
1079
Ola Liljad7e7eb92012-05-24 15:26:25 +02001080/*
1081 * Handler for clock supply widget.
1082 */
1083int dapm_clock_event(struct snd_soc_dapm_widget *w,
1084 struct snd_kcontrol *kcontrol, int event)
1085{
1086 if (!w->clk)
1087 return -EIO;
1088
Mark Browneb270e92013-10-09 13:52:52 +01001089 soc_dapm_async_complete(w->dapm);
1090
Mark Brownec029952012-06-04 08:16:20 +01001091#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001092 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001093 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001094 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001095 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001096 return 0;
1097 }
Mark Brownec029952012-06-04 08:16:20 +01001098#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001099 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001100}
1101EXPORT_SYMBOL_GPL(dapm_clock_event);
1102
Mark Brownd805002b2011-09-28 18:28:23 +01001103static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1104{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001105 if (w->power_checked)
1106 return w->new_power;
1107
Mark Brownd805002b2011-09-28 18:28:23 +01001108 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001109 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +01001110 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001111 w->new_power = w->power_check(w);
1112
1113 w->power_checked = true;
1114
1115 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +01001116}
1117
Mark Browncd0f2d42009-04-20 16:56:59 +01001118/* Generic check to see if a widget should be powered.
1119 */
1120static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1121{
1122 int in, out;
1123
Mark Brownde02d072011-09-20 21:43:24 +01001124 DAPM_UPDATE_STAT(w, power_checks);
1125
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001126 in = is_connected_input_ep(w, NULL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001127 out = is_connected_output_ep(w, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001128 return out != 0 && in != 0;
1129}
1130
Mark Brown246d0a12009-04-22 18:24:55 +01001131/* Check to see if a power supply is needed */
1132static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1133{
1134 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001135
Mark Brownde02d072011-09-20 21:43:24 +01001136 DAPM_UPDATE_STAT(w, power_checks);
1137
Mark Brown246d0a12009-04-22 18:24:55 +01001138 /* Check if one of our outputs is connected */
1139 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001140 DAPM_UPDATE_STAT(w, neighbour_checks);
1141
Mark Brownbf3a9e12011-06-13 16:42:29 +01001142 if (path->weak)
1143 continue;
1144
Mark Brown215edda2009-09-08 18:59:05 +01001145 if (path->connected &&
1146 !path->connected(path->source, path->sink))
1147 continue;
1148
Mark Brownf68d7e12011-10-04 22:57:50 +01001149 if (dapm_widget_power_check(path->sink))
1150 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001151 }
1152
Mark Brownf68d7e12011-10-04 22:57:50 +01001153 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001154}
1155
Mark Brown35c64bc2011-09-28 18:23:53 +01001156static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1157{
1158 return 1;
1159}
1160
Mark Brown38357ab2009-06-06 19:03:23 +01001161static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1162 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001163 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001164{
Mark Brown828a8422011-01-15 13:14:30 +00001165 int *sort;
1166
1167 if (power_up)
1168 sort = dapm_up_seq;
1169 else
1170 sort = dapm_down_seq;
1171
Mark Brown38357ab2009-06-06 19:03:23 +01001172 if (sort[a->id] != sort[b->id])
1173 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001174 if (a->subseq != b->subseq) {
1175 if (power_up)
1176 return a->subseq - b->subseq;
1177 else
1178 return b->subseq - a->subseq;
1179 }
Mark Brownb22ead22009-06-07 12:51:26 +01001180 if (a->reg != b->reg)
1181 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001182 if (a->dapm != b->dapm)
1183 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001184
Mark Brown38357ab2009-06-06 19:03:23 +01001185 return 0;
1186}
Mark Brown42aa3412009-03-01 19:21:10 +00001187
Mark Brown38357ab2009-06-06 19:03:23 +01001188/* Insert a widget in order into a DAPM power sequence. */
1189static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1190 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001191 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001192{
1193 struct snd_soc_dapm_widget *w;
1194
1195 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001196 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001197 list_add_tail(&new_widget->power_list, &w->power_list);
1198 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001199 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001200
Mark Brown38357ab2009-06-06 19:03:23 +01001201 list_add_tail(&new_widget->power_list, list);
1202}
Mark Brown42aa3412009-03-01 19:21:10 +00001203
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001204static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001205 struct snd_soc_dapm_widget *w, int event)
1206{
Mark Brown68f89ad2010-11-03 23:51:49 -04001207 const char *ev_name;
1208 int power, ret;
1209
1210 switch (event) {
1211 case SND_SOC_DAPM_PRE_PMU:
1212 ev_name = "PRE_PMU";
1213 power = 1;
1214 break;
1215 case SND_SOC_DAPM_POST_PMU:
1216 ev_name = "POST_PMU";
1217 power = 1;
1218 break;
1219 case SND_SOC_DAPM_PRE_PMD:
1220 ev_name = "PRE_PMD";
1221 power = 0;
1222 break;
1223 case SND_SOC_DAPM_POST_PMD:
1224 ev_name = "POST_PMD";
1225 power = 0;
1226 break;
Mark Brown80114122013-02-25 15:14:19 +00001227 case SND_SOC_DAPM_WILL_PMU:
1228 ev_name = "WILL_PMU";
1229 power = 1;
1230 break;
1231 case SND_SOC_DAPM_WILL_PMD:
1232 ev_name = "WILL_PMD";
1233 power = 0;
1234 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001235 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001236 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001237 return;
1238 }
1239
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001240 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001241 return;
1242
1243 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001244 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001245 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001246 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001247 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001248 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001249 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001250 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001251 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001252 ev_name, w->name, ret);
1253 }
1254}
1255
Mark Brownb22ead22009-06-07 12:51:26 +01001256/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001257static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001258 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001259{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001260 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001261 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001262 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001263 unsigned int value = 0;
1264 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001265
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001266 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1267 reg = w->reg;
1268 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001269
1270 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001271 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001272 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001273
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001274 mask |= w->mask << w->shift;
1275 if (w->power)
1276 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001277 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001278 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001279
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001280 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001281 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1282 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001283
Mark Brown68f89ad2010-11-03 23:51:49 -04001284 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001285 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1286 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001287 }
1288
Mark Brown81628102009-06-07 13:21:24 +01001289 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001290 /* Any widget will do, they should all be updating the
1291 * same register.
1292 */
Mark Brown29376bc2011-06-19 13:49:28 +01001293
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001294 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001295 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001296 value, mask, reg, card->pop_time);
1297 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001298 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001299 }
1300
1301 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001302 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1303 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001304 }
Mark Brown42aa3412009-03-01 19:21:10 +00001305}
1306
Mark Brownb22ead22009-06-07 12:51:26 +01001307/* Apply a DAPM power sequence.
1308 *
1309 * We walk over a pre-sorted list of widgets to apply power to. In
1310 * order to minimise the number of writes to the device required
1311 * multiple widgets will be updated in a single write where possible.
1312 * Currently anything that requires more than a single write is not
1313 * handled.
1314 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001315static void dapm_seq_run(struct snd_soc_card *card,
1316 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001317{
1318 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001319 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001320 LIST_HEAD(pending);
1321 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001322 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001323 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001324 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001325 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001326 int *sort;
1327
1328 if (power_up)
1329 sort = dapm_up_seq;
1330 else
1331 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001332
Mark Brownb22ead22009-06-07 12:51:26 +01001333 list_for_each_entry_safe(w, n, list, power_list) {
1334 ret = 0;
1335
1336 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001337 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001338 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001339 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001340 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001341
Mark Brown474b62d2011-01-18 16:14:44 +00001342 if (cur_dapm && cur_dapm->seq_notifier) {
1343 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1344 if (sort[i] == cur_sort)
1345 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001346 i,
1347 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001348 }
1349
Mark Browneb270e92013-10-09 13:52:52 +01001350 if (cur_dapm && w->dapm != cur_dapm)
1351 soc_dapm_async_complete(cur_dapm);
1352
Mark Brownb22ead22009-06-07 12:51:26 +01001353 INIT_LIST_HEAD(&pending);
1354 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001355 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001356 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001357 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001358 }
1359
Mark Brown163cac02009-06-07 10:12:52 +01001360 switch (w->id) {
1361 case snd_soc_dapm_pre:
1362 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001363 list_for_each_entry_safe_continue(w, n, list,
1364 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001365
Mark Brownb22ead22009-06-07 12:51:26 +01001366 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001367 ret = w->event(w,
1368 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001369 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001370 ret = w->event(w,
1371 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001372 break;
1373
1374 case snd_soc_dapm_post:
1375 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001376 list_for_each_entry_safe_continue(w, n, list,
1377 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001378
Mark Brownb22ead22009-06-07 12:51:26 +01001379 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001380 ret = w->event(w,
1381 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001382 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001383 ret = w->event(w,
1384 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001385 break;
1386
Mark Brown163cac02009-06-07 10:12:52 +01001387 default:
Mark Brown81628102009-06-07 13:21:24 +01001388 /* Queue it up for application */
1389 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001390 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001391 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001392 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001393 list_move(&w->power_list, &pending);
1394 break;
Mark Brown163cac02009-06-07 10:12:52 +01001395 }
Mark Brownb22ead22009-06-07 12:51:26 +01001396
1397 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001398 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001399 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001400 }
Mark Brownb22ead22009-06-07 12:51:26 +01001401
1402 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001403 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001404
1405 if (cur_dapm && cur_dapm->seq_notifier) {
1406 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1407 if (sort[i] == cur_sort)
1408 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001409 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001410 }
Mark Browneb270e92013-10-09 13:52:52 +01001411
1412 list_for_each_entry(d, &card->dapm_list, list) {
1413 soc_dapm_async_complete(d);
1414 }
Mark Brown163cac02009-06-07 10:12:52 +01001415}
1416
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001417static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001418{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001419 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001420 struct snd_soc_dapm_widget_list *wlist;
1421 struct snd_soc_dapm_widget *w = NULL;
1422 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001423 int ret;
1424
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001425 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001426 return;
1427
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001428 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001429
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001430 for (wi = 0; wi < wlist->num_widgets; wi++) {
1431 w = wlist->widgets[wi];
1432
1433 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1434 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1435 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001436 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001437 w->name, ret);
1438 }
Mark Brown97404f22010-12-14 16:13:57 +00001439 }
1440
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001441 if (!w)
1442 return;
1443
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001444 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1445 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001446 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001447 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001448 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001449
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001450 for (wi = 0; wi < wlist->num_widgets; wi++) {
1451 w = wlist->widgets[wi];
1452
1453 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1454 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1455 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001456 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001457 w->name, ret);
1458 }
Mark Brown97404f22010-12-14 16:13:57 +00001459 }
1460}
1461
Mark Brown9d0624a2011-02-18 11:49:43 -08001462/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1463 * they're changing state.
1464 */
1465static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1466{
1467 struct snd_soc_dapm_context *d = data;
1468 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001469
Mark Brown56fba412011-06-04 11:25:10 +01001470 /* If we're off and we're not supposed to be go into STANDBY */
1471 if (d->bias_level == SND_SOC_BIAS_OFF &&
1472 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001473 if (d->dev)
1474 pm_runtime_get_sync(d->dev);
1475
Mark Brown9d0624a2011-02-18 11:49:43 -08001476 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1477 if (ret != 0)
1478 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001479 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001480 }
1481
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001482 /* Prepare for a transition to ON or away from ON */
1483 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1484 d->bias_level != SND_SOC_BIAS_ON) ||
1485 (d->target_bias_level != SND_SOC_BIAS_ON &&
1486 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001487 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1488 if (ret != 0)
1489 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001490 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001491 }
1492}
1493
1494/* Async callback run prior to DAPM sequences - brings to their final
1495 * state.
1496 */
1497static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1498{
1499 struct snd_soc_dapm_context *d = data;
1500 int ret;
1501
1502 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001503 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1504 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1505 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001506 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1507 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001508 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001509 ret);
1510 }
1511
1512 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001513 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1514 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001515 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1516 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001517 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1518 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001519
1520 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001521 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001522 }
1523
1524 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001525 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1526 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001527 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1528 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001529 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001530 ret);
1531 }
1532}
Mark Brown97404f22010-12-14 16:13:57 +00001533
Mark Brownfe4fda52011-10-03 22:36:57 +01001534static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1535 bool power, bool connect)
1536{
1537 /* If a connection is being made or broken then that update
1538 * will have marked the peer dirty, otherwise the widgets are
1539 * not connected and this update has no impact. */
1540 if (!connect)
1541 return;
1542
1543 /* If the peer is already in the state we're moving to then we
1544 * won't have an impact on it. */
1545 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001546 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001547}
1548
Mark Brown05623c42011-09-28 17:02:31 +01001549static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1550 struct list_head *up_list,
1551 struct list_head *down_list)
1552{
Mark Browndb432b42011-10-03 21:06:40 +01001553 struct snd_soc_dapm_path *path;
1554
Mark Brown05623c42011-09-28 17:02:31 +01001555 if (w->power == power)
1556 return;
1557
1558 trace_snd_soc_dapm_widget_power(w, power);
1559
Mark Browndb432b42011-10-03 21:06:40 +01001560 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001561 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001562 */
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001563 list_for_each_entry(path, &w->sources, list_sink)
1564 dapm_widget_set_peer_power(path->source, power, path->connect);
1565
Mark Brownf3bf3e42011-10-04 22:43:31 +01001566 switch (w->id) {
1567 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001568 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001569 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001570 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001571 /* Supplies can't affect their outputs, only their inputs */
1572 break;
1573 default:
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001574 list_for_each_entry(path, &w->sinks, list_source)
1575 dapm_widget_set_peer_power(path->sink, power,
1576 path->connect);
Mark Brownf3bf3e42011-10-04 22:43:31 +01001577 break;
Mark Browndb432b42011-10-03 21:06:40 +01001578 }
1579
Mark Brown05623c42011-09-28 17:02:31 +01001580 if (power)
1581 dapm_seq_insert(w, up_list, true);
1582 else
1583 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001584}
1585
Mark Brown7c81beb2011-09-20 22:22:32 +01001586static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1587 struct list_head *up_list,
1588 struct list_head *down_list)
1589{
Mark Brown7c81beb2011-09-20 22:22:32 +01001590 int power;
1591
1592 switch (w->id) {
1593 case snd_soc_dapm_pre:
1594 dapm_seq_insert(w, down_list, false);
1595 break;
1596 case snd_soc_dapm_post:
1597 dapm_seq_insert(w, up_list, true);
1598 break;
1599
1600 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001601 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001602
Mark Brown05623c42011-09-28 17:02:31 +01001603 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001604 break;
1605 }
1606}
1607
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001608static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1609{
1610 if (dapm->idle_bias_off)
1611 return true;
1612
1613 switch (snd_power_get_state(dapm->card->snd_card)) {
1614 case SNDRV_CTL_POWER_D3hot:
1615 case SNDRV_CTL_POWER_D3cold:
1616 return dapm->suspend_bias_off;
1617 default:
1618 break;
1619 }
1620
1621 return false;
1622}
1623
Mark Brown42aa3412009-03-01 19:21:10 +00001624/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001625 * Scan each dapm widget for complete audio path.
1626 * A complete path is a route that has valid endpoints i.e.:-
1627 *
1628 * o DAC to output pin.
1629 * o Input Pin to ADC.
1630 * o Input pin to Output pin (bypass, sidetone)
1631 * o DAC to ADC (loopback).
1632 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001633static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001634{
1635 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001636 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001637 LIST_HEAD(up_list);
1638 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001639 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001640 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001641
Mark Brownf9fa2b12014-03-06 16:49:11 +08001642 lockdep_assert_held(&card->dapm_mutex);
1643
Mark Brown84e90932010-11-04 00:07:02 -04001644 trace_snd_soc_dapm_start(card);
1645
Mark Brown56fba412011-06-04 11:25:10 +01001646 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001647 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001648 d->target_bias_level = SND_SOC_BIAS_OFF;
1649 else
1650 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001651 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001652
Liam Girdwood6c120e12012-02-15 15:15:34 +00001653 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001654
Mark Brown6d3ddc82009-05-16 17:47:29 +01001655 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001656 * lists indicating if they should be powered up or down. We
1657 * only check widgets that have been flagged as dirty but note
1658 * that new widgets may be added to the dirty list while we
1659 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001660 */
Mark Browndb432b42011-10-03 21:06:40 +01001661 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001662 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001663 }
1664
Mark Brownf9de6d72011-09-28 17:19:47 +01001665 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001666 switch (w->id) {
1667 case snd_soc_dapm_pre:
1668 case snd_soc_dapm_post:
1669 /* These widgets always need to be powered */
1670 break;
1671 default:
1672 list_del_init(&w->dirty);
1673 break;
1674 }
Mark Browndb432b42011-10-03 21:06:40 +01001675
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001676 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001677 d = w->dapm;
1678
1679 /* Supplies and micbiases only bring the
1680 * context up to STANDBY as unless something
1681 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001682 * generally don't require full power. Signal
1683 * generators are virtual pins and have no
1684 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001685 */
1686 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001687 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001688 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001689 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001690 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001691 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001692 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001693 case snd_soc_dapm_micbias:
1694 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1695 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1696 break;
1697 default:
1698 d->target_bias_level = SND_SOC_BIAS_ON;
1699 break;
1700 }
1701 }
1702
1703 }
1704
Mark Brown85a843c2011-09-21 21:29:47 +01001705 /* Force all contexts in the card to the same bias state if
1706 * they're not ground referenced.
1707 */
Mark Brown56fba412011-06-04 11:25:10 +01001708 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001709 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001710 if (d->target_bias_level > bias)
1711 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001712 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001713 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01001714 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001715
Mark Brownde02d072011-09-20 21:43:24 +01001716 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001717
Xiang Xiao17282ba2014-03-02 00:04:03 +08001718 /* Run card bias changes at first */
1719 dapm_pre_sequence_async(&card->dapm, 0);
1720 /* Run other bias changes in parallel */
1721 list_for_each_entry(d, &card->dapm_list, list) {
1722 if (d != &card->dapm)
1723 async_schedule_domain(dapm_pre_sequence_async, d,
1724 &async_domain);
1725 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001726 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001727
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001728 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001729 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001730 }
1731
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001732 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001733 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001734 }
1735
Mark Brown6d3ddc82009-05-16 17:47:29 +01001736 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001737 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001738
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001739 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001740
Mark Brown6d3ddc82009-05-16 17:47:29 +01001741 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001742 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001743
Mark Brown9d0624a2011-02-18 11:49:43 -08001744 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001745 list_for_each_entry(d, &card->dapm_list, list) {
1746 if (d != &card->dapm)
1747 async_schedule_domain(dapm_post_sequence_async, d,
1748 &async_domain);
1749 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001750 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001751 /* Run card bias changes at last */
1752 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001753
Liam Girdwood8078d872012-02-15 15:15:35 +00001754 /* do we need to notify any clients that DAPM event is complete */
1755 list_for_each_entry(d, &card->dapm_list, list) {
1756 if (d->stream_event)
1757 d->stream_event(d, event);
1758 }
1759
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001760 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001761 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001762 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001763
Mark Brown84e90932010-11-04 00:07:02 -04001764 trace_snd_soc_dapm_done(card);
1765
Mark Brown42aa3412009-03-01 19:21:10 +00001766 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001767}
1768
Mark Brown79fb9382009-08-21 16:38:13 +01001769#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001770static ssize_t dapm_widget_power_read_file(struct file *file,
1771 char __user *user_buf,
1772 size_t count, loff_t *ppos)
1773{
1774 struct snd_soc_dapm_widget *w = file->private_data;
1775 char *buf;
1776 int in, out;
1777 ssize_t ret;
1778 struct snd_soc_dapm_path *p = NULL;
1779
1780 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1781 if (!buf)
1782 return -ENOMEM;
1783
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001784 in = is_connected_input_ep(w, NULL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001785 out = is_connected_output_ep(w, NULL);
Mark Brown79fb9382009-08-21 16:38:13 +01001786
Mark Brownf13ebad2012-03-03 18:01:01 +00001787 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1788 w->name, w->power ? "On" : "Off",
1789 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001790
Mark Brownd033c362009-12-04 15:25:56 +00001791 if (w->reg >= 0)
1792 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001793 " - R%d(0x%x) mask 0x%x",
1794 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001795
1796 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1797
Mark Brown3eef08b2009-09-14 16:49:00 +01001798 if (w->sname)
1799 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1800 w->sname,
1801 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001802
1803 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001804 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001805 continue;
1806
Mark Brown79fb9382009-08-21 16:38:13 +01001807 if (p->connect)
1808 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001809 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001810 p->name ? p->name : "static",
1811 p->source->name);
1812 }
1813 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001814 if (p->connected && !p->connected(w, p->sink))
1815 continue;
1816
Mark Brown79fb9382009-08-21 16:38:13 +01001817 if (p->connect)
1818 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001819 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001820 p->name ? p->name : "static",
1821 p->sink->name);
1822 }
1823
1824 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1825
1826 kfree(buf);
1827 return ret;
1828}
1829
1830static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001831 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001832 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001833 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001834};
1835
Mark Brownef49e4f2011-04-04 20:48:13 +09001836static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1837 size_t count, loff_t *ppos)
1838{
1839 struct snd_soc_dapm_context *dapm = file->private_data;
1840 char *level;
1841
1842 switch (dapm->bias_level) {
1843 case SND_SOC_BIAS_ON:
1844 level = "On\n";
1845 break;
1846 case SND_SOC_BIAS_PREPARE:
1847 level = "Prepare\n";
1848 break;
1849 case SND_SOC_BIAS_STANDBY:
1850 level = "Standby\n";
1851 break;
1852 case SND_SOC_BIAS_OFF:
1853 level = "Off\n";
1854 break;
1855 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001856 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09001857 level = "Unknown\n";
1858 break;
1859 }
1860
1861 return simple_read_from_buffer(user_buf, count, ppos, level,
1862 strlen(level));
1863}
1864
1865static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001866 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001867 .read = dapm_bias_read_file,
1868 .llseek = default_llseek,
1869};
1870
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001871void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1872 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001873{
Mark Brown79fb9382009-08-21 16:38:13 +01001874 struct dentry *d;
1875
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001876 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1877
1878 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001879 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001880 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001881 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001882 }
Mark Brown79fb9382009-08-21 16:38:13 +01001883
Mark Brownef49e4f2011-04-04 20:48:13 +09001884 d = debugfs_create_file("bias_level", 0444,
1885 dapm->debugfs_dapm, dapm,
1886 &dapm_bias_fops);
1887 if (!d)
1888 dev_warn(dapm->dev,
1889 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001890}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001891
1892static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1893{
1894 struct snd_soc_dapm_context *dapm = w->dapm;
1895 struct dentry *d;
1896
1897 if (!dapm->debugfs_dapm || !w->name)
1898 return;
1899
1900 d = debugfs_create_file(w->name, 0444,
1901 dapm->debugfs_dapm, w,
1902 &dapm_widget_power_fops);
1903 if (!d)
1904 dev_warn(w->dapm->dev,
1905 "ASoC: Failed to create %s debugfs file\n",
1906 w->name);
1907}
1908
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001909static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1910{
1911 debugfs_remove_recursive(dapm->debugfs_dapm);
1912}
1913
Mark Brown79fb9382009-08-21 16:38:13 +01001914#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001915void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1916 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001917{
1918}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001919
1920static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1921{
1922}
1923
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001924static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1925{
1926}
1927
Mark Brown79fb9382009-08-21 16:38:13 +01001928#endif
1929
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02001930/*
1931 * soc_dapm_connect_path() - Connects or disconnects a path
1932 * @path: The path to update
1933 * @connect: The new connect state of the path. True if the path is connected,
1934 * false if it is disconneted.
1935 * @reason: The reason why the path changed (for debugging only)
1936 */
1937static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
1938 bool connect, const char *reason)
1939{
1940 if (path->connect == connect)
1941 return;
1942
1943 path->connect = connect;
1944 dapm_mark_dirty(path->source, reason);
1945 dapm_mark_dirty(path->sink, reason);
1946}
1947
Richard Purdie2b97eab2006-10-06 18:32:18 +02001948/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001949static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001950 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001951{
1952 struct snd_soc_dapm_path *path;
1953 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02001954 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001955
Mark Brownf9fa2b12014-03-06 16:49:11 +08001956 lockdep_assert_held(&card->dapm_mutex);
1957
Richard Purdie2b97eab2006-10-06 18:32:18 +02001958 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02001959 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001960 found = 1;
1961 /* we now need to match the string in the enum to the path */
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02001962 if (!(strcmp(path->name, e->texts[mux])))
1963 connect = true;
1964 else
1965 connect = false;
1966
1967 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001968 }
1969
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001970 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001971 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001972
Liam Girdwood618dae12012-04-25 12:12:51 +01001973 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001974}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001975
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001976int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02001977 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
1978 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001979{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001980 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001981 int ret;
1982
Liam Girdwood3cd04342012-03-09 12:02:08 +00001983 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02001984 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001985 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02001986 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001987 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001988 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001989 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001990 return ret;
1991}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001992EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001993
Milan plzik1b075e32008-01-10 14:39:46 +01001994/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001995static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00001996 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001997{
1998 struct snd_soc_dapm_path *path;
1999 int found = 0;
2000
Mark Brownf9fa2b12014-03-06 16:49:11 +08002001 lockdep_assert_held(&card->dapm_mutex);
2002
Richard Purdie2b97eab2006-10-06 18:32:18 +02002003 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002004 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002005 found = 1;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002006 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002007 }
2008
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002009 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002010 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002011
Liam Girdwood618dae12012-04-25 12:12:51 +01002012 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002013}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002014
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002015int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002016 struct snd_kcontrol *kcontrol, int connect,
2017 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002018{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002019 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002020 int ret;
2021
Liam Girdwood3cd04342012-03-09 12:02:08 +00002022 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002023 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002024 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002025 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002026 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002027 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002028 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002029 return ret;
2030}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002031EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002032
Benoit Cousson44ba2642014-07-08 23:19:36 +02002033static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002034{
Richard Purdie2b97eab2006-10-06 18:32:18 +02002035 struct snd_soc_dapm_widget *w;
2036 int count = 0;
2037 char *state = "not set";
2038
Lars-Peter Clausen00200102014-07-17 22:01:07 +02002039 list_for_each_entry(w, &codec->component.card->widgets, list) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002040 if (w->dapm != &codec->dapm)
2041 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002042
2043 /* only display widgets that burnm power */
2044 switch (w->id) {
2045 case snd_soc_dapm_hp:
2046 case snd_soc_dapm_mic:
2047 case snd_soc_dapm_spk:
2048 case snd_soc_dapm_line:
2049 case snd_soc_dapm_micbias:
2050 case snd_soc_dapm_dac:
2051 case snd_soc_dapm_adc:
2052 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002053 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002054 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002055 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002056 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002057 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002058 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002059 if (w->name)
2060 count += sprintf(buf + count, "%s: %s\n",
2061 w->name, w->power ? "On":"Off");
2062 break;
2063 default:
2064 break;
2065 }
2066 }
2067
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002068 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002069 case SND_SOC_BIAS_ON:
2070 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002071 break;
Mark Brown0be98982008-05-19 12:31:28 +02002072 case SND_SOC_BIAS_PREPARE:
2073 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002074 break;
Mark Brown0be98982008-05-19 12:31:28 +02002075 case SND_SOC_BIAS_STANDBY:
2076 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002077 break;
Mark Brown0be98982008-05-19 12:31:28 +02002078 case SND_SOC_BIAS_OFF:
2079 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002080 break;
2081 }
2082 count += sprintf(buf + count, "PM State: %s\n", state);
2083
2084 return count;
2085}
2086
Benoit Cousson44ba2642014-07-08 23:19:36 +02002087/* show dapm widget status in sys fs */
2088static ssize_t dapm_widget_show(struct device *dev,
2089 struct device_attribute *attr, char *buf)
2090{
2091 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2092 int i, count = 0;
2093
2094 for (i = 0; i < rtd->num_codecs; i++) {
2095 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
2096 count += dapm_widget_show_codec(codec, buf + count);
2097 }
2098
2099 return count;
2100}
2101
Richard Purdie2b97eab2006-10-06 18:32:18 +02002102static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2103
2104int snd_soc_dapm_sys_add(struct device *dev)
2105{
Troy Kisky12ef1932008-10-13 17:42:14 -07002106 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002107}
2108
2109static void snd_soc_dapm_sys_remove(struct device *dev)
2110{
Mark Brownaef90842009-05-16 17:53:16 +01002111 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002112}
2113
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002114static void dapm_free_path(struct snd_soc_dapm_path *path)
2115{
2116 list_del(&path->list_sink);
2117 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002118 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002119 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002120 kfree(path);
2121}
2122
Richard Purdie2b97eab2006-10-06 18:32:18 +02002123/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002124static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002125{
2126 struct snd_soc_dapm_widget *w, *next_w;
2127 struct snd_soc_dapm_path *p, *next_p;
2128
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002129 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2130 if (w->dapm != dapm)
2131 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002132 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002133 /*
2134 * remove source and sink paths associated to this widget.
2135 * While removing the path, remove reference to it from both
2136 * source and sink widgets so that path is removed only once.
2137 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002138 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2139 dapm_free_path(p);
2140
2141 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2142 dapm_free_path(p);
2143
Stephen Warrenfad59882011-04-28 17:37:59 -06002144 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002145 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002146 kfree(w);
2147 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002148}
2149
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002150static struct snd_soc_dapm_widget *dapm_find_widget(
2151 struct snd_soc_dapm_context *dapm, const char *pin,
2152 bool search_other_contexts)
2153{
2154 struct snd_soc_dapm_widget *w;
2155 struct snd_soc_dapm_widget *fallback = NULL;
2156
2157 list_for_each_entry(w, &dapm->card->widgets, list) {
2158 if (!strcmp(w->name, pin)) {
2159 if (w->dapm == dapm)
2160 return w;
2161 else
2162 fallback = w;
2163 }
2164 }
2165
2166 if (search_other_contexts)
2167 return fallback;
2168
2169 return NULL;
2170}
2171
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002172static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002173 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002174{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002175 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002176
Mark Brownf9fa2b12014-03-06 16:49:11 +08002177 dapm_assert_locked(dapm);
2178
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002179 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002180 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002181 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002182 }
2183
Mark Brown1a8b2d92012-02-16 11:50:07 -08002184 if (w->connected != status)
2185 dapm_mark_dirty(w, "pin configuration");
2186
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002187 w->connected = status;
2188 if (status == 0)
2189 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002190
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002191 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002192}
2193
Richard Purdie2b97eab2006-10-06 18:32:18 +02002194/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002195 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2196 * @dapm: DAPM context
2197 *
2198 * Walks all dapm audio paths and powers widgets according to their
2199 * stream or path usage.
2200 *
2201 * Requires external locking.
2202 *
2203 * Returns 0 for success.
2204 */
2205int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2206{
2207 /*
2208 * Suppress early reports (eg, jacks syncing their state) to avoid
2209 * silly DAPM runs during card startup.
2210 */
2211 if (!dapm->card || !dapm->card->instantiated)
2212 return 0;
2213
2214 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2215}
2216EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2217
2218/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002219 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002220 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002221 *
2222 * Walks all dapm audio paths and powers widgets according to their
2223 * stream or path usage.
2224 *
2225 * Returns 0 for success.
2226 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002227int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002228{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002229 int ret;
2230
Liam Girdwood3cd04342012-03-09 12:02:08 +00002231 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002232 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002233 mutex_unlock(&dapm->card->dapm_mutex);
2234 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002235}
Liam Girdwooda5302182008-07-07 13:35:17 +01002236EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002237
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002238static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2239 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2240 const char *control,
2241 int (*connected)(struct snd_soc_dapm_widget *source,
2242 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002243{
2244 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002245 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002246
2247 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2248 if (!path)
2249 return -ENOMEM;
2250
2251 path->source = wsource;
2252 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002253 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002254 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002255 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002256 INIT_LIST_HEAD(&path->list_source);
2257 INIT_LIST_HEAD(&path->list_sink);
2258
2259 /* check for external widgets */
2260 if (wsink->id == snd_soc_dapm_input) {
2261 if (wsource->id == snd_soc_dapm_micbias ||
2262 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002263 wsource->id == snd_soc_dapm_line ||
2264 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002265 wsink->ext = 1;
2266 }
2267 if (wsource->id == snd_soc_dapm_output) {
2268 if (wsink->id == snd_soc_dapm_spk ||
2269 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002270 wsink->id == snd_soc_dapm_line ||
2271 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002272 wsource->ext = 1;
2273 }
2274
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002275 dapm_mark_dirty(wsource, "Route added");
2276 dapm_mark_dirty(wsink, "Route added");
2277
Richard Purdie2b97eab2006-10-06 18:32:18 +02002278 /* connect static paths */
2279 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002280 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002281 list_add(&path->list_sink, &wsink->sources);
2282 list_add(&path->list_source, &wsource->sinks);
2283 path->connect = 1;
2284 return 0;
2285 }
2286
2287 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002288 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002289 case snd_soc_dapm_adc:
2290 case snd_soc_dapm_dac:
2291 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002292 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002293 case snd_soc_dapm_input:
2294 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002295 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002296 case snd_soc_dapm_micbias:
2297 case snd_soc_dapm_vmid:
2298 case snd_soc_dapm_pre:
2299 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002300 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002301 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002302 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002303 case snd_soc_dapm_aif_in:
2304 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002305 case snd_soc_dapm_dai_in:
2306 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002307 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002308 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002309 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002310 list_add(&path->list_sink, &wsink->sources);
2311 list_add(&path->list_source, &wsource->sinks);
2312 path->connect = 1;
2313 return 0;
2314 case snd_soc_dapm_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002315 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002316 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002317 if (ret != 0)
2318 goto err;
2319 break;
2320 case snd_soc_dapm_switch:
2321 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002322 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002323 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002324 if (ret != 0)
2325 goto err;
2326 break;
2327 case snd_soc_dapm_hp:
2328 case snd_soc_dapm_mic:
2329 case snd_soc_dapm_line:
2330 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002331 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002332 list_add(&path->list_sink, &wsink->sources);
2333 list_add(&path->list_source, &wsource->sinks);
2334 path->connect = 0;
2335 return 0;
2336 }
Mark Brownfabd0382012-07-05 17:20:06 +01002337
Richard Purdie2b97eab2006-10-06 18:32:18 +02002338 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002339err:
2340 kfree(path);
2341 return ret;
2342}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002343
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002344static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002345 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002346{
2347 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2348 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2349 const char *sink;
2350 const char *source;
2351 char prefixed_sink[80];
2352 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002353 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002354 int ret;
2355
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002356 prefix = soc_dapm_prefix(dapm);
2357 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002358 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002359 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002360 sink = prefixed_sink;
2361 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002362 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002363 source = prefixed_source;
2364 } else {
2365 sink = route->sink;
2366 source = route->source;
2367 }
2368
2369 /*
2370 * find src and dest widgets over all widgets but favor a widget from
2371 * current DAPM context
2372 */
2373 list_for_each_entry(w, &dapm->card->widgets, list) {
2374 if (!wsink && !(strcmp(w->name, sink))) {
2375 wtsink = w;
2376 if (w->dapm == dapm)
2377 wsink = w;
2378 continue;
2379 }
2380 if (!wsource && !(strcmp(w->name, source))) {
2381 wtsource = w;
2382 if (w->dapm == dapm)
2383 wsource = w;
2384 }
2385 }
2386 /* use widget from another DAPM context if not found from this */
2387 if (!wsink)
2388 wsink = wtsink;
2389 if (!wsource)
2390 wsource = wtsource;
2391
2392 if (wsource == NULL) {
2393 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2394 route->source);
2395 return -ENODEV;
2396 }
2397 if (wsink == NULL) {
2398 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2399 route->sink);
2400 return -ENODEV;
2401 }
2402
2403 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2404 route->connected);
2405 if (ret)
2406 goto err;
2407
2408 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002409err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002410 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002411 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002412 return ret;
2413}
Mark Brown105f1c22008-05-13 14:52:19 +02002414
Mark Brownefcc3c62012-07-05 17:24:19 +01002415static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2416 const struct snd_soc_dapm_route *route)
2417{
2418 struct snd_soc_dapm_path *path, *p;
2419 const char *sink;
2420 const char *source;
2421 char prefixed_sink[80];
2422 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002423 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002424
2425 if (route->control) {
2426 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002427 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002428 return -EINVAL;
2429 }
2430
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002431 prefix = soc_dapm_prefix(dapm);
2432 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002433 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002434 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002435 sink = prefixed_sink;
2436 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002437 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002438 source = prefixed_source;
2439 } else {
2440 sink = route->sink;
2441 source = route->source;
2442 }
2443
2444 path = NULL;
2445 list_for_each_entry(p, &dapm->card->paths, list) {
2446 if (strcmp(p->source->name, source) != 0)
2447 continue;
2448 if (strcmp(p->sink->name, sink) != 0)
2449 continue;
2450 path = p;
2451 break;
2452 }
2453
2454 if (path) {
2455 dapm_mark_dirty(path->source, "Route removed");
2456 dapm_mark_dirty(path->sink, "Route removed");
2457
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002458 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002459 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002460 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002461 source, sink);
2462 }
2463
2464 return 0;
2465}
2466
Mark Brown105f1c22008-05-13 14:52:19 +02002467/**
Mark Brown105f1c22008-05-13 14:52:19 +02002468 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002469 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002470 * @route: audio routes
2471 * @num: number of routes
2472 *
2473 * Connects 2 dapm widgets together via a named audio path. The sink is
2474 * the widget receiving the audio signal, whilst the source is the sender
2475 * of the audio signal.
2476 *
2477 * Returns 0 for success else error. On error all resources can be freed
2478 * with a call to snd_soc_card_free().
2479 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002480int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002481 const struct snd_soc_dapm_route *route, int num)
2482{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002483 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002484
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002485 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002486 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002487 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002488 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002489 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2490 route->source,
2491 route->control ? route->control : "direct",
2492 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002493 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002494 }
2495 route++;
2496 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002497 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002498
Dan Carpenter60884c22012-04-13 22:25:43 +03002499 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002500}
2501EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2502
Mark Brownefcc3c62012-07-05 17:24:19 +01002503/**
2504 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2505 * @dapm: DAPM context
2506 * @route: audio routes
2507 * @num: number of routes
2508 *
2509 * Removes routes from the DAPM context.
2510 */
2511int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2512 const struct snd_soc_dapm_route *route, int num)
2513{
2514 int i, ret = 0;
2515
2516 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2517 for (i = 0; i < num; i++) {
2518 snd_soc_dapm_del_route(dapm, route);
2519 route++;
2520 }
2521 mutex_unlock(&dapm->card->dapm_mutex);
2522
2523 return ret;
2524}
2525EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2526
Mark Brownbf3a9e12011-06-13 16:42:29 +01002527static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2528 const struct snd_soc_dapm_route *route)
2529{
2530 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2531 route->source,
2532 true);
2533 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2534 route->sink,
2535 true);
2536 struct snd_soc_dapm_path *path;
2537 int count = 0;
2538
2539 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002540 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002541 route->source);
2542 return -ENODEV;
2543 }
2544
2545 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002546 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002547 route->sink);
2548 return -ENODEV;
2549 }
2550
2551 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002552 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002553 route->source, route->sink);
2554
2555 list_for_each_entry(path, &source->sinks, list_source) {
2556 if (path->sink == sink) {
2557 path->weak = 1;
2558 count++;
2559 }
2560 }
2561
2562 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002563 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002564 route->source, route->sink);
2565 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002566 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002567 count, route->source, route->sink);
2568
2569 return 0;
2570}
2571
2572/**
2573 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2574 * @dapm: DAPM context
2575 * @route: audio routes
2576 * @num: number of routes
2577 *
2578 * Mark existing routes matching those specified in the passed array
2579 * as being weak, meaning that they are ignored for the purpose of
2580 * power decisions. The main intended use case is for sidetone paths
2581 * which couple audio between other independent paths if they are both
2582 * active in order to make the combination work better at the user
2583 * level but which aren't intended to be "used".
2584 *
2585 * Note that CODEC drivers should not use this as sidetone type paths
2586 * can frequently also be used as bypass paths.
2587 */
2588int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2589 const struct snd_soc_dapm_route *route, int num)
2590{
2591 int i, err;
2592 int ret = 0;
2593
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002594 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002595 for (i = 0; i < num; i++) {
2596 err = snd_soc_dapm_weak_route(dapm, route);
2597 if (err)
2598 ret = err;
2599 route++;
2600 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002601 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002602
2603 return ret;
2604}
2605EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2606
Mark Brown105f1c22008-05-13 14:52:19 +02002607/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002608 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002609 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002610 *
2611 * Checks the codec for any new dapm widgets and creates them if found.
2612 *
2613 * Returns 0 for success.
2614 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002615int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002616{
2617 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002618 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002619
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002620 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002621
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002622 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002623 {
2624 if (w->new)
2625 continue;
2626
Stephen Warrenfad59882011-04-28 17:37:59 -06002627 if (w->num_kcontrols) {
2628 w->kcontrols = kzalloc(w->num_kcontrols *
2629 sizeof(struct snd_kcontrol *),
2630 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002631 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002632 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002633 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002634 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002635 }
2636
Richard Purdie2b97eab2006-10-06 18:32:18 +02002637 switch(w->id) {
2638 case snd_soc_dapm_switch:
2639 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002640 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002641 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002642 break;
2643 case snd_soc_dapm_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002644 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002645 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002646 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002647 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002648 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002649 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002650 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002651 break;
2652 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002653
2654 /* Read the initial power state from the device */
2655 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002656 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002657 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002658 val &= w->mask;
2659 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002660 w->power = 1;
2661 }
2662
Richard Purdie2b97eab2006-10-06 18:32:18 +02002663 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002664
Mark Brown7508b122011-10-05 12:09:12 +01002665 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002666 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002667 }
2668
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002669 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2670 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002671 return 0;
2672}
2673EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2674
2675/**
2676 * snd_soc_dapm_get_volsw - dapm mixer get callback
2677 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002678 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002679 *
2680 * Callback to get the value of a dapm mixer control.
2681 *
2682 * Returns 0 for success.
2683 */
2684int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2685 struct snd_ctl_elem_value *ucontrol)
2686{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002687 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2688 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002689 struct soc_mixer_control *mc =
2690 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002691 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002692 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002693 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002694 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002695 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002696 unsigned int val;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002697 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002698
2699 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002700 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002701 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002702 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002703
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002704 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002705 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
2706 ret = soc_dapm_read(dapm, reg, &val);
2707 val = (val >> shift) & mask;
2708 } else {
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002709 val = dapm_kcontrol_get_value(kcontrol);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002710 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002711 mutex_unlock(&card->dapm_mutex);
2712
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002713 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002714 ucontrol->value.integer.value[0] = max - val;
2715 else
2716 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002717
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002718 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002719}
2720EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2721
2722/**
2723 * snd_soc_dapm_put_volsw - dapm mixer set callback
2724 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002725 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002726 *
2727 * Callback to set the value of a dapm mixer control.
2728 *
2729 * Returns 0 for success.
2730 */
2731int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2732 struct snd_ctl_elem_value *ucontrol)
2733{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002734 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2735 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002736 struct soc_mixer_control *mc =
2737 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002738 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002739 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002740 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002741 unsigned int mask = (1 << fls(max)) - 1;
2742 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002743 unsigned int val;
Jarkko Nikula18626c72014-06-09 14:20:29 +03002744 int connect, change, reg_change = 0;
Mark Brown97404f22010-12-14 16:13:57 +00002745 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002746 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002747
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002748 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002749 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002750 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002751 kcontrol->id.name);
2752
Richard Purdie2b97eab2006-10-06 18:32:18 +02002753 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002754 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002755
2756 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002757 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002758
Liam Girdwood3cd04342012-03-09 12:02:08 +00002759 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002760
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002761 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown283375c2009-12-07 18:09:03 +00002762
Jarkko Nikula18626c72014-06-09 14:20:29 +03002763 if (reg != SND_SOC_NOPM) {
2764 mask = mask << shift;
2765 val = val << shift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02002766
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002767 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
Jarkko Nikula18626c72014-06-09 14:20:29 +03002768 }
2769
2770 if (change || reg_change) {
2771 if (reg_change) {
2772 update.kcontrol = kcontrol;
2773 update.reg = reg;
2774 update.mask = mask;
2775 update.val = val;
2776 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002777 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03002778 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00002779
Nenghua Cao52765972013-12-13 20:13:49 +08002780 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002781
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002782 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002783 }
2784
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002785 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002786
2787 if (ret > 0)
2788 soc_dpcm_runtime_update(card);
2789
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002790 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002791}
2792EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2793
2794/**
2795 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2796 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002797 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002798 *
2799 * Callback to get the value of a dapm enumerated double mixer control.
2800 *
2801 * Returns 0 for success.
2802 */
2803int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2804 struct snd_ctl_elem_value *ucontrol)
2805{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002806 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002807 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002808 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002809
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002810 if (e->reg != SND_SOC_NOPM) {
2811 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
2812 if (ret)
2813 return ret;
2814 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002815 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002816 }
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002817
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002818 val = (reg_val >> e->shift_l) & e->mask;
2819 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2820 if (e->shift_l != e->shift_r) {
2821 val = (reg_val >> e->shift_r) & e->mask;
2822 val = snd_soc_enum_val_to_item(e, val);
2823 ucontrol->value.enumerated.item[1] = val;
2824 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002825
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002826 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002827}
2828EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2829
2830/**
2831 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2832 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002833 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002834 *
2835 * Callback to set the value of a dapm enumerated double mixer control.
2836 *
2837 * Returns 0 for success.
2838 */
2839int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2840 struct snd_ctl_elem_value *ucontrol)
2841{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002842 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2843 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002844 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002845 unsigned int *item = ucontrol->value.enumerated.item;
2846 unsigned int val, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002847 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002848 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002849 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002850
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002851 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002852 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002853
2854 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002855 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002856 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002857 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002858 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002859 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002860 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002861 }
2862
Liam Girdwood3cd04342012-03-09 12:02:08 +00002863 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002864
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002865 if (e->reg != SND_SOC_NOPM)
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002866 change = soc_dapm_test_bits(dapm, e->reg, mask, val);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002867 else
2868 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002869
Richard Purdie2b97eab2006-10-06 18:32:18 +02002870 if (change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002871 if (e->reg != SND_SOC_NOPM) {
2872 update.kcontrol = kcontrol;
2873 update.reg = e->reg;
2874 update.mask = mask;
2875 update.val = val;
2876 card->update = &update;
2877 }
Mark Brown3a655772009-10-05 17:23:30 +01002878
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002879 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002880
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002881 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002882 }
2883
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002884 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002885
2886 if (ret > 0)
2887 soc_dpcm_runtime_update(card);
2888
Mark Brown97404f22010-12-14 16:13:57 +00002889 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002890}
2891EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2892
2893/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002894 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2895 *
2896 * @kcontrol: mixer control
2897 * @uinfo: control element information
2898 *
2899 * Callback to provide information about a pin switch control.
2900 */
2901int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2902 struct snd_ctl_elem_info *uinfo)
2903{
2904 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2905 uinfo->count = 1;
2906 uinfo->value.integer.min = 0;
2907 uinfo->value.integer.max = 1;
2908
2909 return 0;
2910}
2911EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2912
2913/**
2914 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2915 *
2916 * @kcontrol: mixer control
2917 * @ucontrol: Value
2918 */
2919int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2920 struct snd_ctl_elem_value *ucontrol)
2921{
Mark Brown48a8c392012-02-14 17:11:15 -08002922 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002923 const char *pin = (const char *)kcontrol->private_value;
2924
Liam Girdwood3cd04342012-03-09 12:02:08 +00002925 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002926
2927 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002928 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002929
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002930 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002931
2932 return 0;
2933}
2934EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2935
2936/**
2937 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2938 *
2939 * @kcontrol: mixer control
2940 * @ucontrol: Value
2941 */
2942int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2943 struct snd_ctl_elem_value *ucontrol)
2944{
Mark Brown48a8c392012-02-14 17:11:15 -08002945 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002946 const char *pin = (const char *)kcontrol->private_value;
2947
Mark Brown8b37dbd2009-02-28 21:14:20 +00002948 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002949 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002950 else
Mark Brown48a8c392012-02-14 17:11:15 -08002951 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002952
Mark Brown48a8c392012-02-14 17:11:15 -08002953 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002954 return 0;
2955}
2956EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2957
Mark Brown5ba06fc2012-02-16 11:07:13 -08002958static struct snd_soc_dapm_widget *
2959snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2960 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002961{
2962 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002963 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00002964 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002965
2966 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08002967 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002968
Mark Brown62ea8742012-01-21 21:14:48 +00002969 switch (w->id) {
2970 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00002971 w->regulator = devm_regulator_get(dapm->dev, w->name);
2972 if (IS_ERR(w->regulator)) {
2973 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002974 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00002975 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002976 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00002977 }
Mark Brown8784c772013-01-10 19:33:47 +00002978
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002979 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00002980 ret = regulator_allow_bypass(w->regulator, true);
2981 if (ret != 0)
2982 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00002983 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00002984 w->name, ret);
2985 }
Mark Brown62ea8742012-01-21 21:14:48 +00002986 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02002987 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01002988#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01002989 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02002990 if (IS_ERR(w->clk)) {
2991 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002992 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02002993 w->name, ret);
2994 return NULL;
2995 }
Mark Brownec029952012-06-04 08:16:20 +01002996#else
2997 return NULL;
2998#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02002999 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003000 default:
3001 break;
3002 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003003
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003004 prefix = soc_dapm_prefix(dapm);
3005 if (prefix)
3006 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003007 else
3008 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3009
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003010 if (w->name == NULL) {
3011 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003012 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003013 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003014
Mark Brown7ca3a182011-10-08 14:04:50 +01003015 switch (w->id) {
3016 case snd_soc_dapm_switch:
3017 case snd_soc_dapm_mixer:
3018 case snd_soc_dapm_mixer_named_ctl:
3019 w->power_check = dapm_generic_check_power;
3020 break;
3021 case snd_soc_dapm_mux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003022 w->power_check = dapm_generic_check_power;
3023 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003024 case snd_soc_dapm_adc:
3025 case snd_soc_dapm_aif_out:
3026 case snd_soc_dapm_dac:
3027 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003028 case snd_soc_dapm_pga:
3029 case snd_soc_dapm_out_drv:
3030 case snd_soc_dapm_input:
3031 case snd_soc_dapm_output:
3032 case snd_soc_dapm_micbias:
3033 case snd_soc_dapm_spk:
3034 case snd_soc_dapm_hp:
3035 case snd_soc_dapm_mic:
3036 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003037 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003038 case snd_soc_dapm_dai_out:
3039 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003040 w->power_check = dapm_generic_check_power;
3041 break;
3042 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003043 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003044 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003045 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003046 w->power_check = dapm_supply_check_power;
3047 break;
3048 default:
3049 w->power_check = dapm_always_on_check_power;
3050 break;
3051 }
3052
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003053 w->dapm = dapm;
Jarkko Nikulab2d9de52014-10-03 15:32:40 +03003054 if (dapm->component)
3055 w->codec = dapm->component->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003056 INIT_LIST_HEAD(&w->sources);
3057 INIT_LIST_HEAD(&w->sinks);
3058 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003059 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003060 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003061
3062 /* machine layer set ups unconnected pins and insertions */
3063 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003064 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003065}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003066
3067/**
Mark Brown4ba13272008-05-13 14:51:19 +02003068 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003069 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003070 * @widget: widget array
3071 * @num: number of widgets
3072 *
3073 * Creates new DAPM controls based upon the templates.
3074 *
3075 * Returns 0 for success else error.
3076 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003077int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003078 const struct snd_soc_dapm_widget *widget,
3079 int num)
3080{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003081 struct snd_soc_dapm_widget *w;
3082 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003083 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003084
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003085 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003086 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003087 w = snd_soc_dapm_new_control(dapm, widget);
3088 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003089 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003090 "ASoC: Failed to create DAPM control %s\n",
3091 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003092 ret = -ENOMEM;
3093 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003094 }
Mark Brown4ba13272008-05-13 14:51:19 +02003095 widget++;
3096 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003097 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003098 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003099}
3100EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3101
Mark Brownc74184e2012-04-04 22:12:09 +01003102static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3103 struct snd_kcontrol *kcontrol, int event)
3104{
3105 struct snd_soc_dapm_path *source_p, *sink_p;
3106 struct snd_soc_dai *source, *sink;
3107 const struct snd_soc_pcm_stream *config = w->params;
3108 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003109 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003110 u64 fmt;
3111 int ret;
3112
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003113 if (WARN_ON(!config) ||
3114 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3115 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003116
3117 /* We only support a single source and sink, pick the first */
3118 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3119 list_sink);
3120 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3121 list_source);
3122
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003123 if (WARN_ON(!source_p || !sink_p) ||
3124 WARN_ON(!sink_p->source || !source_p->sink) ||
3125 WARN_ON(!source_p->source || !sink_p->sink))
3126 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003127
3128 source = source_p->source->priv;
3129 sink = sink_p->sink->priv;
3130
3131 /* Be a little careful as we don't want to overflow the mask array */
3132 if (config->formats) {
3133 fmt = ffs(config->formats) - 1;
3134 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003135 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003136 config->formats);
3137 fmt = 0;
3138 }
3139
3140 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003141 params = kzalloc(sizeof(*params), GFP_KERNEL);
3142 if (!params) {
3143 ret = -ENOMEM;
3144 goto out;
3145 }
3146 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003147
Mark Brown9747cec2012-04-26 19:12:21 +01003148 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003149 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003150 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003151 config->rate_max;
3152
Mark Brown9747cec2012-04-26 19:12:21 +01003153 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003154 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003155 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003156 = config->channels_max;
3157
3158 memset(&substream, 0, sizeof(substream));
3159
3160 switch (event) {
3161 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003162 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3163 ret = soc_dai_hw_params(&substream, params, source);
3164 if (ret < 0)
3165 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003166
Benoit Cousson93e69582014-07-08 23:19:38 +02003167 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3168 ret = soc_dai_hw_params(&substream, params, sink);
3169 if (ret < 0)
3170 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003171 break;
3172
3173 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003174 ret = snd_soc_dai_digital_mute(sink, 0,
3175 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003176 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003177 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003178 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003179 break;
3180
3181 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003182 ret = snd_soc_dai_digital_mute(sink, 1,
3183 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003184 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003185 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003186 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003187 break;
3188
3189 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003190 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003191 return -EINVAL;
3192 }
3193
Mark Brown9747cec2012-04-26 19:12:21 +01003194out:
3195 kfree(params);
3196 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003197}
3198
3199int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3200 const struct snd_soc_pcm_stream *params,
3201 struct snd_soc_dapm_widget *source,
3202 struct snd_soc_dapm_widget *sink)
3203{
Mark Brownc74184e2012-04-04 22:12:09 +01003204 struct snd_soc_dapm_widget template;
3205 struct snd_soc_dapm_widget *w;
3206 size_t len;
3207 char *link_name;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003208 int ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003209
3210 len = strlen(source->name) + strlen(sink->name) + 2;
3211 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3212 if (!link_name)
3213 return -ENOMEM;
3214 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3215
3216 memset(&template, 0, sizeof(template));
3217 template.reg = SND_SOC_NOPM;
3218 template.id = snd_soc_dapm_dai_link;
3219 template.name = link_name;
3220 template.event = snd_soc_dai_link_event;
3221 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3222 SND_SOC_DAPM_PRE_PMD;
3223
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003224 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003225
3226 w = snd_soc_dapm_new_control(&card->dapm, &template);
3227 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003228 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003229 link_name);
3230 return -ENOMEM;
3231 }
3232
3233 w->params = params;
3234
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003235 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3236 if (ret)
3237 return ret;
3238 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Mark Brownc74184e2012-04-04 22:12:09 +01003239}
3240
Mark Brown888df392012-02-16 19:37:51 -08003241int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3242 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003243{
Mark Brown888df392012-02-16 19:37:51 -08003244 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003245 struct snd_soc_dapm_widget *w;
3246
Mark Brown888df392012-02-16 19:37:51 -08003247 WARN_ON(dapm->dev != dai->dev);
3248
3249 memset(&template, 0, sizeof(template));
3250 template.reg = SND_SOC_NOPM;
3251
3252 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003253 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003254 template.name = dai->driver->playback.stream_name;
3255 template.sname = dai->driver->playback.stream_name;
3256
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003257 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003258 template.name);
3259
3260 w = snd_soc_dapm_new_control(dapm, &template);
3261 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003262 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003263 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003264 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003265 }
3266
3267 w->priv = dai;
3268 dai->playback_widget = w;
3269 }
3270
3271 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003272 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003273 template.name = dai->driver->capture.stream_name;
3274 template.sname = dai->driver->capture.stream_name;
3275
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003276 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003277 template.name);
3278
3279 w = snd_soc_dapm_new_control(dapm, &template);
3280 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003281 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003282 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003283 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003284 }
3285
3286 w->priv = dai;
3287 dai->capture_widget = w;
3288 }
3289
3290 return 0;
3291}
3292
3293int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3294{
3295 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003296 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003297 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003298
3299 /* For each DAI widget... */
3300 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003301 switch (dai_w->id) {
3302 case snd_soc_dapm_dai_in:
3303 case snd_soc_dapm_dai_out:
3304 break;
3305 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003306 continue;
Mark Brown46162742013-06-05 19:36:11 +01003307 }
Mark Brown888df392012-02-16 19:37:51 -08003308
3309 dai = dai_w->priv;
3310
3311 /* ...find all widgets with the same stream and link them */
3312 list_for_each_entry(w, &card->widgets, list) {
3313 if (w->dapm != dai_w->dapm)
3314 continue;
3315
Mark Brown46162742013-06-05 19:36:11 +01003316 switch (w->id) {
3317 case snd_soc_dapm_dai_in:
3318 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003319 continue;
Mark Brown46162742013-06-05 19:36:11 +01003320 default:
3321 break;
3322 }
Mark Brown888df392012-02-16 19:37:51 -08003323
Russell King19c2c5f2013-08-04 20:24:03 +01003324 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003325 continue;
3326
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003327 if (dai_w->id == snd_soc_dapm_dai_in) {
3328 src = dai_w;
3329 sink = w;
3330 } else {
3331 src = w;
3332 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003333 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003334 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3335 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003336 }
3337 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003338
Mark Brown888df392012-02-16 19:37:51 -08003339 return 0;
3340}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003341
Benoit Cousson44ba2642014-07-08 23:19:36 +02003342static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3343 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003344{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003345 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003346 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003347 int i;
3348
Benoit Cousson44ba2642014-07-08 23:19:36 +02003349 for (i = 0; i < rtd->num_codecs; i++) {
3350 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003351
3352 /* there is no point in connecting BE DAI links with dummies */
3353 if (snd_soc_dai_is_dummy(codec_dai) ||
3354 snd_soc_dai_is_dummy(cpu_dai))
3355 continue;
3356
3357 /* connect BE DAI playback if widgets are valid */
3358 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003359 source = cpu_dai->playback_widget;
3360 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003361 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003362 cpu_dai->component->name, source->name,
3363 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003364
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003365 snd_soc_dapm_add_path(&card->dapm, source, sink,
3366 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003367 }
3368
3369 /* connect BE DAI capture if widgets are valid */
3370 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003371 source = codec_dai->capture_widget;
3372 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003373 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003374 codec_dai->component->name, source->name,
3375 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003376
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003377 snd_soc_dapm_add_path(&card->dapm, source, sink,
3378 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003379 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003380 }
3381}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003382
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003383static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3384 int event)
3385{
3386 struct snd_soc_dapm_widget *w;
3387
3388 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3389 w = dai->playback_widget;
3390 else
3391 w = dai->capture_widget;
3392
3393 if (w) {
3394 dapm_mark_dirty(w, "stream event");
3395
3396 switch (event) {
3397 case SND_SOC_DAPM_STREAM_START:
3398 w->active = 1;
3399 break;
3400 case SND_SOC_DAPM_STREAM_STOP:
3401 w->active = 0;
3402 break;
3403 case SND_SOC_DAPM_STREAM_SUSPEND:
3404 case SND_SOC_DAPM_STREAM_RESUME:
3405 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3406 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3407 break;
3408 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003409 }
3410}
3411
Benoit Cousson44ba2642014-07-08 23:19:36 +02003412void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3413{
3414 struct snd_soc_pcm_runtime *rtd = card->rtd;
3415 int i;
3416
3417 /* for each BE DAI link... */
3418 for (i = 0; i < card->num_rtd; i++) {
3419 rtd = &card->rtd[i];
3420
3421 /*
3422 * dynamic FE links have no fixed DAI mapping.
3423 * CODEC<->CODEC links have no direct connection.
3424 */
3425 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3426 continue;
3427
3428 dapm_connect_dai_link_widgets(card, rtd);
3429 }
3430}
3431
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003432static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3433 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003434{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003435 int i;
3436
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003437 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Benoit Cousson44ba2642014-07-08 23:19:36 +02003438 for (i = 0; i < rtd->num_codecs; i++)
3439 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003440
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003441 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003442}
3443
3444/**
3445 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3446 * @rtd: PCM runtime data
3447 * @stream: stream name
3448 * @event: stream event
3449 *
3450 * Sends a stream event to the dapm core. The core then makes any
3451 * necessary widget power changes.
3452 *
3453 * Returns 0 for success else error.
3454 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003455void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3456 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003457{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003458 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003459
Liam Girdwood3cd04342012-03-09 12:02:08 +00003460 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003461 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003462 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003463}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003464
3465/**
Charles Keepax11391102014-02-18 15:22:14 +00003466 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3467 * @dapm: DAPM context
3468 * @pin: pin name
3469 *
3470 * Enables input/output pin and its parents or children widgets iff there is
3471 * a valid audio route and active audio stream.
3472 *
3473 * Requires external locking.
3474 *
3475 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3476 * do any widget power switching.
3477 */
3478int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3479 const char *pin)
3480{
3481 return snd_soc_dapm_set_pin(dapm, pin, 1);
3482}
3483EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3484
3485/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003486 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003487 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003488 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003489 *
Mark Brown74b8f952009-06-06 11:26:15 +01003490 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003491 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003492 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003493 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3494 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003495 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003496int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003497{
Charles Keepax11391102014-02-18 15:22:14 +00003498 int ret;
3499
3500 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3501
3502 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3503
3504 mutex_unlock(&dapm->card->dapm_mutex);
3505
3506 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003507}
Liam Girdwooda5302182008-07-07 13:35:17 +01003508EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003509
3510/**
Charles Keepax11391102014-02-18 15:22:14 +00003511 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3512 * @dapm: DAPM context
3513 * @pin: pin name
3514 *
3515 * Enables input/output pin regardless of any other state. This is
3516 * intended for use with microphone bias supplies used in microphone
3517 * jack detection.
3518 *
3519 * Requires external locking.
3520 *
3521 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3522 * do any widget power switching.
3523 */
3524int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3525 const char *pin)
3526{
3527 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3528
3529 if (!w) {
3530 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3531 return -EINVAL;
3532 }
3533
3534 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3535 w->connected = 1;
3536 w->force = 1;
3537 dapm_mark_dirty(w, "force enable");
3538
3539 return 0;
3540}
3541EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3542
3543/**
Mark Brownda341832010-03-15 19:23:37 +00003544 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003545 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003546 * @pin: pin name
3547 *
3548 * Enables input/output pin regardless of any other state. This is
3549 * intended for use with microphone bias supplies used in microphone
3550 * jack detection.
3551 *
3552 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3553 * do any widget power switching.
3554 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003555int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3556 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003557{
Charles Keepax11391102014-02-18 15:22:14 +00003558 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003559
Charles Keepax11391102014-02-18 15:22:14 +00003560 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003561
Charles Keepax11391102014-02-18 15:22:14 +00003562 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003563
Charles Keepax11391102014-02-18 15:22:14 +00003564 mutex_unlock(&dapm->card->dapm_mutex);
3565
3566 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003567}
3568EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3569
3570/**
Charles Keepax11391102014-02-18 15:22:14 +00003571 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3572 * @dapm: DAPM context
3573 * @pin: pin name
3574 *
3575 * Disables input/output pin and its parents or children widgets.
3576 *
3577 * Requires external locking.
3578 *
3579 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3580 * do any widget power switching.
3581 */
3582int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3583 const char *pin)
3584{
3585 return snd_soc_dapm_set_pin(dapm, pin, 0);
3586}
3587EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3588
3589/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003590 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003591 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003592 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003593 *
Mark Brown74b8f952009-06-06 11:26:15 +01003594 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003595 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003596 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3597 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003598 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003599int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3600 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003601{
Charles Keepax11391102014-02-18 15:22:14 +00003602 int ret;
3603
3604 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3605
3606 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3607
3608 mutex_unlock(&dapm->card->dapm_mutex);
3609
3610 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003611}
3612EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3613
3614/**
Charles Keepax11391102014-02-18 15:22:14 +00003615 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3616 * @dapm: DAPM context
3617 * @pin: pin name
3618 *
3619 * Marks the specified pin as being not connected, disabling it along
3620 * any parent or child widgets. At present this is identical to
3621 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3622 * additional things such as disabling controls which only affect
3623 * paths through the pin.
3624 *
3625 * Requires external locking.
3626 *
3627 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3628 * do any widget power switching.
3629 */
3630int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3631 const char *pin)
3632{
3633 return snd_soc_dapm_set_pin(dapm, pin, 0);
3634}
3635EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3636
3637/**
Mark Brown5817b522008-09-24 11:23:11 +01003638 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003639 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003640 * @pin: pin name
3641 *
3642 * Marks the specified pin as being not connected, disabling it along
3643 * any parent or child widgets. At present this is identical to
3644 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3645 * additional things such as disabling controls which only affect
3646 * paths through the pin.
3647 *
3648 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3649 * do any widget power switching.
3650 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003651int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003652{
Charles Keepax11391102014-02-18 15:22:14 +00003653 int ret;
3654
3655 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3656
3657 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3658
3659 mutex_unlock(&dapm->card->dapm_mutex);
3660
3661 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003662}
3663EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3664
3665/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003666 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003667 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003668 * @pin: audio signal pin endpoint (or start point)
3669 *
3670 * Get audio pin status - connected or disconnected.
3671 *
3672 * Returns 1 for connected otherwise 0.
3673 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003674int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3675 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003676{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003677 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003678
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003679 if (w)
3680 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003681
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003682 return 0;
3683}
Liam Girdwooda5302182008-07-07 13:35:17 +01003684EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003685
3686/**
Mark Brown1547aba2010-05-07 21:11:40 +01003687 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003688 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003689 * @pin: audio signal pin endpoint (or start point)
3690 *
3691 * Mark the given endpoint or pin as ignoring suspend. When the
3692 * system is disabled a path between two endpoints flagged as ignoring
3693 * suspend will not be disabled. The path must already be enabled via
3694 * normal means at suspend time, it will not be turned on if it was not
3695 * already enabled.
3696 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003697int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3698 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003699{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003700 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003701
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003702 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003703 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003704 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003705 }
3706
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003707 w->ignore_suspend = 1;
3708
3709 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003710}
3711EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3712
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02003713/**
3714 * dapm_is_external_path() - Checks if a path is a external path
3715 * @card: The card the path belongs to
3716 * @path: The path to check
3717 *
3718 * Returns true if the path is either between two different DAPM contexts or
3719 * between two external pins of the same DAPM context. Otherwise returns
3720 * false.
3721 */
3722static bool dapm_is_external_path(struct snd_soc_card *card,
3723 struct snd_soc_dapm_path *path)
3724{
3725 dev_dbg(card->dev,
3726 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3727 path->source->name, path->source->id, path->source->dapm,
3728 path->sink->name, path->sink->id, path->sink->dapm);
3729
3730 /* Connection between two different DAPM contexts */
3731 if (path->source->dapm != path->sink->dapm)
3732 return true;
3733
3734 /* Loopback connection from external pin to external pin */
3735 if (path->sink->id == snd_soc_dapm_input) {
3736 switch (path->source->id) {
3737 case snd_soc_dapm_output:
3738 case snd_soc_dapm_micbias:
3739 return true;
3740 default:
3741 break;
3742 }
3743 }
3744
3745 return false;
3746}
3747
Stephen Warren16332812011-11-23 12:42:04 -07003748static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3749 struct snd_soc_dapm_widget *w)
3750{
3751 struct snd_soc_dapm_path *p;
3752
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02003753 list_for_each_entry(p, &w->sources, list_sink) {
3754 if (dapm_is_external_path(card, p))
3755 return true;
3756 }
Stephen Warren16332812011-11-23 12:42:04 -07003757
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02003758 list_for_each_entry(p, &w->sinks, list_source) {
3759 if (dapm_is_external_path(card, p))
3760 return true;
Stephen Warren16332812011-11-23 12:42:04 -07003761 }
3762
3763 return false;
3764}
3765
3766/**
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003767 * snd_soc_dapm_auto_nc_pins - call snd_soc_dapm_nc_pin for unused pins
3768 * @card: The card whose pins should be processed
Stephen Warren16332812011-11-23 12:42:04 -07003769 *
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003770 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the card
3771 * which are unused. Pins are used if they are connected externally to a
3772 * component, whether that be to some other device, or a loop-back connection to
3773 * the component itself.
Stephen Warren16332812011-11-23 12:42:04 -07003774 */
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003775void snd_soc_dapm_auto_nc_pins(struct snd_soc_card *card)
Stephen Warren16332812011-11-23 12:42:04 -07003776{
Stephen Warren16332812011-11-23 12:42:04 -07003777 struct snd_soc_dapm_widget *w;
3778
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003779 dev_dbg(card->dev, "ASoC: Auto NC: DAPMs: card:%p\n", &card->dapm);
Stephen Warren16332812011-11-23 12:42:04 -07003780
3781 list_for_each_entry(w, &card->widgets, list) {
Stephen Warren16332812011-11-23 12:42:04 -07003782 switch (w->id) {
3783 case snd_soc_dapm_input:
3784 case snd_soc_dapm_output:
3785 case snd_soc_dapm_micbias:
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003786 dev_dbg(card->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003787 w->name);
3788 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003789 dev_dbg(card->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003790 "... Not in map; disabling\n");
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003791 snd_soc_dapm_nc_pin(w->dapm, w->name);
Stephen Warren16332812011-11-23 12:42:04 -07003792 }
3793 break;
3794 default:
3795 break;
3796 }
3797 }
3798}
3799
Mark Brown1547aba2010-05-07 21:11:40 +01003800/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003801 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003802 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003803 *
3804 * Free all dapm widgets and resources.
3805 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003806void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003807{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003808 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003809 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003810 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003811 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003812}
3813EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3814
Xiang Xiao57996352014-03-02 00:04:02 +08003815static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003816{
Liam Girdwood01005a72012-07-06 16:57:05 +01003817 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003818 struct snd_soc_dapm_widget *w;
3819 LIST_HEAD(down_list);
3820 int powerdown = 0;
3821
Liam Girdwood01005a72012-07-06 16:57:05 +01003822 mutex_lock(&card->dapm_mutex);
3823
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003824 list_for_each_entry(w, &dapm->card->widgets, list) {
3825 if (w->dapm != dapm)
3826 continue;
Mark Brown51737472009-06-22 13:16:51 +01003827 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003828 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003829 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003830 powerdown = 1;
3831 }
3832 }
3833
3834 /* If there were no widgets to power down we're already in
3835 * standby.
3836 */
3837 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003838 if (dapm->bias_level == SND_SOC_BIAS_ON)
3839 snd_soc_dapm_set_bias_level(dapm,
3840 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003841 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003842 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3843 snd_soc_dapm_set_bias_level(dapm,
3844 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003845 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003846
3847 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003848}
Mark Brown51737472009-06-22 13:16:51 +01003849
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003850/*
3851 * snd_soc_dapm_shutdown - callback for system shutdown
3852 */
3853void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3854{
Xiang Xiao57996352014-03-02 00:04:02 +08003855 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003856
Xiang Xiao57996352014-03-02 00:04:02 +08003857 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08003858 if (dapm != &card->dapm) {
3859 soc_dapm_shutdown_dapm(dapm);
3860 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3861 snd_soc_dapm_set_bias_level(dapm,
3862 SND_SOC_BIAS_OFF);
3863 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003864 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08003865
3866 soc_dapm_shutdown_dapm(&card->dapm);
3867 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3868 snd_soc_dapm_set_bias_level(&card->dapm,
3869 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01003870}
3871
Richard Purdie2b97eab2006-10-06 18:32:18 +02003872/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003873MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003874MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3875MODULE_LICENSE("GPL");