blob: 219d73c27a8c5704975468128b97d60482a24409 [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 Brown9949788b2010-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 Brown9949788b2010-05-07 20:24:05 +0100792
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000793 switch (level) {
Mark Brown9949788b2010-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 Brown9949788b2010-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 Browne56235e02011-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 Browne56235e02011-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 Brown246d0a12009-04-22 18:24:55 +01001172/* Check to see if a power supply is needed */
1173static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1174{
1175 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001176
Mark Brownde02d072011-09-20 21:43:24 +01001177 DAPM_UPDATE_STAT(w, power_checks);
1178
Mark Brown246d0a12009-04-22 18:24:55 +01001179 /* Check if one of our outputs is connected */
1180 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001181 DAPM_UPDATE_STAT(w, neighbour_checks);
1182
Mark Brownbf3a9e12011-06-13 16:42:29 +01001183 if (path->weak)
1184 continue;
1185
Mark Brown215edda2009-09-08 18:59:05 +01001186 if (path->connected &&
1187 !path->connected(path->source, path->sink))
1188 continue;
1189
Mark Brownf68d7e12011-10-04 22:57:50 +01001190 if (dapm_widget_power_check(path->sink))
1191 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001192 }
1193
Mark Brownf68d7e12011-10-04 22:57:50 +01001194 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001195}
1196
Mark Brown35c64bc2011-09-28 18:23:53 +01001197static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1198{
1199 return 1;
1200}
1201
Mark Brown38357ab2009-06-06 19:03:23 +01001202static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1203 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001204 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001205{
Mark Brown828a8422011-01-15 13:14:30 +00001206 int *sort;
1207
1208 if (power_up)
1209 sort = dapm_up_seq;
1210 else
1211 sort = dapm_down_seq;
1212
Mark Brown38357ab2009-06-06 19:03:23 +01001213 if (sort[a->id] != sort[b->id])
1214 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001215 if (a->subseq != b->subseq) {
1216 if (power_up)
1217 return a->subseq - b->subseq;
1218 else
1219 return b->subseq - a->subseq;
1220 }
Mark Brownb22ead22009-06-07 12:51:26 +01001221 if (a->reg != b->reg)
1222 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001223 if (a->dapm != b->dapm)
1224 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001225
Mark Brown38357ab2009-06-06 19:03:23 +01001226 return 0;
1227}
Mark Brown42aa3412009-03-01 19:21:10 +00001228
Mark Brown38357ab2009-06-06 19:03:23 +01001229/* Insert a widget in order into a DAPM power sequence. */
1230static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1231 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001232 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001233{
1234 struct snd_soc_dapm_widget *w;
1235
1236 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001237 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001238 list_add_tail(&new_widget->power_list, &w->power_list);
1239 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001240 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001241
Mark Brown38357ab2009-06-06 19:03:23 +01001242 list_add_tail(&new_widget->power_list, list);
1243}
Mark Brown42aa3412009-03-01 19:21:10 +00001244
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001245static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001246 struct snd_soc_dapm_widget *w, int event)
1247{
Mark Brown68f89ad2010-11-03 23:51:49 -04001248 const char *ev_name;
1249 int power, ret;
1250
1251 switch (event) {
1252 case SND_SOC_DAPM_PRE_PMU:
1253 ev_name = "PRE_PMU";
1254 power = 1;
1255 break;
1256 case SND_SOC_DAPM_POST_PMU:
1257 ev_name = "POST_PMU";
1258 power = 1;
1259 break;
1260 case SND_SOC_DAPM_PRE_PMD:
1261 ev_name = "PRE_PMD";
1262 power = 0;
1263 break;
1264 case SND_SOC_DAPM_POST_PMD:
1265 ev_name = "POST_PMD";
1266 power = 0;
1267 break;
Mark Brown80114122013-02-25 15:14:19 +00001268 case SND_SOC_DAPM_WILL_PMU:
1269 ev_name = "WILL_PMU";
1270 power = 1;
1271 break;
1272 case SND_SOC_DAPM_WILL_PMD:
1273 ev_name = "WILL_PMD";
1274 power = 0;
1275 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001276 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001277 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001278 return;
1279 }
1280
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001281 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001282 return;
1283
1284 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001285 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001286 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001287 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001288 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001289 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001290 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001291 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001292 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001293 ev_name, w->name, ret);
1294 }
1295}
1296
Mark Brownb22ead22009-06-07 12:51:26 +01001297/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001298static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001299 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001300{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001301 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001302 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001303 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001304 unsigned int value = 0;
1305 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001306
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001307 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1308 reg = w->reg;
1309 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001310
1311 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001312 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001313 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001314
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001315 mask |= w->mask << w->shift;
1316 if (w->power)
1317 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001318 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001319 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001320
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001321 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001322 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1323 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001324
Mark Brown68f89ad2010-11-03 23:51:49 -04001325 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001326 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1327 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001328 }
1329
Mark Brown81628102009-06-07 13:21:24 +01001330 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001331 /* Any widget will do, they should all be updating the
1332 * same register.
1333 */
Mark Brown29376bc2011-06-19 13:49:28 +01001334
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001335 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001336 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001337 value, mask, reg, card->pop_time);
1338 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001339 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001340 }
1341
1342 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001343 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1344 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001345 }
Mark Brown42aa3412009-03-01 19:21:10 +00001346}
1347
Mark Brownb22ead22009-06-07 12:51:26 +01001348/* Apply a DAPM power sequence.
1349 *
1350 * We walk over a pre-sorted list of widgets to apply power to. In
1351 * order to minimise the number of writes to the device required
1352 * multiple widgets will be updated in a single write where possible.
1353 * Currently anything that requires more than a single write is not
1354 * handled.
1355 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001356static void dapm_seq_run(struct snd_soc_card *card,
1357 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001358{
1359 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001360 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001361 LIST_HEAD(pending);
1362 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001363 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001364 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001365 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001366 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001367 int *sort;
1368
1369 if (power_up)
1370 sort = dapm_up_seq;
1371 else
1372 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001373
Mark Brownb22ead22009-06-07 12:51:26 +01001374 list_for_each_entry_safe(w, n, list, power_list) {
1375 ret = 0;
1376
1377 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001378 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001379 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001380 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001381 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001382
Mark Brown474b62d2011-01-18 16:14:44 +00001383 if (cur_dapm && cur_dapm->seq_notifier) {
1384 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1385 if (sort[i] == cur_sort)
1386 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001387 i,
1388 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001389 }
1390
Mark Browneb270e92013-10-09 13:52:52 +01001391 if (cur_dapm && w->dapm != cur_dapm)
1392 soc_dapm_async_complete(cur_dapm);
1393
Mark Brownb22ead22009-06-07 12:51:26 +01001394 INIT_LIST_HEAD(&pending);
1395 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001396 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001397 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001398 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001399 }
1400
Mark Brown163cac02009-06-07 10:12:52 +01001401 switch (w->id) {
1402 case snd_soc_dapm_pre:
1403 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001404 list_for_each_entry_safe_continue(w, n, list,
1405 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001406
Mark Brownb22ead22009-06-07 12:51:26 +01001407 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001408 ret = w->event(w,
1409 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001410 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001411 ret = w->event(w,
1412 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001413 break;
1414
1415 case snd_soc_dapm_post:
1416 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001417 list_for_each_entry_safe_continue(w, n, list,
1418 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001419
Mark Brownb22ead22009-06-07 12:51:26 +01001420 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001421 ret = w->event(w,
1422 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001423 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001424 ret = w->event(w,
1425 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001426 break;
1427
Mark Brown163cac02009-06-07 10:12:52 +01001428 default:
Mark Brown81628102009-06-07 13:21:24 +01001429 /* Queue it up for application */
1430 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001431 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001432 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001433 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001434 list_move(&w->power_list, &pending);
1435 break;
Mark Brown163cac02009-06-07 10:12:52 +01001436 }
Mark Brownb22ead22009-06-07 12:51:26 +01001437
1438 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001439 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001440 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001441 }
Mark Brownb22ead22009-06-07 12:51:26 +01001442
1443 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001444 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001445
1446 if (cur_dapm && cur_dapm->seq_notifier) {
1447 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1448 if (sort[i] == cur_sort)
1449 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001450 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001451 }
Mark Browneb270e92013-10-09 13:52:52 +01001452
1453 list_for_each_entry(d, &card->dapm_list, list) {
1454 soc_dapm_async_complete(d);
1455 }
Mark Brown163cac02009-06-07 10:12:52 +01001456}
1457
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001458static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001459{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001460 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001461 struct snd_soc_dapm_widget_list *wlist;
1462 struct snd_soc_dapm_widget *w = NULL;
1463 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001464 int ret;
1465
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001466 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001467 return;
1468
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001469 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001470
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001471 for (wi = 0; wi < wlist->num_widgets; wi++) {
1472 w = wlist->widgets[wi];
1473
1474 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1475 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1476 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001477 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001478 w->name, ret);
1479 }
Mark Brown97404f22010-12-14 16:13:57 +00001480 }
1481
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001482 if (!w)
1483 return;
1484
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001485 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1486 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001487 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001488 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001489 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001490
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001491 for (wi = 0; wi < wlist->num_widgets; wi++) {
1492 w = wlist->widgets[wi];
1493
1494 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1495 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1496 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001497 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001498 w->name, ret);
1499 }
Mark Brown97404f22010-12-14 16:13:57 +00001500 }
1501}
1502
Mark Brown9d0624a2011-02-18 11:49:43 -08001503/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1504 * they're changing state.
1505 */
1506static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1507{
1508 struct snd_soc_dapm_context *d = data;
1509 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001510
Mark Brown56fba412011-06-04 11:25:10 +01001511 /* If we're off and we're not supposed to be go into STANDBY */
1512 if (d->bias_level == SND_SOC_BIAS_OFF &&
1513 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001514 if (d->dev)
1515 pm_runtime_get_sync(d->dev);
1516
Mark Brown9d0624a2011-02-18 11:49:43 -08001517 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1518 if (ret != 0)
1519 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001520 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001521 }
1522
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001523 /* Prepare for a transition to ON or away from ON */
1524 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1525 d->bias_level != SND_SOC_BIAS_ON) ||
1526 (d->target_bias_level != SND_SOC_BIAS_ON &&
1527 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001528 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1529 if (ret != 0)
1530 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001531 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001532 }
1533}
1534
1535/* Async callback run prior to DAPM sequences - brings to their final
1536 * state.
1537 */
1538static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1539{
1540 struct snd_soc_dapm_context *d = data;
1541 int ret;
1542
1543 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001544 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1545 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1546 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001547 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1548 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001549 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001550 ret);
1551 }
1552
1553 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001554 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1555 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001556 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1557 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001558 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1559 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001560
1561 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001562 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001563 }
1564
1565 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001566 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1567 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001568 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1569 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001570 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001571 ret);
1572 }
1573}
Mark Brown97404f22010-12-14 16:13:57 +00001574
Mark Brownfe4fda52011-10-03 22:36:57 +01001575static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1576 bool power, bool connect)
1577{
1578 /* If a connection is being made or broken then that update
1579 * will have marked the peer dirty, otherwise the widgets are
1580 * not connected and this update has no impact. */
1581 if (!connect)
1582 return;
1583
1584 /* If the peer is already in the state we're moving to then we
1585 * won't have an impact on it. */
1586 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001587 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001588}
1589
Mark Brown05623c42011-09-28 17:02:31 +01001590static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1591 struct list_head *up_list,
1592 struct list_head *down_list)
1593{
Mark Browndb432b42011-10-03 21:06:40 +01001594 struct snd_soc_dapm_path *path;
1595
Mark Brown05623c42011-09-28 17:02:31 +01001596 if (w->power == power)
1597 return;
1598
1599 trace_snd_soc_dapm_widget_power(w, power);
1600
Mark Browndb432b42011-10-03 21:06:40 +01001601 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001602 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001603 */
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001604 list_for_each_entry(path, &w->sources, list_sink)
1605 dapm_widget_set_peer_power(path->source, power, path->connect);
1606
Mark Brownf3bf3e42011-10-04 22:43:31 +01001607 switch (w->id) {
1608 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001609 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001610 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001611 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001612 /* Supplies can't affect their outputs, only their inputs */
1613 break;
1614 default:
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001615 list_for_each_entry(path, &w->sinks, list_source)
1616 dapm_widget_set_peer_power(path->sink, power,
1617 path->connect);
Mark Brownf3bf3e42011-10-04 22:43:31 +01001618 break;
Mark Browndb432b42011-10-03 21:06:40 +01001619 }
1620
Mark Brown05623c42011-09-28 17:02:31 +01001621 if (power)
1622 dapm_seq_insert(w, up_list, true);
1623 else
1624 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001625}
1626
Mark Brown7c81beb2011-09-20 22:22:32 +01001627static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1628 struct list_head *up_list,
1629 struct list_head *down_list)
1630{
Mark Brown7c81beb2011-09-20 22:22:32 +01001631 int power;
1632
1633 switch (w->id) {
1634 case snd_soc_dapm_pre:
1635 dapm_seq_insert(w, down_list, false);
1636 break;
1637 case snd_soc_dapm_post:
1638 dapm_seq_insert(w, up_list, true);
1639 break;
1640
1641 default:
Mark Brownd8050022011-09-28 18:28:23 +01001642 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001643
Mark Brown05623c42011-09-28 17:02:31 +01001644 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001645 break;
1646 }
1647}
1648
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001649static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1650{
1651 if (dapm->idle_bias_off)
1652 return true;
1653
1654 switch (snd_power_get_state(dapm->card->snd_card)) {
1655 case SNDRV_CTL_POWER_D3hot:
1656 case SNDRV_CTL_POWER_D3cold:
1657 return dapm->suspend_bias_off;
1658 default:
1659 break;
1660 }
1661
1662 return false;
1663}
1664
Mark Brown42aa3412009-03-01 19:21:10 +00001665/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001666 * Scan each dapm widget for complete audio path.
1667 * A complete path is a route that has valid endpoints i.e.:-
1668 *
1669 * o DAC to output pin.
1670 * o Input Pin to ADC.
1671 * o Input pin to Output pin (bypass, sidetone)
1672 * o DAC to ADC (loopback).
1673 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001674static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001675{
1676 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001677 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001678 LIST_HEAD(up_list);
1679 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001680 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001681 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001682
Mark Brownf9fa2b12014-03-06 16:49:11 +08001683 lockdep_assert_held(&card->dapm_mutex);
1684
Mark Brown84e90932010-11-04 00:07:02 -04001685 trace_snd_soc_dapm_start(card);
1686
Mark Brown56fba412011-06-04 11:25:10 +01001687 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001688 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001689 d->target_bias_level = SND_SOC_BIAS_OFF;
1690 else
1691 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001692 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001693
Liam Girdwood6c120e12012-02-15 15:15:34 +00001694 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001695
Mark Brown6d3ddc82009-05-16 17:47:29 +01001696 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001697 * lists indicating if they should be powered up or down. We
1698 * only check widgets that have been flagged as dirty but note
1699 * that new widgets may be added to the dirty list while we
1700 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001701 */
Mark Browndb432b42011-10-03 21:06:40 +01001702 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001703 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001704 }
1705
Mark Brownf9de6d72011-09-28 17:19:47 +01001706 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001707 switch (w->id) {
1708 case snd_soc_dapm_pre:
1709 case snd_soc_dapm_post:
1710 /* These widgets always need to be powered */
1711 break;
1712 default:
1713 list_del_init(&w->dirty);
1714 break;
1715 }
Mark Browndb432b42011-10-03 21:06:40 +01001716
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001717 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001718 d = w->dapm;
1719
1720 /* Supplies and micbiases only bring the
1721 * context up to STANDBY as unless something
1722 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001723 * generally don't require full power. Signal
1724 * generators are virtual pins and have no
1725 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001726 */
1727 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001728 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001729 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001730 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001731 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001732 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001733 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001734 case snd_soc_dapm_micbias:
1735 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1736 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1737 break;
1738 default:
1739 d->target_bias_level = SND_SOC_BIAS_ON;
1740 break;
1741 }
1742 }
1743
1744 }
1745
Mark Brown85a843c2011-09-21 21:29:47 +01001746 /* Force all contexts in the card to the same bias state if
1747 * they're not ground referenced.
1748 */
Mark Brown56fba412011-06-04 11:25:10 +01001749 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001750 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001751 if (d->target_bias_level > bias)
1752 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001753 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001754 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01001755 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001756
Mark Brownde02d072011-09-20 21:43:24 +01001757 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001758
Xiang Xiao17282ba2014-03-02 00:04:03 +08001759 /* Run card bias changes at first */
1760 dapm_pre_sequence_async(&card->dapm, 0);
1761 /* Run other bias changes in parallel */
1762 list_for_each_entry(d, &card->dapm_list, list) {
1763 if (d != &card->dapm)
1764 async_schedule_domain(dapm_pre_sequence_async, d,
1765 &async_domain);
1766 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001767 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001768
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001769 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001770 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001771 }
1772
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001773 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001774 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001775 }
1776
Mark Brown6d3ddc82009-05-16 17:47:29 +01001777 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001778 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001779
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001780 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001781
Mark Brown6d3ddc82009-05-16 17:47:29 +01001782 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001783 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001784
Mark Brown9d0624a2011-02-18 11:49:43 -08001785 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001786 list_for_each_entry(d, &card->dapm_list, list) {
1787 if (d != &card->dapm)
1788 async_schedule_domain(dapm_post_sequence_async, d,
1789 &async_domain);
1790 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001791 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001792 /* Run card bias changes at last */
1793 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001794
Liam Girdwood8078d872012-02-15 15:15:35 +00001795 /* do we need to notify any clients that DAPM event is complete */
1796 list_for_each_entry(d, &card->dapm_list, list) {
1797 if (d->stream_event)
1798 d->stream_event(d, event);
1799 }
1800
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001801 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001802 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001803 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001804
Mark Brown84e90932010-11-04 00:07:02 -04001805 trace_snd_soc_dapm_done(card);
1806
Mark Brown42aa3412009-03-01 19:21:10 +00001807 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001808}
1809
Mark Brown79fb9382009-08-21 16:38:13 +01001810#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001811static ssize_t dapm_widget_power_read_file(struct file *file,
1812 char __user *user_buf,
1813 size_t count, loff_t *ppos)
1814{
1815 struct snd_soc_dapm_widget *w = file->private_data;
1816 char *buf;
1817 int in, out;
1818 ssize_t ret;
1819 struct snd_soc_dapm_path *p = NULL;
1820
1821 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1822 if (!buf)
1823 return -ENOMEM;
1824
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001825 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001826 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001827 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001828 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001829
Mark Brownf13ebad2012-03-03 18:01:01 +00001830 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1831 w->name, w->power ? "On" : "Off",
1832 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001833
Mark Brownd033c362009-12-04 15:25:56 +00001834 if (w->reg >= 0)
1835 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001836 " - R%d(0x%x) mask 0x%x",
1837 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001838
1839 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1840
Mark Brown3eef08b2009-09-14 16:49:00 +01001841 if (w->sname)
1842 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1843 w->sname,
1844 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001845
1846 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001847 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001848 continue;
1849
Mark Brown79fb9382009-08-21 16:38:13 +01001850 if (p->connect)
1851 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001852 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001853 p->name ? p->name : "static",
1854 p->source->name);
1855 }
1856 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001857 if (p->connected && !p->connected(w, p->sink))
1858 continue;
1859
Mark Brown79fb9382009-08-21 16:38:13 +01001860 if (p->connect)
1861 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001862 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001863 p->name ? p->name : "static",
1864 p->sink->name);
1865 }
1866
1867 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1868
1869 kfree(buf);
1870 return ret;
1871}
1872
1873static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001874 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001875 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001876 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001877};
1878
Mark Brownef49e4f2011-04-04 20:48:13 +09001879static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1880 size_t count, loff_t *ppos)
1881{
1882 struct snd_soc_dapm_context *dapm = file->private_data;
1883 char *level;
1884
1885 switch (dapm->bias_level) {
1886 case SND_SOC_BIAS_ON:
1887 level = "On\n";
1888 break;
1889 case SND_SOC_BIAS_PREPARE:
1890 level = "Prepare\n";
1891 break;
1892 case SND_SOC_BIAS_STANDBY:
1893 level = "Standby\n";
1894 break;
1895 case SND_SOC_BIAS_OFF:
1896 level = "Off\n";
1897 break;
1898 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001899 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09001900 level = "Unknown\n";
1901 break;
1902 }
1903
1904 return simple_read_from_buffer(user_buf, count, ppos, level,
1905 strlen(level));
1906}
1907
1908static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001909 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001910 .read = dapm_bias_read_file,
1911 .llseek = default_llseek,
1912};
1913
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001914void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1915 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001916{
Mark Brown79fb9382009-08-21 16:38:13 +01001917 struct dentry *d;
1918
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001919 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1920
1921 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001922 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001923 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001924 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001925 }
Mark Brown79fb9382009-08-21 16:38:13 +01001926
Mark Brownef49e4f2011-04-04 20:48:13 +09001927 d = debugfs_create_file("bias_level", 0444,
1928 dapm->debugfs_dapm, dapm,
1929 &dapm_bias_fops);
1930 if (!d)
1931 dev_warn(dapm->dev,
1932 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001933}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001934
1935static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1936{
1937 struct snd_soc_dapm_context *dapm = w->dapm;
1938 struct dentry *d;
1939
1940 if (!dapm->debugfs_dapm || !w->name)
1941 return;
1942
1943 d = debugfs_create_file(w->name, 0444,
1944 dapm->debugfs_dapm, w,
1945 &dapm_widget_power_fops);
1946 if (!d)
1947 dev_warn(w->dapm->dev,
1948 "ASoC: Failed to create %s debugfs file\n",
1949 w->name);
1950}
1951
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001952static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1953{
1954 debugfs_remove_recursive(dapm->debugfs_dapm);
1955}
1956
Mark Brown79fb9382009-08-21 16:38:13 +01001957#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001958void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1959 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001960{
1961}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001962
1963static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1964{
1965}
1966
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001967static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1968{
1969}
1970
Mark Brown79fb9382009-08-21 16:38:13 +01001971#endif
1972
Richard Purdie2b97eab2006-10-06 18:32:18 +02001973/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001974static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001975 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001976{
1977 struct snd_soc_dapm_path *path;
1978 int found = 0;
1979
Mark Brownf9fa2b12014-03-06 16:49:11 +08001980 lockdep_assert_held(&card->dapm_mutex);
1981
Richard Purdie2b97eab2006-10-06 18:32:18 +02001982 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02001983 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001984 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001985 continue;
1986
1987 found = 1;
1988 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001989 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001990 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001991 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001992 } else {
1993 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001994 dapm_mark_dirty(path->source,
1995 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001996 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001997 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001998 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001999 }
2000
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002001 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002002 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002003
Liam Girdwood618dae12012-04-25 12:12:51 +01002004 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002005}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002006
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002007int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002008 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2009 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002010{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002011 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002012 int ret;
2013
Liam Girdwood3cd04342012-03-09 12:02:08 +00002014 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002015 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002016 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002017 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002018 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002019 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002020 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002021 return ret;
2022}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002023EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002024
Milan plzik1b075e32008-01-10 14:39:46 +01002025/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002026static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002027 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002028{
2029 struct snd_soc_dapm_path *path;
2030 int found = 0;
2031
Mark Brownf9fa2b12014-03-06 16:49:11 +08002032 lockdep_assert_held(&card->dapm_mutex);
2033
Richard Purdie2b97eab2006-10-06 18:32:18 +02002034 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002035 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002036 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002037 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002038 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002039 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002040 }
2041
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002042 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002043 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002044
Liam Girdwood618dae12012-04-25 12:12:51 +01002045 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002046}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002047
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002048int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002049 struct snd_kcontrol *kcontrol, int connect,
2050 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002051{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002052 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002053 int ret;
2054
Liam Girdwood3cd04342012-03-09 12:02:08 +00002055 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002056 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002057 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002058 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002059 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002060 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002061 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002062 return ret;
2063}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002064EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002065
Benoit Cousson44ba2642014-07-08 23:19:36 +02002066static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002067{
Richard Purdie2b97eab2006-10-06 18:32:18 +02002068 struct snd_soc_dapm_widget *w;
2069 int count = 0;
2070 char *state = "not set";
2071
Lars-Peter Clausen00200102014-07-17 22:01:07 +02002072 list_for_each_entry(w, &codec->component.card->widgets, list) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002073 if (w->dapm != &codec->dapm)
2074 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002075
2076 /* only display widgets that burnm power */
2077 switch (w->id) {
2078 case snd_soc_dapm_hp:
2079 case snd_soc_dapm_mic:
2080 case snd_soc_dapm_spk:
2081 case snd_soc_dapm_line:
2082 case snd_soc_dapm_micbias:
2083 case snd_soc_dapm_dac:
2084 case snd_soc_dapm_adc:
2085 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002086 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002087 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002088 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002089 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002090 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002091 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002092 if (w->name)
2093 count += sprintf(buf + count, "%s: %s\n",
2094 w->name, w->power ? "On":"Off");
2095 break;
2096 default:
2097 break;
2098 }
2099 }
2100
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002101 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002102 case SND_SOC_BIAS_ON:
2103 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002104 break;
Mark Brown0be98982008-05-19 12:31:28 +02002105 case SND_SOC_BIAS_PREPARE:
2106 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002107 break;
Mark Brown0be98982008-05-19 12:31:28 +02002108 case SND_SOC_BIAS_STANDBY:
2109 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002110 break;
Mark Brown0be98982008-05-19 12:31:28 +02002111 case SND_SOC_BIAS_OFF:
2112 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002113 break;
2114 }
2115 count += sprintf(buf + count, "PM State: %s\n", state);
2116
2117 return count;
2118}
2119
Benoit Cousson44ba2642014-07-08 23:19:36 +02002120/* show dapm widget status in sys fs */
2121static ssize_t dapm_widget_show(struct device *dev,
2122 struct device_attribute *attr, char *buf)
2123{
2124 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2125 int i, count = 0;
2126
2127 for (i = 0; i < rtd->num_codecs; i++) {
2128 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
2129 count += dapm_widget_show_codec(codec, buf + count);
2130 }
2131
2132 return count;
2133}
2134
Richard Purdie2b97eab2006-10-06 18:32:18 +02002135static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2136
2137int snd_soc_dapm_sys_add(struct device *dev)
2138{
Troy Kisky12ef1932008-10-13 17:42:14 -07002139 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002140}
2141
2142static void snd_soc_dapm_sys_remove(struct device *dev)
2143{
Mark Brownaef90842009-05-16 17:53:16 +01002144 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002145}
2146
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002147static void dapm_free_path(struct snd_soc_dapm_path *path)
2148{
2149 list_del(&path->list_sink);
2150 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002151 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002152 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002153 kfree(path);
2154}
2155
Richard Purdie2b97eab2006-10-06 18:32:18 +02002156/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002157static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002158{
2159 struct snd_soc_dapm_widget *w, *next_w;
2160 struct snd_soc_dapm_path *p, *next_p;
2161
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002162 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2163 if (w->dapm != dapm)
2164 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002165 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002166 /*
2167 * remove source and sink paths associated to this widget.
2168 * While removing the path, remove reference to it from both
2169 * source and sink widgets so that path is removed only once.
2170 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002171 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2172 dapm_free_path(p);
2173
2174 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2175 dapm_free_path(p);
2176
Stephen Warrenfad59882011-04-28 17:37:59 -06002177 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002178 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002179 kfree(w);
2180 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002181}
2182
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002183static struct snd_soc_dapm_widget *dapm_find_widget(
2184 struct snd_soc_dapm_context *dapm, const char *pin,
2185 bool search_other_contexts)
2186{
2187 struct snd_soc_dapm_widget *w;
2188 struct snd_soc_dapm_widget *fallback = NULL;
2189
2190 list_for_each_entry(w, &dapm->card->widgets, list) {
2191 if (!strcmp(w->name, pin)) {
2192 if (w->dapm == dapm)
2193 return w;
2194 else
2195 fallback = w;
2196 }
2197 }
2198
2199 if (search_other_contexts)
2200 return fallback;
2201
2202 return NULL;
2203}
2204
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002205static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002206 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002207{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002208 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002209
Mark Brownf9fa2b12014-03-06 16:49:11 +08002210 dapm_assert_locked(dapm);
2211
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002212 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002213 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002214 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002215 }
2216
Mark Brown1a8b2d92012-02-16 11:50:07 -08002217 if (w->connected != status)
2218 dapm_mark_dirty(w, "pin configuration");
2219
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002220 w->connected = status;
2221 if (status == 0)
2222 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002223
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002224 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002225}
2226
Richard Purdie2b97eab2006-10-06 18:32:18 +02002227/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002228 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2229 * @dapm: DAPM context
2230 *
2231 * Walks all dapm audio paths and powers widgets according to their
2232 * stream or path usage.
2233 *
2234 * Requires external locking.
2235 *
2236 * Returns 0 for success.
2237 */
2238int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2239{
2240 /*
2241 * Suppress early reports (eg, jacks syncing their state) to avoid
2242 * silly DAPM runs during card startup.
2243 */
2244 if (!dapm->card || !dapm->card->instantiated)
2245 return 0;
2246
2247 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2248}
2249EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2250
2251/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002252 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002253 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002254 *
2255 * Walks all dapm audio paths and powers widgets according to their
2256 * stream or path usage.
2257 *
2258 * Returns 0 for success.
2259 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002260int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002261{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002262 int ret;
2263
Liam Girdwood3cd04342012-03-09 12:02:08 +00002264 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002265 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002266 mutex_unlock(&dapm->card->dapm_mutex);
2267 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002268}
Liam Girdwooda5302182008-07-07 13:35:17 +01002269EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002270
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002271static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2272 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2273 const char *control,
2274 int (*connected)(struct snd_soc_dapm_widget *source,
2275 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002276{
2277 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002278 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002279
2280 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2281 if (!path)
2282 return -ENOMEM;
2283
2284 path->source = wsource;
2285 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002286 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002287 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002288 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002289 INIT_LIST_HEAD(&path->list_source);
2290 INIT_LIST_HEAD(&path->list_sink);
2291
2292 /* check for external widgets */
2293 if (wsink->id == snd_soc_dapm_input) {
2294 if (wsource->id == snd_soc_dapm_micbias ||
2295 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002296 wsource->id == snd_soc_dapm_line ||
2297 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002298 wsink->ext = 1;
2299 }
2300 if (wsource->id == snd_soc_dapm_output) {
2301 if (wsink->id == snd_soc_dapm_spk ||
2302 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002303 wsink->id == snd_soc_dapm_line ||
2304 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002305 wsource->ext = 1;
2306 }
2307
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002308 dapm_mark_dirty(wsource, "Route added");
2309 dapm_mark_dirty(wsink, "Route added");
2310
Richard Purdie2b97eab2006-10-06 18:32:18 +02002311 /* connect static paths */
2312 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002313 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002314 list_add(&path->list_sink, &wsink->sources);
2315 list_add(&path->list_source, &wsource->sinks);
2316 path->connect = 1;
2317 return 0;
2318 }
2319
2320 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002321 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002322 case snd_soc_dapm_adc:
2323 case snd_soc_dapm_dac:
2324 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002325 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002326 case snd_soc_dapm_input:
2327 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002328 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002329 case snd_soc_dapm_micbias:
2330 case snd_soc_dapm_vmid:
2331 case snd_soc_dapm_pre:
2332 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002333 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002334 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002335 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002336 case snd_soc_dapm_aif_in:
2337 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002338 case snd_soc_dapm_dai_in:
2339 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002340 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002341 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002342 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002343 list_add(&path->list_sink, &wsink->sources);
2344 list_add(&path->list_source, &wsource->sinks);
2345 path->connect = 1;
2346 return 0;
2347 case snd_soc_dapm_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002348 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002349 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002350 if (ret != 0)
2351 goto err;
2352 break;
2353 case snd_soc_dapm_switch:
2354 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002355 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002356 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002357 if (ret != 0)
2358 goto err;
2359 break;
2360 case snd_soc_dapm_hp:
2361 case snd_soc_dapm_mic:
2362 case snd_soc_dapm_line:
2363 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002364 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002365 list_add(&path->list_sink, &wsink->sources);
2366 list_add(&path->list_source, &wsource->sinks);
2367 path->connect = 0;
2368 return 0;
2369 }
Mark Brownfabd0382012-07-05 17:20:06 +01002370
Richard Purdie2b97eab2006-10-06 18:32:18 +02002371 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002372err:
2373 kfree(path);
2374 return ret;
2375}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002376
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002377static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002378 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002379{
2380 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2381 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2382 const char *sink;
2383 const char *source;
2384 char prefixed_sink[80];
2385 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002386 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002387 int ret;
2388
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002389 prefix = soc_dapm_prefix(dapm);
2390 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002391 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002392 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002393 sink = prefixed_sink;
2394 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002395 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002396 source = prefixed_source;
2397 } else {
2398 sink = route->sink;
2399 source = route->source;
2400 }
2401
2402 /*
2403 * find src and dest widgets over all widgets but favor a widget from
2404 * current DAPM context
2405 */
2406 list_for_each_entry(w, &dapm->card->widgets, list) {
2407 if (!wsink && !(strcmp(w->name, sink))) {
2408 wtsink = w;
2409 if (w->dapm == dapm)
2410 wsink = w;
2411 continue;
2412 }
2413 if (!wsource && !(strcmp(w->name, source))) {
2414 wtsource = w;
2415 if (w->dapm == dapm)
2416 wsource = w;
2417 }
2418 }
2419 /* use widget from another DAPM context if not found from this */
2420 if (!wsink)
2421 wsink = wtsink;
2422 if (!wsource)
2423 wsource = wtsource;
2424
2425 if (wsource == NULL) {
2426 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2427 route->source);
2428 return -ENODEV;
2429 }
2430 if (wsink == NULL) {
2431 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2432 route->sink);
2433 return -ENODEV;
2434 }
2435
2436 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2437 route->connected);
2438 if (ret)
2439 goto err;
2440
2441 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002442err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002443 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002444 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002445 return ret;
2446}
Mark Brown105f1c22008-05-13 14:52:19 +02002447
Mark Brownefcc3c62012-07-05 17:24:19 +01002448static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2449 const struct snd_soc_dapm_route *route)
2450{
2451 struct snd_soc_dapm_path *path, *p;
2452 const char *sink;
2453 const char *source;
2454 char prefixed_sink[80];
2455 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002456 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002457
2458 if (route->control) {
2459 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002460 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002461 return -EINVAL;
2462 }
2463
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002464 prefix = soc_dapm_prefix(dapm);
2465 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002466 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002467 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002468 sink = prefixed_sink;
2469 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002470 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002471 source = prefixed_source;
2472 } else {
2473 sink = route->sink;
2474 source = route->source;
2475 }
2476
2477 path = NULL;
2478 list_for_each_entry(p, &dapm->card->paths, list) {
2479 if (strcmp(p->source->name, source) != 0)
2480 continue;
2481 if (strcmp(p->sink->name, sink) != 0)
2482 continue;
2483 path = p;
2484 break;
2485 }
2486
2487 if (path) {
2488 dapm_mark_dirty(path->source, "Route removed");
2489 dapm_mark_dirty(path->sink, "Route removed");
2490
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002491 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002492 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002493 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002494 source, sink);
2495 }
2496
2497 return 0;
2498}
2499
Mark Brown105f1c22008-05-13 14:52:19 +02002500/**
Mark Brown105f1c22008-05-13 14:52:19 +02002501 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002502 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002503 * @route: audio routes
2504 * @num: number of routes
2505 *
2506 * Connects 2 dapm widgets together via a named audio path. The sink is
2507 * the widget receiving the audio signal, whilst the source is the sender
2508 * of the audio signal.
2509 *
2510 * Returns 0 for success else error. On error all resources can be freed
2511 * with a call to snd_soc_card_free().
2512 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002513int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002514 const struct snd_soc_dapm_route *route, int num)
2515{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002516 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002517
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002518 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002519 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002520 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002521 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002522 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2523 route->source,
2524 route->control ? route->control : "direct",
2525 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002526 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002527 }
2528 route++;
2529 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002530 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002531
Dan Carpenter60884c22012-04-13 22:25:43 +03002532 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002533}
2534EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2535
Mark Brownefcc3c62012-07-05 17:24:19 +01002536/**
2537 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2538 * @dapm: DAPM context
2539 * @route: audio routes
2540 * @num: number of routes
2541 *
2542 * Removes routes from the DAPM context.
2543 */
2544int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2545 const struct snd_soc_dapm_route *route, int num)
2546{
2547 int i, ret = 0;
2548
2549 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2550 for (i = 0; i < num; i++) {
2551 snd_soc_dapm_del_route(dapm, route);
2552 route++;
2553 }
2554 mutex_unlock(&dapm->card->dapm_mutex);
2555
2556 return ret;
2557}
2558EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2559
Mark Brownbf3a9e12011-06-13 16:42:29 +01002560static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2561 const struct snd_soc_dapm_route *route)
2562{
2563 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2564 route->source,
2565 true);
2566 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2567 route->sink,
2568 true);
2569 struct snd_soc_dapm_path *path;
2570 int count = 0;
2571
2572 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002573 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002574 route->source);
2575 return -ENODEV;
2576 }
2577
2578 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002579 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002580 route->sink);
2581 return -ENODEV;
2582 }
2583
2584 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002585 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002586 route->source, route->sink);
2587
2588 list_for_each_entry(path, &source->sinks, list_source) {
2589 if (path->sink == sink) {
2590 path->weak = 1;
2591 count++;
2592 }
2593 }
2594
2595 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002596 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002597 route->source, route->sink);
2598 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002599 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002600 count, route->source, route->sink);
2601
2602 return 0;
2603}
2604
2605/**
2606 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2607 * @dapm: DAPM context
2608 * @route: audio routes
2609 * @num: number of routes
2610 *
2611 * Mark existing routes matching those specified in the passed array
2612 * as being weak, meaning that they are ignored for the purpose of
2613 * power decisions. The main intended use case is for sidetone paths
2614 * which couple audio between other independent paths if they are both
2615 * active in order to make the combination work better at the user
2616 * level but which aren't intended to be "used".
2617 *
2618 * Note that CODEC drivers should not use this as sidetone type paths
2619 * can frequently also be used as bypass paths.
2620 */
2621int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2622 const struct snd_soc_dapm_route *route, int num)
2623{
2624 int i, err;
2625 int ret = 0;
2626
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002627 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002628 for (i = 0; i < num; i++) {
2629 err = snd_soc_dapm_weak_route(dapm, route);
2630 if (err)
2631 ret = err;
2632 route++;
2633 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002634 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002635
2636 return ret;
2637}
2638EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2639
Mark Brown105f1c22008-05-13 14:52:19 +02002640/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002641 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002642 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002643 *
2644 * Checks the codec for any new dapm widgets and creates them if found.
2645 *
2646 * Returns 0 for success.
2647 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002648int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002649{
2650 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002651 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002652
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002653 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002654
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002655 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002656 {
2657 if (w->new)
2658 continue;
2659
Stephen Warrenfad59882011-04-28 17:37:59 -06002660 if (w->num_kcontrols) {
2661 w->kcontrols = kzalloc(w->num_kcontrols *
2662 sizeof(struct snd_kcontrol *),
2663 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002664 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002665 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002666 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002667 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002668 }
2669
Richard Purdie2b97eab2006-10-06 18:32:18 +02002670 switch(w->id) {
2671 case snd_soc_dapm_switch:
2672 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002673 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002674 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002675 break;
2676 case snd_soc_dapm_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002677 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002678 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002679 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002680 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002681 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002682 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002683 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002684 break;
2685 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002686
2687 /* Read the initial power state from the device */
2688 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002689 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002690 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002691 val &= w->mask;
2692 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002693 w->power = 1;
2694 }
2695
Richard Purdie2b97eab2006-10-06 18:32:18 +02002696 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002697
Mark Brown7508b122011-10-05 12:09:12 +01002698 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002699 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002700 }
2701
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002702 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2703 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002704 return 0;
2705}
2706EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2707
2708/**
2709 * snd_soc_dapm_get_volsw - dapm mixer get callback
2710 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002711 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002712 *
2713 * Callback to get the value of a dapm mixer control.
2714 *
2715 * Returns 0 for success.
2716 */
2717int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2718 struct snd_ctl_elem_value *ucontrol)
2719{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002720 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2721 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002722 struct soc_mixer_control *mc =
2723 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002724 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002725 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002726 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002727 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002728 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002729 unsigned int val;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002730 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002731
2732 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002733 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002734 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002735 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002736
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002737 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002738 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
2739 ret = soc_dapm_read(dapm, reg, &val);
2740 val = (val >> shift) & mask;
2741 } else {
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002742 val = dapm_kcontrol_get_value(kcontrol);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002743 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002744 mutex_unlock(&card->dapm_mutex);
2745
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002746 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002747 ucontrol->value.integer.value[0] = max - val;
2748 else
2749 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002750
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002751 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002752}
2753EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2754
2755/**
2756 * snd_soc_dapm_put_volsw - dapm mixer set callback
2757 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002758 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002759 *
2760 * Callback to set the value of a dapm mixer control.
2761 *
2762 * Returns 0 for success.
2763 */
2764int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2765 struct snd_ctl_elem_value *ucontrol)
2766{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002767 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2768 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002769 struct soc_mixer_control *mc =
2770 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002771 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002772 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002773 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002774 unsigned int mask = (1 << fls(max)) - 1;
2775 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002776 unsigned int val;
Jarkko Nikula18626c72014-06-09 14:20:29 +03002777 int connect, change, reg_change = 0;
Mark Brown97404f22010-12-14 16:13:57 +00002778 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002779 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002780
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002781 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002782 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002783 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002784 kcontrol->id.name);
2785
Richard Purdie2b97eab2006-10-06 18:32:18 +02002786 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002787 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002788
2789 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002790 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002791
Liam Girdwood3cd04342012-03-09 12:02:08 +00002792 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002793
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002794 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown283375c2009-12-07 18:09:03 +00002795
Jarkko Nikula18626c72014-06-09 14:20:29 +03002796 if (reg != SND_SOC_NOPM) {
2797 mask = mask << shift;
2798 val = val << shift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02002799
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002800 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
Jarkko Nikula18626c72014-06-09 14:20:29 +03002801 }
2802
2803 if (change || reg_change) {
2804 if (reg_change) {
2805 update.kcontrol = kcontrol;
2806 update.reg = reg;
2807 update.mask = mask;
2808 update.val = val;
2809 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002810 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03002811 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00002812
Nenghua Cao52765972013-12-13 20:13:49 +08002813 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002814
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002815 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002816 }
2817
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002818 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002819
2820 if (ret > 0)
2821 soc_dpcm_runtime_update(card);
2822
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002823 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002824}
2825EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2826
2827/**
2828 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2829 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002830 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002831 *
2832 * Callback to get the value of a dapm enumerated double mixer control.
2833 *
2834 * Returns 0 for success.
2835 */
2836int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2837 struct snd_ctl_elem_value *ucontrol)
2838{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002839 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002840 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002841 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002842
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002843 if (e->reg != SND_SOC_NOPM) {
2844 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
2845 if (ret)
2846 return ret;
2847 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002848 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002849 }
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002850
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002851 val = (reg_val >> e->shift_l) & e->mask;
2852 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2853 if (e->shift_l != e->shift_r) {
2854 val = (reg_val >> e->shift_r) & e->mask;
2855 val = snd_soc_enum_val_to_item(e, val);
2856 ucontrol->value.enumerated.item[1] = val;
2857 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002858
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002859 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002860}
2861EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2862
2863/**
2864 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2865 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002866 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002867 *
2868 * Callback to set the value of a dapm enumerated double mixer control.
2869 *
2870 * Returns 0 for success.
2871 */
2872int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2873 struct snd_ctl_elem_value *ucontrol)
2874{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002875 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2876 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002877 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002878 unsigned int *item = ucontrol->value.enumerated.item;
2879 unsigned int val, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002880 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002881 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002882 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002883
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002884 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002885 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002886
2887 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002888 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002889 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002890 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002891 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002892 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002893 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002894 }
2895
Liam Girdwood3cd04342012-03-09 12:02:08 +00002896 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002897
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002898 if (e->reg != SND_SOC_NOPM)
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002899 change = soc_dapm_test_bits(dapm, e->reg, mask, val);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002900 else
2901 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002902
Richard Purdie2b97eab2006-10-06 18:32:18 +02002903 if (change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002904 if (e->reg != SND_SOC_NOPM) {
2905 update.kcontrol = kcontrol;
2906 update.reg = e->reg;
2907 update.mask = mask;
2908 update.val = val;
2909 card->update = &update;
2910 }
Mark Brown3a655772009-10-05 17:23:30 +01002911
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002912 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002913
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002914 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002915 }
2916
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002917 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002918
2919 if (ret > 0)
2920 soc_dpcm_runtime_update(card);
2921
Mark Brown97404f22010-12-14 16:13:57 +00002922 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002923}
2924EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2925
2926/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002927 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2928 *
2929 * @kcontrol: mixer control
2930 * @uinfo: control element information
2931 *
2932 * Callback to provide information about a pin switch control.
2933 */
2934int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2935 struct snd_ctl_elem_info *uinfo)
2936{
2937 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2938 uinfo->count = 1;
2939 uinfo->value.integer.min = 0;
2940 uinfo->value.integer.max = 1;
2941
2942 return 0;
2943}
2944EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2945
2946/**
2947 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2948 *
2949 * @kcontrol: mixer control
2950 * @ucontrol: Value
2951 */
2952int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2953 struct snd_ctl_elem_value *ucontrol)
2954{
Mark Brown48a8c392012-02-14 17:11:15 -08002955 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002956 const char *pin = (const char *)kcontrol->private_value;
2957
Liam Girdwood3cd04342012-03-09 12:02:08 +00002958 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002959
2960 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002961 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002962
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002963 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002964
2965 return 0;
2966}
2967EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2968
2969/**
2970 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2971 *
2972 * @kcontrol: mixer control
2973 * @ucontrol: Value
2974 */
2975int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2976 struct snd_ctl_elem_value *ucontrol)
2977{
Mark Brown48a8c392012-02-14 17:11:15 -08002978 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002979 const char *pin = (const char *)kcontrol->private_value;
2980
Mark Brown8b37dbd2009-02-28 21:14:20 +00002981 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002982 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002983 else
Mark Brown48a8c392012-02-14 17:11:15 -08002984 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002985
Mark Brown48a8c392012-02-14 17:11:15 -08002986 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002987 return 0;
2988}
2989EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2990
Mark Brown5ba06fc2012-02-16 11:07:13 -08002991static struct snd_soc_dapm_widget *
2992snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2993 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002994{
2995 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002996 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00002997 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002998
2999 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003000 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003001
Mark Brown62ea8742012-01-21 21:14:48 +00003002 switch (w->id) {
3003 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003004 w->regulator = devm_regulator_get(dapm->dev, w->name);
3005 if (IS_ERR(w->regulator)) {
3006 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003007 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003008 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003009 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003010 }
Mark Brown8784c772013-01-10 19:33:47 +00003011
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003012 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003013 ret = regulator_allow_bypass(w->regulator, true);
3014 if (ret != 0)
3015 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003016 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003017 w->name, ret);
3018 }
Mark Brown62ea8742012-01-21 21:14:48 +00003019 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003020 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003021#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003022 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003023 if (IS_ERR(w->clk)) {
3024 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003025 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003026 w->name, ret);
3027 return NULL;
3028 }
Mark Brownec029952012-06-04 08:16:20 +01003029#else
3030 return NULL;
3031#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003032 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003033 default:
3034 break;
3035 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003036
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003037 prefix = soc_dapm_prefix(dapm);
3038 if (prefix)
3039 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003040 else
3041 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3042
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003043 if (w->name == NULL) {
3044 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003045 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003046 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003047
Mark Brown7ca3a182011-10-08 14:04:50 +01003048 switch (w->id) {
3049 case snd_soc_dapm_switch:
3050 case snd_soc_dapm_mixer:
3051 case snd_soc_dapm_mixer_named_ctl:
3052 w->power_check = dapm_generic_check_power;
3053 break;
3054 case snd_soc_dapm_mux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003055 w->power_check = dapm_generic_check_power;
3056 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003057 case snd_soc_dapm_adc:
3058 case snd_soc_dapm_aif_out:
3059 case snd_soc_dapm_dac:
3060 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003061 case snd_soc_dapm_pga:
3062 case snd_soc_dapm_out_drv:
3063 case snd_soc_dapm_input:
3064 case snd_soc_dapm_output:
3065 case snd_soc_dapm_micbias:
3066 case snd_soc_dapm_spk:
3067 case snd_soc_dapm_hp:
3068 case snd_soc_dapm_mic:
3069 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003070 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003071 case snd_soc_dapm_dai_out:
3072 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003073 w->power_check = dapm_generic_check_power;
3074 break;
3075 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003076 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003077 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003078 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003079 w->power_check = dapm_supply_check_power;
3080 break;
3081 default:
3082 w->power_check = dapm_always_on_check_power;
3083 break;
3084 }
3085
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003086 w->dapm = dapm;
Jarkko Nikulab2d9de52014-10-03 15:32:40 +03003087 if (dapm->component)
3088 w->codec = dapm->component->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003089 INIT_LIST_HEAD(&w->sources);
3090 INIT_LIST_HEAD(&w->sinks);
3091 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003092 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003093 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003094
3095 /* machine layer set ups unconnected pins and insertions */
3096 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003097 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003098}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003099
3100/**
Mark Brown4ba13272008-05-13 14:51:19 +02003101 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003102 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003103 * @widget: widget array
3104 * @num: number of widgets
3105 *
3106 * Creates new DAPM controls based upon the templates.
3107 *
3108 * Returns 0 for success else error.
3109 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003110int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003111 const struct snd_soc_dapm_widget *widget,
3112 int num)
3113{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003114 struct snd_soc_dapm_widget *w;
3115 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003116 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003117
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003118 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003119 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003120 w = snd_soc_dapm_new_control(dapm, widget);
3121 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003122 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003123 "ASoC: Failed to create DAPM control %s\n",
3124 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003125 ret = -ENOMEM;
3126 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003127 }
Mark Brown4ba13272008-05-13 14:51:19 +02003128 widget++;
3129 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003130 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003131 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003132}
3133EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3134
Mark Brownc74184e2012-04-04 22:12:09 +01003135static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3136 struct snd_kcontrol *kcontrol, int event)
3137{
3138 struct snd_soc_dapm_path *source_p, *sink_p;
3139 struct snd_soc_dai *source, *sink;
3140 const struct snd_soc_pcm_stream *config = w->params;
3141 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003142 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003143 u64 fmt;
3144 int ret;
3145
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003146 if (WARN_ON(!config) ||
3147 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3148 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003149
3150 /* We only support a single source and sink, pick the first */
3151 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3152 list_sink);
3153 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3154 list_source);
3155
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003156 if (WARN_ON(!source_p || !sink_p) ||
3157 WARN_ON(!sink_p->source || !source_p->sink) ||
3158 WARN_ON(!source_p->source || !sink_p->sink))
3159 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003160
3161 source = source_p->source->priv;
3162 sink = sink_p->sink->priv;
3163
3164 /* Be a little careful as we don't want to overflow the mask array */
3165 if (config->formats) {
3166 fmt = ffs(config->formats) - 1;
3167 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003168 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003169 config->formats);
3170 fmt = 0;
3171 }
3172
3173 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003174 params = kzalloc(sizeof(*params), GFP_KERNEL);
3175 if (!params) {
3176 ret = -ENOMEM;
3177 goto out;
3178 }
3179 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003180
Mark Brown9747cec2012-04-26 19:12:21 +01003181 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003182 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003183 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003184 config->rate_max;
3185
Mark Brown9747cec2012-04-26 19:12:21 +01003186 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003187 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003188 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003189 = config->channels_max;
3190
3191 memset(&substream, 0, sizeof(substream));
3192
3193 switch (event) {
3194 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003195 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3196 ret = soc_dai_hw_params(&substream, params, source);
3197 if (ret < 0)
3198 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003199
Benoit Cousson93e69582014-07-08 23:19:38 +02003200 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3201 ret = soc_dai_hw_params(&substream, params, sink);
3202 if (ret < 0)
3203 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003204 break;
3205
3206 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003207 ret = snd_soc_dai_digital_mute(sink, 0,
3208 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003209 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003210 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003211 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003212 break;
3213
3214 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003215 ret = snd_soc_dai_digital_mute(sink, 1,
3216 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003217 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003218 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003219 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003220 break;
3221
3222 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003223 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003224 return -EINVAL;
3225 }
3226
Mark Brown9747cec2012-04-26 19:12:21 +01003227out:
3228 kfree(params);
3229 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003230}
3231
3232int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3233 const struct snd_soc_pcm_stream *params,
3234 struct snd_soc_dapm_widget *source,
3235 struct snd_soc_dapm_widget *sink)
3236{
Mark Brownc74184e2012-04-04 22:12:09 +01003237 struct snd_soc_dapm_widget template;
3238 struct snd_soc_dapm_widget *w;
3239 size_t len;
3240 char *link_name;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003241 int ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003242
3243 len = strlen(source->name) + strlen(sink->name) + 2;
3244 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3245 if (!link_name)
3246 return -ENOMEM;
3247 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3248
3249 memset(&template, 0, sizeof(template));
3250 template.reg = SND_SOC_NOPM;
3251 template.id = snd_soc_dapm_dai_link;
3252 template.name = link_name;
3253 template.event = snd_soc_dai_link_event;
3254 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3255 SND_SOC_DAPM_PRE_PMD;
3256
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003257 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003258
3259 w = snd_soc_dapm_new_control(&card->dapm, &template);
3260 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003261 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003262 link_name);
3263 return -ENOMEM;
3264 }
3265
3266 w->params = params;
3267
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003268 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3269 if (ret)
3270 return ret;
3271 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Mark Brownc74184e2012-04-04 22:12:09 +01003272}
3273
Mark Brown888df392012-02-16 19:37:51 -08003274int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3275 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003276{
Mark Brown888df392012-02-16 19:37:51 -08003277 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003278 struct snd_soc_dapm_widget *w;
3279
Mark Brown888df392012-02-16 19:37:51 -08003280 WARN_ON(dapm->dev != dai->dev);
3281
3282 memset(&template, 0, sizeof(template));
3283 template.reg = SND_SOC_NOPM;
3284
3285 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003286 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003287 template.name = dai->driver->playback.stream_name;
3288 template.sname = dai->driver->playback.stream_name;
3289
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003290 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003291 template.name);
3292
3293 w = snd_soc_dapm_new_control(dapm, &template);
3294 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003295 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003296 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003297 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003298 }
3299
3300 w->priv = dai;
3301 dai->playback_widget = w;
3302 }
3303
3304 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003305 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003306 template.name = dai->driver->capture.stream_name;
3307 template.sname = dai->driver->capture.stream_name;
3308
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003309 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003310 template.name);
3311
3312 w = snd_soc_dapm_new_control(dapm, &template);
3313 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003314 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003315 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003316 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003317 }
3318
3319 w->priv = dai;
3320 dai->capture_widget = w;
3321 }
3322
3323 return 0;
3324}
3325
3326int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3327{
3328 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003329 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003330 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003331
3332 /* For each DAI widget... */
3333 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003334 switch (dai_w->id) {
3335 case snd_soc_dapm_dai_in:
3336 case snd_soc_dapm_dai_out:
3337 break;
3338 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003339 continue;
Mark Brown46162742013-06-05 19:36:11 +01003340 }
Mark Brown888df392012-02-16 19:37:51 -08003341
3342 dai = dai_w->priv;
3343
3344 /* ...find all widgets with the same stream and link them */
3345 list_for_each_entry(w, &card->widgets, list) {
3346 if (w->dapm != dai_w->dapm)
3347 continue;
3348
Mark Brown46162742013-06-05 19:36:11 +01003349 switch (w->id) {
3350 case snd_soc_dapm_dai_in:
3351 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003352 continue;
Mark Brown46162742013-06-05 19:36:11 +01003353 default:
3354 break;
3355 }
Mark Brown888df392012-02-16 19:37:51 -08003356
Russell King19c2c5f2013-08-04 20:24:03 +01003357 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003358 continue;
3359
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003360 if (dai_w->id == snd_soc_dapm_dai_in) {
3361 src = dai_w;
3362 sink = w;
3363 } else {
3364 src = w;
3365 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003366 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003367 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3368 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003369 }
3370 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003371
Mark Brown888df392012-02-16 19:37:51 -08003372 return 0;
3373}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003374
Benoit Cousson44ba2642014-07-08 23:19:36 +02003375static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3376 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003377{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003378 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003379 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003380 int i;
3381
Benoit Cousson44ba2642014-07-08 23:19:36 +02003382 for (i = 0; i < rtd->num_codecs; i++) {
3383 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003384
3385 /* there is no point in connecting BE DAI links with dummies */
3386 if (snd_soc_dai_is_dummy(codec_dai) ||
3387 snd_soc_dai_is_dummy(cpu_dai))
3388 continue;
3389
3390 /* connect BE DAI playback if widgets are valid */
3391 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003392 source = cpu_dai->playback_widget;
3393 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003394 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003395 cpu_dai->component->name, source->name,
3396 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003397
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003398 snd_soc_dapm_add_path(&card->dapm, source, sink,
3399 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003400 }
3401
3402 /* connect BE DAI capture if widgets are valid */
3403 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003404 source = codec_dai->capture_widget;
3405 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003406 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003407 codec_dai->component->name, source->name,
3408 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003409
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003410 snd_soc_dapm_add_path(&card->dapm, source, sink,
3411 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003412 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003413 }
3414}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003415
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003416static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3417 int event)
3418{
3419 struct snd_soc_dapm_widget *w;
3420
3421 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3422 w = dai->playback_widget;
3423 else
3424 w = dai->capture_widget;
3425
3426 if (w) {
3427 dapm_mark_dirty(w, "stream event");
3428
3429 switch (event) {
3430 case SND_SOC_DAPM_STREAM_START:
3431 w->active = 1;
3432 break;
3433 case SND_SOC_DAPM_STREAM_STOP:
3434 w->active = 0;
3435 break;
3436 case SND_SOC_DAPM_STREAM_SUSPEND:
3437 case SND_SOC_DAPM_STREAM_RESUME:
3438 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3439 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3440 break;
3441 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003442 }
3443}
3444
Benoit Cousson44ba2642014-07-08 23:19:36 +02003445void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3446{
3447 struct snd_soc_pcm_runtime *rtd = card->rtd;
3448 int i;
3449
3450 /* for each BE DAI link... */
3451 for (i = 0; i < card->num_rtd; i++) {
3452 rtd = &card->rtd[i];
3453
3454 /*
3455 * dynamic FE links have no fixed DAI mapping.
3456 * CODEC<->CODEC links have no direct connection.
3457 */
3458 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3459 continue;
3460
3461 dapm_connect_dai_link_widgets(card, rtd);
3462 }
3463}
3464
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003465static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3466 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003467{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003468 int i;
3469
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003470 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Benoit Cousson44ba2642014-07-08 23:19:36 +02003471 for (i = 0; i < rtd->num_codecs; i++)
3472 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003473
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003474 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003475}
3476
3477/**
3478 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3479 * @rtd: PCM runtime data
3480 * @stream: stream name
3481 * @event: stream event
3482 *
3483 * Sends a stream event to the dapm core. The core then makes any
3484 * necessary widget power changes.
3485 *
3486 * Returns 0 for success else error.
3487 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003488void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3489 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003490{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003491 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003492
Liam Girdwood3cd04342012-03-09 12:02:08 +00003493 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003494 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003495 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003496}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003497
3498/**
Charles Keepax11391102014-02-18 15:22:14 +00003499 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3500 * @dapm: DAPM context
3501 * @pin: pin name
3502 *
3503 * Enables input/output pin and its parents or children widgets iff there is
3504 * a valid audio route and active audio stream.
3505 *
3506 * Requires external locking.
3507 *
3508 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3509 * do any widget power switching.
3510 */
3511int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3512 const char *pin)
3513{
3514 return snd_soc_dapm_set_pin(dapm, pin, 1);
3515}
3516EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3517
3518/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003519 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003520 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003521 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003522 *
Mark Brown74b8f952009-06-06 11:26:15 +01003523 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003524 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003525 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003526 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3527 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003528 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003529int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003530{
Charles Keepax11391102014-02-18 15:22:14 +00003531 int ret;
3532
3533 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3534
3535 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3536
3537 mutex_unlock(&dapm->card->dapm_mutex);
3538
3539 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003540}
Liam Girdwooda5302182008-07-07 13:35:17 +01003541EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003542
3543/**
Charles Keepax11391102014-02-18 15:22:14 +00003544 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3545 * @dapm: DAPM context
3546 * @pin: pin name
3547 *
3548 * Enables input/output pin regardless of any other state. This is
3549 * intended for use with microphone bias supplies used in microphone
3550 * jack detection.
3551 *
3552 * Requires external locking.
3553 *
3554 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3555 * do any widget power switching.
3556 */
3557int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3558 const char *pin)
3559{
3560 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3561
3562 if (!w) {
3563 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3564 return -EINVAL;
3565 }
3566
3567 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3568 w->connected = 1;
3569 w->force = 1;
3570 dapm_mark_dirty(w, "force enable");
3571
3572 return 0;
3573}
3574EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3575
3576/**
Mark Brownda341832010-03-15 19:23:37 +00003577 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003578 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003579 * @pin: pin name
3580 *
3581 * Enables input/output pin regardless of any other state. This is
3582 * intended for use with microphone bias supplies used in microphone
3583 * jack detection.
3584 *
3585 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3586 * do any widget power switching.
3587 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003588int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3589 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003590{
Charles Keepax11391102014-02-18 15:22:14 +00003591 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003592
Charles Keepax11391102014-02-18 15:22:14 +00003593 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003594
Charles Keepax11391102014-02-18 15:22:14 +00003595 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003596
Charles Keepax11391102014-02-18 15:22:14 +00003597 mutex_unlock(&dapm->card->dapm_mutex);
3598
3599 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003600}
3601EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3602
3603/**
Charles Keepax11391102014-02-18 15:22:14 +00003604 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3605 * @dapm: DAPM context
3606 * @pin: pin name
3607 *
3608 * Disables input/output pin and its parents or children widgets.
3609 *
3610 * Requires external locking.
3611 *
3612 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3613 * do any widget power switching.
3614 */
3615int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3616 const char *pin)
3617{
3618 return snd_soc_dapm_set_pin(dapm, pin, 0);
3619}
3620EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3621
3622/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003623 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003624 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003625 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003626 *
Mark Brown74b8f952009-06-06 11:26:15 +01003627 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003628 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003629 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3630 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003631 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003632int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3633 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003634{
Charles Keepax11391102014-02-18 15:22:14 +00003635 int ret;
3636
3637 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3638
3639 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3640
3641 mutex_unlock(&dapm->card->dapm_mutex);
3642
3643 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003644}
3645EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3646
3647/**
Charles Keepax11391102014-02-18 15:22:14 +00003648 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3649 * @dapm: DAPM context
3650 * @pin: pin name
3651 *
3652 * Marks the specified pin as being not connected, disabling it along
3653 * any parent or child widgets. At present this is identical to
3654 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3655 * additional things such as disabling controls which only affect
3656 * paths through the pin.
3657 *
3658 * Requires external locking.
3659 *
3660 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3661 * do any widget power switching.
3662 */
3663int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3664 const char *pin)
3665{
3666 return snd_soc_dapm_set_pin(dapm, pin, 0);
3667}
3668EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3669
3670/**
Mark Brown5817b522008-09-24 11:23:11 +01003671 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003672 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003673 * @pin: pin name
3674 *
3675 * Marks the specified pin as being not connected, disabling it along
3676 * any parent or child widgets. At present this is identical to
3677 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3678 * additional things such as disabling controls which only affect
3679 * paths through the pin.
3680 *
3681 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3682 * do any widget power switching.
3683 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003684int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003685{
Charles Keepax11391102014-02-18 15:22:14 +00003686 int ret;
3687
3688 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3689
3690 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3691
3692 mutex_unlock(&dapm->card->dapm_mutex);
3693
3694 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003695}
3696EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3697
3698/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003699 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003700 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003701 * @pin: audio signal pin endpoint (or start point)
3702 *
3703 * Get audio pin status - connected or disconnected.
3704 *
3705 * Returns 1 for connected otherwise 0.
3706 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003707int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3708 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003709{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003710 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003711
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003712 if (w)
3713 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003714
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003715 return 0;
3716}
Liam Girdwooda5302182008-07-07 13:35:17 +01003717EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003718
3719/**
Mark Brown1547aba2010-05-07 21:11:40 +01003720 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003721 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003722 * @pin: audio signal pin endpoint (or start point)
3723 *
3724 * Mark the given endpoint or pin as ignoring suspend. When the
3725 * system is disabled a path between two endpoints flagged as ignoring
3726 * suspend will not be disabled. The path must already be enabled via
3727 * normal means at suspend time, it will not be turned on if it was not
3728 * already enabled.
3729 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003730int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3731 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003732{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003733 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003734
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003735 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003736 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003737 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003738 }
3739
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003740 w->ignore_suspend = 1;
3741
3742 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003743}
3744EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3745
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02003746/**
3747 * dapm_is_external_path() - Checks if a path is a external path
3748 * @card: The card the path belongs to
3749 * @path: The path to check
3750 *
3751 * Returns true if the path is either between two different DAPM contexts or
3752 * between two external pins of the same DAPM context. Otherwise returns
3753 * false.
3754 */
3755static bool dapm_is_external_path(struct snd_soc_card *card,
3756 struct snd_soc_dapm_path *path)
3757{
3758 dev_dbg(card->dev,
3759 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3760 path->source->name, path->source->id, path->source->dapm,
3761 path->sink->name, path->sink->id, path->sink->dapm);
3762
3763 /* Connection between two different DAPM contexts */
3764 if (path->source->dapm != path->sink->dapm)
3765 return true;
3766
3767 /* Loopback connection from external pin to external pin */
3768 if (path->sink->id == snd_soc_dapm_input) {
3769 switch (path->source->id) {
3770 case snd_soc_dapm_output:
3771 case snd_soc_dapm_micbias:
3772 return true;
3773 default:
3774 break;
3775 }
3776 }
3777
3778 return false;
3779}
3780
Stephen Warren16332812011-11-23 12:42:04 -07003781static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3782 struct snd_soc_dapm_widget *w)
3783{
3784 struct snd_soc_dapm_path *p;
3785
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02003786 list_for_each_entry(p, &w->sources, list_sink) {
3787 if (dapm_is_external_path(card, p))
3788 return true;
3789 }
Stephen Warren16332812011-11-23 12:42:04 -07003790
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02003791 list_for_each_entry(p, &w->sinks, list_source) {
3792 if (dapm_is_external_path(card, p))
3793 return true;
Stephen Warren16332812011-11-23 12:42:04 -07003794 }
3795
3796 return false;
3797}
3798
3799/**
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003800 * snd_soc_dapm_auto_nc_pins - call snd_soc_dapm_nc_pin for unused pins
3801 * @card: The card whose pins should be processed
Stephen Warren16332812011-11-23 12:42:04 -07003802 *
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003803 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the card
3804 * which are unused. Pins are used if they are connected externally to a
3805 * component, whether that be to some other device, or a loop-back connection to
3806 * the component itself.
Stephen Warren16332812011-11-23 12:42:04 -07003807 */
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003808void snd_soc_dapm_auto_nc_pins(struct snd_soc_card *card)
Stephen Warren16332812011-11-23 12:42:04 -07003809{
Stephen Warren16332812011-11-23 12:42:04 -07003810 struct snd_soc_dapm_widget *w;
3811
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003812 dev_dbg(card->dev, "ASoC: Auto NC: DAPMs: card:%p\n", &card->dapm);
Stephen Warren16332812011-11-23 12:42:04 -07003813
3814 list_for_each_entry(w, &card->widgets, list) {
Stephen Warren16332812011-11-23 12:42:04 -07003815 switch (w->id) {
3816 case snd_soc_dapm_input:
3817 case snd_soc_dapm_output:
3818 case snd_soc_dapm_micbias:
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003819 dev_dbg(card->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003820 w->name);
3821 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003822 dev_dbg(card->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003823 "... Not in map; disabling\n");
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003824 snd_soc_dapm_nc_pin(w->dapm, w->name);
Stephen Warren16332812011-11-23 12:42:04 -07003825 }
3826 break;
3827 default:
3828 break;
3829 }
3830 }
3831}
3832
Mark Brown1547aba2010-05-07 21:11:40 +01003833/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003834 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003835 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003836 *
3837 * Free all dapm widgets and resources.
3838 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003839void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003840{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003841 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003842 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003843 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003844 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003845}
3846EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3847
Xiang Xiao57996352014-03-02 00:04:02 +08003848static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003849{
Liam Girdwood01005a72012-07-06 16:57:05 +01003850 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003851 struct snd_soc_dapm_widget *w;
3852 LIST_HEAD(down_list);
3853 int powerdown = 0;
3854
Liam Girdwood01005a72012-07-06 16:57:05 +01003855 mutex_lock(&card->dapm_mutex);
3856
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003857 list_for_each_entry(w, &dapm->card->widgets, list) {
3858 if (w->dapm != dapm)
3859 continue;
Mark Brown51737472009-06-22 13:16:51 +01003860 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003861 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003862 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003863 powerdown = 1;
3864 }
3865 }
3866
3867 /* If there were no widgets to power down we're already in
3868 * standby.
3869 */
3870 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003871 if (dapm->bias_level == SND_SOC_BIAS_ON)
3872 snd_soc_dapm_set_bias_level(dapm,
3873 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003874 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003875 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3876 snd_soc_dapm_set_bias_level(dapm,
3877 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003878 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003879
3880 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003881}
Mark Brown51737472009-06-22 13:16:51 +01003882
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003883/*
3884 * snd_soc_dapm_shutdown - callback for system shutdown
3885 */
3886void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3887{
Xiang Xiao57996352014-03-02 00:04:02 +08003888 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003889
Xiang Xiao57996352014-03-02 00:04:02 +08003890 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08003891 if (dapm != &card->dapm) {
3892 soc_dapm_shutdown_dapm(dapm);
3893 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3894 snd_soc_dapm_set_bias_level(dapm,
3895 SND_SOC_BIAS_OFF);
3896 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003897 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08003898
3899 soc_dapm_shutdown_dapm(&card->dapm);
3900 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3901 snd_soc_dapm_set_bias_level(&card->dapm,
3902 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01003903}
3904
Richard Purdie2b97eab2006-10-06 18:32:18 +02003905/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003906MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003907MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3908MODULE_LICENSE("GPL");