blob: 28269f219e1680654bdb50c51b94261efcf902f9 [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,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200472 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
473 struct snd_soc_dapm_path *path, const char *control_name,
474 const struct snd_kcontrol_new *kcontrol)
475{
476 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100477 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200478 int i;
479
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100480 if (e->reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200481 soc_dapm_read(dapm, e->reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100482 val = (val >> e->shift_l) & e->mask;
483 item = snd_soc_enum_val_to_item(e, val);
484 } else {
485 /* since a virtual mux has no backing registers to
486 * decide which path to connect, it will try to match
487 * with the first enumeration. This is to ensure
488 * that the default mux choice (the first) will be
489 * correctly powered up during initialization.
490 */
491 item = 0;
492 }
493
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100494 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200495 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200496 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200497 list_add(&path->list_sink, &dest->sources);
498 list_add(&path->list_source, &src->sinks);
Rasmus Villemoes98ad73c2014-10-21 17:01:15 +0200499 path->name = e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100500 if (i == item)
501 path->connect = 1;
502 else
503 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200504 return 0;
505 }
506 }
507
508 return -ENODEV;
509}
510
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100511/* set up initial codec paths */
512static void dapm_set_mixer_path_status(struct snd_soc_dapm_widget *w,
513 struct snd_soc_dapm_path *p, int i)
514{
515 struct soc_mixer_control *mc = (struct soc_mixer_control *)
516 w->kcontrol_news[i].private_value;
517 unsigned int reg = mc->reg;
518 unsigned int shift = mc->shift;
519 unsigned int max = mc->max;
520 unsigned int mask = (1 << fls(max)) - 1;
521 unsigned int invert = mc->invert;
522 unsigned int val;
523
524 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200525 soc_dapm_read(w->dapm, reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100526 val = (val >> shift) & mask;
527 if (invert)
528 val = max - val;
529 p->connect = !!val;
530 } else {
531 p->connect = 0;
532 }
533}
534
Mark Brown74b8f952009-06-06 11:26:15 +0100535/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200536static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200537 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
538 struct snd_soc_dapm_path *path, const char *control_name)
539{
540 int i;
541
542 /* search for mixer kcontrol */
543 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600544 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200545 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200546 list_add(&path->list_sink, &dest->sources);
547 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600548 path->name = dest->kcontrol_news[i].name;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100549 dapm_set_mixer_path_status(dest, path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200550 return 0;
551 }
552 }
553 return -ENODEV;
554}
555
Stephen Warrenaf468002011-04-28 17:38:01 -0600556static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600557 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600558 const struct snd_kcontrol_new *kcontrol_new,
559 struct snd_kcontrol **kcontrol)
560{
561 struct snd_soc_dapm_widget *w;
562 int i;
563
564 *kcontrol = NULL;
565
566 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600567 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
568 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600569 for (i = 0; i < w->num_kcontrols; i++) {
570 if (&w->kcontrol_news[i] == kcontrol_new) {
571 if (w->kcontrols)
572 *kcontrol = w->kcontrols[i];
573 return 1;
574 }
575 }
576 }
577
578 return 0;
579}
580
Stephen Warren85762e72013-03-29 15:40:10 -0600581/*
582 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
583 * create it. Either way, add the widget into the control's widget list
584 */
585static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100586 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200587{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200588 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000589 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000590 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600591 size_t prefix_len;
592 int shared;
593 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600594 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200595 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600596 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200597 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000598
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200599 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000600 if (prefix)
601 prefix_len = strlen(prefix) + 1;
602 else
603 prefix_len = 0;
604
Stephen Warren85762e72013-03-29 15:40:10 -0600605 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
606 &kcontrol);
607
Stephen Warren85762e72013-03-29 15:40:10 -0600608 if (!kcontrol) {
609 if (shared) {
610 wname_in_long_name = false;
611 kcname_in_long_name = true;
612 } else {
613 switch (w->id) {
614 case snd_soc_dapm_switch:
615 case snd_soc_dapm_mixer:
616 wname_in_long_name = true;
617 kcname_in_long_name = true;
618 break;
619 case snd_soc_dapm_mixer_named_ctl:
620 wname_in_long_name = false;
621 kcname_in_long_name = true;
622 break;
623 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600624 wname_in_long_name = true;
625 kcname_in_long_name = false;
626 break;
627 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600628 return -EINVAL;
629 }
630 }
631
632 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600633 /*
634 * The control will get a prefix from the control
635 * creation process but we're also using the same
636 * prefix for widgets so cut the prefix off the
637 * front of the widget name.
638 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200639 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600640 w->name + prefix_len,
641 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200642 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200643 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600644
645 name = long_name;
646 } else if (wname_in_long_name) {
647 long_name = NULL;
648 name = w->name + prefix_len;
649 } else {
650 long_name = NULL;
651 name = w->kcontrol_news[kci].name;
652 }
653
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200654 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600655 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200656 if (!kcontrol) {
657 ret = -ENOMEM;
658 goto exit_free;
659 }
660
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200661 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200662
663 ret = dapm_kcontrol_data_alloc(w, kcontrol);
664 if (ret) {
665 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200666 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200667 }
668
Stephen Warren85762e72013-03-29 15:40:10 -0600669 ret = snd_ctl_add(card, kcontrol);
670 if (ret < 0) {
671 dev_err(dapm->dev,
672 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
673 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200674 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600675 }
Stephen Warren85762e72013-03-29 15:40:10 -0600676 }
677
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200678 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200679 if (ret == 0)
680 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200681
Daniel Macke5092c92014-10-07 13:41:24 +0200682exit_free:
683 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600684
Daniel Macke5092c92014-10-07 13:41:24 +0200685 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600686}
687
688/* create new dapm mixer control */
689static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
690{
691 int i, ret;
692 struct snd_soc_dapm_path *path;
693
Richard Purdie2b97eab2006-10-06 18:32:18 +0200694 /* add kcontrol */
695 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200696 /* match name */
697 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200698 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600699 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200700 continue;
701
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200702 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200703 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200704 continue;
705 }
706
Mark Brown946d92a2013-08-12 23:28:42 +0100707 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600708 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200709 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100710
711 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200712 }
713 }
Stephen Warren85762e72013-03-29 15:40:10 -0600714
715 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200716}
717
718/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200719static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200720{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200721 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600722 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600723 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200724
Stephen Warrenaf468002011-04-28 17:38:01 -0600725 if (w->num_kcontrols != 1) {
726 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000727 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600728 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200729 return -EINVAL;
730 }
731
Lars-Peter Clausenfe581392013-08-01 18:30:38 +0200732 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600733 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
734 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600735 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200736
Mark Brown946d92a2013-08-12 23:28:42 +0100737 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600738 if (ret < 0)
739 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600740
Richard Purdie2b97eab2006-10-06 18:32:18 +0200741 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200742 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200743
Stephen Warrenaf468002011-04-28 17:38:01 -0600744 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200745}
746
747/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200748static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200749{
Mark Browna6c65732010-03-03 17:45:21 +0000750 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200751 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000752 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200753
Mark Browna6c65732010-03-03 17:45:21 +0000754 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200755}
756
757/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100758static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
759 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200760{
761 struct snd_soc_dapm_path *p;
762
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100763 list_for_each_entry(p, sink, list_source) {
764 if (p->walked) {
765 p->walked = 0;
766 dapm_clear_walk_output(dapm, &p->sink->sinks);
767 }
768 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200769}
770
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100771static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
772 struct list_head *source)
773{
774 struct snd_soc_dapm_path *p;
775
776 list_for_each_entry(p, source, list_sink) {
777 if (p->walked) {
778 p->walked = 0;
779 dapm_clear_walk_input(dapm, &p->source->sources);
780 }
781 }
782}
783
784
Mark Brown99497882010-05-07 20:24:05 +0100785/* We implement power down on suspend by checking the power state of
786 * the ALSA card - when we are suspending the ALSA state for the card
787 * is set to D3.
788 */
789static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
790{
Mark Brown12ea2c72011-03-02 18:17:32 +0000791 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100792
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000793 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100794 case SNDRV_CTL_POWER_D3hot:
795 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100796 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000797 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200798 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100799 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100800 default:
801 return 1;
802 }
803}
804
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100805/* add widget to list if it's not already in the list */
806static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
807 struct snd_soc_dapm_widget *w)
808{
809 struct snd_soc_dapm_widget_list *wlist;
810 int wlistsize, wlistentries, i;
811
812 if (*list == NULL)
813 return -EINVAL;
814
815 wlist = *list;
816
817 /* is this widget already in the list */
818 for (i = 0; i < wlist->num_widgets; i++) {
819 if (wlist->widgets[i] == w)
820 return 0;
821 }
822
823 /* allocate some new space */
824 wlistentries = wlist->num_widgets + 1;
825 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
826 wlistentries * sizeof(struct snd_soc_dapm_widget *);
827 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
828 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000829 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100830 w->name);
831 return -ENOMEM;
832 }
833 wlist = *list;
834
835 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000836 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100837 w->name, wlist->num_widgets);
838
839 wlist->widgets[wlist->num_widgets] = w;
840 wlist->num_widgets++;
841 return 1;
842}
843
Richard Purdie2b97eab2006-10-06 18:32:18 +0200844/*
845 * Recursively check for a completed path to an active or physically connected
846 * output widget. Returns number of complete paths.
847 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100848static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
849 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200850{
851 struct snd_soc_dapm_path *path;
852 int con = 0;
853
Mark Brown024dc072011-10-09 11:52:05 +0100854 if (widget->outputs >= 0)
855 return widget->outputs;
856
Mark Brownde02d072011-09-20 21:43:24 +0100857 DAPM_UPDATE_STAT(widget, path_checks);
858
Mark Brown62ea8742012-01-21 21:14:48 +0000859 switch (widget->id) {
860 case snd_soc_dapm_supply:
861 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200862 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200863 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100864 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000865 default:
866 break;
867 }
Mark Brown246d0a12009-04-22 18:24:55 +0100868
Mark Brown010ff262009-08-17 17:39:22 +0100869 switch (widget->id) {
870 case snd_soc_dapm_adc:
871 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100872 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100873 if (widget->active) {
874 widget->outputs = snd_soc_dapm_suspend_check(widget);
875 return widget->outputs;
876 }
Mark Brown010ff262009-08-17 17:39:22 +0100877 default:
878 break;
879 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200880
881 if (widget->connected) {
882 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100883 if (widget->id == snd_soc_dapm_output && !widget->ext) {
884 widget->outputs = snd_soc_dapm_suspend_check(widget);
885 return widget->outputs;
886 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200887
888 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100889 if (widget->id == snd_soc_dapm_hp ||
890 widget->id == snd_soc_dapm_spk ||
891 (widget->id == snd_soc_dapm_line &&
892 !list_empty(&widget->sources))) {
893 widget->outputs = snd_soc_dapm_suspend_check(widget);
894 return widget->outputs;
895 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200896 }
897
898 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100899 DAPM_UPDATE_STAT(widget, neighbour_checks);
900
Mark Brownbf3a9e12011-06-13 16:42:29 +0100901 if (path->weak)
902 continue;
903
Mark Brown8af294b2013-02-22 17:48:15 +0000904 if (path->walking)
905 return 1;
906
Richard Purdie2b97eab2006-10-06 18:32:18 +0200907 if (path->walked)
908 continue;
909
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100910 trace_snd_soc_dapm_output_path(widget, path);
911
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +0200912 if (path->connect) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200913 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000914 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100915
916 /* do we need to add this widget to the list ? */
917 if (list) {
918 int err;
919 err = dapm_list_add_widget(list, path->sink);
920 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000921 dev_err(widget->dapm->dev,
922 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100923 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000924 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100925 return con;
926 }
927 }
928
929 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000930
931 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200932 }
933 }
934
Mark Brown024dc072011-10-09 11:52:05 +0100935 widget->outputs = con;
936
Richard Purdie2b97eab2006-10-06 18:32:18 +0200937 return con;
938}
939
940/*
941 * Recursively check for a completed path to an active or physically connected
942 * input widget. Returns number of complete paths.
943 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100944static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
945 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200946{
947 struct snd_soc_dapm_path *path;
948 int con = 0;
949
Mark Brown024dc072011-10-09 11:52:05 +0100950 if (widget->inputs >= 0)
951 return widget->inputs;
952
Mark Brownde02d072011-09-20 21:43:24 +0100953 DAPM_UPDATE_STAT(widget, path_checks);
954
Mark Brown62ea8742012-01-21 21:14:48 +0000955 switch (widget->id) {
956 case snd_soc_dapm_supply:
957 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200958 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200959 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100960 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000961 default:
962 break;
963 }
Mark Brown246d0a12009-04-22 18:24:55 +0100964
Richard Purdie2b97eab2006-10-06 18:32:18 +0200965 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100966 switch (widget->id) {
967 case snd_soc_dapm_dac:
968 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +0100969 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +0100970 if (widget->active) {
971 widget->inputs = snd_soc_dapm_suspend_check(widget);
972 return widget->inputs;
973 }
Mark Brown010ff262009-08-17 17:39:22 +0100974 default:
975 break;
976 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200977
978 if (widget->connected) {
979 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100980 if (widget->id == snd_soc_dapm_input && !widget->ext) {
981 widget->inputs = snd_soc_dapm_suspend_check(widget);
982 return widget->inputs;
983 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200984
985 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100986 if (widget->id == snd_soc_dapm_vmid) {
987 widget->inputs = snd_soc_dapm_suspend_check(widget);
988 return widget->inputs;
989 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200990
991 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300992 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100993 (widget->id == snd_soc_dapm_line &&
994 !list_empty(&widget->sinks))) {
995 widget->inputs = snd_soc_dapm_suspend_check(widget);
996 return widget->inputs;
997 }
998
Mark Brown1ab97c82011-11-27 16:21:51 +0000999 /* signal generator */
1000 if (widget->id == snd_soc_dapm_siggen) {
1001 widget->inputs = snd_soc_dapm_suspend_check(widget);
1002 return widget->inputs;
1003 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001004 }
1005
1006 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +01001007 DAPM_UPDATE_STAT(widget, neighbour_checks);
1008
Mark Brownbf3a9e12011-06-13 16:42:29 +01001009 if (path->weak)
1010 continue;
1011
Mark Brown8af294b2013-02-22 17:48:15 +00001012 if (path->walking)
1013 return 1;
1014
Richard Purdie2b97eab2006-10-06 18:32:18 +02001015 if (path->walked)
1016 continue;
1017
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001018 trace_snd_soc_dapm_input_path(widget, path);
1019
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001020 if (path->connect) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001021 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001022 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001023
1024 /* do we need to add this widget to the list ? */
1025 if (list) {
1026 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001027 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001028 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001029 dev_err(widget->dapm->dev,
1030 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001031 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001032 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001033 return con;
1034 }
1035 }
1036
1037 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001038
1039 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001040 }
1041 }
1042
Mark Brown024dc072011-10-09 11:52:05 +01001043 widget->inputs = con;
1044
Richard Purdie2b97eab2006-10-06 18:32:18 +02001045 return con;
1046}
1047
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001048/**
1049 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1050 * @dai: the soc DAI.
1051 * @stream: stream direction.
1052 * @list: list of active widgets for this stream.
1053 *
1054 * Queries DAPM graph as to whether an valid audio stream path exists for
1055 * the initial stream specified by name. This takes into account
1056 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1057 *
1058 * Returns the number of valid paths or negative error.
1059 */
1060int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1061 struct snd_soc_dapm_widget_list **list)
1062{
1063 struct snd_soc_card *card = dai->card;
1064 int paths;
1065
1066 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1067 dapm_reset(card);
1068
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001069 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001070 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001071 dapm_clear_walk_output(&card->dapm,
1072 &dai->playback_widget->sinks);
1073 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001074 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001075 dapm_clear_walk_input(&card->dapm,
1076 &dai->capture_widget->sources);
1077 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001078
1079 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001080 mutex_unlock(&card->dapm_mutex);
1081
1082 return paths;
1083}
1084
Richard Purdie2b97eab2006-10-06 18:32:18 +02001085/*
Mark Brown62ea8742012-01-21 21:14:48 +00001086 * Handler for regulator supply widget.
1087 */
1088int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1089 struct snd_kcontrol *kcontrol, int event)
1090{
Mark Brownc05b84d2012-09-07 12:57:11 +08001091 int ret;
1092
Mark Browneb270e92013-10-09 13:52:52 +01001093 soc_dapm_async_complete(w->dapm);
1094
Mark Brownc05b84d2012-09-07 12:57:11 +08001095 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001096 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001097 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001098 if (ret != 0)
1099 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001100 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001101 w->name, ret);
1102 }
1103
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001104 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001105 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001106 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001107 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001108 if (ret != 0)
1109 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001110 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001111 w->name, ret);
1112 }
1113
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001114 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001115 }
Mark Brown62ea8742012-01-21 21:14:48 +00001116}
1117EXPORT_SYMBOL_GPL(dapm_regulator_event);
1118
Ola Liljad7e7eb92012-05-24 15:26:25 +02001119/*
1120 * Handler for clock supply widget.
1121 */
1122int dapm_clock_event(struct snd_soc_dapm_widget *w,
1123 struct snd_kcontrol *kcontrol, int event)
1124{
1125 if (!w->clk)
1126 return -EIO;
1127
Mark Browneb270e92013-10-09 13:52:52 +01001128 soc_dapm_async_complete(w->dapm);
1129
Mark Brownec029952012-06-04 08:16:20 +01001130#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001131 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001132 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001133 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001134 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001135 return 0;
1136 }
Mark Brownec029952012-06-04 08:16:20 +01001137#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001138 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001139}
1140EXPORT_SYMBOL_GPL(dapm_clock_event);
1141
Mark Brownd8050022011-09-28 18:28:23 +01001142static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1143{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001144 if (w->power_checked)
1145 return w->new_power;
1146
Mark Brownd8050022011-09-28 18:28:23 +01001147 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001148 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001149 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001150 w->new_power = w->power_check(w);
1151
1152 w->power_checked = true;
1153
1154 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001155}
1156
Mark Browncd0f2d42009-04-20 16:56:59 +01001157/* Generic check to see if a widget should be powered.
1158 */
1159static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1160{
1161 int in, out;
1162
Mark Brownde02d072011-09-20 21:43:24 +01001163 DAPM_UPDATE_STAT(w, power_checks);
1164
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001165 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001166 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001167 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001168 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001169 return out != 0 && in != 0;
1170}
1171
Mark Brown6ea31b92009-04-20 17:15:41 +01001172/* Check to see if an ADC has power */
1173static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1174{
1175 int in;
1176
Mark Brownde02d072011-09-20 21:43:24 +01001177 DAPM_UPDATE_STAT(w, power_checks);
1178
Mark Brown6ea31b92009-04-20 17:15:41 +01001179 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001180 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001181 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001182 return in != 0;
1183 } else {
1184 return dapm_generic_check_power(w);
1185 }
1186}
1187
1188/* Check to see if a DAC has power */
1189static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1190{
1191 int out;
1192
Mark Brownde02d072011-09-20 21:43:24 +01001193 DAPM_UPDATE_STAT(w, power_checks);
1194
Mark Brown6ea31b92009-04-20 17:15:41 +01001195 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001196 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001197 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001198 return out != 0;
1199 } else {
1200 return dapm_generic_check_power(w);
1201 }
1202}
1203
Mark Brown246d0a12009-04-22 18:24:55 +01001204/* Check to see if a power supply is needed */
1205static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1206{
1207 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001208
Mark Brownde02d072011-09-20 21:43:24 +01001209 DAPM_UPDATE_STAT(w, power_checks);
1210
Mark Brown246d0a12009-04-22 18:24:55 +01001211 /* Check if one of our outputs is connected */
1212 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001213 DAPM_UPDATE_STAT(w, neighbour_checks);
1214
Mark Brownbf3a9e12011-06-13 16:42:29 +01001215 if (path->weak)
1216 continue;
1217
Mark Brown215edda2009-09-08 18:59:05 +01001218 if (path->connected &&
1219 !path->connected(path->source, path->sink))
1220 continue;
1221
Mark Brownf68d7e12011-10-04 22:57:50 +01001222 if (dapm_widget_power_check(path->sink))
1223 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001224 }
1225
Mark Brownf68d7e12011-10-04 22:57:50 +01001226 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001227}
1228
Mark Brown35c64bc2011-09-28 18:23:53 +01001229static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1230{
1231 return 1;
1232}
1233
Mark Brown38357ab2009-06-06 19:03:23 +01001234static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1235 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001236 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001237{
Mark Brown828a8422011-01-15 13:14:30 +00001238 int *sort;
1239
1240 if (power_up)
1241 sort = dapm_up_seq;
1242 else
1243 sort = dapm_down_seq;
1244
Mark Brown38357ab2009-06-06 19:03:23 +01001245 if (sort[a->id] != sort[b->id])
1246 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001247 if (a->subseq != b->subseq) {
1248 if (power_up)
1249 return a->subseq - b->subseq;
1250 else
1251 return b->subseq - a->subseq;
1252 }
Mark Brownb22ead22009-06-07 12:51:26 +01001253 if (a->reg != b->reg)
1254 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001255 if (a->dapm != b->dapm)
1256 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001257
Mark Brown38357ab2009-06-06 19:03:23 +01001258 return 0;
1259}
Mark Brown42aa3412009-03-01 19:21:10 +00001260
Mark Brown38357ab2009-06-06 19:03:23 +01001261/* Insert a widget in order into a DAPM power sequence. */
1262static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1263 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001264 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001265{
1266 struct snd_soc_dapm_widget *w;
1267
1268 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001269 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001270 list_add_tail(&new_widget->power_list, &w->power_list);
1271 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001272 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001273
Mark Brown38357ab2009-06-06 19:03:23 +01001274 list_add_tail(&new_widget->power_list, list);
1275}
Mark Brown42aa3412009-03-01 19:21:10 +00001276
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001277static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001278 struct snd_soc_dapm_widget *w, int event)
1279{
Mark Brown68f89ad2010-11-03 23:51:49 -04001280 const char *ev_name;
1281 int power, ret;
1282
1283 switch (event) {
1284 case SND_SOC_DAPM_PRE_PMU:
1285 ev_name = "PRE_PMU";
1286 power = 1;
1287 break;
1288 case SND_SOC_DAPM_POST_PMU:
1289 ev_name = "POST_PMU";
1290 power = 1;
1291 break;
1292 case SND_SOC_DAPM_PRE_PMD:
1293 ev_name = "PRE_PMD";
1294 power = 0;
1295 break;
1296 case SND_SOC_DAPM_POST_PMD:
1297 ev_name = "POST_PMD";
1298 power = 0;
1299 break;
Mark Brown80114122013-02-25 15:14:19 +00001300 case SND_SOC_DAPM_WILL_PMU:
1301 ev_name = "WILL_PMU";
1302 power = 1;
1303 break;
1304 case SND_SOC_DAPM_WILL_PMD:
1305 ev_name = "WILL_PMD";
1306 power = 0;
1307 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001308 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001309 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001310 return;
1311 }
1312
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001313 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001314 return;
1315
1316 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001317 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001318 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001319 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001320 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001321 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001322 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001323 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001324 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001325 ev_name, w->name, ret);
1326 }
1327}
1328
Mark Brownb22ead22009-06-07 12:51:26 +01001329/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001330static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001331 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001332{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001333 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001334 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001335 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001336 unsigned int value = 0;
1337 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001338
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001339 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1340 reg = w->reg;
1341 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001342
1343 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001344 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001345 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001346
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001347 mask |= w->mask << w->shift;
1348 if (w->power)
1349 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001350 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001351 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001352
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001353 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001354 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1355 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001356
Mark Brown68f89ad2010-11-03 23:51:49 -04001357 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001358 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1359 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001360 }
1361
Mark Brown81628102009-06-07 13:21:24 +01001362 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001363 /* Any widget will do, they should all be updating the
1364 * same register.
1365 */
Mark Brown29376bc2011-06-19 13:49:28 +01001366
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001367 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001368 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001369 value, mask, reg, card->pop_time);
1370 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001371 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001372 }
1373
1374 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001375 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1376 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001377 }
Mark Brown42aa3412009-03-01 19:21:10 +00001378}
1379
Mark Brownb22ead22009-06-07 12:51:26 +01001380/* Apply a DAPM power sequence.
1381 *
1382 * We walk over a pre-sorted list of widgets to apply power to. In
1383 * order to minimise the number of writes to the device required
1384 * multiple widgets will be updated in a single write where possible.
1385 * Currently anything that requires more than a single write is not
1386 * handled.
1387 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001388static void dapm_seq_run(struct snd_soc_card *card,
1389 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001390{
1391 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001392 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001393 LIST_HEAD(pending);
1394 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001395 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001396 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001397 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001398 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001399 int *sort;
1400
1401 if (power_up)
1402 sort = dapm_up_seq;
1403 else
1404 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001405
Mark Brownb22ead22009-06-07 12:51:26 +01001406 list_for_each_entry_safe(w, n, list, power_list) {
1407 ret = 0;
1408
1409 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001410 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001411 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001412 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001413 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001414
Mark Brown474b62d2011-01-18 16:14:44 +00001415 if (cur_dapm && cur_dapm->seq_notifier) {
1416 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1417 if (sort[i] == cur_sort)
1418 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001419 i,
1420 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001421 }
1422
Mark Browneb270e92013-10-09 13:52:52 +01001423 if (cur_dapm && w->dapm != cur_dapm)
1424 soc_dapm_async_complete(cur_dapm);
1425
Mark Brownb22ead22009-06-07 12:51:26 +01001426 INIT_LIST_HEAD(&pending);
1427 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001428 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001429 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001430 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001431 }
1432
Mark Brown163cac02009-06-07 10:12:52 +01001433 switch (w->id) {
1434 case snd_soc_dapm_pre:
1435 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001436 list_for_each_entry_safe_continue(w, n, list,
1437 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001438
Mark Brownb22ead22009-06-07 12:51:26 +01001439 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001440 ret = w->event(w,
1441 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001442 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001443 ret = w->event(w,
1444 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001445 break;
1446
1447 case snd_soc_dapm_post:
1448 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001449 list_for_each_entry_safe_continue(w, n, list,
1450 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001451
Mark Brownb22ead22009-06-07 12:51:26 +01001452 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001453 ret = w->event(w,
1454 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001455 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001456 ret = w->event(w,
1457 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001458 break;
1459
Mark Brown163cac02009-06-07 10:12:52 +01001460 default:
Mark Brown81628102009-06-07 13:21:24 +01001461 /* Queue it up for application */
1462 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001463 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001464 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001465 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001466 list_move(&w->power_list, &pending);
1467 break;
Mark Brown163cac02009-06-07 10:12:52 +01001468 }
Mark Brownb22ead22009-06-07 12:51:26 +01001469
1470 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001471 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001472 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001473 }
Mark Brownb22ead22009-06-07 12:51:26 +01001474
1475 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001476 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001477
1478 if (cur_dapm && cur_dapm->seq_notifier) {
1479 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1480 if (sort[i] == cur_sort)
1481 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001482 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001483 }
Mark Browneb270e92013-10-09 13:52:52 +01001484
1485 list_for_each_entry(d, &card->dapm_list, list) {
1486 soc_dapm_async_complete(d);
1487 }
Mark Brown163cac02009-06-07 10:12:52 +01001488}
1489
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001490static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001491{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001492 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001493 struct snd_soc_dapm_widget_list *wlist;
1494 struct snd_soc_dapm_widget *w = NULL;
1495 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001496 int ret;
1497
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001498 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001499 return;
1500
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001501 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001502
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001503 for (wi = 0; wi < wlist->num_widgets; wi++) {
1504 w = wlist->widgets[wi];
1505
1506 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1507 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1508 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001509 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001510 w->name, ret);
1511 }
Mark Brown97404f22010-12-14 16:13:57 +00001512 }
1513
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001514 if (!w)
1515 return;
1516
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001517 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1518 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001519 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001520 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001521 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001522
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001523 for (wi = 0; wi < wlist->num_widgets; wi++) {
1524 w = wlist->widgets[wi];
1525
1526 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1527 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1528 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001529 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001530 w->name, ret);
1531 }
Mark Brown97404f22010-12-14 16:13:57 +00001532 }
1533}
1534
Mark Brown9d0624a2011-02-18 11:49:43 -08001535/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1536 * they're changing state.
1537 */
1538static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1539{
1540 struct snd_soc_dapm_context *d = data;
1541 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001542
Mark Brown56fba412011-06-04 11:25:10 +01001543 /* If we're off and we're not supposed to be go into STANDBY */
1544 if (d->bias_level == SND_SOC_BIAS_OFF &&
1545 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001546 if (d->dev)
1547 pm_runtime_get_sync(d->dev);
1548
Mark Brown9d0624a2011-02-18 11:49:43 -08001549 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1550 if (ret != 0)
1551 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001552 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001553 }
1554
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001555 /* Prepare for a transition to ON or away from ON */
1556 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1557 d->bias_level != SND_SOC_BIAS_ON) ||
1558 (d->target_bias_level != SND_SOC_BIAS_ON &&
1559 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001560 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1561 if (ret != 0)
1562 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001563 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001564 }
1565}
1566
1567/* Async callback run prior to DAPM sequences - brings to their final
1568 * state.
1569 */
1570static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1571{
1572 struct snd_soc_dapm_context *d = data;
1573 int ret;
1574
1575 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001576 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1577 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1578 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001579 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1580 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001581 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001582 ret);
1583 }
1584
1585 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001586 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1587 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001588 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1589 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001590 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1591 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001592
1593 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001594 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001595 }
1596
1597 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001598 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1599 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001600 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1601 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001602 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001603 ret);
1604 }
1605}
Mark Brown97404f22010-12-14 16:13:57 +00001606
Mark Brownfe4fda52011-10-03 22:36:57 +01001607static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1608 bool power, bool connect)
1609{
1610 /* If a connection is being made or broken then that update
1611 * will have marked the peer dirty, otherwise the widgets are
1612 * not connected and this update has no impact. */
1613 if (!connect)
1614 return;
1615
1616 /* If the peer is already in the state we're moving to then we
1617 * won't have an impact on it. */
1618 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001619 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001620}
1621
Mark Brown05623c42011-09-28 17:02:31 +01001622static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1623 struct list_head *up_list,
1624 struct list_head *down_list)
1625{
Mark Browndb432b42011-10-03 21:06:40 +01001626 struct snd_soc_dapm_path *path;
1627
Mark Brown05623c42011-09-28 17:02:31 +01001628 if (w->power == power)
1629 return;
1630
1631 trace_snd_soc_dapm_widget_power(w, power);
1632
Mark Browndb432b42011-10-03 21:06:40 +01001633 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001634 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001635 */
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001636 list_for_each_entry(path, &w->sources, list_sink)
1637 dapm_widget_set_peer_power(path->source, power, path->connect);
1638
Mark Brownf3bf3e42011-10-04 22:43:31 +01001639 switch (w->id) {
1640 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001641 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001642 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001643 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001644 /* Supplies can't affect their outputs, only their inputs */
1645 break;
1646 default:
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001647 list_for_each_entry(path, &w->sinks, list_source)
1648 dapm_widget_set_peer_power(path->sink, power,
1649 path->connect);
Mark Brownf3bf3e42011-10-04 22:43:31 +01001650 break;
Mark Browndb432b42011-10-03 21:06:40 +01001651 }
1652
Mark Brown05623c42011-09-28 17:02:31 +01001653 if (power)
1654 dapm_seq_insert(w, up_list, true);
1655 else
1656 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001657}
1658
Mark Brown7c81beb2011-09-20 22:22:32 +01001659static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1660 struct list_head *up_list,
1661 struct list_head *down_list)
1662{
Mark Brown7c81beb2011-09-20 22:22:32 +01001663 int power;
1664
1665 switch (w->id) {
1666 case snd_soc_dapm_pre:
1667 dapm_seq_insert(w, down_list, false);
1668 break;
1669 case snd_soc_dapm_post:
1670 dapm_seq_insert(w, up_list, true);
1671 break;
1672
1673 default:
Mark Brownd8050022011-09-28 18:28:23 +01001674 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001675
Mark Brown05623c42011-09-28 17:02:31 +01001676 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001677 break;
1678 }
1679}
1680
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001681static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1682{
1683 if (dapm->idle_bias_off)
1684 return true;
1685
1686 switch (snd_power_get_state(dapm->card->snd_card)) {
1687 case SNDRV_CTL_POWER_D3hot:
1688 case SNDRV_CTL_POWER_D3cold:
1689 return dapm->suspend_bias_off;
1690 default:
1691 break;
1692 }
1693
1694 return false;
1695}
1696
Mark Brown42aa3412009-03-01 19:21:10 +00001697/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001698 * Scan each dapm widget for complete audio path.
1699 * A complete path is a route that has valid endpoints i.e.:-
1700 *
1701 * o DAC to output pin.
1702 * o Input Pin to ADC.
1703 * o Input pin to Output pin (bypass, sidetone)
1704 * o DAC to ADC (loopback).
1705 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001706static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001707{
1708 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001709 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001710 LIST_HEAD(up_list);
1711 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001712 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001713 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001714
Mark Brownf9fa2b12014-03-06 16:49:11 +08001715 lockdep_assert_held(&card->dapm_mutex);
1716
Mark Brown84e90932010-11-04 00:07:02 -04001717 trace_snd_soc_dapm_start(card);
1718
Mark Brown56fba412011-06-04 11:25:10 +01001719 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001720 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001721 d->target_bias_level = SND_SOC_BIAS_OFF;
1722 else
1723 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001724 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001725
Liam Girdwood6c120e12012-02-15 15:15:34 +00001726 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001727
Mark Brown6d3ddc82009-05-16 17:47:29 +01001728 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001729 * lists indicating if they should be powered up or down. We
1730 * only check widgets that have been flagged as dirty but note
1731 * that new widgets may be added to the dirty list while we
1732 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001733 */
Mark Browndb432b42011-10-03 21:06:40 +01001734 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001735 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001736 }
1737
Mark Brownf9de6d72011-09-28 17:19:47 +01001738 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001739 switch (w->id) {
1740 case snd_soc_dapm_pre:
1741 case snd_soc_dapm_post:
1742 /* These widgets always need to be powered */
1743 break;
1744 default:
1745 list_del_init(&w->dirty);
1746 break;
1747 }
Mark Browndb432b42011-10-03 21:06:40 +01001748
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001749 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001750 d = w->dapm;
1751
1752 /* Supplies and micbiases only bring the
1753 * context up to STANDBY as unless something
1754 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001755 * generally don't require full power. Signal
1756 * generators are virtual pins and have no
1757 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001758 */
1759 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001760 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001761 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001762 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001763 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001764 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001765 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001766 case snd_soc_dapm_micbias:
1767 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1768 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1769 break;
1770 default:
1771 d->target_bias_level = SND_SOC_BIAS_ON;
1772 break;
1773 }
1774 }
1775
1776 }
1777
Mark Brown85a843c2011-09-21 21:29:47 +01001778 /* Force all contexts in the card to the same bias state if
1779 * they're not ground referenced.
1780 */
Mark Brown56fba412011-06-04 11:25:10 +01001781 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001782 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001783 if (d->target_bias_level > bias)
1784 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001785 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001786 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01001787 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001788
Mark Brownde02d072011-09-20 21:43:24 +01001789 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001790
Xiang Xiao17282ba2014-03-02 00:04:03 +08001791 /* Run card bias changes at first */
1792 dapm_pre_sequence_async(&card->dapm, 0);
1793 /* Run other bias changes in parallel */
1794 list_for_each_entry(d, &card->dapm_list, list) {
1795 if (d != &card->dapm)
1796 async_schedule_domain(dapm_pre_sequence_async, d,
1797 &async_domain);
1798 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001799 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001800
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001801 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001802 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001803 }
1804
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001805 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001806 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001807 }
1808
Mark Brown6d3ddc82009-05-16 17:47:29 +01001809 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001810 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001811
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001812 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001813
Mark Brown6d3ddc82009-05-16 17:47:29 +01001814 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001815 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001816
Mark Brown9d0624a2011-02-18 11:49:43 -08001817 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001818 list_for_each_entry(d, &card->dapm_list, list) {
1819 if (d != &card->dapm)
1820 async_schedule_domain(dapm_post_sequence_async, d,
1821 &async_domain);
1822 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001823 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001824 /* Run card bias changes at last */
1825 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001826
Liam Girdwood8078d872012-02-15 15:15:35 +00001827 /* do we need to notify any clients that DAPM event is complete */
1828 list_for_each_entry(d, &card->dapm_list, list) {
1829 if (d->stream_event)
1830 d->stream_event(d, event);
1831 }
1832
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001833 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001834 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001835 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001836
Mark Brown84e90932010-11-04 00:07:02 -04001837 trace_snd_soc_dapm_done(card);
1838
Mark Brown42aa3412009-03-01 19:21:10 +00001839 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001840}
1841
Mark Brown79fb9382009-08-21 16:38:13 +01001842#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001843static ssize_t dapm_widget_power_read_file(struct file *file,
1844 char __user *user_buf,
1845 size_t count, loff_t *ppos)
1846{
1847 struct snd_soc_dapm_widget *w = file->private_data;
1848 char *buf;
1849 int in, out;
1850 ssize_t ret;
1851 struct snd_soc_dapm_path *p = NULL;
1852
1853 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1854 if (!buf)
1855 return -ENOMEM;
1856
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001857 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001858 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001859 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001860 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001861
Mark Brownf13ebad2012-03-03 18:01:01 +00001862 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1863 w->name, w->power ? "On" : "Off",
1864 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001865
Mark Brownd033c362009-12-04 15:25:56 +00001866 if (w->reg >= 0)
1867 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001868 " - R%d(0x%x) mask 0x%x",
1869 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001870
1871 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1872
Mark Brown3eef08b2009-09-14 16:49:00 +01001873 if (w->sname)
1874 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1875 w->sname,
1876 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001877
1878 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001879 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001880 continue;
1881
Mark Brown79fb9382009-08-21 16:38:13 +01001882 if (p->connect)
1883 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001884 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001885 p->name ? p->name : "static",
1886 p->source->name);
1887 }
1888 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001889 if (p->connected && !p->connected(w, p->sink))
1890 continue;
1891
Mark Brown79fb9382009-08-21 16:38:13 +01001892 if (p->connect)
1893 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001894 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001895 p->name ? p->name : "static",
1896 p->sink->name);
1897 }
1898
1899 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1900
1901 kfree(buf);
1902 return ret;
1903}
1904
1905static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001906 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001907 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001908 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001909};
1910
Mark Brownef49e4f2011-04-04 20:48:13 +09001911static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1912 size_t count, loff_t *ppos)
1913{
1914 struct snd_soc_dapm_context *dapm = file->private_data;
1915 char *level;
1916
1917 switch (dapm->bias_level) {
1918 case SND_SOC_BIAS_ON:
1919 level = "On\n";
1920 break;
1921 case SND_SOC_BIAS_PREPARE:
1922 level = "Prepare\n";
1923 break;
1924 case SND_SOC_BIAS_STANDBY:
1925 level = "Standby\n";
1926 break;
1927 case SND_SOC_BIAS_OFF:
1928 level = "Off\n";
1929 break;
1930 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001931 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09001932 level = "Unknown\n";
1933 break;
1934 }
1935
1936 return simple_read_from_buffer(user_buf, count, ppos, level,
1937 strlen(level));
1938}
1939
1940static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001941 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001942 .read = dapm_bias_read_file,
1943 .llseek = default_llseek,
1944};
1945
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001946void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1947 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001948{
Mark Brown79fb9382009-08-21 16:38:13 +01001949 struct dentry *d;
1950
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001951 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1952
1953 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001954 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001955 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001956 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001957 }
Mark Brown79fb9382009-08-21 16:38:13 +01001958
Mark Brownef49e4f2011-04-04 20:48:13 +09001959 d = debugfs_create_file("bias_level", 0444,
1960 dapm->debugfs_dapm, dapm,
1961 &dapm_bias_fops);
1962 if (!d)
1963 dev_warn(dapm->dev,
1964 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001965}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001966
1967static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1968{
1969 struct snd_soc_dapm_context *dapm = w->dapm;
1970 struct dentry *d;
1971
1972 if (!dapm->debugfs_dapm || !w->name)
1973 return;
1974
1975 d = debugfs_create_file(w->name, 0444,
1976 dapm->debugfs_dapm, w,
1977 &dapm_widget_power_fops);
1978 if (!d)
1979 dev_warn(w->dapm->dev,
1980 "ASoC: Failed to create %s debugfs file\n",
1981 w->name);
1982}
1983
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001984static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1985{
1986 debugfs_remove_recursive(dapm->debugfs_dapm);
1987}
1988
Mark Brown79fb9382009-08-21 16:38:13 +01001989#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001990void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1991 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001992{
1993}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001994
1995static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1996{
1997}
1998
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001999static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2000{
2001}
2002
Mark Brown79fb9382009-08-21 16:38:13 +01002003#endif
2004
Richard Purdie2b97eab2006-10-06 18:32:18 +02002005/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002006static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002007 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002008{
2009 struct snd_soc_dapm_path *path;
2010 int found = 0;
2011
Mark Brownf9fa2b12014-03-06 16:49:11 +08002012 lockdep_assert_held(&card->dapm_mutex);
2013
Richard Purdie2b97eab2006-10-06 18:32:18 +02002014 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002015 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002016 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002017 continue;
2018
2019 found = 1;
2020 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002021 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002022 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002023 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002024 } else {
2025 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002026 dapm_mark_dirty(path->source,
2027 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002028 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002029 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002030 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002031 }
2032
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002033 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002034 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002035
Liam Girdwood618dae12012-04-25 12:12:51 +01002036 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002037}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002038
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002039int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002040 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2041 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002042{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002043 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002044 int ret;
2045
Liam Girdwood3cd04342012-03-09 12:02:08 +00002046 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002047 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002048 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002049 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002050 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002051 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002052 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002053 return ret;
2054}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002055EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002056
Milan plzik1b075e32008-01-10 14:39:46 +01002057/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002058static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002059 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002060{
2061 struct snd_soc_dapm_path *path;
2062 int found = 0;
2063
Mark Brownf9fa2b12014-03-06 16:49:11 +08002064 lockdep_assert_held(&card->dapm_mutex);
2065
Richard Purdie2b97eab2006-10-06 18:32:18 +02002066 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002067 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002068 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002069 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002070 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002071 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002072 }
2073
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002074 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002075 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002076
Liam Girdwood618dae12012-04-25 12:12:51 +01002077 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002078}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002079
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002080int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002081 struct snd_kcontrol *kcontrol, int connect,
2082 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002083{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002084 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002085 int ret;
2086
Liam Girdwood3cd04342012-03-09 12:02:08 +00002087 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002088 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002089 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002090 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002091 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002092 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002093 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002094 return ret;
2095}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002096EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002097
Benoit Cousson44ba2642014-07-08 23:19:36 +02002098static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002099{
Richard Purdie2b97eab2006-10-06 18:32:18 +02002100 struct snd_soc_dapm_widget *w;
2101 int count = 0;
2102 char *state = "not set";
2103
Lars-Peter Clausen00200102014-07-17 22:01:07 +02002104 list_for_each_entry(w, &codec->component.card->widgets, list) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002105 if (w->dapm != &codec->dapm)
2106 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002107
2108 /* only display widgets that burnm power */
2109 switch (w->id) {
2110 case snd_soc_dapm_hp:
2111 case snd_soc_dapm_mic:
2112 case snd_soc_dapm_spk:
2113 case snd_soc_dapm_line:
2114 case snd_soc_dapm_micbias:
2115 case snd_soc_dapm_dac:
2116 case snd_soc_dapm_adc:
2117 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002118 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002119 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002120 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002121 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002122 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002123 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002124 if (w->name)
2125 count += sprintf(buf + count, "%s: %s\n",
2126 w->name, w->power ? "On":"Off");
2127 break;
2128 default:
2129 break;
2130 }
2131 }
2132
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002133 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002134 case SND_SOC_BIAS_ON:
2135 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002136 break;
Mark Brown0be98982008-05-19 12:31:28 +02002137 case SND_SOC_BIAS_PREPARE:
2138 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002139 break;
Mark Brown0be98982008-05-19 12:31:28 +02002140 case SND_SOC_BIAS_STANDBY:
2141 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002142 break;
Mark Brown0be98982008-05-19 12:31:28 +02002143 case SND_SOC_BIAS_OFF:
2144 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002145 break;
2146 }
2147 count += sprintf(buf + count, "PM State: %s\n", state);
2148
2149 return count;
2150}
2151
Benoit Cousson44ba2642014-07-08 23:19:36 +02002152/* show dapm widget status in sys fs */
2153static ssize_t dapm_widget_show(struct device *dev,
2154 struct device_attribute *attr, char *buf)
2155{
2156 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2157 int i, count = 0;
2158
2159 for (i = 0; i < rtd->num_codecs; i++) {
2160 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
2161 count += dapm_widget_show_codec(codec, buf + count);
2162 }
2163
2164 return count;
2165}
2166
Richard Purdie2b97eab2006-10-06 18:32:18 +02002167static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2168
2169int snd_soc_dapm_sys_add(struct device *dev)
2170{
Troy Kisky12ef1932008-10-13 17:42:14 -07002171 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002172}
2173
2174static void snd_soc_dapm_sys_remove(struct device *dev)
2175{
Mark Brownaef90842009-05-16 17:53:16 +01002176 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002177}
2178
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002179static void dapm_free_path(struct snd_soc_dapm_path *path)
2180{
2181 list_del(&path->list_sink);
2182 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002183 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002184 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002185 kfree(path);
2186}
2187
Richard Purdie2b97eab2006-10-06 18:32:18 +02002188/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002189static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002190{
2191 struct snd_soc_dapm_widget *w, *next_w;
2192 struct snd_soc_dapm_path *p, *next_p;
2193
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002194 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2195 if (w->dapm != dapm)
2196 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002197 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002198 /*
2199 * remove source and sink paths associated to this widget.
2200 * While removing the path, remove reference to it from both
2201 * source and sink widgets so that path is removed only once.
2202 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002203 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2204 dapm_free_path(p);
2205
2206 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2207 dapm_free_path(p);
2208
Stephen Warrenfad59882011-04-28 17:37:59 -06002209 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002210 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002211 kfree(w);
2212 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002213}
2214
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002215static struct snd_soc_dapm_widget *dapm_find_widget(
2216 struct snd_soc_dapm_context *dapm, const char *pin,
2217 bool search_other_contexts)
2218{
2219 struct snd_soc_dapm_widget *w;
2220 struct snd_soc_dapm_widget *fallback = NULL;
2221
2222 list_for_each_entry(w, &dapm->card->widgets, list) {
2223 if (!strcmp(w->name, pin)) {
2224 if (w->dapm == dapm)
2225 return w;
2226 else
2227 fallback = w;
2228 }
2229 }
2230
2231 if (search_other_contexts)
2232 return fallback;
2233
2234 return NULL;
2235}
2236
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002237static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002238 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002239{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002240 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002241
Mark Brownf9fa2b12014-03-06 16:49:11 +08002242 dapm_assert_locked(dapm);
2243
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002244 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002245 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002246 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002247 }
2248
Mark Brown1a8b2d92012-02-16 11:50:07 -08002249 if (w->connected != status)
2250 dapm_mark_dirty(w, "pin configuration");
2251
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002252 w->connected = status;
2253 if (status == 0)
2254 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002255
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002256 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002257}
2258
Richard Purdie2b97eab2006-10-06 18:32:18 +02002259/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002260 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2261 * @dapm: DAPM context
2262 *
2263 * Walks all dapm audio paths and powers widgets according to their
2264 * stream or path usage.
2265 *
2266 * Requires external locking.
2267 *
2268 * Returns 0 for success.
2269 */
2270int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2271{
2272 /*
2273 * Suppress early reports (eg, jacks syncing their state) to avoid
2274 * silly DAPM runs during card startup.
2275 */
2276 if (!dapm->card || !dapm->card->instantiated)
2277 return 0;
2278
2279 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2280}
2281EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2282
2283/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002284 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002285 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002286 *
2287 * Walks all dapm audio paths and powers widgets according to their
2288 * stream or path usage.
2289 *
2290 * Returns 0 for success.
2291 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002292int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002293{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002294 int ret;
2295
Liam Girdwood3cd04342012-03-09 12:02:08 +00002296 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002297 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002298 mutex_unlock(&dapm->card->dapm_mutex);
2299 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002300}
Liam Girdwooda5302182008-07-07 13:35:17 +01002301EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002302
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002303static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2304 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2305 const char *control,
2306 int (*connected)(struct snd_soc_dapm_widget *source,
2307 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002308{
2309 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002310 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002311
2312 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2313 if (!path)
2314 return -ENOMEM;
2315
2316 path->source = wsource;
2317 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002318 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002319 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002320 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002321 INIT_LIST_HEAD(&path->list_source);
2322 INIT_LIST_HEAD(&path->list_sink);
2323
2324 /* check for external widgets */
2325 if (wsink->id == snd_soc_dapm_input) {
2326 if (wsource->id == snd_soc_dapm_micbias ||
2327 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002328 wsource->id == snd_soc_dapm_line ||
2329 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002330 wsink->ext = 1;
2331 }
2332 if (wsource->id == snd_soc_dapm_output) {
2333 if (wsink->id == snd_soc_dapm_spk ||
2334 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002335 wsink->id == snd_soc_dapm_line ||
2336 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002337 wsource->ext = 1;
2338 }
2339
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002340 dapm_mark_dirty(wsource, "Route added");
2341 dapm_mark_dirty(wsink, "Route added");
2342
Richard Purdie2b97eab2006-10-06 18:32:18 +02002343 /* connect static paths */
2344 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002345 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002346 list_add(&path->list_sink, &wsink->sources);
2347 list_add(&path->list_source, &wsource->sinks);
2348 path->connect = 1;
2349 return 0;
2350 }
2351
2352 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002353 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002354 case snd_soc_dapm_adc:
2355 case snd_soc_dapm_dac:
2356 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002357 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002358 case snd_soc_dapm_input:
2359 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002360 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002361 case snd_soc_dapm_micbias:
2362 case snd_soc_dapm_vmid:
2363 case snd_soc_dapm_pre:
2364 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002365 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002366 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002367 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002368 case snd_soc_dapm_aif_in:
2369 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002370 case snd_soc_dapm_dai_in:
2371 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002372 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002373 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002374 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002375 list_add(&path->list_sink, &wsink->sources);
2376 list_add(&path->list_source, &wsource->sinks);
2377 path->connect = 1;
2378 return 0;
2379 case snd_soc_dapm_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002380 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002381 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002382 if (ret != 0)
2383 goto err;
2384 break;
2385 case snd_soc_dapm_switch:
2386 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002387 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002388 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002389 if (ret != 0)
2390 goto err;
2391 break;
2392 case snd_soc_dapm_hp:
2393 case snd_soc_dapm_mic:
2394 case snd_soc_dapm_line:
2395 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002396 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002397 list_add(&path->list_sink, &wsink->sources);
2398 list_add(&path->list_source, &wsource->sinks);
2399 path->connect = 0;
2400 return 0;
2401 }
Mark Brownfabd0382012-07-05 17:20:06 +01002402
Richard Purdie2b97eab2006-10-06 18:32:18 +02002403 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002404err:
2405 kfree(path);
2406 return ret;
2407}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002408
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002409static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002410 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002411{
2412 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2413 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2414 const char *sink;
2415 const char *source;
2416 char prefixed_sink[80];
2417 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002418 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002419 int ret;
2420
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002421 prefix = soc_dapm_prefix(dapm);
2422 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002423 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002424 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002425 sink = prefixed_sink;
2426 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002427 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002428 source = prefixed_source;
2429 } else {
2430 sink = route->sink;
2431 source = route->source;
2432 }
2433
2434 /*
2435 * find src and dest widgets over all widgets but favor a widget from
2436 * current DAPM context
2437 */
2438 list_for_each_entry(w, &dapm->card->widgets, list) {
2439 if (!wsink && !(strcmp(w->name, sink))) {
2440 wtsink = w;
2441 if (w->dapm == dapm)
2442 wsink = w;
2443 continue;
2444 }
2445 if (!wsource && !(strcmp(w->name, source))) {
2446 wtsource = w;
2447 if (w->dapm == dapm)
2448 wsource = w;
2449 }
2450 }
2451 /* use widget from another DAPM context if not found from this */
2452 if (!wsink)
2453 wsink = wtsink;
2454 if (!wsource)
2455 wsource = wtsource;
2456
2457 if (wsource == NULL) {
2458 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2459 route->source);
2460 return -ENODEV;
2461 }
2462 if (wsink == NULL) {
2463 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2464 route->sink);
2465 return -ENODEV;
2466 }
2467
2468 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2469 route->connected);
2470 if (ret)
2471 goto err;
2472
2473 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002474err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002475 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002476 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002477 return ret;
2478}
Mark Brown105f1c22008-05-13 14:52:19 +02002479
Mark Brownefcc3c62012-07-05 17:24:19 +01002480static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2481 const struct snd_soc_dapm_route *route)
2482{
2483 struct snd_soc_dapm_path *path, *p;
2484 const char *sink;
2485 const char *source;
2486 char prefixed_sink[80];
2487 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002488 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002489
2490 if (route->control) {
2491 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002492 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002493 return -EINVAL;
2494 }
2495
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002496 prefix = soc_dapm_prefix(dapm);
2497 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002498 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002499 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002500 sink = prefixed_sink;
2501 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002502 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002503 source = prefixed_source;
2504 } else {
2505 sink = route->sink;
2506 source = route->source;
2507 }
2508
2509 path = NULL;
2510 list_for_each_entry(p, &dapm->card->paths, list) {
2511 if (strcmp(p->source->name, source) != 0)
2512 continue;
2513 if (strcmp(p->sink->name, sink) != 0)
2514 continue;
2515 path = p;
2516 break;
2517 }
2518
2519 if (path) {
2520 dapm_mark_dirty(path->source, "Route removed");
2521 dapm_mark_dirty(path->sink, "Route removed");
2522
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002523 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002524 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002525 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002526 source, sink);
2527 }
2528
2529 return 0;
2530}
2531
Mark Brown105f1c22008-05-13 14:52:19 +02002532/**
Mark Brown105f1c22008-05-13 14:52:19 +02002533 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002534 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002535 * @route: audio routes
2536 * @num: number of routes
2537 *
2538 * Connects 2 dapm widgets together via a named audio path. The sink is
2539 * the widget receiving the audio signal, whilst the source is the sender
2540 * of the audio signal.
2541 *
2542 * Returns 0 for success else error. On error all resources can be freed
2543 * with a call to snd_soc_card_free().
2544 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002545int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002546 const struct snd_soc_dapm_route *route, int num)
2547{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002548 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002549
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002550 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002551 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002552 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002553 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002554 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2555 route->source,
2556 route->control ? route->control : "direct",
2557 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002558 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002559 }
2560 route++;
2561 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002562 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002563
Dan Carpenter60884c22012-04-13 22:25:43 +03002564 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002565}
2566EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2567
Mark Brownefcc3c62012-07-05 17:24:19 +01002568/**
2569 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2570 * @dapm: DAPM context
2571 * @route: audio routes
2572 * @num: number of routes
2573 *
2574 * Removes routes from the DAPM context.
2575 */
2576int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2577 const struct snd_soc_dapm_route *route, int num)
2578{
2579 int i, ret = 0;
2580
2581 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2582 for (i = 0; i < num; i++) {
2583 snd_soc_dapm_del_route(dapm, route);
2584 route++;
2585 }
2586 mutex_unlock(&dapm->card->dapm_mutex);
2587
2588 return ret;
2589}
2590EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2591
Mark Brownbf3a9e12011-06-13 16:42:29 +01002592static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2593 const struct snd_soc_dapm_route *route)
2594{
2595 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2596 route->source,
2597 true);
2598 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2599 route->sink,
2600 true);
2601 struct snd_soc_dapm_path *path;
2602 int count = 0;
2603
2604 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002605 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002606 route->source);
2607 return -ENODEV;
2608 }
2609
2610 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002611 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002612 route->sink);
2613 return -ENODEV;
2614 }
2615
2616 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002617 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002618 route->source, route->sink);
2619
2620 list_for_each_entry(path, &source->sinks, list_source) {
2621 if (path->sink == sink) {
2622 path->weak = 1;
2623 count++;
2624 }
2625 }
2626
2627 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002628 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002629 route->source, route->sink);
2630 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002631 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002632 count, route->source, route->sink);
2633
2634 return 0;
2635}
2636
2637/**
2638 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2639 * @dapm: DAPM context
2640 * @route: audio routes
2641 * @num: number of routes
2642 *
2643 * Mark existing routes matching those specified in the passed array
2644 * as being weak, meaning that they are ignored for the purpose of
2645 * power decisions. The main intended use case is for sidetone paths
2646 * which couple audio between other independent paths if they are both
2647 * active in order to make the combination work better at the user
2648 * level but which aren't intended to be "used".
2649 *
2650 * Note that CODEC drivers should not use this as sidetone type paths
2651 * can frequently also be used as bypass paths.
2652 */
2653int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2654 const struct snd_soc_dapm_route *route, int num)
2655{
2656 int i, err;
2657 int ret = 0;
2658
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002659 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002660 for (i = 0; i < num; i++) {
2661 err = snd_soc_dapm_weak_route(dapm, route);
2662 if (err)
2663 ret = err;
2664 route++;
2665 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002666 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002667
2668 return ret;
2669}
2670EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2671
Mark Brown105f1c22008-05-13 14:52:19 +02002672/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002673 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002674 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002675 *
2676 * Checks the codec for any new dapm widgets and creates them if found.
2677 *
2678 * Returns 0 for success.
2679 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002680int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002681{
2682 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002683 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002684
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002685 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002686
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002687 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002688 {
2689 if (w->new)
2690 continue;
2691
Stephen Warrenfad59882011-04-28 17:37:59 -06002692 if (w->num_kcontrols) {
2693 w->kcontrols = kzalloc(w->num_kcontrols *
2694 sizeof(struct snd_kcontrol *),
2695 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002696 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002697 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002698 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002699 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002700 }
2701
Richard Purdie2b97eab2006-10-06 18:32:18 +02002702 switch(w->id) {
2703 case snd_soc_dapm_switch:
2704 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002705 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002706 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002707 break;
2708 case snd_soc_dapm_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002709 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002710 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002711 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002712 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002713 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002714 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002715 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002716 break;
2717 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002718
2719 /* Read the initial power state from the device */
2720 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002721 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002722 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002723 val &= w->mask;
2724 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002725 w->power = 1;
2726 }
2727
Richard Purdie2b97eab2006-10-06 18:32:18 +02002728 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002729
Mark Brown7508b122011-10-05 12:09:12 +01002730 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002731 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002732 }
2733
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002734 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2735 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002736 return 0;
2737}
2738EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2739
2740/**
2741 * snd_soc_dapm_get_volsw - dapm mixer get callback
2742 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002743 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002744 *
2745 * Callback to get the value of a dapm mixer control.
2746 *
2747 * Returns 0 for success.
2748 */
2749int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2750 struct snd_ctl_elem_value *ucontrol)
2751{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002752 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2753 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002754 struct soc_mixer_control *mc =
2755 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002756 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002757 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002758 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002759 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002760 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002761 unsigned int val;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002762 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002763
2764 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002765 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002766 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002767 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002768
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002769 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002770 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
2771 ret = soc_dapm_read(dapm, reg, &val);
2772 val = (val >> shift) & mask;
2773 } else {
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002774 val = dapm_kcontrol_get_value(kcontrol);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002775 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002776 mutex_unlock(&card->dapm_mutex);
2777
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002778 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002779 ucontrol->value.integer.value[0] = max - val;
2780 else
2781 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002782
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002783 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002784}
2785EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2786
2787/**
2788 * snd_soc_dapm_put_volsw - dapm mixer set callback
2789 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002790 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002791 *
2792 * Callback to set the value of a dapm mixer control.
2793 *
2794 * Returns 0 for success.
2795 */
2796int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2797 struct snd_ctl_elem_value *ucontrol)
2798{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002799 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2800 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002801 struct soc_mixer_control *mc =
2802 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002803 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002804 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002805 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002806 unsigned int mask = (1 << fls(max)) - 1;
2807 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002808 unsigned int val;
Jarkko Nikula18626c72014-06-09 14:20:29 +03002809 int connect, change, reg_change = 0;
Mark Brown97404f22010-12-14 16:13:57 +00002810 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002811 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002812
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002813 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002814 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002815 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002816 kcontrol->id.name);
2817
Richard Purdie2b97eab2006-10-06 18:32:18 +02002818 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002819 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002820
2821 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002822 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002823
Liam Girdwood3cd04342012-03-09 12:02:08 +00002824 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002825
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002826 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown283375c2009-12-07 18:09:03 +00002827
Jarkko Nikula18626c72014-06-09 14:20:29 +03002828 if (reg != SND_SOC_NOPM) {
2829 mask = mask << shift;
2830 val = val << shift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02002831
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002832 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
Jarkko Nikula18626c72014-06-09 14:20:29 +03002833 }
2834
2835 if (change || reg_change) {
2836 if (reg_change) {
2837 update.kcontrol = kcontrol;
2838 update.reg = reg;
2839 update.mask = mask;
2840 update.val = val;
2841 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002842 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03002843 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00002844
Nenghua Cao52765972013-12-13 20:13:49 +08002845 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002846
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002847 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002848 }
2849
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002850 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002851
2852 if (ret > 0)
2853 soc_dpcm_runtime_update(card);
2854
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002855 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002856}
2857EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2858
2859/**
2860 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2861 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002862 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002863 *
2864 * Callback to get the value of a dapm enumerated double mixer control.
2865 *
2866 * Returns 0 for success.
2867 */
2868int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2869 struct snd_ctl_elem_value *ucontrol)
2870{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002871 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002872 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002873 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002874
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002875 if (e->reg != SND_SOC_NOPM) {
2876 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
2877 if (ret)
2878 return ret;
2879 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002880 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002881 }
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002882
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002883 val = (reg_val >> e->shift_l) & e->mask;
2884 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2885 if (e->shift_l != e->shift_r) {
2886 val = (reg_val >> e->shift_r) & e->mask;
2887 val = snd_soc_enum_val_to_item(e, val);
2888 ucontrol->value.enumerated.item[1] = val;
2889 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002890
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002891 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002892}
2893EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2894
2895/**
2896 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2897 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002898 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002899 *
2900 * Callback to set the value of a dapm enumerated double mixer control.
2901 *
2902 * Returns 0 for success.
2903 */
2904int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2905 struct snd_ctl_elem_value *ucontrol)
2906{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002907 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2908 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002909 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002910 unsigned int *item = ucontrol->value.enumerated.item;
2911 unsigned int val, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002912 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002913 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002914 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002915
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002916 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002917 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002918
2919 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002920 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002921 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002922 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002923 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002924 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002925 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002926 }
2927
Liam Girdwood3cd04342012-03-09 12:02:08 +00002928 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002929
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002930 if (e->reg != SND_SOC_NOPM)
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002931 change = soc_dapm_test_bits(dapm, e->reg, mask, val);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002932 else
2933 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002934
Richard Purdie2b97eab2006-10-06 18:32:18 +02002935 if (change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002936 if (e->reg != SND_SOC_NOPM) {
2937 update.kcontrol = kcontrol;
2938 update.reg = e->reg;
2939 update.mask = mask;
2940 update.val = val;
2941 card->update = &update;
2942 }
Mark Brown3a655772009-10-05 17:23:30 +01002943
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002944 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002945
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002946 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002947 }
2948
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002949 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002950
2951 if (ret > 0)
2952 soc_dpcm_runtime_update(card);
2953
Mark Brown97404f22010-12-14 16:13:57 +00002954 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002955}
2956EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2957
2958/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002959 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2960 *
2961 * @kcontrol: mixer control
2962 * @uinfo: control element information
2963 *
2964 * Callback to provide information about a pin switch control.
2965 */
2966int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2967 struct snd_ctl_elem_info *uinfo)
2968{
2969 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2970 uinfo->count = 1;
2971 uinfo->value.integer.min = 0;
2972 uinfo->value.integer.max = 1;
2973
2974 return 0;
2975}
2976EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2977
2978/**
2979 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2980 *
2981 * @kcontrol: mixer control
2982 * @ucontrol: Value
2983 */
2984int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2985 struct snd_ctl_elem_value *ucontrol)
2986{
Mark Brown48a8c392012-02-14 17:11:15 -08002987 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002988 const char *pin = (const char *)kcontrol->private_value;
2989
Liam Girdwood3cd04342012-03-09 12:02:08 +00002990 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002991
2992 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002993 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002994
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002995 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002996
2997 return 0;
2998}
2999EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3000
3001/**
3002 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3003 *
3004 * @kcontrol: mixer control
3005 * @ucontrol: Value
3006 */
3007int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3008 struct snd_ctl_elem_value *ucontrol)
3009{
Mark Brown48a8c392012-02-14 17:11:15 -08003010 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003011 const char *pin = (const char *)kcontrol->private_value;
3012
Mark Brown8b37dbd2009-02-28 21:14:20 +00003013 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003014 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003015 else
Mark Brown48a8c392012-02-14 17:11:15 -08003016 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003017
Mark Brown48a8c392012-02-14 17:11:15 -08003018 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003019 return 0;
3020}
3021EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3022
Mark Brown5ba06fc2012-02-16 11:07:13 -08003023static struct snd_soc_dapm_widget *
3024snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3025 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003026{
3027 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003028 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003029 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003030
3031 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003032 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003033
Mark Brown62ea8742012-01-21 21:14:48 +00003034 switch (w->id) {
3035 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003036 w->regulator = devm_regulator_get(dapm->dev, w->name);
3037 if (IS_ERR(w->regulator)) {
3038 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003039 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003040 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003041 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003042 }
Mark Brown8784c772013-01-10 19:33:47 +00003043
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003044 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003045 ret = regulator_allow_bypass(w->regulator, true);
3046 if (ret != 0)
3047 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003048 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003049 w->name, ret);
3050 }
Mark Brown62ea8742012-01-21 21:14:48 +00003051 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003052 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003053#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003054 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003055 if (IS_ERR(w->clk)) {
3056 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003057 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003058 w->name, ret);
3059 return NULL;
3060 }
Mark Brownec029952012-06-04 08:16:20 +01003061#else
3062 return NULL;
3063#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003064 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003065 default:
3066 break;
3067 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003068
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003069 prefix = soc_dapm_prefix(dapm);
3070 if (prefix)
3071 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003072 else
3073 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3074
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003075 if (w->name == NULL) {
3076 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003077 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003078 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003079
Mark Brown7ca3a182011-10-08 14:04:50 +01003080 switch (w->id) {
3081 case snd_soc_dapm_switch:
3082 case snd_soc_dapm_mixer:
3083 case snd_soc_dapm_mixer_named_ctl:
3084 w->power_check = dapm_generic_check_power;
3085 break;
3086 case snd_soc_dapm_mux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003087 w->power_check = dapm_generic_check_power;
3088 break;
Mark Brown46162742013-06-05 19:36:11 +01003089 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003090 w->power_check = dapm_adc_check_power;
3091 break;
Mark Brown46162742013-06-05 19:36:11 +01003092 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003093 w->power_check = dapm_dac_check_power;
3094 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003095 case snd_soc_dapm_adc:
3096 case snd_soc_dapm_aif_out:
3097 case snd_soc_dapm_dac:
3098 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003099 case snd_soc_dapm_pga:
3100 case snd_soc_dapm_out_drv:
3101 case snd_soc_dapm_input:
3102 case snd_soc_dapm_output:
3103 case snd_soc_dapm_micbias:
3104 case snd_soc_dapm_spk:
3105 case snd_soc_dapm_hp:
3106 case snd_soc_dapm_mic:
3107 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003108 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003109 w->power_check = dapm_generic_check_power;
3110 break;
3111 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003112 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003113 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003114 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003115 w->power_check = dapm_supply_check_power;
3116 break;
3117 default:
3118 w->power_check = dapm_always_on_check_power;
3119 break;
3120 }
3121
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003122 w->dapm = dapm;
Jarkko Nikulab2d9de52014-10-03 15:32:40 +03003123 if (dapm->component)
3124 w->codec = dapm->component->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003125 INIT_LIST_HEAD(&w->sources);
3126 INIT_LIST_HEAD(&w->sinks);
3127 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003128 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003129 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003130
3131 /* machine layer set ups unconnected pins and insertions */
3132 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003133 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003134}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003135
3136/**
Mark Brown4ba13272008-05-13 14:51:19 +02003137 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003138 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003139 * @widget: widget array
3140 * @num: number of widgets
3141 *
3142 * Creates new DAPM controls based upon the templates.
3143 *
3144 * Returns 0 for success else error.
3145 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003146int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003147 const struct snd_soc_dapm_widget *widget,
3148 int num)
3149{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003150 struct snd_soc_dapm_widget *w;
3151 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003152 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003153
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003154 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003155 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003156 w = snd_soc_dapm_new_control(dapm, widget);
3157 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003158 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003159 "ASoC: Failed to create DAPM control %s\n",
3160 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003161 ret = -ENOMEM;
3162 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003163 }
Mark Brown4ba13272008-05-13 14:51:19 +02003164 widget++;
3165 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003166 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003167 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003168}
3169EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3170
Mark Brownc74184e2012-04-04 22:12:09 +01003171static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3172 struct snd_kcontrol *kcontrol, int event)
3173{
3174 struct snd_soc_dapm_path *source_p, *sink_p;
3175 struct snd_soc_dai *source, *sink;
3176 const struct snd_soc_pcm_stream *config = w->params;
3177 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003178 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003179 u64 fmt;
3180 int ret;
3181
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003182 if (WARN_ON(!config) ||
3183 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3184 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003185
3186 /* We only support a single source and sink, pick the first */
3187 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3188 list_sink);
3189 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3190 list_source);
3191
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003192 if (WARN_ON(!source_p || !sink_p) ||
3193 WARN_ON(!sink_p->source || !source_p->sink) ||
3194 WARN_ON(!source_p->source || !sink_p->sink))
3195 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003196
3197 source = source_p->source->priv;
3198 sink = sink_p->sink->priv;
3199
3200 /* Be a little careful as we don't want to overflow the mask array */
3201 if (config->formats) {
3202 fmt = ffs(config->formats) - 1;
3203 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003204 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003205 config->formats);
3206 fmt = 0;
3207 }
3208
3209 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003210 params = kzalloc(sizeof(*params), GFP_KERNEL);
3211 if (!params) {
3212 ret = -ENOMEM;
3213 goto out;
3214 }
3215 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003216
Mark Brown9747cec2012-04-26 19:12:21 +01003217 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003218 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003219 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003220 config->rate_max;
3221
Mark Brown9747cec2012-04-26 19:12:21 +01003222 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003223 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003224 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003225 = config->channels_max;
3226
3227 memset(&substream, 0, sizeof(substream));
3228
3229 switch (event) {
3230 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003231 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3232 ret = soc_dai_hw_params(&substream, params, source);
3233 if (ret < 0)
3234 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003235
Benoit Cousson93e69582014-07-08 23:19:38 +02003236 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3237 ret = soc_dai_hw_params(&substream, params, sink);
3238 if (ret < 0)
3239 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003240 break;
3241
3242 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003243 ret = snd_soc_dai_digital_mute(sink, 0,
3244 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003245 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003246 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003247 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003248 break;
3249
3250 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003251 ret = snd_soc_dai_digital_mute(sink, 1,
3252 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003253 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003254 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003255 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003256 break;
3257
3258 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003259 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003260 return -EINVAL;
3261 }
3262
Mark Brown9747cec2012-04-26 19:12:21 +01003263out:
3264 kfree(params);
3265 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003266}
3267
3268int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3269 const struct snd_soc_pcm_stream *params,
3270 struct snd_soc_dapm_widget *source,
3271 struct snd_soc_dapm_widget *sink)
3272{
Mark Brownc74184e2012-04-04 22:12:09 +01003273 struct snd_soc_dapm_widget template;
3274 struct snd_soc_dapm_widget *w;
3275 size_t len;
3276 char *link_name;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003277 int ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003278
3279 len = strlen(source->name) + strlen(sink->name) + 2;
3280 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3281 if (!link_name)
3282 return -ENOMEM;
3283 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3284
3285 memset(&template, 0, sizeof(template));
3286 template.reg = SND_SOC_NOPM;
3287 template.id = snd_soc_dapm_dai_link;
3288 template.name = link_name;
3289 template.event = snd_soc_dai_link_event;
3290 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3291 SND_SOC_DAPM_PRE_PMD;
3292
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003293 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003294
3295 w = snd_soc_dapm_new_control(&card->dapm, &template);
3296 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003297 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003298 link_name);
3299 return -ENOMEM;
3300 }
3301
3302 w->params = params;
3303
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003304 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3305 if (ret)
3306 return ret;
3307 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Mark Brownc74184e2012-04-04 22:12:09 +01003308}
3309
Mark Brown888df392012-02-16 19:37:51 -08003310int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3311 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003312{
Mark Brown888df392012-02-16 19:37:51 -08003313 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003314 struct snd_soc_dapm_widget *w;
3315
Mark Brown888df392012-02-16 19:37:51 -08003316 WARN_ON(dapm->dev != dai->dev);
3317
3318 memset(&template, 0, sizeof(template));
3319 template.reg = SND_SOC_NOPM;
3320
3321 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003322 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003323 template.name = dai->driver->playback.stream_name;
3324 template.sname = dai->driver->playback.stream_name;
3325
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003326 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003327 template.name);
3328
3329 w = snd_soc_dapm_new_control(dapm, &template);
3330 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003331 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003332 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003333 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003334 }
3335
3336 w->priv = dai;
3337 dai->playback_widget = w;
3338 }
3339
3340 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003341 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003342 template.name = dai->driver->capture.stream_name;
3343 template.sname = dai->driver->capture.stream_name;
3344
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003345 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003346 template.name);
3347
3348 w = snd_soc_dapm_new_control(dapm, &template);
3349 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003350 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003351 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003352 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003353 }
3354
3355 w->priv = dai;
3356 dai->capture_widget = w;
3357 }
3358
3359 return 0;
3360}
3361
3362int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3363{
3364 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003365 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003366 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003367
3368 /* For each DAI widget... */
3369 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003370 switch (dai_w->id) {
3371 case snd_soc_dapm_dai_in:
3372 case snd_soc_dapm_dai_out:
3373 break;
3374 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003375 continue;
Mark Brown46162742013-06-05 19:36:11 +01003376 }
Mark Brown888df392012-02-16 19:37:51 -08003377
3378 dai = dai_w->priv;
3379
3380 /* ...find all widgets with the same stream and link them */
3381 list_for_each_entry(w, &card->widgets, list) {
3382 if (w->dapm != dai_w->dapm)
3383 continue;
3384
Mark Brown46162742013-06-05 19:36:11 +01003385 switch (w->id) {
3386 case snd_soc_dapm_dai_in:
3387 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003388 continue;
Mark Brown46162742013-06-05 19:36:11 +01003389 default:
3390 break;
3391 }
Mark Brown888df392012-02-16 19:37:51 -08003392
Russell King19c2c5f2013-08-04 20:24:03 +01003393 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003394 continue;
3395
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003396 if (dai_w->id == snd_soc_dapm_dai_in) {
3397 src = dai_w;
3398 sink = w;
3399 } else {
3400 src = w;
3401 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003402 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003403 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3404 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003405 }
3406 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003407
Mark Brown888df392012-02-16 19:37:51 -08003408 return 0;
3409}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003410
Benoit Cousson44ba2642014-07-08 23:19:36 +02003411static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3412 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003413{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003414 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003415 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003416 int i;
3417
Benoit Cousson44ba2642014-07-08 23:19:36 +02003418 for (i = 0; i < rtd->num_codecs; i++) {
3419 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003420
3421 /* there is no point in connecting BE DAI links with dummies */
3422 if (snd_soc_dai_is_dummy(codec_dai) ||
3423 snd_soc_dai_is_dummy(cpu_dai))
3424 continue;
3425
3426 /* connect BE DAI playback if widgets are valid */
3427 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003428 source = cpu_dai->playback_widget;
3429 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003430 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003431 cpu_dai->component->name, source->name,
3432 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003433
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003434 snd_soc_dapm_add_path(&card->dapm, source, sink,
3435 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003436 }
3437
3438 /* connect BE DAI capture if widgets are valid */
3439 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003440 source = codec_dai->capture_widget;
3441 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003442 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003443 codec_dai->component->name, source->name,
3444 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003445
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003446 snd_soc_dapm_add_path(&card->dapm, source, sink,
3447 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003448 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003449 }
3450}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003451
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003452static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3453 int event)
3454{
3455 struct snd_soc_dapm_widget *w;
3456
3457 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3458 w = dai->playback_widget;
3459 else
3460 w = dai->capture_widget;
3461
3462 if (w) {
3463 dapm_mark_dirty(w, "stream event");
3464
3465 switch (event) {
3466 case SND_SOC_DAPM_STREAM_START:
3467 w->active = 1;
3468 break;
3469 case SND_SOC_DAPM_STREAM_STOP:
3470 w->active = 0;
3471 break;
3472 case SND_SOC_DAPM_STREAM_SUSPEND:
3473 case SND_SOC_DAPM_STREAM_RESUME:
3474 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3475 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3476 break;
3477 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003478 }
3479}
3480
Benoit Cousson44ba2642014-07-08 23:19:36 +02003481void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3482{
3483 struct snd_soc_pcm_runtime *rtd = card->rtd;
3484 int i;
3485
3486 /* for each BE DAI link... */
3487 for (i = 0; i < card->num_rtd; i++) {
3488 rtd = &card->rtd[i];
3489
3490 /*
3491 * dynamic FE links have no fixed DAI mapping.
3492 * CODEC<->CODEC links have no direct connection.
3493 */
3494 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3495 continue;
3496
3497 dapm_connect_dai_link_widgets(card, rtd);
3498 }
3499}
3500
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003501static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3502 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003503{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003504 int i;
3505
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003506 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Benoit Cousson44ba2642014-07-08 23:19:36 +02003507 for (i = 0; i < rtd->num_codecs; i++)
3508 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003509
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003510 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003511}
3512
3513/**
3514 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3515 * @rtd: PCM runtime data
3516 * @stream: stream name
3517 * @event: stream event
3518 *
3519 * Sends a stream event to the dapm core. The core then makes any
3520 * necessary widget power changes.
3521 *
3522 * Returns 0 for success else error.
3523 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003524void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3525 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003526{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003527 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003528
Liam Girdwood3cd04342012-03-09 12:02:08 +00003529 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003530 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003531 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003532}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003533
3534/**
Charles Keepax11391102014-02-18 15:22:14 +00003535 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3536 * @dapm: DAPM context
3537 * @pin: pin name
3538 *
3539 * Enables input/output pin and its parents or children widgets iff there is
3540 * a valid audio route and active audio stream.
3541 *
3542 * Requires external locking.
3543 *
3544 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3545 * do any widget power switching.
3546 */
3547int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3548 const char *pin)
3549{
3550 return snd_soc_dapm_set_pin(dapm, pin, 1);
3551}
3552EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3553
3554/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003555 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003556 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003557 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003558 *
Mark Brown74b8f952009-06-06 11:26:15 +01003559 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003560 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003561 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003562 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3563 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003564 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003565int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003566{
Charles Keepax11391102014-02-18 15:22:14 +00003567 int ret;
3568
3569 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3570
3571 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3572
3573 mutex_unlock(&dapm->card->dapm_mutex);
3574
3575 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003576}
Liam Girdwooda5302182008-07-07 13:35:17 +01003577EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003578
3579/**
Charles Keepax11391102014-02-18 15:22:14 +00003580 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3581 * @dapm: DAPM context
3582 * @pin: pin name
3583 *
3584 * Enables input/output pin regardless of any other state. This is
3585 * intended for use with microphone bias supplies used in microphone
3586 * jack detection.
3587 *
3588 * Requires external locking.
3589 *
3590 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3591 * do any widget power switching.
3592 */
3593int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3594 const char *pin)
3595{
3596 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3597
3598 if (!w) {
3599 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3600 return -EINVAL;
3601 }
3602
3603 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3604 w->connected = 1;
3605 w->force = 1;
3606 dapm_mark_dirty(w, "force enable");
3607
3608 return 0;
3609}
3610EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3611
3612/**
Mark Brownda341832010-03-15 19:23:37 +00003613 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003614 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003615 * @pin: pin name
3616 *
3617 * Enables input/output pin regardless of any other state. This is
3618 * intended for use with microphone bias supplies used in microphone
3619 * jack detection.
3620 *
3621 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3622 * do any widget power switching.
3623 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003624int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3625 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003626{
Charles Keepax11391102014-02-18 15:22:14 +00003627 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003628
Charles Keepax11391102014-02-18 15:22:14 +00003629 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003630
Charles Keepax11391102014-02-18 15:22:14 +00003631 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003632
Charles Keepax11391102014-02-18 15:22:14 +00003633 mutex_unlock(&dapm->card->dapm_mutex);
3634
3635 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003636}
3637EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3638
3639/**
Charles Keepax11391102014-02-18 15:22:14 +00003640 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3641 * @dapm: DAPM context
3642 * @pin: pin name
3643 *
3644 * Disables input/output pin and its parents or children widgets.
3645 *
3646 * Requires external locking.
3647 *
3648 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3649 * do any widget power switching.
3650 */
3651int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3652 const char *pin)
3653{
3654 return snd_soc_dapm_set_pin(dapm, pin, 0);
3655}
3656EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3657
3658/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003659 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003660 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003661 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003662 *
Mark Brown74b8f952009-06-06 11:26:15 +01003663 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003664 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003665 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3666 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003667 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003668int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3669 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003670{
Charles Keepax11391102014-02-18 15:22:14 +00003671 int ret;
3672
3673 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3674
3675 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3676
3677 mutex_unlock(&dapm->card->dapm_mutex);
3678
3679 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003680}
3681EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3682
3683/**
Charles Keepax11391102014-02-18 15:22:14 +00003684 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3685 * @dapm: DAPM context
3686 * @pin: pin name
3687 *
3688 * Marks the specified pin as being not connected, disabling it along
3689 * any parent or child widgets. At present this is identical to
3690 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3691 * additional things such as disabling controls which only affect
3692 * paths through the pin.
3693 *
3694 * Requires external locking.
3695 *
3696 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3697 * do any widget power switching.
3698 */
3699int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3700 const char *pin)
3701{
3702 return snd_soc_dapm_set_pin(dapm, pin, 0);
3703}
3704EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3705
3706/**
Mark Brown5817b522008-09-24 11:23:11 +01003707 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003708 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003709 * @pin: pin name
3710 *
3711 * Marks the specified pin as being not connected, disabling it along
3712 * any parent or child widgets. At present this is identical to
3713 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3714 * additional things such as disabling controls which only affect
3715 * paths through the pin.
3716 *
3717 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3718 * do any widget power switching.
3719 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003720int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003721{
Charles Keepax11391102014-02-18 15:22:14 +00003722 int ret;
3723
3724 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3725
3726 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3727
3728 mutex_unlock(&dapm->card->dapm_mutex);
3729
3730 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003731}
3732EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3733
3734/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003735 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003736 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003737 * @pin: audio signal pin endpoint (or start point)
3738 *
3739 * Get audio pin status - connected or disconnected.
3740 *
3741 * Returns 1 for connected otherwise 0.
3742 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003743int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3744 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003745{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003746 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003747
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003748 if (w)
3749 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003750
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003751 return 0;
3752}
Liam Girdwooda5302182008-07-07 13:35:17 +01003753EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003754
3755/**
Mark Brown1547aba2010-05-07 21:11:40 +01003756 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003757 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003758 * @pin: audio signal pin endpoint (or start point)
3759 *
3760 * Mark the given endpoint or pin as ignoring suspend. When the
3761 * system is disabled a path between two endpoints flagged as ignoring
3762 * suspend will not be disabled. The path must already be enabled via
3763 * normal means at suspend time, it will not be turned on if it was not
3764 * already enabled.
3765 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003766int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3767 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003768{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003769 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003770
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003771 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003772 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003773 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003774 }
3775
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003776 w->ignore_suspend = 1;
3777
3778 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003779}
3780EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3781
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02003782/**
3783 * dapm_is_external_path() - Checks if a path is a external path
3784 * @card: The card the path belongs to
3785 * @path: The path to check
3786 *
3787 * Returns true if the path is either between two different DAPM contexts or
3788 * between two external pins of the same DAPM context. Otherwise returns
3789 * false.
3790 */
3791static bool dapm_is_external_path(struct snd_soc_card *card,
3792 struct snd_soc_dapm_path *path)
3793{
3794 dev_dbg(card->dev,
3795 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3796 path->source->name, path->source->id, path->source->dapm,
3797 path->sink->name, path->sink->id, path->sink->dapm);
3798
3799 /* Connection between two different DAPM contexts */
3800 if (path->source->dapm != path->sink->dapm)
3801 return true;
3802
3803 /* Loopback connection from external pin to external pin */
3804 if (path->sink->id == snd_soc_dapm_input) {
3805 switch (path->source->id) {
3806 case snd_soc_dapm_output:
3807 case snd_soc_dapm_micbias:
3808 return true;
3809 default:
3810 break;
3811 }
3812 }
3813
3814 return false;
3815}
3816
Stephen Warren16332812011-11-23 12:42:04 -07003817static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3818 struct snd_soc_dapm_widget *w)
3819{
3820 struct snd_soc_dapm_path *p;
3821
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02003822 list_for_each_entry(p, &w->sources, list_sink) {
3823 if (dapm_is_external_path(card, p))
3824 return true;
3825 }
Stephen Warren16332812011-11-23 12:42:04 -07003826
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02003827 list_for_each_entry(p, &w->sinks, list_source) {
3828 if (dapm_is_external_path(card, p))
3829 return true;
Stephen Warren16332812011-11-23 12:42:04 -07003830 }
3831
3832 return false;
3833}
3834
3835/**
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003836 * snd_soc_dapm_auto_nc_pins - call snd_soc_dapm_nc_pin for unused pins
3837 * @card: The card whose pins should be processed
Stephen Warren16332812011-11-23 12:42:04 -07003838 *
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003839 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the card
3840 * which are unused. Pins are used if they are connected externally to a
3841 * component, whether that be to some other device, or a loop-back connection to
3842 * the component itself.
Stephen Warren16332812011-11-23 12:42:04 -07003843 */
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003844void snd_soc_dapm_auto_nc_pins(struct snd_soc_card *card)
Stephen Warren16332812011-11-23 12:42:04 -07003845{
Stephen Warren16332812011-11-23 12:42:04 -07003846 struct snd_soc_dapm_widget *w;
3847
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003848 dev_dbg(card->dev, "ASoC: Auto NC: DAPMs: card:%p\n", &card->dapm);
Stephen Warren16332812011-11-23 12:42:04 -07003849
3850 list_for_each_entry(w, &card->widgets, list) {
Stephen Warren16332812011-11-23 12:42:04 -07003851 switch (w->id) {
3852 case snd_soc_dapm_input:
3853 case snd_soc_dapm_output:
3854 case snd_soc_dapm_micbias:
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003855 dev_dbg(card->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003856 w->name);
3857 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003858 dev_dbg(card->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003859 "... Not in map; disabling\n");
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003860 snd_soc_dapm_nc_pin(w->dapm, w->name);
Stephen Warren16332812011-11-23 12:42:04 -07003861 }
3862 break;
3863 default:
3864 break;
3865 }
3866 }
3867}
3868
Mark Brown1547aba2010-05-07 21:11:40 +01003869/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003870 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003871 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003872 *
3873 * Free all dapm widgets and resources.
3874 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003875void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003876{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003877 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003878 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003879 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003880 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003881}
3882EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3883
Xiang Xiao57996352014-03-02 00:04:02 +08003884static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003885{
Liam Girdwood01005a72012-07-06 16:57:05 +01003886 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003887 struct snd_soc_dapm_widget *w;
3888 LIST_HEAD(down_list);
3889 int powerdown = 0;
3890
Liam Girdwood01005a72012-07-06 16:57:05 +01003891 mutex_lock(&card->dapm_mutex);
3892
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003893 list_for_each_entry(w, &dapm->card->widgets, list) {
3894 if (w->dapm != dapm)
3895 continue;
Mark Brown51737472009-06-22 13:16:51 +01003896 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003897 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003898 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003899 powerdown = 1;
3900 }
3901 }
3902
3903 /* If there were no widgets to power down we're already in
3904 * standby.
3905 */
3906 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003907 if (dapm->bias_level == SND_SOC_BIAS_ON)
3908 snd_soc_dapm_set_bias_level(dapm,
3909 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003910 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003911 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3912 snd_soc_dapm_set_bias_level(dapm,
3913 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003914 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003915
3916 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003917}
Mark Brown51737472009-06-22 13:16:51 +01003918
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003919/*
3920 * snd_soc_dapm_shutdown - callback for system shutdown
3921 */
3922void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3923{
Xiang Xiao57996352014-03-02 00:04:02 +08003924 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003925
Xiang Xiao57996352014-03-02 00:04:02 +08003926 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08003927 if (dapm != &card->dapm) {
3928 soc_dapm_shutdown_dapm(dapm);
3929 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3930 snd_soc_dapm_set_bias_level(dapm,
3931 SND_SOC_BIAS_OFF);
3932 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003933 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08003934
3935 soc_dapm_shutdown_dapm(&card->dapm);
3936 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3937 snd_soc_dapm_set_bias_level(&card->dapm,
3938 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01003939}
3940
Richard Purdie2b97eab2006-10-06 18:32:18 +02003941/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003942MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003943MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3944MODULE_LICENSE("GPL");