blob: d89be153a9e0196c280e14f4f782b114fb8caa70 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
Liam Girdwood612a3fe2012-02-06 16:05:29 +000017 * mic/headphone insertion events.
Richard Purdie2b97eab2006-10-06 18:32:18 +020018 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Liam Girdwood612a3fe2012-02-06 16:05:29 +000021 * o Delayed power down of audio subsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020024 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080029#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020030#include <linux/delay.h>
31#include <linux/pm.h>
32#include <linux/bitops.h>
33#include <linux/platform_device.h>
34#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020035#include <linux/debugfs.h>
Mark Brownf1aac482011-12-05 15:17:06 +000036#include <linux/pm_runtime.h>
Mark Brown62ea8742012-01-21 21:14:48 +000037#include <linux/regulator/consumer.h>
Ola Liljad7e7eb92012-05-24 15:26:25 +020038#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020040#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020043#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020044#include <sound/initval.h>
45
Mark Brown84e90932010-11-04 00:07:02 -040046#include <trace/events/asoc.h>
47
Mark Brownde02d072011-09-20 21:43:24 +010048#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49
Lars-Peter Clausen57295072013-08-05 11:27:31 +020050static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
51 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
52 const char *control,
53 int (*connected)(struct snd_soc_dapm_widget *source,
54 struct snd_soc_dapm_widget *sink));
55static struct snd_soc_dapm_widget *
56snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
57 const struct snd_soc_dapm_widget *widget);
58
Richard Purdie2b97eab2006-10-06 18:32:18 +020059/* dapm power sequences - make this per codec in the future */
60static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010061 [snd_soc_dapm_pre] = 0,
Mark Brown62ea8742012-01-21 21:14:48 +000062 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020063 [snd_soc_dapm_clock_supply] = 1,
Mark Brown1dd275b2013-10-09 13:56:37 +010064 [snd_soc_dapm_supply] = 2,
65 [snd_soc_dapm_micbias] = 3,
Mark Brownc74184e2012-04-04 22:12:09 +010066 [snd_soc_dapm_dai_link] = 2,
Mark Brown1dd275b2013-10-09 13:56:37 +010067 [snd_soc_dapm_dai_in] = 4,
68 [snd_soc_dapm_dai_out] = 4,
69 [snd_soc_dapm_aif_in] = 4,
70 [snd_soc_dapm_aif_out] = 4,
71 [snd_soc_dapm_mic] = 5,
72 [snd_soc_dapm_mux] = 6,
Mark Brown1dd275b2013-10-09 13:56:37 +010073 [snd_soc_dapm_dac] = 7,
74 [snd_soc_dapm_switch] = 8,
75 [snd_soc_dapm_mixer] = 8,
76 [snd_soc_dapm_mixer_named_ctl] = 8,
77 [snd_soc_dapm_pga] = 9,
78 [snd_soc_dapm_adc] = 10,
79 [snd_soc_dapm_out_drv] = 11,
80 [snd_soc_dapm_hp] = 11,
81 [snd_soc_dapm_spk] = 11,
82 [snd_soc_dapm_line] = 11,
83 [snd_soc_dapm_kcontrol] = 12,
84 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +020085};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000086
Richard Purdie2b97eab2006-10-06 18:32:18 +020087static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010088 [snd_soc_dapm_pre] = 0,
Lars-Peter Clausen57295072013-08-05 11:27:31 +020089 [snd_soc_dapm_kcontrol] = 1,
90 [snd_soc_dapm_adc] = 2,
91 [snd_soc_dapm_hp] = 3,
92 [snd_soc_dapm_spk] = 3,
93 [snd_soc_dapm_line] = 3,
94 [snd_soc_dapm_out_drv] = 3,
Mark Brown38357ab2009-06-06 19:03:23 +010095 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020096 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +010097 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010098 [snd_soc_dapm_mixer] = 5,
99 [snd_soc_dapm_dac] = 6,
100 [snd_soc_dapm_mic] = 7,
101 [snd_soc_dapm_micbias] = 8,
102 [snd_soc_dapm_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +0100103 [snd_soc_dapm_aif_in] = 10,
104 [snd_soc_dapm_aif_out] = 10,
Mark Brown46162742013-06-05 19:36:11 +0100105 [snd_soc_dapm_dai_in] = 10,
106 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100107 [snd_soc_dapm_dai_link] = 11,
Mark Brownc74184e2012-04-04 22:12:09 +0100108 [snd_soc_dapm_supply] = 12,
Mark Brown1dd275b2013-10-09 13:56:37 +0100109 [snd_soc_dapm_clock_supply] = 13,
110 [snd_soc_dapm_regulator_supply] = 13,
111 [snd_soc_dapm_post] = 14,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200112};
113
Mark Brownf9fa2b12014-03-06 16:49:11 +0800114static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
115{
116 if (dapm->card && dapm->card->instantiated)
117 lockdep_assert_held(&dapm->card->dapm_mutex);
118}
119
Troy Kisky12ef1932008-10-13 17:42:14 -0700120static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100121{
122 if (pop_time)
123 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
124}
125
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200126static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100127{
128 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200129 char *buf;
130
131 if (!pop_time)
132 return;
133
134 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
135 if (buf == NULL)
136 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100137
138 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200139 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100140 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100141 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200142
143 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100144}
145
Mark Browndb432b42011-10-03 21:06:40 +0100146static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
147{
148 return !list_empty(&w->dirty);
149}
150
Mark Brown492c0a12014-03-06 16:15:48 +0800151static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100152{
Mark Brownf9fa2b12014-03-06 16:49:11 +0800153 dapm_assert_locked(w->dapm);
154
Mark Brown75c1f892011-10-04 22:28:08 +0100155 if (!dapm_dirty_widget(w)) {
156 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
157 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100158 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100159 }
Mark Browndb432b42011-10-03 21:06:40 +0100160}
161
Mark Browne2d32ff2012-08-31 17:38:32 -0700162void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
163{
164 struct snd_soc_card *card = dapm->card;
165 struct snd_soc_dapm_widget *w;
166
167 mutex_lock(&card->dapm_mutex);
168
169 list_for_each_entry(w, &card->widgets, list) {
170 switch (w->id) {
171 case snd_soc_dapm_input:
172 case snd_soc_dapm_output:
173 dapm_mark_dirty(w, "Rechecking inputs and outputs");
174 break;
175 default:
176 break;
177 }
178 }
179
180 mutex_unlock(&card->dapm_mutex);
181}
182EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
183
Richard Purdie2b97eab2006-10-06 18:32:18 +0200184/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100185static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200186 const struct snd_soc_dapm_widget *_widget)
187{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100188 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200189}
190
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200191struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200192 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200193 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200194 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200195 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200196};
197
198static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
199 struct snd_kcontrol *kcontrol)
200{
201 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200202 struct soc_mixer_control *mc;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200203
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200204 data = kzalloc(sizeof(*data), GFP_KERNEL);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200205 if (!data) {
206 dev_err(widget->dapm->dev,
207 "ASoC: can't allocate kcontrol data for %s\n",
208 widget->name);
209 return -ENOMEM;
210 }
211
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200212 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200213
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200214 switch (widget->id) {
215 case snd_soc_dapm_switch:
216 case snd_soc_dapm_mixer:
217 case snd_soc_dapm_mixer_named_ctl:
218 mc = (struct soc_mixer_control *)kcontrol->private_value;
219
220 if (mc->autodisable) {
221 struct snd_soc_dapm_widget template;
222
223 memset(&template, 0, sizeof(template));
224 template.reg = mc->reg;
225 template.mask = (1 << fls(mc->max)) - 1;
226 template.shift = mc->shift;
227 if (mc->invert)
228 template.off_val = mc->max;
229 else
230 template.off_val = 0;
231 template.on_val = template.off_val;
232 template.id = snd_soc_dapm_kcontrol;
233 template.name = kcontrol->id.name;
234
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200235 data->value = template.on_val;
236
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200237 data->widget = snd_soc_dapm_new_control(widget->dapm,
238 &template);
239 if (!data->widget) {
240 kfree(data);
241 return -ENOMEM;
242 }
243 }
244 break;
245 default:
246 break;
247 }
248
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200249 kcontrol->private_data = data;
250
251 return 0;
252}
253
254static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
255{
256 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200257 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200258 kfree(data);
259}
260
261static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
262 const struct snd_kcontrol *kcontrol)
263{
264 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
265
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200266 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200267}
268
269static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
270 struct snd_soc_dapm_widget *widget)
271{
272 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200273 struct snd_soc_dapm_widget_list *new_wlist;
274 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200275
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200276 if (data->wlist)
277 n = data->wlist->num_widgets + 1;
278 else
279 n = 1;
280
281 new_wlist = krealloc(data->wlist,
282 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
283 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200284 return -ENOMEM;
285
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200286 new_wlist->widgets[n - 1] = widget;
287 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200288
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200289 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200290
291 return 0;
292}
293
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200294static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
295 struct snd_soc_dapm_path *path)
296{
297 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
298
299 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200300
301 if (data->widget) {
302 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
303 path->source, NULL, NULL);
304 }
305}
306
307static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
308{
309 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
310
311 if (!data->widget)
312 return true;
313
314 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200315}
316
317static struct list_head *dapm_kcontrol_get_path_list(
318 const struct snd_kcontrol *kcontrol)
319{
320 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
321
322 return &data->paths;
323}
324
325#define dapm_kcontrol_for_each_path(path, kcontrol) \
326 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
327 list_kcontrol)
328
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,
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200472 struct snd_soc_dapm_path *path, const char *control_name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200473{
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200474 const struct snd_kcontrol_new *kcontrol = &path->sink->kcontrol_news[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200475 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100476 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200477 int i;
478
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100479 if (e->reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200480 soc_dapm_read(dapm, e->reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100481 val = (val >> e->shift_l) & e->mask;
482 item = snd_soc_enum_val_to_item(e, val);
483 } else {
484 /* since a virtual mux has no backing registers to
485 * decide which path to connect, it will try to match
486 * with the first enumeration. This is to ensure
487 * that the default mux choice (the first) will be
488 * correctly powered up during initialization.
489 */
490 item = 0;
491 }
492
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100493 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200494 if (!(strcmp(control_name, e->texts[i]))) {
Rasmus Villemoes98ad73c2014-10-21 17:01:15 +0200495 path->name = e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100496 if (i == item)
497 path->connect = 1;
498 else
499 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200500 return 0;
501 }
502 }
503
504 return -ENODEV;
505}
506
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100507/* set up initial codec paths */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200508static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i)
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100509{
510 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200511 p->sink->kcontrol_news[i].private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100512 unsigned int reg = mc->reg;
513 unsigned int shift = mc->shift;
514 unsigned int max = mc->max;
515 unsigned int mask = (1 << fls(max)) - 1;
516 unsigned int invert = mc->invert;
517 unsigned int val;
518
519 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200520 soc_dapm_read(p->sink->dapm, reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100521 val = (val >> shift) & mask;
522 if (invert)
523 val = max - val;
524 p->connect = !!val;
525 } else {
526 p->connect = 0;
527 }
528}
529
Mark Brown74b8f952009-06-06 11:26:15 +0100530/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200531static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200532 struct snd_soc_dapm_path *path, const char *control_name)
533{
534 int i;
535
536 /* search for mixer kcontrol */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200537 for (i = 0; i < path->sink->num_kcontrols; i++) {
538 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
539 path->name = path->sink->kcontrol_news[i].name;
540 dapm_set_mixer_path_status(path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200541 return 0;
542 }
543 }
544 return -ENODEV;
545}
546
Stephen Warrenaf468002011-04-28 17:38:01 -0600547static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600548 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600549 const struct snd_kcontrol_new *kcontrol_new,
550 struct snd_kcontrol **kcontrol)
551{
552 struct snd_soc_dapm_widget *w;
553 int i;
554
555 *kcontrol = NULL;
556
557 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600558 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
559 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600560 for (i = 0; i < w->num_kcontrols; i++) {
561 if (&w->kcontrol_news[i] == kcontrol_new) {
562 if (w->kcontrols)
563 *kcontrol = w->kcontrols[i];
564 return 1;
565 }
566 }
567 }
568
569 return 0;
570}
571
Stephen Warren85762e72013-03-29 15:40:10 -0600572/*
573 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
574 * create it. Either way, add the widget into the control's widget list
575 */
576static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100577 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200578{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200579 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000580 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000581 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600582 size_t prefix_len;
583 int shared;
584 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600585 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200586 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600587 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200588 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000589
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200590 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000591 if (prefix)
592 prefix_len = strlen(prefix) + 1;
593 else
594 prefix_len = 0;
595
Stephen Warren85762e72013-03-29 15:40:10 -0600596 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
597 &kcontrol);
598
Stephen Warren85762e72013-03-29 15:40:10 -0600599 if (!kcontrol) {
600 if (shared) {
601 wname_in_long_name = false;
602 kcname_in_long_name = true;
603 } else {
604 switch (w->id) {
605 case snd_soc_dapm_switch:
606 case snd_soc_dapm_mixer:
607 wname_in_long_name = true;
608 kcname_in_long_name = true;
609 break;
610 case snd_soc_dapm_mixer_named_ctl:
611 wname_in_long_name = false;
612 kcname_in_long_name = true;
613 break;
614 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600615 wname_in_long_name = true;
616 kcname_in_long_name = false;
617 break;
618 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600619 return -EINVAL;
620 }
621 }
622
623 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600624 /*
625 * The control will get a prefix from the control
626 * creation process but we're also using the same
627 * prefix for widgets so cut the prefix off the
628 * front of the widget name.
629 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200630 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600631 w->name + prefix_len,
632 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200633 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200634 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600635
636 name = long_name;
637 } else if (wname_in_long_name) {
638 long_name = NULL;
639 name = w->name + prefix_len;
640 } else {
641 long_name = NULL;
642 name = w->kcontrol_news[kci].name;
643 }
644
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200645 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600646 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200647 if (!kcontrol) {
648 ret = -ENOMEM;
649 goto exit_free;
650 }
651
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200652 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200653
654 ret = dapm_kcontrol_data_alloc(w, kcontrol);
655 if (ret) {
656 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200657 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200658 }
659
Stephen Warren85762e72013-03-29 15:40:10 -0600660 ret = snd_ctl_add(card, kcontrol);
661 if (ret < 0) {
662 dev_err(dapm->dev,
663 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
664 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200665 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600666 }
Stephen Warren85762e72013-03-29 15:40:10 -0600667 }
668
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200669 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200670 if (ret == 0)
671 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200672
Daniel Macke5092c92014-10-07 13:41:24 +0200673exit_free:
674 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600675
Daniel Macke5092c92014-10-07 13:41:24 +0200676 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600677}
678
679/* create new dapm mixer control */
680static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
681{
682 int i, ret;
683 struct snd_soc_dapm_path *path;
684
Richard Purdie2b97eab2006-10-06 18:32:18 +0200685 /* add kcontrol */
686 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200687 /* match name */
688 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200689 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600690 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200691 continue;
692
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200693 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200694 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200695 continue;
696 }
697
Mark Brown946d92a2013-08-12 23:28:42 +0100698 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600699 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200700 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100701
702 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200703 }
704 }
Stephen Warren85762e72013-03-29 15:40:10 -0600705
706 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200707}
708
709/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200710static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200711{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200712 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600713 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600714 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200715
Stephen Warrenaf468002011-04-28 17:38:01 -0600716 if (w->num_kcontrols != 1) {
717 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000718 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600719 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200720 return -EINVAL;
721 }
722
Lars-Peter Clausenfe5813912013-08-01 18:30:38 +0200723 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600724 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
725 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600726 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200727
Mark Brown946d92a2013-08-12 23:28:42 +0100728 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600729 if (ret < 0)
730 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600731
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +0200732 list_for_each_entry(path, &w->sources, list_sink) {
733 if (path->name)
734 dapm_kcontrol_add_path(w->kcontrols[0], path);
735 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200736
Stephen Warrenaf468002011-04-28 17:38:01 -0600737 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200738}
739
740/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200741static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200742{
Mark Browna6c65732010-03-03 17:45:21 +0000743 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200744 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000745 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200746
Mark Browna6c65732010-03-03 17:45:21 +0000747 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200748}
749
Mark Brown99497882010-05-07 20:24:05 +0100750/* We implement power down on suspend by checking the power state of
751 * the ALSA card - when we are suspending the ALSA state for the card
752 * is set to D3.
753 */
754static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
755{
Mark Brown12ea2c72011-03-02 18:17:32 +0000756 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100757
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000758 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100759 case SNDRV_CTL_POWER_D3hot:
760 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100761 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000762 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200763 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100764 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100765 default:
766 return 1;
767 }
768}
769
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100770/* add widget to list if it's not already in the list */
771static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
772 struct snd_soc_dapm_widget *w)
773{
774 struct snd_soc_dapm_widget_list *wlist;
775 int wlistsize, wlistentries, i;
776
777 if (*list == NULL)
778 return -EINVAL;
779
780 wlist = *list;
781
782 /* is this widget already in the list */
783 for (i = 0; i < wlist->num_widgets; i++) {
784 if (wlist->widgets[i] == w)
785 return 0;
786 }
787
788 /* allocate some new space */
789 wlistentries = wlist->num_widgets + 1;
790 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
791 wlistentries * sizeof(struct snd_soc_dapm_widget *);
792 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
793 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000794 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100795 w->name);
796 return -ENOMEM;
797 }
798 wlist = *list;
799
800 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000801 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100802 w->name, wlist->num_widgets);
803
804 wlist->widgets[wlist->num_widgets] = w;
805 wlist->num_widgets++;
806 return 1;
807}
808
Richard Purdie2b97eab2006-10-06 18:32:18 +0200809/*
810 * Recursively check for a completed path to an active or physically connected
811 * output widget. Returns number of complete paths.
812 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100813static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
814 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200815{
816 struct snd_soc_dapm_path *path;
817 int con = 0;
818
Mark Brown024dc072011-10-09 11:52:05 +0100819 if (widget->outputs >= 0)
820 return widget->outputs;
821
Mark Brownde02d072011-09-20 21:43:24 +0100822 DAPM_UPDATE_STAT(widget, path_checks);
823
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +0200824 if (widget->is_sink && widget->connected) {
825 widget->outputs = snd_soc_dapm_suspend_check(widget);
826 return widget->outputs;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200827 }
828
829 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100830 DAPM_UPDATE_STAT(widget, neighbour_checks);
831
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +0200832 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +0100833 continue;
834
Mark Brown8af294b2013-02-22 17:48:15 +0000835 if (path->walking)
836 return 1;
837
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100838 trace_snd_soc_dapm_output_path(widget, path);
839
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +0200840 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +0000841 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100842
843 /* do we need to add this widget to the list ? */
844 if (list) {
845 int err;
846 err = dapm_list_add_widget(list, path->sink);
847 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000848 dev_err(widget->dapm->dev,
849 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100850 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000851 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100852 return con;
853 }
854 }
855
856 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000857
858 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200859 }
860 }
861
Mark Brown024dc072011-10-09 11:52:05 +0100862 widget->outputs = con;
863
Richard Purdie2b97eab2006-10-06 18:32:18 +0200864 return con;
865}
866
867/*
868 * Recursively check for a completed path to an active or physically connected
869 * input widget. Returns number of complete paths.
870 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100871static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
872 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200873{
874 struct snd_soc_dapm_path *path;
875 int con = 0;
876
Mark Brown024dc072011-10-09 11:52:05 +0100877 if (widget->inputs >= 0)
878 return widget->inputs;
879
Mark Brownde02d072011-09-20 21:43:24 +0100880 DAPM_UPDATE_STAT(widget, path_checks);
881
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +0200882 if (widget->is_source && widget->connected) {
883 widget->inputs = snd_soc_dapm_suspend_check(widget);
884 return widget->inputs;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200885 }
886
887 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +0100888 DAPM_UPDATE_STAT(widget, neighbour_checks);
889
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +0200890 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +0100891 continue;
892
Mark Brown8af294b2013-02-22 17:48:15 +0000893 if (path->walking)
894 return 1;
895
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100896 trace_snd_soc_dapm_input_path(widget, path);
897
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +0200898 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +0000899 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100900
901 /* do we need to add this widget to the list ? */
902 if (list) {
903 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +0100904 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100905 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000906 dev_err(widget->dapm->dev,
907 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100908 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000909 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100910 return con;
911 }
912 }
913
914 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000915
916 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200917 }
918 }
919
Mark Brown024dc072011-10-09 11:52:05 +0100920 widget->inputs = con;
921
Richard Purdie2b97eab2006-10-06 18:32:18 +0200922 return con;
923}
924
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100925/**
926 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
927 * @dai: the soc DAI.
928 * @stream: stream direction.
929 * @list: list of active widgets for this stream.
930 *
931 * Queries DAPM graph as to whether an valid audio stream path exists for
932 * the initial stream specified by name. This takes into account
933 * current mixer and mux kcontrol settings. Creates list of valid widgets.
934 *
935 * Returns the number of valid paths or negative error.
936 */
937int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
938 struct snd_soc_dapm_widget_list **list)
939{
940 struct snd_soc_card *card = dai->card;
941 int paths;
942
943 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
944 dapm_reset(card);
945
Lars-Peter Clausen130897a2014-10-20 19:36:39 +0200946 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100947 paths = is_connected_output_ep(dai->playback_widget, list);
Lars-Peter Clausen130897a2014-10-20 19:36:39 +0200948 else
Liam Girdwoodd298caa2012-06-01 18:03:00 +0100949 paths = is_connected_input_ep(dai->capture_widget, list);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100950
951 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100952 mutex_unlock(&card->dapm_mutex);
953
954 return paths;
955}
956
Richard Purdie2b97eab2006-10-06 18:32:18 +0200957/*
Mark Brown62ea8742012-01-21 21:14:48 +0000958 * Handler for regulator supply widget.
959 */
960int dapm_regulator_event(struct snd_soc_dapm_widget *w,
961 struct snd_kcontrol *kcontrol, int event)
962{
Mark Brownc05b84d2012-09-07 12:57:11 +0800963 int ret;
964
Mark Browneb270e92013-10-09 13:52:52 +0100965 soc_dapm_async_complete(w->dapm);
966
Mark Brownc05b84d2012-09-07 12:57:11 +0800967 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +0200968 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +0000969 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +0800970 if (ret != 0)
971 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +0000972 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +0800973 w->name, ret);
974 }
975
Liam Girdwooda3cc0562012-03-09 17:20:16 +0000976 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +0800977 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +0200978 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +0000979 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +0800980 if (ret != 0)
981 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +0000982 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +0800983 w->name, ret);
984 }
985
Liam Girdwooda3cc0562012-03-09 17:20:16 +0000986 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +0800987 }
Mark Brown62ea8742012-01-21 21:14:48 +0000988}
989EXPORT_SYMBOL_GPL(dapm_regulator_event);
990
Ola Liljad7e7eb92012-05-24 15:26:25 +0200991/*
992 * Handler for clock supply widget.
993 */
994int dapm_clock_event(struct snd_soc_dapm_widget *w,
995 struct snd_kcontrol *kcontrol, int event)
996{
997 if (!w->clk)
998 return -EIO;
999
Mark Browneb270e92013-10-09 13:52:52 +01001000 soc_dapm_async_complete(w->dapm);
1001
Mark Brownec029952012-06-04 08:16:20 +01001002#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001003 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001004 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001005 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001006 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001007 return 0;
1008 }
Mark Brownec029952012-06-04 08:16:20 +01001009#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001010 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001011}
1012EXPORT_SYMBOL_GPL(dapm_clock_event);
1013
Mark Brownd8050022011-09-28 18:28:23 +01001014static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1015{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001016 if (w->power_checked)
1017 return w->new_power;
1018
Mark Brownd8050022011-09-28 18:28:23 +01001019 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001020 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001021 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001022 w->new_power = w->power_check(w);
1023
1024 w->power_checked = true;
1025
1026 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001027}
1028
Mark Browncd0f2d42009-04-20 16:56:59 +01001029/* Generic check to see if a widget should be powered.
1030 */
1031static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1032{
1033 int in, out;
1034
Mark Brownde02d072011-09-20 21:43:24 +01001035 DAPM_UPDATE_STAT(w, power_checks);
1036
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001037 in = is_connected_input_ep(w, NULL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001038 out = is_connected_output_ep(w, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001039 return out != 0 && in != 0;
1040}
1041
Mark Brown246d0a12009-04-22 18:24:55 +01001042/* Check to see if a power supply is needed */
1043static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1044{
1045 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001046
Mark Brownde02d072011-09-20 21:43:24 +01001047 DAPM_UPDATE_STAT(w, power_checks);
1048
Mark Brown246d0a12009-04-22 18:24:55 +01001049 /* Check if one of our outputs is connected */
1050 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001051 DAPM_UPDATE_STAT(w, neighbour_checks);
1052
Mark Brownbf3a9e12011-06-13 16:42:29 +01001053 if (path->weak)
1054 continue;
1055
Mark Brown215edda2009-09-08 18:59:05 +01001056 if (path->connected &&
1057 !path->connected(path->source, path->sink))
1058 continue;
1059
Mark Brownf68d7e12011-10-04 22:57:50 +01001060 if (dapm_widget_power_check(path->sink))
1061 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001062 }
1063
Mark Brownf68d7e12011-10-04 22:57:50 +01001064 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001065}
1066
Mark Brown35c64bc2011-09-28 18:23:53 +01001067static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1068{
1069 return 1;
1070}
1071
Mark Brown38357ab2009-06-06 19:03:23 +01001072static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1073 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001074 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001075{
Mark Brown828a8422011-01-15 13:14:30 +00001076 int *sort;
1077
1078 if (power_up)
1079 sort = dapm_up_seq;
1080 else
1081 sort = dapm_down_seq;
1082
Mark Brown38357ab2009-06-06 19:03:23 +01001083 if (sort[a->id] != sort[b->id])
1084 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001085 if (a->subseq != b->subseq) {
1086 if (power_up)
1087 return a->subseq - b->subseq;
1088 else
1089 return b->subseq - a->subseq;
1090 }
Mark Brownb22ead22009-06-07 12:51:26 +01001091 if (a->reg != b->reg)
1092 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001093 if (a->dapm != b->dapm)
1094 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001095
Mark Brown38357ab2009-06-06 19:03:23 +01001096 return 0;
1097}
Mark Brown42aa3412009-03-01 19:21:10 +00001098
Mark Brown38357ab2009-06-06 19:03:23 +01001099/* Insert a widget in order into a DAPM power sequence. */
1100static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1101 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001102 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001103{
1104 struct snd_soc_dapm_widget *w;
1105
1106 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001107 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001108 list_add_tail(&new_widget->power_list, &w->power_list);
1109 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001110 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001111
Mark Brown38357ab2009-06-06 19:03:23 +01001112 list_add_tail(&new_widget->power_list, list);
1113}
Mark Brown42aa3412009-03-01 19:21:10 +00001114
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001115static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001116 struct snd_soc_dapm_widget *w, int event)
1117{
Mark Brown68f89ad2010-11-03 23:51:49 -04001118 const char *ev_name;
1119 int power, ret;
1120
1121 switch (event) {
1122 case SND_SOC_DAPM_PRE_PMU:
1123 ev_name = "PRE_PMU";
1124 power = 1;
1125 break;
1126 case SND_SOC_DAPM_POST_PMU:
1127 ev_name = "POST_PMU";
1128 power = 1;
1129 break;
1130 case SND_SOC_DAPM_PRE_PMD:
1131 ev_name = "PRE_PMD";
1132 power = 0;
1133 break;
1134 case SND_SOC_DAPM_POST_PMD:
1135 ev_name = "POST_PMD";
1136 power = 0;
1137 break;
Mark Brown80114122013-02-25 15:14:19 +00001138 case SND_SOC_DAPM_WILL_PMU:
1139 ev_name = "WILL_PMU";
1140 power = 1;
1141 break;
1142 case SND_SOC_DAPM_WILL_PMD:
1143 ev_name = "WILL_PMD";
1144 power = 0;
1145 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001146 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001147 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001148 return;
1149 }
1150
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001151 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001152 return;
1153
1154 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001155 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001156 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001157 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001158 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001159 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001160 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001161 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001162 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001163 ev_name, w->name, ret);
1164 }
1165}
1166
Mark Brownb22ead22009-06-07 12:51:26 +01001167/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001168static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001169 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001170{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001171 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001172 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001173 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001174 unsigned int value = 0;
1175 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001176
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001177 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1178 reg = w->reg;
1179 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001180
1181 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001182 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001183 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001184
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001185 mask |= w->mask << w->shift;
1186 if (w->power)
1187 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001188 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001189 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001190
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001191 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001192 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1193 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001194
Mark Brown68f89ad2010-11-03 23:51:49 -04001195 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001196 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1197 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001198 }
1199
Mark Brown81628102009-06-07 13:21:24 +01001200 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001201 /* Any widget will do, they should all be updating the
1202 * same register.
1203 */
Mark Brown29376bc2011-06-19 13:49:28 +01001204
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001205 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001206 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001207 value, mask, reg, card->pop_time);
1208 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001209 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001210 }
1211
1212 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001213 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1214 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001215 }
Mark Brown42aa3412009-03-01 19:21:10 +00001216}
1217
Mark Brownb22ead22009-06-07 12:51:26 +01001218/* Apply a DAPM power sequence.
1219 *
1220 * We walk over a pre-sorted list of widgets to apply power to. In
1221 * order to minimise the number of writes to the device required
1222 * multiple widgets will be updated in a single write where possible.
1223 * Currently anything that requires more than a single write is not
1224 * handled.
1225 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001226static void dapm_seq_run(struct snd_soc_card *card,
1227 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001228{
1229 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001230 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001231 LIST_HEAD(pending);
1232 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001233 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001234 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001235 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001236 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001237 int *sort;
1238
1239 if (power_up)
1240 sort = dapm_up_seq;
1241 else
1242 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001243
Mark Brownb22ead22009-06-07 12:51:26 +01001244 list_for_each_entry_safe(w, n, list, power_list) {
1245 ret = 0;
1246
1247 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001248 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001249 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001250 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001251 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001252
Mark Brown474b62d2011-01-18 16:14:44 +00001253 if (cur_dapm && cur_dapm->seq_notifier) {
1254 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1255 if (sort[i] == cur_sort)
1256 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001257 i,
1258 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001259 }
1260
Mark Browneb270e92013-10-09 13:52:52 +01001261 if (cur_dapm && w->dapm != cur_dapm)
1262 soc_dapm_async_complete(cur_dapm);
1263
Mark Brownb22ead22009-06-07 12:51:26 +01001264 INIT_LIST_HEAD(&pending);
1265 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001266 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001267 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001268 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001269 }
1270
Mark Brown163cac02009-06-07 10:12:52 +01001271 switch (w->id) {
1272 case snd_soc_dapm_pre:
1273 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001274 list_for_each_entry_safe_continue(w, n, list,
1275 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001276
Mark Brownb22ead22009-06-07 12:51:26 +01001277 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001278 ret = w->event(w,
1279 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001280 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001281 ret = w->event(w,
1282 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001283 break;
1284
1285 case snd_soc_dapm_post:
1286 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001287 list_for_each_entry_safe_continue(w, n, list,
1288 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001289
Mark Brownb22ead22009-06-07 12:51:26 +01001290 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001291 ret = w->event(w,
1292 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001293 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001294 ret = w->event(w,
1295 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001296 break;
1297
Mark Brown163cac02009-06-07 10:12:52 +01001298 default:
Mark Brown81628102009-06-07 13:21:24 +01001299 /* Queue it up for application */
1300 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001301 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001302 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001303 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001304 list_move(&w->power_list, &pending);
1305 break;
Mark Brown163cac02009-06-07 10:12:52 +01001306 }
Mark Brownb22ead22009-06-07 12:51:26 +01001307
1308 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001309 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001310 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001311 }
Mark Brownb22ead22009-06-07 12:51:26 +01001312
1313 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001314 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001315
1316 if (cur_dapm && cur_dapm->seq_notifier) {
1317 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1318 if (sort[i] == cur_sort)
1319 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001320 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001321 }
Mark Browneb270e92013-10-09 13:52:52 +01001322
1323 list_for_each_entry(d, &card->dapm_list, list) {
1324 soc_dapm_async_complete(d);
1325 }
Mark Brown163cac02009-06-07 10:12:52 +01001326}
1327
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001328static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001329{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001330 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001331 struct snd_soc_dapm_widget_list *wlist;
1332 struct snd_soc_dapm_widget *w = NULL;
1333 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001334 int ret;
1335
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001336 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001337 return;
1338
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001339 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001340
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001341 for (wi = 0; wi < wlist->num_widgets; wi++) {
1342 w = wlist->widgets[wi];
1343
1344 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1345 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1346 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001347 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001348 w->name, ret);
1349 }
Mark Brown97404f22010-12-14 16:13:57 +00001350 }
1351
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001352 if (!w)
1353 return;
1354
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001355 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1356 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001357 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001358 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001359 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001360
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001361 for (wi = 0; wi < wlist->num_widgets; wi++) {
1362 w = wlist->widgets[wi];
1363
1364 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1365 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1366 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001367 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001368 w->name, ret);
1369 }
Mark Brown97404f22010-12-14 16:13:57 +00001370 }
1371}
1372
Mark Brown9d0624a2011-02-18 11:49:43 -08001373/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1374 * they're changing state.
1375 */
1376static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1377{
1378 struct snd_soc_dapm_context *d = data;
1379 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001380
Mark Brown56fba412011-06-04 11:25:10 +01001381 /* If we're off and we're not supposed to be go into STANDBY */
1382 if (d->bias_level == SND_SOC_BIAS_OFF &&
1383 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001384 if (d->dev)
1385 pm_runtime_get_sync(d->dev);
1386
Mark Brown9d0624a2011-02-18 11:49:43 -08001387 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1388 if (ret != 0)
1389 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001390 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001391 }
1392
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001393 /* Prepare for a transition to ON or away from ON */
1394 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1395 d->bias_level != SND_SOC_BIAS_ON) ||
1396 (d->target_bias_level != SND_SOC_BIAS_ON &&
1397 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001398 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1399 if (ret != 0)
1400 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001401 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001402 }
1403}
1404
1405/* Async callback run prior to DAPM sequences - brings to their final
1406 * state.
1407 */
1408static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1409{
1410 struct snd_soc_dapm_context *d = data;
1411 int ret;
1412
1413 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001414 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1415 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1416 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001417 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1418 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001419 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001420 ret);
1421 }
1422
1423 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001424 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1425 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001426 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1427 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001428 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1429 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001430
1431 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001432 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001433 }
1434
1435 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001436 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1437 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001438 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1439 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001440 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001441 ret);
1442 }
1443}
Mark Brown97404f22010-12-14 16:13:57 +00001444
Mark Brownfe4fda52011-10-03 22:36:57 +01001445static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1446 bool power, bool connect)
1447{
1448 /* If a connection is being made or broken then that update
1449 * will have marked the peer dirty, otherwise the widgets are
1450 * not connected and this update has no impact. */
1451 if (!connect)
1452 return;
1453
1454 /* If the peer is already in the state we're moving to then we
1455 * won't have an impact on it. */
1456 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001457 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001458}
1459
Mark Brown05623c42011-09-28 17:02:31 +01001460static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1461 struct list_head *up_list,
1462 struct list_head *down_list)
1463{
Mark Browndb432b42011-10-03 21:06:40 +01001464 struct snd_soc_dapm_path *path;
1465
Mark Brown05623c42011-09-28 17:02:31 +01001466 if (w->power == power)
1467 return;
1468
1469 trace_snd_soc_dapm_widget_power(w, power);
1470
Mark Browndb432b42011-10-03 21:06:40 +01001471 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001472 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001473 */
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001474 list_for_each_entry(path, &w->sources, list_sink)
1475 dapm_widget_set_peer_power(path->source, power, path->connect);
1476
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001477 /* Supplies can't affect their outputs, only their inputs */
1478 if (!w->is_supply) {
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001479 list_for_each_entry(path, &w->sinks, list_source)
1480 dapm_widget_set_peer_power(path->sink, power,
1481 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001482 }
1483
Mark Brown05623c42011-09-28 17:02:31 +01001484 if (power)
1485 dapm_seq_insert(w, up_list, true);
1486 else
1487 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001488}
1489
Mark Brown7c81beb2011-09-20 22:22:32 +01001490static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1491 struct list_head *up_list,
1492 struct list_head *down_list)
1493{
Mark Brown7c81beb2011-09-20 22:22:32 +01001494 int power;
1495
1496 switch (w->id) {
1497 case snd_soc_dapm_pre:
1498 dapm_seq_insert(w, down_list, false);
1499 break;
1500 case snd_soc_dapm_post:
1501 dapm_seq_insert(w, up_list, true);
1502 break;
1503
1504 default:
Mark Brownd8050022011-09-28 18:28:23 +01001505 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001506
Mark Brown05623c42011-09-28 17:02:31 +01001507 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001508 break;
1509 }
1510}
1511
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001512static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1513{
1514 if (dapm->idle_bias_off)
1515 return true;
1516
1517 switch (snd_power_get_state(dapm->card->snd_card)) {
1518 case SNDRV_CTL_POWER_D3hot:
1519 case SNDRV_CTL_POWER_D3cold:
1520 return dapm->suspend_bias_off;
1521 default:
1522 break;
1523 }
1524
1525 return false;
1526}
1527
Mark Brown42aa3412009-03-01 19:21:10 +00001528/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001529 * Scan each dapm widget for complete audio path.
1530 * A complete path is a route that has valid endpoints i.e.:-
1531 *
1532 * o DAC to output pin.
1533 * o Input Pin to ADC.
1534 * o Input pin to Output pin (bypass, sidetone)
1535 * o DAC to ADC (loopback).
1536 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001537static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001538{
1539 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001540 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001541 LIST_HEAD(up_list);
1542 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001543 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001544 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001545
Mark Brownf9fa2b12014-03-06 16:49:11 +08001546 lockdep_assert_held(&card->dapm_mutex);
1547
Mark Brown84e90932010-11-04 00:07:02 -04001548 trace_snd_soc_dapm_start(card);
1549
Mark Brown56fba412011-06-04 11:25:10 +01001550 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001551 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001552 d->target_bias_level = SND_SOC_BIAS_OFF;
1553 else
1554 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001555 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001556
Liam Girdwood6c120e12012-02-15 15:15:34 +00001557 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001558
Mark Brown6d3ddc82009-05-16 17:47:29 +01001559 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001560 * lists indicating if they should be powered up or down. We
1561 * only check widgets that have been flagged as dirty but note
1562 * that new widgets may be added to the dirty list while we
1563 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001564 */
Mark Browndb432b42011-10-03 21:06:40 +01001565 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001566 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001567 }
1568
Mark Brownf9de6d72011-09-28 17:19:47 +01001569 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001570 switch (w->id) {
1571 case snd_soc_dapm_pre:
1572 case snd_soc_dapm_post:
1573 /* These widgets always need to be powered */
1574 break;
1575 default:
1576 list_del_init(&w->dirty);
1577 break;
1578 }
Mark Browndb432b42011-10-03 21:06:40 +01001579
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001580 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001581 d = w->dapm;
1582
1583 /* Supplies and micbiases only bring the
1584 * context up to STANDBY as unless something
1585 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001586 * generally don't require full power. Signal
1587 * generators are virtual pins and have no
1588 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001589 */
1590 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001591 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001592 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001593 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001594 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001595 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001596 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001597 case snd_soc_dapm_micbias:
1598 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1599 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1600 break;
1601 default:
1602 d->target_bias_level = SND_SOC_BIAS_ON;
1603 break;
1604 }
1605 }
1606
1607 }
1608
Mark Brown85a843c2011-09-21 21:29:47 +01001609 /* Force all contexts in the card to the same bias state if
1610 * they're not ground referenced.
1611 */
Mark Brown56fba412011-06-04 11:25:10 +01001612 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001613 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001614 if (d->target_bias_level > bias)
1615 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001616 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001617 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01001618 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001619
Mark Brownde02d072011-09-20 21:43:24 +01001620 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001621
Xiang Xiao17282ba2014-03-02 00:04:03 +08001622 /* Run card bias changes at first */
1623 dapm_pre_sequence_async(&card->dapm, 0);
1624 /* Run other bias changes in parallel */
1625 list_for_each_entry(d, &card->dapm_list, list) {
1626 if (d != &card->dapm)
1627 async_schedule_domain(dapm_pre_sequence_async, d,
1628 &async_domain);
1629 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001630 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001631
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001632 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001633 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001634 }
1635
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001636 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001637 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001638 }
1639
Mark Brown6d3ddc82009-05-16 17:47:29 +01001640 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001641 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001642
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001643 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001644
Mark Brown6d3ddc82009-05-16 17:47:29 +01001645 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001646 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001647
Mark Brown9d0624a2011-02-18 11:49:43 -08001648 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001649 list_for_each_entry(d, &card->dapm_list, list) {
1650 if (d != &card->dapm)
1651 async_schedule_domain(dapm_post_sequence_async, d,
1652 &async_domain);
1653 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001654 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001655 /* Run card bias changes at last */
1656 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001657
Liam Girdwood8078d872012-02-15 15:15:35 +00001658 /* do we need to notify any clients that DAPM event is complete */
1659 list_for_each_entry(d, &card->dapm_list, list) {
1660 if (d->stream_event)
1661 d->stream_event(d, event);
1662 }
1663
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001664 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001665 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001666 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001667
Mark Brown84e90932010-11-04 00:07:02 -04001668 trace_snd_soc_dapm_done(card);
1669
Mark Brown42aa3412009-03-01 19:21:10 +00001670 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001671}
1672
Mark Brown79fb9382009-08-21 16:38:13 +01001673#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001674static ssize_t dapm_widget_power_read_file(struct file *file,
1675 char __user *user_buf,
1676 size_t count, loff_t *ppos)
1677{
1678 struct snd_soc_dapm_widget *w = file->private_data;
1679 char *buf;
1680 int in, out;
1681 ssize_t ret;
1682 struct snd_soc_dapm_path *p = NULL;
1683
1684 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1685 if (!buf)
1686 return -ENOMEM;
1687
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001688 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
1689 if (w->is_supply) {
1690 in = 0;
1691 out = 0;
1692 } else {
1693 in = is_connected_input_ep(w, NULL);
1694 out = is_connected_output_ep(w, NULL);
1695 }
Mark Brown79fb9382009-08-21 16:38:13 +01001696
Mark Brownf13ebad2012-03-03 18:01:01 +00001697 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1698 w->name, w->power ? "On" : "Off",
1699 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001700
Mark Brownd033c362009-12-04 15:25:56 +00001701 if (w->reg >= 0)
1702 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001703 " - R%d(0x%x) mask 0x%x",
1704 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001705
1706 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1707
Mark Brown3eef08b2009-09-14 16:49:00 +01001708 if (w->sname)
1709 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1710 w->sname,
1711 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001712
1713 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001714 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001715 continue;
1716
Mark Brown79fb9382009-08-21 16:38:13 +01001717 if (p->connect)
1718 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001719 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001720 p->name ? p->name : "static",
1721 p->source->name);
1722 }
1723 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001724 if (p->connected && !p->connected(w, p->sink))
1725 continue;
1726
Mark Brown79fb9382009-08-21 16:38:13 +01001727 if (p->connect)
1728 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001729 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001730 p->name ? p->name : "static",
1731 p->sink->name);
1732 }
1733
1734 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1735
1736 kfree(buf);
1737 return ret;
1738}
1739
1740static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001741 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001742 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001743 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001744};
1745
Mark Brownef49e4f2011-04-04 20:48:13 +09001746static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1747 size_t count, loff_t *ppos)
1748{
1749 struct snd_soc_dapm_context *dapm = file->private_data;
1750 char *level;
1751
1752 switch (dapm->bias_level) {
1753 case SND_SOC_BIAS_ON:
1754 level = "On\n";
1755 break;
1756 case SND_SOC_BIAS_PREPARE:
1757 level = "Prepare\n";
1758 break;
1759 case SND_SOC_BIAS_STANDBY:
1760 level = "Standby\n";
1761 break;
1762 case SND_SOC_BIAS_OFF:
1763 level = "Off\n";
1764 break;
1765 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001766 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09001767 level = "Unknown\n";
1768 break;
1769 }
1770
1771 return simple_read_from_buffer(user_buf, count, ppos, level,
1772 strlen(level));
1773}
1774
1775static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001776 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001777 .read = dapm_bias_read_file,
1778 .llseek = default_llseek,
1779};
1780
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001781void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1782 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001783{
Mark Brown79fb9382009-08-21 16:38:13 +01001784 struct dentry *d;
1785
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001786 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1787
1788 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001789 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001790 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001791 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001792 }
Mark Brown79fb9382009-08-21 16:38:13 +01001793
Mark Brownef49e4f2011-04-04 20:48:13 +09001794 d = debugfs_create_file("bias_level", 0444,
1795 dapm->debugfs_dapm, dapm,
1796 &dapm_bias_fops);
1797 if (!d)
1798 dev_warn(dapm->dev,
1799 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001800}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001801
1802static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1803{
1804 struct snd_soc_dapm_context *dapm = w->dapm;
1805 struct dentry *d;
1806
1807 if (!dapm->debugfs_dapm || !w->name)
1808 return;
1809
1810 d = debugfs_create_file(w->name, 0444,
1811 dapm->debugfs_dapm, w,
1812 &dapm_widget_power_fops);
1813 if (!d)
1814 dev_warn(w->dapm->dev,
1815 "ASoC: Failed to create %s debugfs file\n",
1816 w->name);
1817}
1818
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001819static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1820{
1821 debugfs_remove_recursive(dapm->debugfs_dapm);
1822}
1823
Mark Brown79fb9382009-08-21 16:38:13 +01001824#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001825void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1826 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001827{
1828}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001829
1830static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1831{
1832}
1833
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001834static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1835{
1836}
1837
Mark Brown79fb9382009-08-21 16:38:13 +01001838#endif
1839
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02001840/*
1841 * soc_dapm_connect_path() - Connects or disconnects a path
1842 * @path: The path to update
1843 * @connect: The new connect state of the path. True if the path is connected,
1844 * false if it is disconneted.
1845 * @reason: The reason why the path changed (for debugging only)
1846 */
1847static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
1848 bool connect, const char *reason)
1849{
1850 if (path->connect == connect)
1851 return;
1852
1853 path->connect = connect;
1854 dapm_mark_dirty(path->source, reason);
1855 dapm_mark_dirty(path->sink, reason);
1856}
1857
Richard Purdie2b97eab2006-10-06 18:32:18 +02001858/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001859static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001860 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001861{
1862 struct snd_soc_dapm_path *path;
1863 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02001864 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001865
Mark Brownf9fa2b12014-03-06 16:49:11 +08001866 lockdep_assert_held(&card->dapm_mutex);
1867
Richard Purdie2b97eab2006-10-06 18:32:18 +02001868 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02001869 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001870 found = 1;
1871 /* we now need to match the string in the enum to the path */
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02001872 if (!(strcmp(path->name, e->texts[mux])))
1873 connect = true;
1874 else
1875 connect = false;
1876
1877 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001878 }
1879
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001880 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001881 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001882
Liam Girdwood618dae12012-04-25 12:12:51 +01001883 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001884}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001885
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001886int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02001887 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
1888 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001889{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001890 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001891 int ret;
1892
Liam Girdwood3cd04342012-03-09 12:02:08 +00001893 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02001894 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001895 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02001896 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001897 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001898 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001899 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001900 return ret;
1901}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001902EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001903
Milan plzik1b075e32008-01-10 14:39:46 +01001904/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001905static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00001906 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001907{
1908 struct snd_soc_dapm_path *path;
1909 int found = 0;
1910
Mark Brownf9fa2b12014-03-06 16:49:11 +08001911 lockdep_assert_held(&card->dapm_mutex);
1912
Richard Purdie2b97eab2006-10-06 18:32:18 +02001913 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02001914 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001915 found = 1;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02001916 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001917 }
1918
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001919 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001920 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001921
Liam Girdwood618dae12012-04-25 12:12:51 +01001922 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001923}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001924
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001925int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02001926 struct snd_kcontrol *kcontrol, int connect,
1927 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001928{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001929 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001930 int ret;
1931
Liam Girdwood3cd04342012-03-09 12:02:08 +00001932 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02001933 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001934 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02001935 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001936 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001937 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001938 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001939 return ret;
1940}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001941EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001942
Benoit Cousson44ba2642014-07-08 23:19:36 +02001943static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001944{
Richard Purdie2b97eab2006-10-06 18:32:18 +02001945 struct snd_soc_dapm_widget *w;
1946 int count = 0;
1947 char *state = "not set";
1948
Lars-Peter Clausen00200102014-07-17 22:01:07 +02001949 list_for_each_entry(w, &codec->component.card->widgets, list) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001950 if (w->dapm != &codec->dapm)
1951 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001952
1953 /* only display widgets that burnm power */
1954 switch (w->id) {
1955 case snd_soc_dapm_hp:
1956 case snd_soc_dapm_mic:
1957 case snd_soc_dapm_spk:
1958 case snd_soc_dapm_line:
1959 case snd_soc_dapm_micbias:
1960 case snd_soc_dapm_dac:
1961 case snd_soc_dapm_adc:
1962 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001963 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001964 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001965 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001966 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001967 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001968 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001969 if (w->name)
1970 count += sprintf(buf + count, "%s: %s\n",
1971 w->name, w->power ? "On":"Off");
1972 break;
1973 default:
1974 break;
1975 }
1976 }
1977
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001978 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001979 case SND_SOC_BIAS_ON:
1980 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001981 break;
Mark Brown0be98982008-05-19 12:31:28 +02001982 case SND_SOC_BIAS_PREPARE:
1983 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001984 break;
Mark Brown0be98982008-05-19 12:31:28 +02001985 case SND_SOC_BIAS_STANDBY:
1986 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001987 break;
Mark Brown0be98982008-05-19 12:31:28 +02001988 case SND_SOC_BIAS_OFF:
1989 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001990 break;
1991 }
1992 count += sprintf(buf + count, "PM State: %s\n", state);
1993
1994 return count;
1995}
1996
Benoit Cousson44ba2642014-07-08 23:19:36 +02001997/* show dapm widget status in sys fs */
1998static ssize_t dapm_widget_show(struct device *dev,
1999 struct device_attribute *attr, char *buf)
2000{
2001 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2002 int i, count = 0;
2003
2004 for (i = 0; i < rtd->num_codecs; i++) {
2005 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
2006 count += dapm_widget_show_codec(codec, buf + count);
2007 }
2008
2009 return count;
2010}
2011
Richard Purdie2b97eab2006-10-06 18:32:18 +02002012static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2013
2014int snd_soc_dapm_sys_add(struct device *dev)
2015{
Troy Kisky12ef1932008-10-13 17:42:14 -07002016 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002017}
2018
2019static void snd_soc_dapm_sys_remove(struct device *dev)
2020{
Mark Brownaef90842009-05-16 17:53:16 +01002021 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002022}
2023
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002024static void dapm_free_path(struct snd_soc_dapm_path *path)
2025{
2026 list_del(&path->list_sink);
2027 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002028 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002029 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002030 kfree(path);
2031}
2032
Richard Purdie2b97eab2006-10-06 18:32:18 +02002033/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002034static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002035{
2036 struct snd_soc_dapm_widget *w, *next_w;
2037 struct snd_soc_dapm_path *p, *next_p;
2038
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002039 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2040 if (w->dapm != dapm)
2041 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002042 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002043 /*
2044 * remove source and sink paths associated to this widget.
2045 * While removing the path, remove reference to it from both
2046 * source and sink widgets so that path is removed only once.
2047 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002048 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2049 dapm_free_path(p);
2050
2051 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2052 dapm_free_path(p);
2053
Stephen Warrenfad59882011-04-28 17:37:59 -06002054 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002055 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002056 kfree(w);
2057 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002058}
2059
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002060static struct snd_soc_dapm_widget *dapm_find_widget(
2061 struct snd_soc_dapm_context *dapm, const char *pin,
2062 bool search_other_contexts)
2063{
2064 struct snd_soc_dapm_widget *w;
2065 struct snd_soc_dapm_widget *fallback = NULL;
2066
2067 list_for_each_entry(w, &dapm->card->widgets, list) {
2068 if (!strcmp(w->name, pin)) {
2069 if (w->dapm == dapm)
2070 return w;
2071 else
2072 fallback = w;
2073 }
2074 }
2075
2076 if (search_other_contexts)
2077 return fallback;
2078
2079 return NULL;
2080}
2081
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002082static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002083 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002084{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002085 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002086
Mark Brownf9fa2b12014-03-06 16:49:11 +08002087 dapm_assert_locked(dapm);
2088
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002089 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002090 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002091 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002092 }
2093
Mark Brown1a8b2d92012-02-16 11:50:07 -08002094 if (w->connected != status)
2095 dapm_mark_dirty(w, "pin configuration");
2096
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002097 w->connected = status;
2098 if (status == 0)
2099 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002100
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002101 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002102}
2103
Richard Purdie2b97eab2006-10-06 18:32:18 +02002104/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002105 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2106 * @dapm: DAPM context
2107 *
2108 * Walks all dapm audio paths and powers widgets according to their
2109 * stream or path usage.
2110 *
2111 * Requires external locking.
2112 *
2113 * Returns 0 for success.
2114 */
2115int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2116{
2117 /*
2118 * Suppress early reports (eg, jacks syncing their state) to avoid
2119 * silly DAPM runs during card startup.
2120 */
2121 if (!dapm->card || !dapm->card->instantiated)
2122 return 0;
2123
2124 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2125}
2126EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2127
2128/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002129 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002130 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002131 *
2132 * Walks all dapm audio paths and powers widgets according to their
2133 * stream or path usage.
2134 *
2135 * Returns 0 for success.
2136 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002137int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002138{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002139 int ret;
2140
Liam Girdwood3cd04342012-03-09 12:02:08 +00002141 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002142 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002143 mutex_unlock(&dapm->card->dapm_mutex);
2144 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002145}
Liam Girdwooda5302182008-07-07 13:35:17 +01002146EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002147
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002148/*
2149 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2150 * @w: The widget for which to update the flags
2151 *
2152 * Some widgets have a dynamic category which depends on which neighbors they
2153 * are connected to. This function update the category for these widgets.
2154 *
2155 * This function must be called whenever a path is added or removed to a widget.
2156 */
2157static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2158{
2159 struct snd_soc_dapm_path *p;
2160
2161 switch (w->id) {
2162 case snd_soc_dapm_input:
2163 w->is_source = 1;
2164 list_for_each_entry(p, &w->sources, list_sink) {
2165 if (p->source->id == snd_soc_dapm_micbias ||
2166 p->source->id == snd_soc_dapm_mic ||
2167 p->source->id == snd_soc_dapm_line ||
2168 p->source->id == snd_soc_dapm_output) {
2169 w->is_source = 0;
2170 break;
2171 }
2172 }
2173 break;
2174 case snd_soc_dapm_output:
2175 w->is_sink = 1;
2176 list_for_each_entry(p, &w->sinks, list_source) {
2177 if (p->sink->id == snd_soc_dapm_spk ||
2178 p->sink->id == snd_soc_dapm_hp ||
2179 p->sink->id == snd_soc_dapm_line ||
2180 p->sink->id == snd_soc_dapm_input) {
2181 w->is_sink = 0;
2182 break;
2183 }
2184 }
2185 break;
2186 case snd_soc_dapm_line:
2187 w->is_sink = !list_empty(&w->sources);
2188 w->is_source = !list_empty(&w->sinks);
2189 break;
2190 default:
2191 break;
2192 }
2193}
2194
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002195static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2196 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2197 const char *control,
2198 int (*connected)(struct snd_soc_dapm_widget *source,
2199 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002200{
2201 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002202 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002203
2204 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2205 if (!path)
2206 return -ENOMEM;
2207
2208 path->source = wsource;
2209 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002210 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002211 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002212 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002213 INIT_LIST_HEAD(&path->list_source);
2214 INIT_LIST_HEAD(&path->list_sink);
2215
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002216 if (wsource->is_supply || wsink->is_supply)
2217 path->is_supply = 1;
2218
Richard Purdie2b97eab2006-10-06 18:32:18 +02002219 /* connect static paths */
2220 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002221 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002222 } else {
2223 /* connect dynamic paths */
2224 switch (wsink->id) {
2225 case snd_soc_dapm_mux:
2226 ret = dapm_connect_mux(dapm, path, control);
2227 if (ret != 0)
2228 goto err;
2229 break;
2230 case snd_soc_dapm_switch:
2231 case snd_soc_dapm_mixer:
2232 case snd_soc_dapm_mixer_named_ctl:
2233 ret = dapm_connect_mixer(dapm, path, control);
2234 if (ret != 0)
2235 goto err;
2236 break;
2237 default:
2238 dev_err(dapm->dev,
2239 "Control not supported for path %s -> [%s] -> %s\n",
2240 wsource->name, control, wsink->name);
2241 ret = -EINVAL;
2242 goto err;
2243 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244 }
2245
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002246 list_add(&path->list, &dapm->card->paths);
2247 list_add(&path->list_sink, &wsink->sources);
2248 list_add(&path->list_source, &wsource->sinks);
2249
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002250 dapm_update_widget_flags(wsource);
2251 dapm_update_widget_flags(wsink);
2252
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002253 dapm_mark_dirty(wsource, "Route added");
2254 dapm_mark_dirty(wsink, "Route added");
Mark Brownfabd0382012-07-05 17:20:06 +01002255
Richard Purdie2b97eab2006-10-06 18:32:18 +02002256 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002257err:
2258 kfree(path);
2259 return ret;
2260}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002261
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002262static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002263 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002264{
2265 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2266 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2267 const char *sink;
2268 const char *source;
2269 char prefixed_sink[80];
2270 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002271 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002272 int ret;
2273
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002274 prefix = soc_dapm_prefix(dapm);
2275 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002276 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002277 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002278 sink = prefixed_sink;
2279 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002280 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002281 source = prefixed_source;
2282 } else {
2283 sink = route->sink;
2284 source = route->source;
2285 }
2286
2287 /*
2288 * find src and dest widgets over all widgets but favor a widget from
2289 * current DAPM context
2290 */
2291 list_for_each_entry(w, &dapm->card->widgets, list) {
2292 if (!wsink && !(strcmp(w->name, sink))) {
2293 wtsink = w;
2294 if (w->dapm == dapm)
2295 wsink = w;
2296 continue;
2297 }
2298 if (!wsource && !(strcmp(w->name, source))) {
2299 wtsource = w;
2300 if (w->dapm == dapm)
2301 wsource = w;
2302 }
2303 }
2304 /* use widget from another DAPM context if not found from this */
2305 if (!wsink)
2306 wsink = wtsink;
2307 if (!wsource)
2308 wsource = wtsource;
2309
2310 if (wsource == NULL) {
2311 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2312 route->source);
2313 return -ENODEV;
2314 }
2315 if (wsink == NULL) {
2316 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2317 route->sink);
2318 return -ENODEV;
2319 }
2320
2321 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2322 route->connected);
2323 if (ret)
2324 goto err;
2325
2326 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002327err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002328 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002329 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002330 return ret;
2331}
Mark Brown105f1c22008-05-13 14:52:19 +02002332
Mark Brownefcc3c62012-07-05 17:24:19 +01002333static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2334 const struct snd_soc_dapm_route *route)
2335{
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002336 struct snd_soc_dapm_widget *wsource, *wsink;
Mark Brownefcc3c62012-07-05 17:24:19 +01002337 struct snd_soc_dapm_path *path, *p;
2338 const char *sink;
2339 const char *source;
2340 char prefixed_sink[80];
2341 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002342 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002343
2344 if (route->control) {
2345 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002346 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002347 return -EINVAL;
2348 }
2349
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002350 prefix = soc_dapm_prefix(dapm);
2351 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002352 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002353 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002354 sink = prefixed_sink;
2355 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002356 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002357 source = prefixed_source;
2358 } else {
2359 sink = route->sink;
2360 source = route->source;
2361 }
2362
2363 path = NULL;
2364 list_for_each_entry(p, &dapm->card->paths, list) {
2365 if (strcmp(p->source->name, source) != 0)
2366 continue;
2367 if (strcmp(p->sink->name, sink) != 0)
2368 continue;
2369 path = p;
2370 break;
2371 }
2372
2373 if (path) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002374 wsource = path->source;
2375 wsink = path->sink;
2376
2377 dapm_mark_dirty(wsource, "Route removed");
2378 dapm_mark_dirty(wsink, "Route removed");
Mark Brownefcc3c62012-07-05 17:24:19 +01002379
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002380 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002381
2382 /* Update any path related flags */
2383 dapm_update_widget_flags(wsource);
2384 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002385 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002386 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002387 source, sink);
2388 }
2389
2390 return 0;
2391}
2392
Mark Brown105f1c22008-05-13 14:52:19 +02002393/**
Mark Brown105f1c22008-05-13 14:52:19 +02002394 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002395 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002396 * @route: audio routes
2397 * @num: number of routes
2398 *
2399 * Connects 2 dapm widgets together via a named audio path. The sink is
2400 * the widget receiving the audio signal, whilst the source is the sender
2401 * of the audio signal.
2402 *
2403 * Returns 0 for success else error. On error all resources can be freed
2404 * with a call to snd_soc_card_free().
2405 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002406int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002407 const struct snd_soc_dapm_route *route, int num)
2408{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002409 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002410
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002411 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002412 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002413 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002414 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002415 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2416 route->source,
2417 route->control ? route->control : "direct",
2418 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002419 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002420 }
2421 route++;
2422 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002423 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002424
Dan Carpenter60884c22012-04-13 22:25:43 +03002425 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002426}
2427EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2428
Mark Brownefcc3c62012-07-05 17:24:19 +01002429/**
2430 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2431 * @dapm: DAPM context
2432 * @route: audio routes
2433 * @num: number of routes
2434 *
2435 * Removes routes from the DAPM context.
2436 */
2437int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2438 const struct snd_soc_dapm_route *route, int num)
2439{
2440 int i, ret = 0;
2441
2442 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2443 for (i = 0; i < num; i++) {
2444 snd_soc_dapm_del_route(dapm, route);
2445 route++;
2446 }
2447 mutex_unlock(&dapm->card->dapm_mutex);
2448
2449 return ret;
2450}
2451EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2452
Mark Brownbf3a9e12011-06-13 16:42:29 +01002453static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2454 const struct snd_soc_dapm_route *route)
2455{
2456 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2457 route->source,
2458 true);
2459 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2460 route->sink,
2461 true);
2462 struct snd_soc_dapm_path *path;
2463 int count = 0;
2464
2465 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002466 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002467 route->source);
2468 return -ENODEV;
2469 }
2470
2471 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002472 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002473 route->sink);
2474 return -ENODEV;
2475 }
2476
2477 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002478 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002479 route->source, route->sink);
2480
2481 list_for_each_entry(path, &source->sinks, list_source) {
2482 if (path->sink == sink) {
2483 path->weak = 1;
2484 count++;
2485 }
2486 }
2487
2488 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002489 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002490 route->source, route->sink);
2491 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002492 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002493 count, route->source, route->sink);
2494
2495 return 0;
2496}
2497
2498/**
2499 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2500 * @dapm: DAPM context
2501 * @route: audio routes
2502 * @num: number of routes
2503 *
2504 * Mark existing routes matching those specified in the passed array
2505 * as being weak, meaning that they are ignored for the purpose of
2506 * power decisions. The main intended use case is for sidetone paths
2507 * which couple audio between other independent paths if they are both
2508 * active in order to make the combination work better at the user
2509 * level but which aren't intended to be "used".
2510 *
2511 * Note that CODEC drivers should not use this as sidetone type paths
2512 * can frequently also be used as bypass paths.
2513 */
2514int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2515 const struct snd_soc_dapm_route *route, int num)
2516{
2517 int i, err;
2518 int ret = 0;
2519
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002520 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002521 for (i = 0; i < num; i++) {
2522 err = snd_soc_dapm_weak_route(dapm, route);
2523 if (err)
2524 ret = err;
2525 route++;
2526 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002527 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002528
2529 return ret;
2530}
2531EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2532
Mark Brown105f1c22008-05-13 14:52:19 +02002533/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002534 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002535 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002536 *
2537 * Checks the codec for any new dapm widgets and creates them if found.
2538 *
2539 * Returns 0 for success.
2540 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002541int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002542{
2543 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002544 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002545
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002546 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002547
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002548 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002549 {
2550 if (w->new)
2551 continue;
2552
Stephen Warrenfad59882011-04-28 17:37:59 -06002553 if (w->num_kcontrols) {
2554 w->kcontrols = kzalloc(w->num_kcontrols *
2555 sizeof(struct snd_kcontrol *),
2556 GFP_KERNEL);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002557 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002558 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002559 return -ENOMEM;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002560 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002561 }
2562
Richard Purdie2b97eab2006-10-06 18:32:18 +02002563 switch(w->id) {
2564 case snd_soc_dapm_switch:
2565 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002566 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002567 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002568 break;
2569 case snd_soc_dapm_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002570 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002571 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002572 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002573 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002574 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002575 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002576 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002577 break;
2578 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002579
2580 /* Read the initial power state from the device */
2581 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002582 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002583 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002584 val &= w->mask;
2585 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002586 w->power = 1;
2587 }
2588
Richard Purdie2b97eab2006-10-06 18:32:18 +02002589 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002590
Mark Brown7508b122011-10-05 12:09:12 +01002591 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002592 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002593 }
2594
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002595 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2596 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002597 return 0;
2598}
2599EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2600
2601/**
2602 * snd_soc_dapm_get_volsw - dapm mixer get callback
2603 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002604 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002605 *
2606 * Callback to get the value of a dapm mixer control.
2607 *
2608 * Returns 0 for success.
2609 */
2610int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2611 struct snd_ctl_elem_value *ucontrol)
2612{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002613 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2614 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002615 struct soc_mixer_control *mc =
2616 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002617 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002618 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002619 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002620 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002621 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002622 unsigned int val;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002623 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002624
2625 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002626 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002627 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002628 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002629
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002630 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002631 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
2632 ret = soc_dapm_read(dapm, reg, &val);
2633 val = (val >> shift) & mask;
2634 } else {
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002635 val = dapm_kcontrol_get_value(kcontrol);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002636 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002637 mutex_unlock(&card->dapm_mutex);
2638
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002639 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002640 ucontrol->value.integer.value[0] = max - val;
2641 else
2642 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002643
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002644 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002645}
2646EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2647
2648/**
2649 * snd_soc_dapm_put_volsw - dapm mixer set callback
2650 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002651 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002652 *
2653 * Callback to set the value of a dapm mixer control.
2654 *
2655 * Returns 0 for success.
2656 */
2657int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2658 struct snd_ctl_elem_value *ucontrol)
2659{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002660 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2661 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002662 struct soc_mixer_control *mc =
2663 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002664 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002665 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002666 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002667 unsigned int mask = (1 << fls(max)) - 1;
2668 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002669 unsigned int val;
Jarkko Nikula18626c72014-06-09 14:20:29 +03002670 int connect, change, reg_change = 0;
Mark Brown97404f22010-12-14 16:13:57 +00002671 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002672 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002673
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002674 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002675 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002676 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002677 kcontrol->id.name);
2678
Richard Purdie2b97eab2006-10-06 18:32:18 +02002679 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002680 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002681
2682 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002683 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002684
Liam Girdwood3cd04342012-03-09 12:02:08 +00002685 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002686
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002687 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown283375c2009-12-07 18:09:03 +00002688
Jarkko Nikula18626c72014-06-09 14:20:29 +03002689 if (reg != SND_SOC_NOPM) {
2690 mask = mask << shift;
2691 val = val << shift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02002692
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002693 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
Jarkko Nikula18626c72014-06-09 14:20:29 +03002694 }
2695
2696 if (change || reg_change) {
2697 if (reg_change) {
2698 update.kcontrol = kcontrol;
2699 update.reg = reg;
2700 update.mask = mask;
2701 update.val = val;
2702 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002703 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03002704 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00002705
Nenghua Cao52765972013-12-13 20:13:49 +08002706 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002707
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002708 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002709 }
2710
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002711 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002712
2713 if (ret > 0)
2714 soc_dpcm_runtime_update(card);
2715
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002716 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002717}
2718EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2719
2720/**
2721 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2722 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002723 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002724 *
2725 * Callback to get the value of a dapm enumerated double mixer control.
2726 *
2727 * Returns 0 for success.
2728 */
2729int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2730 struct snd_ctl_elem_value *ucontrol)
2731{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002732 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002733 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002734 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002735
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002736 if (e->reg != SND_SOC_NOPM) {
2737 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
2738 if (ret)
2739 return ret;
2740 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002741 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002742 }
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002743
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002744 val = (reg_val >> e->shift_l) & e->mask;
2745 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2746 if (e->shift_l != e->shift_r) {
2747 val = (reg_val >> e->shift_r) & e->mask;
2748 val = snd_soc_enum_val_to_item(e, val);
2749 ucontrol->value.enumerated.item[1] = val;
2750 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002751
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002752 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002753}
2754EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2755
2756/**
2757 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2758 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002759 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002760 *
2761 * Callback to set the value of a dapm enumerated double mixer control.
2762 *
2763 * Returns 0 for success.
2764 */
2765int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2766 struct snd_ctl_elem_value *ucontrol)
2767{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002768 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2769 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002770 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002771 unsigned int *item = ucontrol->value.enumerated.item;
2772 unsigned int val, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002773 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002774 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002775 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002776
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002777 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002778 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002779
2780 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002781 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002782 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002783 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002784 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002785 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002786 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002787 }
2788
Liam Girdwood3cd04342012-03-09 12:02:08 +00002789 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002790
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002791 if (e->reg != SND_SOC_NOPM)
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002792 change = soc_dapm_test_bits(dapm, e->reg, mask, val);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002793 else
2794 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002795
Richard Purdie2b97eab2006-10-06 18:32:18 +02002796 if (change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002797 if (e->reg != SND_SOC_NOPM) {
2798 update.kcontrol = kcontrol;
2799 update.reg = e->reg;
2800 update.mask = mask;
2801 update.val = val;
2802 card->update = &update;
2803 }
Mark Brown3a655772009-10-05 17:23:30 +01002804
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002805 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002806
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002807 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002808 }
2809
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002810 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002811
2812 if (ret > 0)
2813 soc_dpcm_runtime_update(card);
2814
Mark Brown97404f22010-12-14 16:13:57 +00002815 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002816}
2817EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2818
2819/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002820 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2821 *
2822 * @kcontrol: mixer control
2823 * @uinfo: control element information
2824 *
2825 * Callback to provide information about a pin switch control.
2826 */
2827int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2828 struct snd_ctl_elem_info *uinfo)
2829{
2830 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2831 uinfo->count = 1;
2832 uinfo->value.integer.min = 0;
2833 uinfo->value.integer.max = 1;
2834
2835 return 0;
2836}
2837EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2838
2839/**
2840 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2841 *
2842 * @kcontrol: mixer control
2843 * @ucontrol: Value
2844 */
2845int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2846 struct snd_ctl_elem_value *ucontrol)
2847{
Mark Brown48a8c392012-02-14 17:11:15 -08002848 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002849 const char *pin = (const char *)kcontrol->private_value;
2850
Liam Girdwood3cd04342012-03-09 12:02:08 +00002851 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002852
2853 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002854 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002855
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002856 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002857
2858 return 0;
2859}
2860EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2861
2862/**
2863 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2864 *
2865 * @kcontrol: mixer control
2866 * @ucontrol: Value
2867 */
2868int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2869 struct snd_ctl_elem_value *ucontrol)
2870{
Mark Brown48a8c392012-02-14 17:11:15 -08002871 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002872 const char *pin = (const char *)kcontrol->private_value;
2873
Mark Brown8b37dbd2009-02-28 21:14:20 +00002874 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002875 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002876 else
Mark Brown48a8c392012-02-14 17:11:15 -08002877 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002878
Mark Brown48a8c392012-02-14 17:11:15 -08002879 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002880 return 0;
2881}
2882EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2883
Mark Brown5ba06fc2012-02-16 11:07:13 -08002884static struct snd_soc_dapm_widget *
2885snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2886 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002887{
2888 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002889 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00002890 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002891
2892 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08002893 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002894
Mark Brown62ea8742012-01-21 21:14:48 +00002895 switch (w->id) {
2896 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00002897 w->regulator = devm_regulator_get(dapm->dev, w->name);
2898 if (IS_ERR(w->regulator)) {
2899 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002900 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00002901 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002902 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00002903 }
Mark Brown8784c772013-01-10 19:33:47 +00002904
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002905 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00002906 ret = regulator_allow_bypass(w->regulator, true);
2907 if (ret != 0)
2908 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00002909 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00002910 w->name, ret);
2911 }
Mark Brown62ea8742012-01-21 21:14:48 +00002912 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02002913 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01002914#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01002915 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02002916 if (IS_ERR(w->clk)) {
2917 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002918 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02002919 w->name, ret);
2920 return NULL;
2921 }
Mark Brownec029952012-06-04 08:16:20 +01002922#else
2923 return NULL;
2924#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02002925 break;
Mark Brown62ea8742012-01-21 21:14:48 +00002926 default:
2927 break;
2928 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002929
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002930 prefix = soc_dapm_prefix(dapm);
2931 if (prefix)
2932 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002933 else
2934 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
2935
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002936 if (w->name == NULL) {
2937 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002938 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002939 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002940
Mark Brown7ca3a182011-10-08 14:04:50 +01002941 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002942 case snd_soc_dapm_mic:
2943 case snd_soc_dapm_input:
2944 w->is_source = 1;
2945 w->power_check = dapm_generic_check_power;
2946 break;
2947 case snd_soc_dapm_spk:
2948 case snd_soc_dapm_hp:
2949 case snd_soc_dapm_output:
2950 w->is_sink = 1;
2951 w->power_check = dapm_generic_check_power;
2952 break;
2953 case snd_soc_dapm_vmid:
2954 case snd_soc_dapm_siggen:
2955 w->is_source = 1;
2956 w->power_check = dapm_always_on_check_power;
2957 break;
2958 case snd_soc_dapm_mux:
Mark Brown7ca3a182011-10-08 14:04:50 +01002959 case snd_soc_dapm_switch:
2960 case snd_soc_dapm_mixer:
2961 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01002962 case snd_soc_dapm_adc:
2963 case snd_soc_dapm_aif_out:
2964 case snd_soc_dapm_dac:
2965 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01002966 case snd_soc_dapm_pga:
2967 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01002968 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01002969 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01002970 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02002971 case snd_soc_dapm_dai_out:
2972 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01002973 w->power_check = dapm_generic_check_power;
2974 break;
2975 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002976 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002977 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002978 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002979 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01002980 w->power_check = dapm_supply_check_power;
2981 break;
2982 default:
2983 w->power_check = dapm_always_on_check_power;
2984 break;
2985 }
2986
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002987 w->dapm = dapm;
Jarkko Nikulab2d9de52014-10-03 15:32:40 +03002988 if (dapm->component)
2989 w->codec = dapm->component->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002990 INIT_LIST_HEAD(&w->sources);
2991 INIT_LIST_HEAD(&w->sinks);
2992 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01002993 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002994 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002995
2996 /* machine layer set ups unconnected pins and insertions */
2997 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08002998 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002999}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003000
3001/**
Mark Brown4ba13272008-05-13 14:51:19 +02003002 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003003 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003004 * @widget: widget array
3005 * @num: number of widgets
3006 *
3007 * Creates new DAPM controls based upon the templates.
3008 *
3009 * Returns 0 for success else error.
3010 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003011int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003012 const struct snd_soc_dapm_widget *widget,
3013 int num)
3014{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003015 struct snd_soc_dapm_widget *w;
3016 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003017 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003018
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003019 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003020 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003021 w = snd_soc_dapm_new_control(dapm, widget);
3022 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003023 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003024 "ASoC: Failed to create DAPM control %s\n",
3025 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003026 ret = -ENOMEM;
3027 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003028 }
Mark Brown4ba13272008-05-13 14:51:19 +02003029 widget++;
3030 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003031 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003032 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003033}
3034EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3035
Mark Brownc74184e2012-04-04 22:12:09 +01003036static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3037 struct snd_kcontrol *kcontrol, int event)
3038{
3039 struct snd_soc_dapm_path *source_p, *sink_p;
3040 struct snd_soc_dai *source, *sink;
3041 const struct snd_soc_pcm_stream *config = w->params;
3042 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003043 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003044 u64 fmt;
3045 int ret;
3046
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003047 if (WARN_ON(!config) ||
3048 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3049 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003050
3051 /* We only support a single source and sink, pick the first */
3052 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3053 list_sink);
3054 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3055 list_source);
3056
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003057 if (WARN_ON(!source_p || !sink_p) ||
3058 WARN_ON(!sink_p->source || !source_p->sink) ||
3059 WARN_ON(!source_p->source || !sink_p->sink))
3060 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003061
3062 source = source_p->source->priv;
3063 sink = sink_p->sink->priv;
3064
3065 /* Be a little careful as we don't want to overflow the mask array */
3066 if (config->formats) {
3067 fmt = ffs(config->formats) - 1;
3068 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003069 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003070 config->formats);
3071 fmt = 0;
3072 }
3073
3074 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003075 params = kzalloc(sizeof(*params), GFP_KERNEL);
3076 if (!params) {
3077 ret = -ENOMEM;
3078 goto out;
3079 }
3080 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003081
Mark Brown9747cec2012-04-26 19:12:21 +01003082 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003083 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003084 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003085 config->rate_max;
3086
Mark Brown9747cec2012-04-26 19:12:21 +01003087 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003088 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003089 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003090 = config->channels_max;
3091
3092 memset(&substream, 0, sizeof(substream));
3093
3094 switch (event) {
3095 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003096 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3097 ret = soc_dai_hw_params(&substream, params, source);
3098 if (ret < 0)
3099 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003100
Benoit Cousson93e69582014-07-08 23:19:38 +02003101 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3102 ret = soc_dai_hw_params(&substream, params, sink);
3103 if (ret < 0)
3104 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003105 break;
3106
3107 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003108 ret = snd_soc_dai_digital_mute(sink, 0,
3109 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003110 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003111 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003112 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003113 break;
3114
3115 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003116 ret = snd_soc_dai_digital_mute(sink, 1,
3117 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003118 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003119 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003120 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003121 break;
3122
3123 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003124 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003125 return -EINVAL;
3126 }
3127
Mark Brown9747cec2012-04-26 19:12:21 +01003128out:
3129 kfree(params);
3130 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003131}
3132
3133int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3134 const struct snd_soc_pcm_stream *params,
3135 struct snd_soc_dapm_widget *source,
3136 struct snd_soc_dapm_widget *sink)
3137{
Mark Brownc74184e2012-04-04 22:12:09 +01003138 struct snd_soc_dapm_widget template;
3139 struct snd_soc_dapm_widget *w;
3140 size_t len;
3141 char *link_name;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003142 int ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003143
3144 len = strlen(source->name) + strlen(sink->name) + 2;
3145 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3146 if (!link_name)
3147 return -ENOMEM;
3148 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3149
3150 memset(&template, 0, sizeof(template));
3151 template.reg = SND_SOC_NOPM;
3152 template.id = snd_soc_dapm_dai_link;
3153 template.name = link_name;
3154 template.event = snd_soc_dai_link_event;
3155 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3156 SND_SOC_DAPM_PRE_PMD;
3157
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003158 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003159
3160 w = snd_soc_dapm_new_control(&card->dapm, &template);
3161 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003162 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003163 link_name);
3164 return -ENOMEM;
3165 }
3166
3167 w->params = params;
3168
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003169 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3170 if (ret)
3171 return ret;
3172 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Mark Brownc74184e2012-04-04 22:12:09 +01003173}
3174
Mark Brown888df392012-02-16 19:37:51 -08003175int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3176 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003177{
Mark Brown888df392012-02-16 19:37:51 -08003178 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003179 struct snd_soc_dapm_widget *w;
3180
Mark Brown888df392012-02-16 19:37:51 -08003181 WARN_ON(dapm->dev != dai->dev);
3182
3183 memset(&template, 0, sizeof(template));
3184 template.reg = SND_SOC_NOPM;
3185
3186 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003187 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003188 template.name = dai->driver->playback.stream_name;
3189 template.sname = dai->driver->playback.stream_name;
3190
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003191 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003192 template.name);
3193
3194 w = snd_soc_dapm_new_control(dapm, &template);
3195 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003196 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003197 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003198 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003199 }
3200
3201 w->priv = dai;
3202 dai->playback_widget = w;
3203 }
3204
3205 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003206 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003207 template.name = dai->driver->capture.stream_name;
3208 template.sname = dai->driver->capture.stream_name;
3209
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003210 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003211 template.name);
3212
3213 w = snd_soc_dapm_new_control(dapm, &template);
3214 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003215 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003216 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003217 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003218 }
3219
3220 w->priv = dai;
3221 dai->capture_widget = w;
3222 }
3223
3224 return 0;
3225}
3226
3227int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3228{
3229 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003230 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003231 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003232
3233 /* For each DAI widget... */
3234 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003235 switch (dai_w->id) {
3236 case snd_soc_dapm_dai_in:
3237 case snd_soc_dapm_dai_out:
3238 break;
3239 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003240 continue;
Mark Brown46162742013-06-05 19:36:11 +01003241 }
Mark Brown888df392012-02-16 19:37:51 -08003242
3243 dai = dai_w->priv;
3244
3245 /* ...find all widgets with the same stream and link them */
3246 list_for_each_entry(w, &card->widgets, list) {
3247 if (w->dapm != dai_w->dapm)
3248 continue;
3249
Mark Brown46162742013-06-05 19:36:11 +01003250 switch (w->id) {
3251 case snd_soc_dapm_dai_in:
3252 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003253 continue;
Mark Brown46162742013-06-05 19:36:11 +01003254 default:
3255 break;
3256 }
Mark Brown888df392012-02-16 19:37:51 -08003257
Russell King19c2c5f2013-08-04 20:24:03 +01003258 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003259 continue;
3260
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003261 if (dai_w->id == snd_soc_dapm_dai_in) {
3262 src = dai_w;
3263 sink = w;
3264 } else {
3265 src = w;
3266 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003267 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003268 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3269 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003270 }
3271 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003272
Mark Brown888df392012-02-16 19:37:51 -08003273 return 0;
3274}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003275
Benoit Cousson44ba2642014-07-08 23:19:36 +02003276static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3277 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003278{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003279 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003280 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003281 int i;
3282
Benoit Cousson44ba2642014-07-08 23:19:36 +02003283 for (i = 0; i < rtd->num_codecs; i++) {
3284 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003285
3286 /* there is no point in connecting BE DAI links with dummies */
3287 if (snd_soc_dai_is_dummy(codec_dai) ||
3288 snd_soc_dai_is_dummy(cpu_dai))
3289 continue;
3290
3291 /* connect BE DAI playback if widgets are valid */
3292 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003293 source = cpu_dai->playback_widget;
3294 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003295 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003296 cpu_dai->component->name, source->name,
3297 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003298
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003299 snd_soc_dapm_add_path(&card->dapm, source, sink,
3300 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003301 }
3302
3303 /* connect BE DAI capture if widgets are valid */
3304 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003305 source = codec_dai->capture_widget;
3306 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003307 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003308 codec_dai->component->name, source->name,
3309 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003310
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003311 snd_soc_dapm_add_path(&card->dapm, source, sink,
3312 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003313 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003314 }
3315}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003316
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003317static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3318 int event)
3319{
3320 struct snd_soc_dapm_widget *w;
3321
3322 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3323 w = dai->playback_widget;
3324 else
3325 w = dai->capture_widget;
3326
3327 if (w) {
3328 dapm_mark_dirty(w, "stream event");
3329
3330 switch (event) {
3331 case SND_SOC_DAPM_STREAM_START:
3332 w->active = 1;
3333 break;
3334 case SND_SOC_DAPM_STREAM_STOP:
3335 w->active = 0;
3336 break;
3337 case SND_SOC_DAPM_STREAM_SUSPEND:
3338 case SND_SOC_DAPM_STREAM_RESUME:
3339 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3340 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3341 break;
3342 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003343
3344 if (w->id == snd_soc_dapm_dai_in)
3345 w->is_source = w->active;
3346 else
3347 w->is_sink = w->active;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003348 }
3349}
3350
Benoit Cousson44ba2642014-07-08 23:19:36 +02003351void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3352{
3353 struct snd_soc_pcm_runtime *rtd = card->rtd;
3354 int i;
3355
3356 /* for each BE DAI link... */
3357 for (i = 0; i < card->num_rtd; i++) {
3358 rtd = &card->rtd[i];
3359
3360 /*
3361 * dynamic FE links have no fixed DAI mapping.
3362 * CODEC<->CODEC links have no direct connection.
3363 */
3364 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3365 continue;
3366
3367 dapm_connect_dai_link_widgets(card, rtd);
3368 }
3369}
3370
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003371static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3372 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003373{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003374 int i;
3375
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003376 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Benoit Cousson44ba2642014-07-08 23:19:36 +02003377 for (i = 0; i < rtd->num_codecs; i++)
3378 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003379
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003380 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003381}
3382
3383/**
3384 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3385 * @rtd: PCM runtime data
3386 * @stream: stream name
3387 * @event: stream event
3388 *
3389 * Sends a stream event to the dapm core. The core then makes any
3390 * necessary widget power changes.
3391 *
3392 * Returns 0 for success else error.
3393 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003394void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3395 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003396{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003397 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003398
Liam Girdwood3cd04342012-03-09 12:02:08 +00003399 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003400 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003401 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003402}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003403
3404/**
Charles Keepax11391102014-02-18 15:22:14 +00003405 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3406 * @dapm: DAPM context
3407 * @pin: pin name
3408 *
3409 * Enables input/output pin and its parents or children widgets iff there is
3410 * a valid audio route and active audio stream.
3411 *
3412 * Requires external locking.
3413 *
3414 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3415 * do any widget power switching.
3416 */
3417int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3418 const char *pin)
3419{
3420 return snd_soc_dapm_set_pin(dapm, pin, 1);
3421}
3422EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3423
3424/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003425 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003426 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003427 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003428 *
Mark Brown74b8f952009-06-06 11:26:15 +01003429 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003430 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003431 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003432 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3433 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003434 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003435int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003436{
Charles Keepax11391102014-02-18 15:22:14 +00003437 int ret;
3438
3439 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3440
3441 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3442
3443 mutex_unlock(&dapm->card->dapm_mutex);
3444
3445 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003446}
Liam Girdwooda5302182008-07-07 13:35:17 +01003447EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003448
3449/**
Charles Keepax11391102014-02-18 15:22:14 +00003450 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3451 * @dapm: DAPM context
3452 * @pin: pin name
3453 *
3454 * Enables input/output pin regardless of any other state. This is
3455 * intended for use with microphone bias supplies used in microphone
3456 * jack detection.
3457 *
3458 * Requires external locking.
3459 *
3460 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3461 * do any widget power switching.
3462 */
3463int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3464 const char *pin)
3465{
3466 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3467
3468 if (!w) {
3469 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3470 return -EINVAL;
3471 }
3472
3473 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3474 w->connected = 1;
3475 w->force = 1;
3476 dapm_mark_dirty(w, "force enable");
3477
3478 return 0;
3479}
3480EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3481
3482/**
Mark Brownda341832010-03-15 19:23:37 +00003483 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003484 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003485 * @pin: pin name
3486 *
3487 * Enables input/output pin regardless of any other state. This is
3488 * intended for use with microphone bias supplies used in microphone
3489 * jack detection.
3490 *
3491 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3492 * do any widget power switching.
3493 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003494int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3495 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003496{
Charles Keepax11391102014-02-18 15:22:14 +00003497 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003498
Charles Keepax11391102014-02-18 15:22:14 +00003499 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003500
Charles Keepax11391102014-02-18 15:22:14 +00003501 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003502
Charles Keepax11391102014-02-18 15:22:14 +00003503 mutex_unlock(&dapm->card->dapm_mutex);
3504
3505 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003506}
3507EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3508
3509/**
Charles Keepax11391102014-02-18 15:22:14 +00003510 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3511 * @dapm: DAPM context
3512 * @pin: pin name
3513 *
3514 * Disables input/output pin and its parents or children widgets.
3515 *
3516 * Requires external locking.
3517 *
3518 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3519 * do any widget power switching.
3520 */
3521int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3522 const char *pin)
3523{
3524 return snd_soc_dapm_set_pin(dapm, pin, 0);
3525}
3526EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3527
3528/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003529 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003530 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003531 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003532 *
Mark Brown74b8f952009-06-06 11:26:15 +01003533 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003534 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003535 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3536 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003537 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003538int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3539 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003540{
Charles Keepax11391102014-02-18 15:22:14 +00003541 int ret;
3542
3543 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3544
3545 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3546
3547 mutex_unlock(&dapm->card->dapm_mutex);
3548
3549 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003550}
3551EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3552
3553/**
Charles Keepax11391102014-02-18 15:22:14 +00003554 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3555 * @dapm: DAPM context
3556 * @pin: pin name
3557 *
3558 * Marks the specified pin as being not connected, disabling it along
3559 * any parent or child widgets. At present this is identical to
3560 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3561 * additional things such as disabling controls which only affect
3562 * paths through the pin.
3563 *
3564 * Requires external locking.
3565 *
3566 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3567 * do any widget power switching.
3568 */
3569int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3570 const char *pin)
3571{
3572 return snd_soc_dapm_set_pin(dapm, pin, 0);
3573}
3574EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3575
3576/**
Mark Brown5817b522008-09-24 11:23:11 +01003577 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003578 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003579 * @pin: pin name
3580 *
3581 * Marks the specified pin as being not connected, disabling it along
3582 * any parent or child widgets. At present this is identical to
3583 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3584 * additional things such as disabling controls which only affect
3585 * paths through the pin.
3586 *
3587 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3588 * do any widget power switching.
3589 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003590int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003591{
Charles Keepax11391102014-02-18 15:22:14 +00003592 int ret;
3593
3594 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3595
3596 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3597
3598 mutex_unlock(&dapm->card->dapm_mutex);
3599
3600 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003601}
3602EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3603
3604/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003605 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003606 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003607 * @pin: audio signal pin endpoint (or start point)
3608 *
3609 * Get audio pin status - connected or disconnected.
3610 *
3611 * Returns 1 for connected otherwise 0.
3612 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003613int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3614 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003615{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003616 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003617
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003618 if (w)
3619 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003620
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003621 return 0;
3622}
Liam Girdwooda5302182008-07-07 13:35:17 +01003623EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003624
3625/**
Mark Brown1547aba2010-05-07 21:11:40 +01003626 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003627 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003628 * @pin: audio signal pin endpoint (or start point)
3629 *
3630 * Mark the given endpoint or pin as ignoring suspend. When the
3631 * system is disabled a path between two endpoints flagged as ignoring
3632 * suspend will not be disabled. The path must already be enabled via
3633 * normal means at suspend time, it will not be turned on if it was not
3634 * already enabled.
3635 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003636int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3637 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003638{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003639 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003640
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003641 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003642 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003643 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003644 }
3645
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003646 w->ignore_suspend = 1;
3647
3648 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003649}
3650EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3651
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02003652/**
3653 * dapm_is_external_path() - Checks if a path is a external path
3654 * @card: The card the path belongs to
3655 * @path: The path to check
3656 *
3657 * Returns true if the path is either between two different DAPM contexts or
3658 * between two external pins of the same DAPM context. Otherwise returns
3659 * false.
3660 */
3661static bool dapm_is_external_path(struct snd_soc_card *card,
3662 struct snd_soc_dapm_path *path)
3663{
3664 dev_dbg(card->dev,
3665 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3666 path->source->name, path->source->id, path->source->dapm,
3667 path->sink->name, path->sink->id, path->sink->dapm);
3668
3669 /* Connection between two different DAPM contexts */
3670 if (path->source->dapm != path->sink->dapm)
3671 return true;
3672
3673 /* Loopback connection from external pin to external pin */
3674 if (path->sink->id == snd_soc_dapm_input) {
3675 switch (path->source->id) {
3676 case snd_soc_dapm_output:
3677 case snd_soc_dapm_micbias:
3678 return true;
3679 default:
3680 break;
3681 }
3682 }
3683
3684 return false;
3685}
3686
Stephen Warren16332812011-11-23 12:42:04 -07003687static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3688 struct snd_soc_dapm_widget *w)
3689{
3690 struct snd_soc_dapm_path *p;
3691
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02003692 list_for_each_entry(p, &w->sources, list_sink) {
3693 if (dapm_is_external_path(card, p))
3694 return true;
3695 }
Stephen Warren16332812011-11-23 12:42:04 -07003696
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02003697 list_for_each_entry(p, &w->sinks, list_source) {
3698 if (dapm_is_external_path(card, p))
3699 return true;
Stephen Warren16332812011-11-23 12:42:04 -07003700 }
3701
3702 return false;
3703}
3704
3705/**
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003706 * snd_soc_dapm_auto_nc_pins - call snd_soc_dapm_nc_pin for unused pins
3707 * @card: The card whose pins should be processed
Stephen Warren16332812011-11-23 12:42:04 -07003708 *
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003709 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the card
3710 * which are unused. Pins are used if they are connected externally to a
3711 * component, whether that be to some other device, or a loop-back connection to
3712 * the component itself.
Stephen Warren16332812011-11-23 12:42:04 -07003713 */
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003714void snd_soc_dapm_auto_nc_pins(struct snd_soc_card *card)
Stephen Warren16332812011-11-23 12:42:04 -07003715{
Stephen Warren16332812011-11-23 12:42:04 -07003716 struct snd_soc_dapm_widget *w;
3717
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003718 dev_dbg(card->dev, "ASoC: Auto NC: DAPMs: card:%p\n", &card->dapm);
Stephen Warren16332812011-11-23 12:42:04 -07003719
3720 list_for_each_entry(w, &card->widgets, list) {
Stephen Warren16332812011-11-23 12:42:04 -07003721 switch (w->id) {
3722 case snd_soc_dapm_input:
3723 case snd_soc_dapm_output:
3724 case snd_soc_dapm_micbias:
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003725 dev_dbg(card->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003726 w->name);
3727 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003728 dev_dbg(card->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003729 "... Not in map; disabling\n");
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003730 snd_soc_dapm_nc_pin(w->dapm, w->name);
Stephen Warren16332812011-11-23 12:42:04 -07003731 }
3732 break;
3733 default:
3734 break;
3735 }
3736 }
3737}
3738
Mark Brown1547aba2010-05-07 21:11:40 +01003739/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003740 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003741 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003742 *
3743 * Free all dapm widgets and resources.
3744 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003745void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003746{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003747 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003748 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003749 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003750 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003751}
3752EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3753
Xiang Xiao57996352014-03-02 00:04:02 +08003754static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003755{
Liam Girdwood01005a72012-07-06 16:57:05 +01003756 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003757 struct snd_soc_dapm_widget *w;
3758 LIST_HEAD(down_list);
3759 int powerdown = 0;
3760
Liam Girdwood01005a72012-07-06 16:57:05 +01003761 mutex_lock(&card->dapm_mutex);
3762
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003763 list_for_each_entry(w, &dapm->card->widgets, list) {
3764 if (w->dapm != dapm)
3765 continue;
Mark Brown51737472009-06-22 13:16:51 +01003766 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003767 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003768 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003769 powerdown = 1;
3770 }
3771 }
3772
3773 /* If there were no widgets to power down we're already in
3774 * standby.
3775 */
3776 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003777 if (dapm->bias_level == SND_SOC_BIAS_ON)
3778 snd_soc_dapm_set_bias_level(dapm,
3779 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003780 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003781 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3782 snd_soc_dapm_set_bias_level(dapm,
3783 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003784 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003785
3786 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003787}
Mark Brown51737472009-06-22 13:16:51 +01003788
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003789/*
3790 * snd_soc_dapm_shutdown - callback for system shutdown
3791 */
3792void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3793{
Xiang Xiao57996352014-03-02 00:04:02 +08003794 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003795
Xiang Xiao57996352014-03-02 00:04:02 +08003796 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08003797 if (dapm != &card->dapm) {
3798 soc_dapm_shutdown_dapm(dapm);
3799 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3800 snd_soc_dapm_set_bias_level(dapm,
3801 SND_SOC_BIAS_OFF);
3802 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003803 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08003804
3805 soc_dapm_shutdown_dapm(&card->dapm);
3806 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3807 snd_soc_dapm_set_bias_level(&card->dapm,
3808 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01003809}
3810
Richard Purdie2b97eab2006-10-06 18:32:18 +02003811/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003812MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003813MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3814MODULE_LICENSE("GPL");