blob: 08c79f09034f4f13183798e2d572833d19603e37 [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);
499 path->name = (char*)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;
Stephen Warren85762e72013-03-29 15:40:10 -0600595 char *long_name;
596 const char *name;
597 int ret;
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);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200656 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200657 if (!kcontrol)
658 return -ENOMEM;
659 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200660
661 ret = dapm_kcontrol_data_alloc(w, kcontrol);
662 if (ret) {
663 snd_ctl_free_one(kcontrol);
664 return ret;
665 }
666
Stephen Warren85762e72013-03-29 15:40:10 -0600667 ret = snd_ctl_add(card, kcontrol);
668 if (ret < 0) {
669 dev_err(dapm->dev,
670 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
671 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600672 return ret;
673 }
Stephen Warren85762e72013-03-29 15:40:10 -0600674 }
675
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200676 ret = dapm_kcontrol_add_widget(kcontrol, w);
677 if (ret)
678 return ret;
679
Stephen Warren85762e72013-03-29 15:40:10 -0600680 w->kcontrols[kci] = kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600681
682 return 0;
683}
684
685/* create new dapm mixer control */
686static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
687{
688 int i, ret;
689 struct snd_soc_dapm_path *path;
690
Richard Purdie2b97eab2006-10-06 18:32:18 +0200691 /* add kcontrol */
692 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200693 /* match name */
694 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200695 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600696 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200697 continue;
698
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200699 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200700 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200701 continue;
702 }
703
Mark Brown946d92a2013-08-12 23:28:42 +0100704 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600705 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200706 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100707
708 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200709 }
710 }
Stephen Warren85762e72013-03-29 15:40:10 -0600711
712 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200713}
714
715/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200716static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200717{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200718 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600719 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600720 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200721
Stephen Warrenaf468002011-04-28 17:38:01 -0600722 if (w->num_kcontrols != 1) {
723 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000724 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600725 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200726 return -EINVAL;
727 }
728
Lars-Peter Clausenfe5813912013-08-01 18:30:38 +0200729 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600730 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
731 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600732 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200733
Mark Brown946d92a2013-08-12 23:28:42 +0100734 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600735 if (ret < 0)
736 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600737
Richard Purdie2b97eab2006-10-06 18:32:18 +0200738 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200739 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200740
Stephen Warrenaf468002011-04-28 17:38:01 -0600741 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200742}
743
744/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200745static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200746{
Mark Browna6c65732010-03-03 17:45:21 +0000747 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200748 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000749 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200750
Mark Browna6c65732010-03-03 17:45:21 +0000751 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200752}
753
754/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100755static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
756 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200757{
758 struct snd_soc_dapm_path *p;
759
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100760 list_for_each_entry(p, sink, list_source) {
761 if (p->walked) {
762 p->walked = 0;
763 dapm_clear_walk_output(dapm, &p->sink->sinks);
764 }
765 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200766}
767
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100768static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
769 struct list_head *source)
770{
771 struct snd_soc_dapm_path *p;
772
773 list_for_each_entry(p, source, list_sink) {
774 if (p->walked) {
775 p->walked = 0;
776 dapm_clear_walk_input(dapm, &p->source->sources);
777 }
778 }
779}
780
781
Mark Brown99497882010-05-07 20:24:05 +0100782/* We implement power down on suspend by checking the power state of
783 * the ALSA card - when we are suspending the ALSA state for the card
784 * is set to D3.
785 */
786static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
787{
Mark Brown12ea2c72011-03-02 18:17:32 +0000788 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100789
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000790 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100791 case SNDRV_CTL_POWER_D3hot:
792 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100793 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000794 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200795 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100796 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100797 default:
798 return 1;
799 }
800}
801
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100802/* add widget to list if it's not already in the list */
803static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
804 struct snd_soc_dapm_widget *w)
805{
806 struct snd_soc_dapm_widget_list *wlist;
807 int wlistsize, wlistentries, i;
808
809 if (*list == NULL)
810 return -EINVAL;
811
812 wlist = *list;
813
814 /* is this widget already in the list */
815 for (i = 0; i < wlist->num_widgets; i++) {
816 if (wlist->widgets[i] == w)
817 return 0;
818 }
819
820 /* allocate some new space */
821 wlistentries = wlist->num_widgets + 1;
822 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
823 wlistentries * sizeof(struct snd_soc_dapm_widget *);
824 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
825 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000826 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100827 w->name);
828 return -ENOMEM;
829 }
830 wlist = *list;
831
832 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000833 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100834 w->name, wlist->num_widgets);
835
836 wlist->widgets[wlist->num_widgets] = w;
837 wlist->num_widgets++;
838 return 1;
839}
840
Richard Purdie2b97eab2006-10-06 18:32:18 +0200841/*
842 * Recursively check for a completed path to an active or physically connected
843 * output widget. Returns number of complete paths.
844 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100845static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
846 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200847{
848 struct snd_soc_dapm_path *path;
849 int con = 0;
850
Mark Brown024dc072011-10-09 11:52:05 +0100851 if (widget->outputs >= 0)
852 return widget->outputs;
853
Mark Brownde02d072011-09-20 21:43:24 +0100854 DAPM_UPDATE_STAT(widget, path_checks);
855
Mark Brown62ea8742012-01-21 21:14:48 +0000856 switch (widget->id) {
857 case snd_soc_dapm_supply:
858 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200859 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200860 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100861 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000862 default:
863 break;
864 }
Mark Brown246d0a12009-04-22 18:24:55 +0100865
Mark Brown010ff262009-08-17 17:39:22 +0100866 switch (widget->id) {
867 case snd_soc_dapm_adc:
868 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100869 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100870 if (widget->active) {
871 widget->outputs = snd_soc_dapm_suspend_check(widget);
872 return widget->outputs;
873 }
Mark Brown010ff262009-08-17 17:39:22 +0100874 default:
875 break;
876 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200877
878 if (widget->connected) {
879 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100880 if (widget->id == snd_soc_dapm_output && !widget->ext) {
881 widget->outputs = snd_soc_dapm_suspend_check(widget);
882 return widget->outputs;
883 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200884
885 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100886 if (widget->id == snd_soc_dapm_hp ||
887 widget->id == snd_soc_dapm_spk ||
888 (widget->id == snd_soc_dapm_line &&
889 !list_empty(&widget->sources))) {
890 widget->outputs = snd_soc_dapm_suspend_check(widget);
891 return widget->outputs;
892 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200893 }
894
895 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100896 DAPM_UPDATE_STAT(widget, neighbour_checks);
897
Mark Brownbf3a9e12011-06-13 16:42:29 +0100898 if (path->weak)
899 continue;
900
Mark Brown8af294b2013-02-22 17:48:15 +0000901 if (path->walking)
902 return 1;
903
Richard Purdie2b97eab2006-10-06 18:32:18 +0200904 if (path->walked)
905 continue;
906
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100907 trace_snd_soc_dapm_output_path(widget, path);
908
Richard Purdie2b97eab2006-10-06 18:32:18 +0200909 if (path->sink && path->connect) {
910 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000911 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100912
913 /* do we need to add this widget to the list ? */
914 if (list) {
915 int err;
916 err = dapm_list_add_widget(list, path->sink);
917 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000918 dev_err(widget->dapm->dev,
919 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100920 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000921 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100922 return con;
923 }
924 }
925
926 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000927
928 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200929 }
930 }
931
Mark Brown024dc072011-10-09 11:52:05 +0100932 widget->outputs = con;
933
Richard Purdie2b97eab2006-10-06 18:32:18 +0200934 return con;
935}
936
937/*
938 * Recursively check for a completed path to an active or physically connected
939 * input widget. Returns number of complete paths.
940 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100941static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
942 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200943{
944 struct snd_soc_dapm_path *path;
945 int con = 0;
946
Mark Brown024dc072011-10-09 11:52:05 +0100947 if (widget->inputs >= 0)
948 return widget->inputs;
949
Mark Brownde02d072011-09-20 21:43:24 +0100950 DAPM_UPDATE_STAT(widget, path_checks);
951
Mark Brown62ea8742012-01-21 21:14:48 +0000952 switch (widget->id) {
953 case snd_soc_dapm_supply:
954 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200955 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200956 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100957 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000958 default:
959 break;
960 }
Mark Brown246d0a12009-04-22 18:24:55 +0100961
Richard Purdie2b97eab2006-10-06 18:32:18 +0200962 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100963 switch (widget->id) {
964 case snd_soc_dapm_dac:
965 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +0100966 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +0100967 if (widget->active) {
968 widget->inputs = snd_soc_dapm_suspend_check(widget);
969 return widget->inputs;
970 }
Mark Brown010ff262009-08-17 17:39:22 +0100971 default:
972 break;
973 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200974
975 if (widget->connected) {
976 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100977 if (widget->id == snd_soc_dapm_input && !widget->ext) {
978 widget->inputs = snd_soc_dapm_suspend_check(widget);
979 return widget->inputs;
980 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200981
982 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100983 if (widget->id == snd_soc_dapm_vmid) {
984 widget->inputs = snd_soc_dapm_suspend_check(widget);
985 return widget->inputs;
986 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200987
988 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300989 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100990 (widget->id == snd_soc_dapm_line &&
991 !list_empty(&widget->sinks))) {
992 widget->inputs = snd_soc_dapm_suspend_check(widget);
993 return widget->inputs;
994 }
995
Mark Brown1ab97c82011-11-27 16:21:51 +0000996 /* signal generator */
997 if (widget->id == snd_soc_dapm_siggen) {
998 widget->inputs = snd_soc_dapm_suspend_check(widget);
999 return widget->inputs;
1000 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001001 }
1002
1003 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +01001004 DAPM_UPDATE_STAT(widget, neighbour_checks);
1005
Mark Brownbf3a9e12011-06-13 16:42:29 +01001006 if (path->weak)
1007 continue;
1008
Mark Brown8af294b2013-02-22 17:48:15 +00001009 if (path->walking)
1010 return 1;
1011
Richard Purdie2b97eab2006-10-06 18:32:18 +02001012 if (path->walked)
1013 continue;
1014
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001015 trace_snd_soc_dapm_input_path(widget, path);
1016
Richard Purdie2b97eab2006-10-06 18:32:18 +02001017 if (path->source && path->connect) {
1018 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001019 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001020
1021 /* do we need to add this widget to the list ? */
1022 if (list) {
1023 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001024 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001025 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001026 dev_err(widget->dapm->dev,
1027 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001028 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001029 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001030 return con;
1031 }
1032 }
1033
1034 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001035
1036 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001037 }
1038 }
1039
Mark Brown024dc072011-10-09 11:52:05 +01001040 widget->inputs = con;
1041
Richard Purdie2b97eab2006-10-06 18:32:18 +02001042 return con;
1043}
1044
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001045/**
1046 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1047 * @dai: the soc DAI.
1048 * @stream: stream direction.
1049 * @list: list of active widgets for this stream.
1050 *
1051 * Queries DAPM graph as to whether an valid audio stream path exists for
1052 * the initial stream specified by name. This takes into account
1053 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1054 *
1055 * Returns the number of valid paths or negative error.
1056 */
1057int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1058 struct snd_soc_dapm_widget_list **list)
1059{
1060 struct snd_soc_card *card = dai->card;
1061 int paths;
1062
1063 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1064 dapm_reset(card);
1065
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001066 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001067 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001068 dapm_clear_walk_output(&card->dapm,
1069 &dai->playback_widget->sinks);
1070 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001071 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001072 dapm_clear_walk_input(&card->dapm,
1073 &dai->capture_widget->sources);
1074 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001075
1076 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001077 mutex_unlock(&card->dapm_mutex);
1078
1079 return paths;
1080}
1081
Richard Purdie2b97eab2006-10-06 18:32:18 +02001082/*
Mark Brown62ea8742012-01-21 21:14:48 +00001083 * Handler for regulator supply widget.
1084 */
1085int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1086 struct snd_kcontrol *kcontrol, int event)
1087{
Mark Brownc05b84d2012-09-07 12:57:11 +08001088 int ret;
1089
Mark Browneb270e92013-10-09 13:52:52 +01001090 soc_dapm_async_complete(w->dapm);
1091
Mark Brownc05b84d2012-09-07 12:57:11 +08001092 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001093 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001094 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001095 if (ret != 0)
1096 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001097 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001098 w->name, ret);
1099 }
1100
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001101 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001102 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001103 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001104 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001105 if (ret != 0)
1106 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001107 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001108 w->name, ret);
1109 }
1110
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001111 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001112 }
Mark Brown62ea8742012-01-21 21:14:48 +00001113}
1114EXPORT_SYMBOL_GPL(dapm_regulator_event);
1115
Ola Liljad7e7eb92012-05-24 15:26:25 +02001116/*
1117 * Handler for clock supply widget.
1118 */
1119int dapm_clock_event(struct snd_soc_dapm_widget *w,
1120 struct snd_kcontrol *kcontrol, int event)
1121{
1122 if (!w->clk)
1123 return -EIO;
1124
Mark Browneb270e92013-10-09 13:52:52 +01001125 soc_dapm_async_complete(w->dapm);
1126
Mark Brownec029952012-06-04 08:16:20 +01001127#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001128 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001129 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001130 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001131 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001132 return 0;
1133 }
Mark Brownec029952012-06-04 08:16:20 +01001134#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001135 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001136}
1137EXPORT_SYMBOL_GPL(dapm_clock_event);
1138
Mark Brownd8050022011-09-28 18:28:23 +01001139static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1140{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001141 if (w->power_checked)
1142 return w->new_power;
1143
Mark Brownd8050022011-09-28 18:28:23 +01001144 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001145 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001146 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001147 w->new_power = w->power_check(w);
1148
1149 w->power_checked = true;
1150
1151 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001152}
1153
Mark Browncd0f2d42009-04-20 16:56:59 +01001154/* Generic check to see if a widget should be powered.
1155 */
1156static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1157{
1158 int in, out;
1159
Mark Brownde02d072011-09-20 21:43:24 +01001160 DAPM_UPDATE_STAT(w, power_checks);
1161
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001162 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001163 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001164 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001165 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001166 return out != 0 && in != 0;
1167}
1168
Mark Brown6ea31b92009-04-20 17:15:41 +01001169/* Check to see if an ADC has power */
1170static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1171{
1172 int in;
1173
Mark Brownde02d072011-09-20 21:43:24 +01001174 DAPM_UPDATE_STAT(w, power_checks);
1175
Mark Brown6ea31b92009-04-20 17:15:41 +01001176 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001177 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001178 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001179 return in != 0;
1180 } else {
1181 return dapm_generic_check_power(w);
1182 }
1183}
1184
1185/* Check to see if a DAC has power */
1186static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1187{
1188 int out;
1189
Mark Brownde02d072011-09-20 21:43:24 +01001190 DAPM_UPDATE_STAT(w, power_checks);
1191
Mark Brown6ea31b92009-04-20 17:15:41 +01001192 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001193 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001194 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001195 return out != 0;
1196 } else {
1197 return dapm_generic_check_power(w);
1198 }
1199}
1200
Mark Brown246d0a12009-04-22 18:24:55 +01001201/* Check to see if a power supply is needed */
1202static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1203{
1204 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001205
Mark Brownde02d072011-09-20 21:43:24 +01001206 DAPM_UPDATE_STAT(w, power_checks);
1207
Mark Brown246d0a12009-04-22 18:24:55 +01001208 /* Check if one of our outputs is connected */
1209 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001210 DAPM_UPDATE_STAT(w, neighbour_checks);
1211
Mark Brownbf3a9e12011-06-13 16:42:29 +01001212 if (path->weak)
1213 continue;
1214
Mark Brown215edda2009-09-08 18:59:05 +01001215 if (path->connected &&
1216 !path->connected(path->source, path->sink))
1217 continue;
1218
Mark Brown30173582011-02-11 11:42:19 +00001219 if (!path->sink)
1220 continue;
1221
Mark Brownf68d7e12011-10-04 22:57:50 +01001222 if (dapm_widget_power_check(path->sink))
1223 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001224 }
1225
Mark Brownf68d7e12011-10-04 22:57:50 +01001226 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001227}
1228
Mark Brown35c64bc2011-09-28 18:23:53 +01001229static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1230{
1231 return 1;
1232}
1233
Mark Brown38357ab2009-06-06 19:03:23 +01001234static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1235 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001236 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001237{
Mark Brown828a8422011-01-15 13:14:30 +00001238 int *sort;
1239
1240 if (power_up)
1241 sort = dapm_up_seq;
1242 else
1243 sort = dapm_down_seq;
1244
Mark Brown38357ab2009-06-06 19:03:23 +01001245 if (sort[a->id] != sort[b->id])
1246 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001247 if (a->subseq != b->subseq) {
1248 if (power_up)
1249 return a->subseq - b->subseq;
1250 else
1251 return b->subseq - a->subseq;
1252 }
Mark Brownb22ead22009-06-07 12:51:26 +01001253 if (a->reg != b->reg)
1254 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001255 if (a->dapm != b->dapm)
1256 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001257
Mark Brown38357ab2009-06-06 19:03:23 +01001258 return 0;
1259}
Mark Brown42aa3412009-03-01 19:21:10 +00001260
Mark Brown38357ab2009-06-06 19:03:23 +01001261/* Insert a widget in order into a DAPM power sequence. */
1262static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1263 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001264 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001265{
1266 struct snd_soc_dapm_widget *w;
1267
1268 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001269 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001270 list_add_tail(&new_widget->power_list, &w->power_list);
1271 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001272 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001273
Mark Brown38357ab2009-06-06 19:03:23 +01001274 list_add_tail(&new_widget->power_list, list);
1275}
Mark Brown42aa3412009-03-01 19:21:10 +00001276
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001277static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001278 struct snd_soc_dapm_widget *w, int event)
1279{
Mark Brown68f89ad2010-11-03 23:51:49 -04001280 const char *ev_name;
1281 int power, ret;
1282
1283 switch (event) {
1284 case SND_SOC_DAPM_PRE_PMU:
1285 ev_name = "PRE_PMU";
1286 power = 1;
1287 break;
1288 case SND_SOC_DAPM_POST_PMU:
1289 ev_name = "POST_PMU";
1290 power = 1;
1291 break;
1292 case SND_SOC_DAPM_PRE_PMD:
1293 ev_name = "PRE_PMD";
1294 power = 0;
1295 break;
1296 case SND_SOC_DAPM_POST_PMD:
1297 ev_name = "POST_PMD";
1298 power = 0;
1299 break;
Mark Brown80114122013-02-25 15:14:19 +00001300 case SND_SOC_DAPM_WILL_PMU:
1301 ev_name = "WILL_PMU";
1302 power = 1;
1303 break;
1304 case SND_SOC_DAPM_WILL_PMD:
1305 ev_name = "WILL_PMD";
1306 power = 0;
1307 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001308 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001309 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001310 return;
1311 }
1312
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001313 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001314 return;
1315
1316 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001317 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001318 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001319 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001320 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001321 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001322 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001323 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001324 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001325 ev_name, w->name, ret);
1326 }
1327}
1328
Mark Brownb22ead22009-06-07 12:51:26 +01001329/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001330static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001331 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001332{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001333 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001334 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001335 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001336 unsigned int value = 0;
1337 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001338
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001339 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1340 reg = w->reg;
1341 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001342
1343 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001344 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001345 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001346
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001347 mask |= w->mask << w->shift;
1348 if (w->power)
1349 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001350 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001351 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001352
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001353 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001354 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1355 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001356
Mark Brown68f89ad2010-11-03 23:51:49 -04001357 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001358 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1359 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001360 }
1361
Mark Brown81628102009-06-07 13:21:24 +01001362 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001363 /* Any widget will do, they should all be updating the
1364 * same register.
1365 */
Mark Brown29376bc2011-06-19 13:49:28 +01001366
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001367 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001368 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001369 value, mask, reg, card->pop_time);
1370 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001371 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001372 }
1373
1374 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001375 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1376 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001377 }
Mark Brown42aa3412009-03-01 19:21:10 +00001378}
1379
Mark Brownb22ead22009-06-07 12:51:26 +01001380/* Apply a DAPM power sequence.
1381 *
1382 * We walk over a pre-sorted list of widgets to apply power to. In
1383 * order to minimise the number of writes to the device required
1384 * multiple widgets will be updated in a single write where possible.
1385 * Currently anything that requires more than a single write is not
1386 * handled.
1387 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001388static void dapm_seq_run(struct snd_soc_card *card,
1389 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001390{
1391 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001392 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001393 LIST_HEAD(pending);
1394 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001395 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001396 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001397 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001398 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001399 int *sort;
1400
1401 if (power_up)
1402 sort = dapm_up_seq;
1403 else
1404 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001405
Mark Brownb22ead22009-06-07 12:51:26 +01001406 list_for_each_entry_safe(w, n, list, power_list) {
1407 ret = 0;
1408
1409 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001410 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001411 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001412 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001413 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001414
Mark Brown474b62d2011-01-18 16:14:44 +00001415 if (cur_dapm && cur_dapm->seq_notifier) {
1416 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1417 if (sort[i] == cur_sort)
1418 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001419 i,
1420 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001421 }
1422
Mark Browneb270e92013-10-09 13:52:52 +01001423 if (cur_dapm && w->dapm != cur_dapm)
1424 soc_dapm_async_complete(cur_dapm);
1425
Mark Brownb22ead22009-06-07 12:51:26 +01001426 INIT_LIST_HEAD(&pending);
1427 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001428 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001429 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001430 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001431 }
1432
Mark Brown163cac02009-06-07 10:12:52 +01001433 switch (w->id) {
1434 case snd_soc_dapm_pre:
1435 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001436 list_for_each_entry_safe_continue(w, n, list,
1437 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001438
Mark Brownb22ead22009-06-07 12:51:26 +01001439 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001440 ret = w->event(w,
1441 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001442 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001443 ret = w->event(w,
1444 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001445 break;
1446
1447 case snd_soc_dapm_post:
1448 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001449 list_for_each_entry_safe_continue(w, n, list,
1450 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001451
Mark Brownb22ead22009-06-07 12:51:26 +01001452 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001453 ret = w->event(w,
1454 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001455 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001456 ret = w->event(w,
1457 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001458 break;
1459
Mark Brown163cac02009-06-07 10:12:52 +01001460 default:
Mark Brown81628102009-06-07 13:21:24 +01001461 /* Queue it up for application */
1462 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001463 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001464 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001465 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001466 list_move(&w->power_list, &pending);
1467 break;
Mark Brown163cac02009-06-07 10:12:52 +01001468 }
Mark Brownb22ead22009-06-07 12:51:26 +01001469
1470 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001471 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001472 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001473 }
Mark Brownb22ead22009-06-07 12:51:26 +01001474
1475 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001476 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001477
1478 if (cur_dapm && cur_dapm->seq_notifier) {
1479 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1480 if (sort[i] == cur_sort)
1481 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001482 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001483 }
Mark Browneb270e92013-10-09 13:52:52 +01001484
1485 list_for_each_entry(d, &card->dapm_list, list) {
1486 soc_dapm_async_complete(d);
1487 }
Mark Brown163cac02009-06-07 10:12:52 +01001488}
1489
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001490static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001491{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001492 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001493 struct snd_soc_dapm_widget_list *wlist;
1494 struct snd_soc_dapm_widget *w = NULL;
1495 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001496 int ret;
1497
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001498 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001499 return;
1500
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001501 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001502
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001503 for (wi = 0; wi < wlist->num_widgets; wi++) {
1504 w = wlist->widgets[wi];
1505
1506 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1507 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1508 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001509 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001510 w->name, ret);
1511 }
Mark Brown97404f22010-12-14 16:13:57 +00001512 }
1513
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001514 if (!w)
1515 return;
1516
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001517 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1518 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001519 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001520 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001521 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001522
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001523 for (wi = 0; wi < wlist->num_widgets; wi++) {
1524 w = wlist->widgets[wi];
1525
1526 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1527 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1528 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001529 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001530 w->name, ret);
1531 }
Mark Brown97404f22010-12-14 16:13:57 +00001532 }
1533}
1534
Mark Brown9d0624a2011-02-18 11:49:43 -08001535/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1536 * they're changing state.
1537 */
1538static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1539{
1540 struct snd_soc_dapm_context *d = data;
1541 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001542
Mark Brown56fba412011-06-04 11:25:10 +01001543 /* If we're off and we're not supposed to be go into STANDBY */
1544 if (d->bias_level == SND_SOC_BIAS_OFF &&
1545 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001546 if (d->dev)
1547 pm_runtime_get_sync(d->dev);
1548
Mark Brown9d0624a2011-02-18 11:49:43 -08001549 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1550 if (ret != 0)
1551 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001552 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001553 }
1554
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001555 /* Prepare for a transition to ON or away from ON */
1556 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1557 d->bias_level != SND_SOC_BIAS_ON) ||
1558 (d->target_bias_level != SND_SOC_BIAS_ON &&
1559 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001560 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1561 if (ret != 0)
1562 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001563 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001564 }
1565}
1566
1567/* Async callback run prior to DAPM sequences - brings to their final
1568 * state.
1569 */
1570static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1571{
1572 struct snd_soc_dapm_context *d = data;
1573 int ret;
1574
1575 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001576 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1577 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1578 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001579 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1580 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001581 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001582 ret);
1583 }
1584
1585 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001586 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1587 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001588 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1589 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001590 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1591 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001592
1593 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001594 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001595 }
1596
1597 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001598 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1599 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001600 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1601 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001602 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001603 ret);
1604 }
1605}
Mark Brown97404f22010-12-14 16:13:57 +00001606
Mark Brownfe4fda52011-10-03 22:36:57 +01001607static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1608 bool power, bool connect)
1609{
1610 /* If a connection is being made or broken then that update
1611 * will have marked the peer dirty, otherwise the widgets are
1612 * not connected and this update has no impact. */
1613 if (!connect)
1614 return;
1615
1616 /* If the peer is already in the state we're moving to then we
1617 * won't have an impact on it. */
1618 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001619 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001620}
1621
Mark Brown05623c42011-09-28 17:02:31 +01001622static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1623 struct list_head *up_list,
1624 struct list_head *down_list)
1625{
Mark Browndb432b42011-10-03 21:06:40 +01001626 struct snd_soc_dapm_path *path;
1627
Mark Brown05623c42011-09-28 17:02:31 +01001628 if (w->power == power)
1629 return;
1630
1631 trace_snd_soc_dapm_widget_power(w, power);
1632
Mark Browndb432b42011-10-03 21:06:40 +01001633 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001634 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001635 */
1636 list_for_each_entry(path, &w->sources, list_sink) {
1637 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001638 dapm_widget_set_peer_power(path->source, power,
1639 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001640 }
1641 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001642 switch (w->id) {
1643 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001644 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001645 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001646 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001647 /* Supplies can't affect their outputs, only their inputs */
1648 break;
1649 default:
1650 list_for_each_entry(path, &w->sinks, list_source) {
1651 if (path->sink) {
1652 dapm_widget_set_peer_power(path->sink, power,
1653 path->connect);
1654 }
Mark Browndb432b42011-10-03 21:06:40 +01001655 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001656 break;
Mark Browndb432b42011-10-03 21:06:40 +01001657 }
1658
Mark Brown05623c42011-09-28 17:02:31 +01001659 if (power)
1660 dapm_seq_insert(w, up_list, true);
1661 else
1662 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001663}
1664
Mark Brown7c81beb2011-09-20 22:22:32 +01001665static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1666 struct list_head *up_list,
1667 struct list_head *down_list)
1668{
Mark Brown7c81beb2011-09-20 22:22:32 +01001669 int power;
1670
1671 switch (w->id) {
1672 case snd_soc_dapm_pre:
1673 dapm_seq_insert(w, down_list, false);
1674 break;
1675 case snd_soc_dapm_post:
1676 dapm_seq_insert(w, up_list, true);
1677 break;
1678
1679 default:
Mark Brownd8050022011-09-28 18:28:23 +01001680 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001681
Mark Brown05623c42011-09-28 17:02:31 +01001682 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001683 break;
1684 }
1685}
1686
Mark Brown42aa3412009-03-01 19:21:10 +00001687/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001688 * Scan each dapm widget for complete audio path.
1689 * A complete path is a route that has valid endpoints i.e.:-
1690 *
1691 * o DAC to output pin.
1692 * o Input Pin to ADC.
1693 * o Input pin to Output pin (bypass, sidetone)
1694 * o DAC to ADC (loopback).
1695 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001696static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001697{
1698 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001699 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001700 LIST_HEAD(up_list);
1701 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001702 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001703 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001704
Mark Brownf9fa2b12014-03-06 16:49:11 +08001705 lockdep_assert_held(&card->dapm_mutex);
1706
Mark Brown84e90932010-11-04 00:07:02 -04001707 trace_snd_soc_dapm_start(card);
1708
Mark Brown56fba412011-06-04 11:25:10 +01001709 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001710 if (d->idle_bias_off)
1711 d->target_bias_level = SND_SOC_BIAS_OFF;
1712 else
1713 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001714 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001715
Liam Girdwood6c120e12012-02-15 15:15:34 +00001716 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001717
Mark Brown6d3ddc82009-05-16 17:47:29 +01001718 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001719 * lists indicating if they should be powered up or down. We
1720 * only check widgets that have been flagged as dirty but note
1721 * that new widgets may be added to the dirty list while we
1722 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001723 */
Mark Browndb432b42011-10-03 21:06:40 +01001724 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001725 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001726 }
1727
Mark Brownf9de6d72011-09-28 17:19:47 +01001728 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001729 switch (w->id) {
1730 case snd_soc_dapm_pre:
1731 case snd_soc_dapm_post:
1732 /* These widgets always need to be powered */
1733 break;
1734 default:
1735 list_del_init(&w->dirty);
1736 break;
1737 }
Mark Browndb432b42011-10-03 21:06:40 +01001738
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001739 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001740 d = w->dapm;
1741
1742 /* Supplies and micbiases only bring the
1743 * context up to STANDBY as unless something
1744 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001745 * generally don't require full power. Signal
1746 * generators are virtual pins and have no
1747 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001748 */
1749 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001750 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001751 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001752 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001753 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001754 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001755 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001756 case snd_soc_dapm_micbias:
1757 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1758 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1759 break;
1760 default:
1761 d->target_bias_level = SND_SOC_BIAS_ON;
1762 break;
1763 }
1764 }
1765
1766 }
1767
Mark Brown85a843c2011-09-21 21:29:47 +01001768 /* Force all contexts in the card to the same bias state if
1769 * they're not ground referenced.
1770 */
Mark Brown56fba412011-06-04 11:25:10 +01001771 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001772 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001773 if (d->target_bias_level > bias)
1774 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001775 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001776 if (!d->idle_bias_off)
1777 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001778
Mark Brownde02d072011-09-20 21:43:24 +01001779 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001780
Xiang Xiao17282ba2014-03-02 00:04:03 +08001781 /* Run card bias changes at first */
1782 dapm_pre_sequence_async(&card->dapm, 0);
1783 /* Run other bias changes in parallel */
1784 list_for_each_entry(d, &card->dapm_list, list) {
1785 if (d != &card->dapm)
1786 async_schedule_domain(dapm_pre_sequence_async, d,
1787 &async_domain);
1788 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001789 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001790
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001791 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001792 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001793 }
1794
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001795 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001796 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001797 }
1798
Mark Brown6d3ddc82009-05-16 17:47:29 +01001799 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001800 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001801
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001802 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001803
Mark Brown6d3ddc82009-05-16 17:47:29 +01001804 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001805 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001806
Mark Brown9d0624a2011-02-18 11:49:43 -08001807 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001808 list_for_each_entry(d, &card->dapm_list, list) {
1809 if (d != &card->dapm)
1810 async_schedule_domain(dapm_post_sequence_async, d,
1811 &async_domain);
1812 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001813 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001814 /* Run card bias changes at last */
1815 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001816
Liam Girdwood8078d872012-02-15 15:15:35 +00001817 /* do we need to notify any clients that DAPM event is complete */
1818 list_for_each_entry(d, &card->dapm_list, list) {
1819 if (d->stream_event)
1820 d->stream_event(d, event);
1821 }
1822
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001823 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001824 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001825 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001826
Mark Brown84e90932010-11-04 00:07:02 -04001827 trace_snd_soc_dapm_done(card);
1828
Mark Brown42aa3412009-03-01 19:21:10 +00001829 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001830}
1831
Mark Brown79fb9382009-08-21 16:38:13 +01001832#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001833static ssize_t dapm_widget_power_read_file(struct file *file,
1834 char __user *user_buf,
1835 size_t count, loff_t *ppos)
1836{
1837 struct snd_soc_dapm_widget *w = file->private_data;
1838 char *buf;
1839 int in, out;
1840 ssize_t ret;
1841 struct snd_soc_dapm_path *p = NULL;
1842
1843 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1844 if (!buf)
1845 return -ENOMEM;
1846
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001847 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001848 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001849 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001850 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001851
Mark Brownf13ebad2012-03-03 18:01:01 +00001852 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1853 w->name, w->power ? "On" : "Off",
1854 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001855
Mark Brownd033c362009-12-04 15:25:56 +00001856 if (w->reg >= 0)
1857 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001858 " - R%d(0x%x) mask 0x%x",
1859 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001860
1861 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1862
Mark Brown3eef08b2009-09-14 16:49:00 +01001863 if (w->sname)
1864 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1865 w->sname,
1866 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001867
1868 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001869 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001870 continue;
1871
Mark Brown79fb9382009-08-21 16:38:13 +01001872 if (p->connect)
1873 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001874 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001875 p->name ? p->name : "static",
1876 p->source->name);
1877 }
1878 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001879 if (p->connected && !p->connected(w, p->sink))
1880 continue;
1881
Mark Brown79fb9382009-08-21 16:38:13 +01001882 if (p->connect)
1883 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001884 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001885 p->name ? p->name : "static",
1886 p->sink->name);
1887 }
1888
1889 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1890
1891 kfree(buf);
1892 return ret;
1893}
1894
1895static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001896 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001897 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001898 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001899};
1900
Mark Brownef49e4f2011-04-04 20:48:13 +09001901static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1902 size_t count, loff_t *ppos)
1903{
1904 struct snd_soc_dapm_context *dapm = file->private_data;
1905 char *level;
1906
1907 switch (dapm->bias_level) {
1908 case SND_SOC_BIAS_ON:
1909 level = "On\n";
1910 break;
1911 case SND_SOC_BIAS_PREPARE:
1912 level = "Prepare\n";
1913 break;
1914 case SND_SOC_BIAS_STANDBY:
1915 level = "Standby\n";
1916 break;
1917 case SND_SOC_BIAS_OFF:
1918 level = "Off\n";
1919 break;
1920 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001921 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09001922 level = "Unknown\n";
1923 break;
1924 }
1925
1926 return simple_read_from_buffer(user_buf, count, ppos, level,
1927 strlen(level));
1928}
1929
1930static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001931 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001932 .read = dapm_bias_read_file,
1933 .llseek = default_llseek,
1934};
1935
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001936void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1937 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001938{
Mark Brown79fb9382009-08-21 16:38:13 +01001939 struct dentry *d;
1940
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001941 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1942
1943 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001944 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001945 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001946 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001947 }
Mark Brown79fb9382009-08-21 16:38:13 +01001948
Mark Brownef49e4f2011-04-04 20:48:13 +09001949 d = debugfs_create_file("bias_level", 0444,
1950 dapm->debugfs_dapm, dapm,
1951 &dapm_bias_fops);
1952 if (!d)
1953 dev_warn(dapm->dev,
1954 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001955}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001956
1957static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1958{
1959 struct snd_soc_dapm_context *dapm = w->dapm;
1960 struct dentry *d;
1961
1962 if (!dapm->debugfs_dapm || !w->name)
1963 return;
1964
1965 d = debugfs_create_file(w->name, 0444,
1966 dapm->debugfs_dapm, w,
1967 &dapm_widget_power_fops);
1968 if (!d)
1969 dev_warn(w->dapm->dev,
1970 "ASoC: Failed to create %s debugfs file\n",
1971 w->name);
1972}
1973
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001974static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1975{
1976 debugfs_remove_recursive(dapm->debugfs_dapm);
1977}
1978
Mark Brown79fb9382009-08-21 16:38:13 +01001979#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001980void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1981 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001982{
1983}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001984
1985static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1986{
1987}
1988
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001989static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1990{
1991}
1992
Mark Brown79fb9382009-08-21 16:38:13 +01001993#endif
1994
Richard Purdie2b97eab2006-10-06 18:32:18 +02001995/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001996static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001997 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001998{
1999 struct snd_soc_dapm_path *path;
2000 int found = 0;
2001
Mark Brownf9fa2b12014-03-06 16:49:11 +08002002 lockdep_assert_held(&card->dapm_mutex);
2003
Richard Purdie2b97eab2006-10-06 18:32:18 +02002004 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002005 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002006 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002007 continue;
2008
2009 found = 1;
2010 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002011 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002012 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002013 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002014 } else {
2015 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002016 dapm_mark_dirty(path->source,
2017 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002018 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002019 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002020 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002021 }
2022
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002023 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002024 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002025
Liam Girdwood618dae12012-04-25 12:12:51 +01002026 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002027}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002028
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002029int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002030 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2031 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002032{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002033 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002034 int ret;
2035
Liam Girdwood3cd04342012-03-09 12:02:08 +00002036 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002037 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002038 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002039 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002040 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002041 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002042 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002043 return ret;
2044}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002045EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002046
Milan plzik1b075e32008-01-10 14:39:46 +01002047/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002048static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002049 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002050{
2051 struct snd_soc_dapm_path *path;
2052 int found = 0;
2053
Mark Brownf9fa2b12014-03-06 16:49:11 +08002054 lockdep_assert_held(&card->dapm_mutex);
2055
Richard Purdie2b97eab2006-10-06 18:32:18 +02002056 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002057 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002058 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002059 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002060 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002061 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002062 }
2063
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002064 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002065 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002066
Liam Girdwood618dae12012-04-25 12:12:51 +01002067 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002068}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002069
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002070int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002071 struct snd_kcontrol *kcontrol, int connect,
2072 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002073{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002074 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002075 int ret;
2076
Liam Girdwood3cd04342012-03-09 12:02:08 +00002077 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002078 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002079 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002080 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002081 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002082 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002083 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002084 return ret;
2085}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002086EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002087
Benoit Cousson44ba2642014-07-08 23:19:36 +02002088static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002089{
Richard Purdie2b97eab2006-10-06 18:32:18 +02002090 struct snd_soc_dapm_widget *w;
2091 int count = 0;
2092 char *state = "not set";
2093
Lars-Peter Clausen00200102014-07-17 22:01:07 +02002094 list_for_each_entry(w, &codec->component.card->widgets, list) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002095 if (w->dapm != &codec->dapm)
2096 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002097
2098 /* only display widgets that burnm power */
2099 switch (w->id) {
2100 case snd_soc_dapm_hp:
2101 case snd_soc_dapm_mic:
2102 case snd_soc_dapm_spk:
2103 case snd_soc_dapm_line:
2104 case snd_soc_dapm_micbias:
2105 case snd_soc_dapm_dac:
2106 case snd_soc_dapm_adc:
2107 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002108 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002109 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002110 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002111 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002112 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002113 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002114 if (w->name)
2115 count += sprintf(buf + count, "%s: %s\n",
2116 w->name, w->power ? "On":"Off");
2117 break;
2118 default:
2119 break;
2120 }
2121 }
2122
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002123 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002124 case SND_SOC_BIAS_ON:
2125 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002126 break;
Mark Brown0be98982008-05-19 12:31:28 +02002127 case SND_SOC_BIAS_PREPARE:
2128 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002129 break;
Mark Brown0be98982008-05-19 12:31:28 +02002130 case SND_SOC_BIAS_STANDBY:
2131 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002132 break;
Mark Brown0be98982008-05-19 12:31:28 +02002133 case SND_SOC_BIAS_OFF:
2134 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002135 break;
2136 }
2137 count += sprintf(buf + count, "PM State: %s\n", state);
2138
2139 return count;
2140}
2141
Benoit Cousson44ba2642014-07-08 23:19:36 +02002142/* show dapm widget status in sys fs */
2143static ssize_t dapm_widget_show(struct device *dev,
2144 struct device_attribute *attr, char *buf)
2145{
2146 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2147 int i, count = 0;
2148
2149 for (i = 0; i < rtd->num_codecs; i++) {
2150 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
2151 count += dapm_widget_show_codec(codec, buf + count);
2152 }
2153
2154 return count;
2155}
2156
Richard Purdie2b97eab2006-10-06 18:32:18 +02002157static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2158
2159int snd_soc_dapm_sys_add(struct device *dev)
2160{
Troy Kisky12ef1932008-10-13 17:42:14 -07002161 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002162}
2163
2164static void snd_soc_dapm_sys_remove(struct device *dev)
2165{
Mark Brownaef90842009-05-16 17:53:16 +01002166 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002167}
2168
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002169static void dapm_free_path(struct snd_soc_dapm_path *path)
2170{
2171 list_del(&path->list_sink);
2172 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002173 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002174 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002175 kfree(path);
2176}
2177
Richard Purdie2b97eab2006-10-06 18:32:18 +02002178/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002179static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002180{
2181 struct snd_soc_dapm_widget *w, *next_w;
2182 struct snd_soc_dapm_path *p, *next_p;
2183
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002184 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2185 if (w->dapm != dapm)
2186 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002187 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002188 /*
2189 * remove source and sink paths associated to this widget.
2190 * While removing the path, remove reference to it from both
2191 * source and sink widgets so that path is removed only once.
2192 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002193 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2194 dapm_free_path(p);
2195
2196 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2197 dapm_free_path(p);
2198
Stephen Warrenfad59882011-04-28 17:37:59 -06002199 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002200 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002201 kfree(w);
2202 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002203}
2204
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002205static struct snd_soc_dapm_widget *dapm_find_widget(
2206 struct snd_soc_dapm_context *dapm, const char *pin,
2207 bool search_other_contexts)
2208{
2209 struct snd_soc_dapm_widget *w;
2210 struct snd_soc_dapm_widget *fallback = NULL;
2211
2212 list_for_each_entry(w, &dapm->card->widgets, list) {
2213 if (!strcmp(w->name, pin)) {
2214 if (w->dapm == dapm)
2215 return w;
2216 else
2217 fallback = w;
2218 }
2219 }
2220
2221 if (search_other_contexts)
2222 return fallback;
2223
2224 return NULL;
2225}
2226
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002227static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002228 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002229{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002230 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002231
Mark Brownf9fa2b12014-03-06 16:49:11 +08002232 dapm_assert_locked(dapm);
2233
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002234 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002235 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002236 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002237 }
2238
Mark Brown1a8b2d92012-02-16 11:50:07 -08002239 if (w->connected != status)
2240 dapm_mark_dirty(w, "pin configuration");
2241
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002242 w->connected = status;
2243 if (status == 0)
2244 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002245
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002246 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002247}
2248
Richard Purdie2b97eab2006-10-06 18:32:18 +02002249/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002250 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2251 * @dapm: DAPM context
2252 *
2253 * Walks all dapm audio paths and powers widgets according to their
2254 * stream or path usage.
2255 *
2256 * Requires external locking.
2257 *
2258 * Returns 0 for success.
2259 */
2260int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2261{
2262 /*
2263 * Suppress early reports (eg, jacks syncing their state) to avoid
2264 * silly DAPM runs during card startup.
2265 */
2266 if (!dapm->card || !dapm->card->instantiated)
2267 return 0;
2268
2269 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2270}
2271EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2272
2273/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002274 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002275 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002276 *
2277 * Walks all dapm audio paths and powers widgets according to their
2278 * stream or path usage.
2279 *
2280 * Returns 0 for success.
2281 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002282int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002283{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002284 int ret;
2285
Liam Girdwood3cd04342012-03-09 12:02:08 +00002286 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002287 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002288 mutex_unlock(&dapm->card->dapm_mutex);
2289 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002290}
Liam Girdwooda5302182008-07-07 13:35:17 +01002291EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002292
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002293static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2294 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2295 const char *control,
2296 int (*connected)(struct snd_soc_dapm_widget *source,
2297 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002298{
2299 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002300 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002301
2302 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2303 if (!path)
2304 return -ENOMEM;
2305
2306 path->source = wsource;
2307 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002308 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002309 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002310 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002311 INIT_LIST_HEAD(&path->list_source);
2312 INIT_LIST_HEAD(&path->list_sink);
2313
2314 /* check for external widgets */
2315 if (wsink->id == snd_soc_dapm_input) {
2316 if (wsource->id == snd_soc_dapm_micbias ||
2317 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002318 wsource->id == snd_soc_dapm_line ||
2319 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002320 wsink->ext = 1;
2321 }
2322 if (wsource->id == snd_soc_dapm_output) {
2323 if (wsink->id == snd_soc_dapm_spk ||
2324 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002325 wsink->id == snd_soc_dapm_line ||
2326 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002327 wsource->ext = 1;
2328 }
2329
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002330 dapm_mark_dirty(wsource, "Route added");
2331 dapm_mark_dirty(wsink, "Route added");
2332
Richard Purdie2b97eab2006-10-06 18:32:18 +02002333 /* connect static paths */
2334 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002335 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002336 list_add(&path->list_sink, &wsink->sources);
2337 list_add(&path->list_source, &wsource->sinks);
2338 path->connect = 1;
2339 return 0;
2340 }
2341
2342 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002343 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002344 case snd_soc_dapm_adc:
2345 case snd_soc_dapm_dac:
2346 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002347 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002348 case snd_soc_dapm_input:
2349 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002350 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002351 case snd_soc_dapm_micbias:
2352 case snd_soc_dapm_vmid:
2353 case snd_soc_dapm_pre:
2354 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002355 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002356 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002357 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002358 case snd_soc_dapm_aif_in:
2359 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002360 case snd_soc_dapm_dai_in:
2361 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002362 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002363 case snd_soc_dapm_kcontrol:
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 = 1;
2368 return 0;
2369 case snd_soc_dapm_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002370 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002371 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002372 if (ret != 0)
2373 goto err;
2374 break;
2375 case snd_soc_dapm_switch:
2376 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002377 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002378 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002379 if (ret != 0)
2380 goto err;
2381 break;
2382 case snd_soc_dapm_hp:
2383 case snd_soc_dapm_mic:
2384 case snd_soc_dapm_line:
2385 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002386 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002387 list_add(&path->list_sink, &wsink->sources);
2388 list_add(&path->list_source, &wsource->sinks);
2389 path->connect = 0;
2390 return 0;
2391 }
Mark Brownfabd0382012-07-05 17:20:06 +01002392
Richard Purdie2b97eab2006-10-06 18:32:18 +02002393 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002394err:
2395 kfree(path);
2396 return ret;
2397}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002398
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002399static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002400 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002401{
2402 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2403 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2404 const char *sink;
2405 const char *source;
2406 char prefixed_sink[80];
2407 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002408 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002409 int ret;
2410
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002411 prefix = soc_dapm_prefix(dapm);
2412 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002413 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002414 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002415 sink = prefixed_sink;
2416 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002417 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002418 source = prefixed_source;
2419 } else {
2420 sink = route->sink;
2421 source = route->source;
2422 }
2423
2424 /*
2425 * find src and dest widgets over all widgets but favor a widget from
2426 * current DAPM context
2427 */
2428 list_for_each_entry(w, &dapm->card->widgets, list) {
2429 if (!wsink && !(strcmp(w->name, sink))) {
2430 wtsink = w;
2431 if (w->dapm == dapm)
2432 wsink = w;
2433 continue;
2434 }
2435 if (!wsource && !(strcmp(w->name, source))) {
2436 wtsource = w;
2437 if (w->dapm == dapm)
2438 wsource = w;
2439 }
2440 }
2441 /* use widget from another DAPM context if not found from this */
2442 if (!wsink)
2443 wsink = wtsink;
2444 if (!wsource)
2445 wsource = wtsource;
2446
2447 if (wsource == NULL) {
2448 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2449 route->source);
2450 return -ENODEV;
2451 }
2452 if (wsink == NULL) {
2453 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2454 route->sink);
2455 return -ENODEV;
2456 }
2457
2458 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2459 route->connected);
2460 if (ret)
2461 goto err;
2462
2463 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002464err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002465 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002466 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002467 return ret;
2468}
Mark Brown105f1c22008-05-13 14:52:19 +02002469
Mark Brownefcc3c62012-07-05 17:24:19 +01002470static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2471 const struct snd_soc_dapm_route *route)
2472{
2473 struct snd_soc_dapm_path *path, *p;
2474 const char *sink;
2475 const char *source;
2476 char prefixed_sink[80];
2477 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002478 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002479
2480 if (route->control) {
2481 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002482 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002483 return -EINVAL;
2484 }
2485
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002486 prefix = soc_dapm_prefix(dapm);
2487 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002488 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002489 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002490 sink = prefixed_sink;
2491 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002492 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002493 source = prefixed_source;
2494 } else {
2495 sink = route->sink;
2496 source = route->source;
2497 }
2498
2499 path = NULL;
2500 list_for_each_entry(p, &dapm->card->paths, list) {
2501 if (strcmp(p->source->name, source) != 0)
2502 continue;
2503 if (strcmp(p->sink->name, sink) != 0)
2504 continue;
2505 path = p;
2506 break;
2507 }
2508
2509 if (path) {
2510 dapm_mark_dirty(path->source, "Route removed");
2511 dapm_mark_dirty(path->sink, "Route removed");
2512
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002513 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002514 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002515 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002516 source, sink);
2517 }
2518
2519 return 0;
2520}
2521
Mark Brown105f1c22008-05-13 14:52:19 +02002522/**
Mark Brown105f1c22008-05-13 14:52:19 +02002523 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002524 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002525 * @route: audio routes
2526 * @num: number of routes
2527 *
2528 * Connects 2 dapm widgets together via a named audio path. The sink is
2529 * the widget receiving the audio signal, whilst the source is the sender
2530 * of the audio signal.
2531 *
2532 * Returns 0 for success else error. On error all resources can be freed
2533 * with a call to snd_soc_card_free().
2534 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002535int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002536 const struct snd_soc_dapm_route *route, int num)
2537{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002538 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002539
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002540 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002541 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002542 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002543 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002544 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2545 route->source,
2546 route->control ? route->control : "direct",
2547 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002548 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002549 }
2550 route++;
2551 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002552 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002553
Dan Carpenter60884c22012-04-13 22:25:43 +03002554 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002555}
2556EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2557
Mark Brownefcc3c62012-07-05 17:24:19 +01002558/**
2559 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2560 * @dapm: DAPM context
2561 * @route: audio routes
2562 * @num: number of routes
2563 *
2564 * Removes routes from the DAPM context.
2565 */
2566int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2567 const struct snd_soc_dapm_route *route, int num)
2568{
2569 int i, ret = 0;
2570
2571 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2572 for (i = 0; i < num; i++) {
2573 snd_soc_dapm_del_route(dapm, route);
2574 route++;
2575 }
2576 mutex_unlock(&dapm->card->dapm_mutex);
2577
2578 return ret;
2579}
2580EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2581
Mark Brownbf3a9e12011-06-13 16:42:29 +01002582static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2583 const struct snd_soc_dapm_route *route)
2584{
2585 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2586 route->source,
2587 true);
2588 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2589 route->sink,
2590 true);
2591 struct snd_soc_dapm_path *path;
2592 int count = 0;
2593
2594 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002595 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002596 route->source);
2597 return -ENODEV;
2598 }
2599
2600 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002601 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002602 route->sink);
2603 return -ENODEV;
2604 }
2605
2606 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002607 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002608 route->source, route->sink);
2609
2610 list_for_each_entry(path, &source->sinks, list_source) {
2611 if (path->sink == sink) {
2612 path->weak = 1;
2613 count++;
2614 }
2615 }
2616
2617 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002618 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002619 route->source, route->sink);
2620 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002621 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002622 count, route->source, route->sink);
2623
2624 return 0;
2625}
2626
2627/**
2628 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2629 * @dapm: DAPM context
2630 * @route: audio routes
2631 * @num: number of routes
2632 *
2633 * Mark existing routes matching those specified in the passed array
2634 * as being weak, meaning that they are ignored for the purpose of
2635 * power decisions. The main intended use case is for sidetone paths
2636 * which couple audio between other independent paths if they are both
2637 * active in order to make the combination work better at the user
2638 * level but which aren't intended to be "used".
2639 *
2640 * Note that CODEC drivers should not use this as sidetone type paths
2641 * can frequently also be used as bypass paths.
2642 */
2643int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2644 const struct snd_soc_dapm_route *route, int num)
2645{
2646 int i, err;
2647 int ret = 0;
2648
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002649 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002650 for (i = 0; i < num; i++) {
2651 err = snd_soc_dapm_weak_route(dapm, route);
2652 if (err)
2653 ret = err;
2654 route++;
2655 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002656 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002657
2658 return ret;
2659}
2660EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2661
Mark Brown105f1c22008-05-13 14:52:19 +02002662/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002663 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002664 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002665 *
2666 * Checks the codec for any new dapm widgets and creates them if found.
2667 *
2668 * Returns 0 for success.
2669 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002670int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002671{
2672 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002673 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002674
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002675 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002676
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002677 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002678 {
2679 if (w->new)
2680 continue;
2681
Stephen Warrenfad59882011-04-28 17:37:59 -06002682 if (w->num_kcontrols) {
2683 w->kcontrols = kzalloc(w->num_kcontrols *
2684 sizeof(struct snd_kcontrol *),
2685 GFP_KERNEL);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002686 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002687 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002688 return -ENOMEM;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002689 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002690 }
2691
Richard Purdie2b97eab2006-10-06 18:32:18 +02002692 switch(w->id) {
2693 case snd_soc_dapm_switch:
2694 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002695 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002696 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002697 break;
2698 case snd_soc_dapm_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002699 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002700 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002701 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002702 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002703 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002704 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002705 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002706 break;
2707 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002708
2709 /* Read the initial power state from the device */
2710 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002711 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002712 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002713 val &= w->mask;
2714 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002715 w->power = 1;
2716 }
2717
Richard Purdie2b97eab2006-10-06 18:32:18 +02002718 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002719
Mark Brown7508b122011-10-05 12:09:12 +01002720 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002721 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002722 }
2723
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002724 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2725 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002726 return 0;
2727}
2728EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2729
2730/**
2731 * snd_soc_dapm_get_volsw - dapm mixer get callback
2732 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002733 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002734 *
2735 * Callback to get the value of a dapm mixer control.
2736 *
2737 * Returns 0 for success.
2738 */
2739int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2740 struct snd_ctl_elem_value *ucontrol)
2741{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002742 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2743 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002744 struct soc_mixer_control *mc =
2745 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002746 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002747 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002748 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002749 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002750 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002751 unsigned int val;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002752 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002753
2754 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002755 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002756 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002757 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002758
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002759 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002760 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
2761 ret = soc_dapm_read(dapm, reg, &val);
2762 val = (val >> shift) & mask;
2763 } else {
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002764 val = dapm_kcontrol_get_value(kcontrol);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002765 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002766 mutex_unlock(&card->dapm_mutex);
2767
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002768 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002769 ucontrol->value.integer.value[0] = max - val;
2770 else
2771 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002772
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002773 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002774}
2775EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2776
2777/**
2778 * snd_soc_dapm_put_volsw - dapm mixer set callback
2779 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002780 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002781 *
2782 * Callback to set the value of a dapm mixer control.
2783 *
2784 * Returns 0 for success.
2785 */
2786int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2787 struct snd_ctl_elem_value *ucontrol)
2788{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002789 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2790 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002791 struct soc_mixer_control *mc =
2792 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002793 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002794 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002795 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002796 unsigned int mask = (1 << fls(max)) - 1;
2797 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002798 unsigned int val;
Jarkko Nikula18626c72014-06-09 14:20:29 +03002799 int connect, change, reg_change = 0;
Mark Brown97404f22010-12-14 16:13:57 +00002800 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002801 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002802
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002803 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002804 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002805 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002806 kcontrol->id.name);
2807
Richard Purdie2b97eab2006-10-06 18:32:18 +02002808 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002809 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002810
2811 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002812 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002813
Liam Girdwood3cd04342012-03-09 12:02:08 +00002814 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002815
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002816 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown283375c2009-12-07 18:09:03 +00002817
Jarkko Nikula18626c72014-06-09 14:20:29 +03002818 if (reg != SND_SOC_NOPM) {
2819 mask = mask << shift;
2820 val = val << shift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02002821
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002822 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
Jarkko Nikula18626c72014-06-09 14:20:29 +03002823 }
2824
2825 if (change || reg_change) {
2826 if (reg_change) {
2827 update.kcontrol = kcontrol;
2828 update.reg = reg;
2829 update.mask = mask;
2830 update.val = val;
2831 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002832 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03002833 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00002834
Nenghua Cao52765972013-12-13 20:13:49 +08002835 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002836
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002837 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002838 }
2839
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002840 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002841
2842 if (ret > 0)
2843 soc_dpcm_runtime_update(card);
2844
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002845 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002846}
2847EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2848
2849/**
2850 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2851 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002852 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002853 *
2854 * Callback to get the value of a dapm enumerated double mixer control.
2855 *
2856 * Returns 0 for success.
2857 */
2858int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2859 struct snd_ctl_elem_value *ucontrol)
2860{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002861 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002862 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002863 unsigned int reg_val, val;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002864 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002865
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002866 if (e->reg != SND_SOC_NOPM)
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002867 ret = soc_dapm_read(dapm, e->reg, &reg_val);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002868 else
2869 reg_val = dapm_kcontrol_get_value(kcontrol);
2870
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002871 val = (reg_val >> e->shift_l) & e->mask;
2872 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2873 if (e->shift_l != e->shift_r) {
2874 val = (reg_val >> e->shift_r) & e->mask;
2875 val = snd_soc_enum_val_to_item(e, val);
2876 ucontrol->value.enumerated.item[1] = val;
2877 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002878
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002879 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002880}
2881EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2882
2883/**
2884 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2885 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002886 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002887 *
2888 * Callback to set the value of a dapm enumerated double mixer control.
2889 *
2890 * Returns 0 for success.
2891 */
2892int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2893 struct snd_ctl_elem_value *ucontrol)
2894{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002895 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2896 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002897 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002898 unsigned int *item = ucontrol->value.enumerated.item;
2899 unsigned int val, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002900 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002901 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002902 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002903
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002904 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002905 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002906
2907 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002908 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002909 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002910 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002911 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002912 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002913 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002914 }
2915
Liam Girdwood3cd04342012-03-09 12:02:08 +00002916 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002917
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002918 if (e->reg != SND_SOC_NOPM)
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002919 change = soc_dapm_test_bits(dapm, e->reg, mask, val);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002920 else
2921 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002922
Richard Purdie2b97eab2006-10-06 18:32:18 +02002923 if (change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002924 if (e->reg != SND_SOC_NOPM) {
2925 update.kcontrol = kcontrol;
2926 update.reg = e->reg;
2927 update.mask = mask;
2928 update.val = val;
2929 card->update = &update;
2930 }
Mark Brown3a655772009-10-05 17:23:30 +01002931
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002932 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002933
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002934 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002935 }
2936
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002937 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002938
2939 if (ret > 0)
2940 soc_dpcm_runtime_update(card);
2941
Mark Brown97404f22010-12-14 16:13:57 +00002942 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002943}
2944EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2945
2946/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002947 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2948 *
2949 * @kcontrol: mixer control
2950 * @uinfo: control element information
2951 *
2952 * Callback to provide information about a pin switch control.
2953 */
2954int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2955 struct snd_ctl_elem_info *uinfo)
2956{
2957 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2958 uinfo->count = 1;
2959 uinfo->value.integer.min = 0;
2960 uinfo->value.integer.max = 1;
2961
2962 return 0;
2963}
2964EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2965
2966/**
2967 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2968 *
2969 * @kcontrol: mixer control
2970 * @ucontrol: Value
2971 */
2972int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2973 struct snd_ctl_elem_value *ucontrol)
2974{
Mark Brown48a8c392012-02-14 17:11:15 -08002975 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002976 const char *pin = (const char *)kcontrol->private_value;
2977
Liam Girdwood3cd04342012-03-09 12:02:08 +00002978 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002979
2980 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002981 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002982
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002983 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002984
2985 return 0;
2986}
2987EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2988
2989/**
2990 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2991 *
2992 * @kcontrol: mixer control
2993 * @ucontrol: Value
2994 */
2995int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2996 struct snd_ctl_elem_value *ucontrol)
2997{
Mark Brown48a8c392012-02-14 17:11:15 -08002998 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002999 const char *pin = (const char *)kcontrol->private_value;
3000
Mark Brown8b37dbd2009-02-28 21:14:20 +00003001 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003002 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003003 else
Mark Brown48a8c392012-02-14 17:11:15 -08003004 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003005
Mark Brown48a8c392012-02-14 17:11:15 -08003006 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003007 return 0;
3008}
3009EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3010
Mark Brown5ba06fc2012-02-16 11:07:13 -08003011static struct snd_soc_dapm_widget *
3012snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3013 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003014{
3015 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003016 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003017 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003018
3019 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003020 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003021
Mark Brown62ea8742012-01-21 21:14:48 +00003022 switch (w->id) {
3023 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003024 w->regulator = devm_regulator_get(dapm->dev, w->name);
3025 if (IS_ERR(w->regulator)) {
3026 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003027 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003028 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003029 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003030 }
Mark Brown8784c772013-01-10 19:33:47 +00003031
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003032 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003033 ret = regulator_allow_bypass(w->regulator, true);
3034 if (ret != 0)
3035 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003036 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003037 w->name, ret);
3038 }
Mark Brown62ea8742012-01-21 21:14:48 +00003039 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003040 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003041#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003042 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003043 if (IS_ERR(w->clk)) {
3044 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003045 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003046 w->name, ret);
3047 return NULL;
3048 }
Mark Brownec029952012-06-04 08:16:20 +01003049#else
3050 return NULL;
3051#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003052 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003053 default:
3054 break;
3055 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003056
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003057 prefix = soc_dapm_prefix(dapm);
3058 if (prefix)
3059 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003060 else
3061 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3062
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003063 if (w->name == NULL) {
3064 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003065 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003066 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003067
Mark Brown7ca3a182011-10-08 14:04:50 +01003068 switch (w->id) {
3069 case snd_soc_dapm_switch:
3070 case snd_soc_dapm_mixer:
3071 case snd_soc_dapm_mixer_named_ctl:
3072 w->power_check = dapm_generic_check_power;
3073 break;
3074 case snd_soc_dapm_mux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003075 w->power_check = dapm_generic_check_power;
3076 break;
Mark Brown46162742013-06-05 19:36:11 +01003077 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003078 w->power_check = dapm_adc_check_power;
3079 break;
Mark Brown46162742013-06-05 19:36:11 +01003080 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003081 w->power_check = dapm_dac_check_power;
3082 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003083 case snd_soc_dapm_adc:
3084 case snd_soc_dapm_aif_out:
3085 case snd_soc_dapm_dac:
3086 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003087 case snd_soc_dapm_pga:
3088 case snd_soc_dapm_out_drv:
3089 case snd_soc_dapm_input:
3090 case snd_soc_dapm_output:
3091 case snd_soc_dapm_micbias:
3092 case snd_soc_dapm_spk:
3093 case snd_soc_dapm_hp:
3094 case snd_soc_dapm_mic:
3095 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003096 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003097 w->power_check = dapm_generic_check_power;
3098 break;
3099 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003100 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003101 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003102 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003103 w->power_check = dapm_supply_check_power;
3104 break;
3105 default:
3106 w->power_check = dapm_always_on_check_power;
3107 break;
3108 }
3109
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003110 w->dapm = dapm;
3111 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003112 INIT_LIST_HEAD(&w->sources);
3113 INIT_LIST_HEAD(&w->sinks);
3114 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003115 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003116 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003117
3118 /* machine layer set ups unconnected pins and insertions */
3119 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003120 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003121}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003122
3123/**
Mark Brown4ba13272008-05-13 14:51:19 +02003124 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003125 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003126 * @widget: widget array
3127 * @num: number of widgets
3128 *
3129 * Creates new DAPM controls based upon the templates.
3130 *
3131 * Returns 0 for success else error.
3132 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003133int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003134 const struct snd_soc_dapm_widget *widget,
3135 int num)
3136{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003137 struct snd_soc_dapm_widget *w;
3138 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003139 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003140
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003141 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003142 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003143 w = snd_soc_dapm_new_control(dapm, widget);
3144 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003145 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003146 "ASoC: Failed to create DAPM control %s\n",
3147 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003148 ret = -ENOMEM;
3149 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003150 }
Mark Brown4ba13272008-05-13 14:51:19 +02003151 widget++;
3152 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003153 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003154 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003155}
3156EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3157
Mark Brownc74184e2012-04-04 22:12:09 +01003158static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3159 struct snd_kcontrol *kcontrol, int event)
3160{
3161 struct snd_soc_dapm_path *source_p, *sink_p;
3162 struct snd_soc_dai *source, *sink;
3163 const struct snd_soc_pcm_stream *config = w->params;
3164 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003165 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003166 u64 fmt;
3167 int ret;
3168
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003169 if (WARN_ON(!config) ||
3170 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3171 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003172
3173 /* We only support a single source and sink, pick the first */
3174 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3175 list_sink);
3176 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3177 list_source);
3178
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003179 if (WARN_ON(!source_p || !sink_p) ||
3180 WARN_ON(!sink_p->source || !source_p->sink) ||
3181 WARN_ON(!source_p->source || !sink_p->sink))
3182 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003183
3184 source = source_p->source->priv;
3185 sink = sink_p->sink->priv;
3186
3187 /* Be a little careful as we don't want to overflow the mask array */
3188 if (config->formats) {
3189 fmt = ffs(config->formats) - 1;
3190 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003191 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003192 config->formats);
3193 fmt = 0;
3194 }
3195
3196 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003197 params = kzalloc(sizeof(*params), GFP_KERNEL);
3198 if (!params) {
3199 ret = -ENOMEM;
3200 goto out;
3201 }
3202 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003203
Mark Brown9747cec2012-04-26 19:12:21 +01003204 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003205 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003206 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003207 config->rate_max;
3208
Mark Brown9747cec2012-04-26 19:12:21 +01003209 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003210 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003211 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003212 = config->channels_max;
3213
3214 memset(&substream, 0, sizeof(substream));
3215
3216 switch (event) {
3217 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003218 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3219 ret = soc_dai_hw_params(&substream, params, source);
3220 if (ret < 0)
3221 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003222
Benoit Cousson93e69582014-07-08 23:19:38 +02003223 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3224 ret = soc_dai_hw_params(&substream, params, sink);
3225 if (ret < 0)
3226 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003227 break;
3228
3229 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003230 ret = snd_soc_dai_digital_mute(sink, 0,
3231 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003232 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003233 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003234 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003235 break;
3236
3237 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003238 ret = snd_soc_dai_digital_mute(sink, 1,
3239 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003240 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003241 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003242 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003243 break;
3244
3245 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003246 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003247 return -EINVAL;
3248 }
3249
Mark Brown9747cec2012-04-26 19:12:21 +01003250out:
3251 kfree(params);
3252 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003253}
3254
3255int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3256 const struct snd_soc_pcm_stream *params,
3257 struct snd_soc_dapm_widget *source,
3258 struct snd_soc_dapm_widget *sink)
3259{
Mark Brownc74184e2012-04-04 22:12:09 +01003260 struct snd_soc_dapm_widget template;
3261 struct snd_soc_dapm_widget *w;
3262 size_t len;
3263 char *link_name;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003264 int ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003265
3266 len = strlen(source->name) + strlen(sink->name) + 2;
3267 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3268 if (!link_name)
3269 return -ENOMEM;
3270 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3271
3272 memset(&template, 0, sizeof(template));
3273 template.reg = SND_SOC_NOPM;
3274 template.id = snd_soc_dapm_dai_link;
3275 template.name = link_name;
3276 template.event = snd_soc_dai_link_event;
3277 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3278 SND_SOC_DAPM_PRE_PMD;
3279
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003280 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003281
3282 w = snd_soc_dapm_new_control(&card->dapm, &template);
3283 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003284 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003285 link_name);
3286 return -ENOMEM;
3287 }
3288
3289 w->params = params;
3290
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003291 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3292 if (ret)
3293 return ret;
3294 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Mark Brownc74184e2012-04-04 22:12:09 +01003295}
3296
Mark Brown888df392012-02-16 19:37:51 -08003297int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3298 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003299{
Mark Brown888df392012-02-16 19:37:51 -08003300 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003301 struct snd_soc_dapm_widget *w;
3302
Mark Brown888df392012-02-16 19:37:51 -08003303 WARN_ON(dapm->dev != dai->dev);
3304
3305 memset(&template, 0, sizeof(template));
3306 template.reg = SND_SOC_NOPM;
3307
3308 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003309 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003310 template.name = dai->driver->playback.stream_name;
3311 template.sname = dai->driver->playback.stream_name;
3312
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003313 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003314 template.name);
3315
3316 w = snd_soc_dapm_new_control(dapm, &template);
3317 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003318 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003319 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003320 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003321 }
3322
3323 w->priv = dai;
3324 dai->playback_widget = w;
3325 }
3326
3327 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003328 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003329 template.name = dai->driver->capture.stream_name;
3330 template.sname = dai->driver->capture.stream_name;
3331
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003332 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003333 template.name);
3334
3335 w = snd_soc_dapm_new_control(dapm, &template);
3336 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003337 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003338 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003339 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003340 }
3341
3342 w->priv = dai;
3343 dai->capture_widget = w;
3344 }
3345
3346 return 0;
3347}
3348
3349int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3350{
3351 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003352 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003353 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003354
3355 /* For each DAI widget... */
3356 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003357 switch (dai_w->id) {
3358 case snd_soc_dapm_dai_in:
3359 case snd_soc_dapm_dai_out:
3360 break;
3361 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003362 continue;
Mark Brown46162742013-06-05 19:36:11 +01003363 }
Mark Brown888df392012-02-16 19:37:51 -08003364
3365 dai = dai_w->priv;
3366
3367 /* ...find all widgets with the same stream and link them */
3368 list_for_each_entry(w, &card->widgets, list) {
3369 if (w->dapm != dai_w->dapm)
3370 continue;
3371
Mark Brown46162742013-06-05 19:36:11 +01003372 switch (w->id) {
3373 case snd_soc_dapm_dai_in:
3374 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003375 continue;
Mark Brown46162742013-06-05 19:36:11 +01003376 default:
3377 break;
3378 }
Mark Brown888df392012-02-16 19:37:51 -08003379
Russell King19c2c5f2013-08-04 20:24:03 +01003380 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003381 continue;
3382
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003383 if (dai_w->id == snd_soc_dapm_dai_in) {
3384 src = dai_w;
3385 sink = w;
3386 } else {
3387 src = w;
3388 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003389 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003390 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3391 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003392 }
3393 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003394
Mark Brown888df392012-02-16 19:37:51 -08003395 return 0;
3396}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003397
Benoit Cousson44ba2642014-07-08 23:19:36 +02003398static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3399 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003400{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003401 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003402 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003403 int i;
3404
Benoit Cousson44ba2642014-07-08 23:19:36 +02003405 for (i = 0; i < rtd->num_codecs; i++) {
3406 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003407
3408 /* there is no point in connecting BE DAI links with dummies */
3409 if (snd_soc_dai_is_dummy(codec_dai) ||
3410 snd_soc_dai_is_dummy(cpu_dai))
3411 continue;
3412
3413 /* connect BE DAI playback if widgets are valid */
3414 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003415 source = cpu_dai->playback_widget;
3416 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003417 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003418 cpu_dai->component->name, source->name,
3419 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003420
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003421 snd_soc_dapm_add_path(&card->dapm, source, sink,
3422 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003423 }
3424
3425 /* connect BE DAI capture if widgets are valid */
3426 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003427 source = codec_dai->capture_widget;
3428 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003429 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003430 codec_dai->component->name, source->name,
3431 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003432
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003433 snd_soc_dapm_add_path(&card->dapm, source, sink,
3434 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003435 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003436 }
3437}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003438
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003439static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3440 int event)
3441{
3442 struct snd_soc_dapm_widget *w;
3443
3444 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3445 w = dai->playback_widget;
3446 else
3447 w = dai->capture_widget;
3448
3449 if (w) {
3450 dapm_mark_dirty(w, "stream event");
3451
3452 switch (event) {
3453 case SND_SOC_DAPM_STREAM_START:
3454 w->active = 1;
3455 break;
3456 case SND_SOC_DAPM_STREAM_STOP:
3457 w->active = 0;
3458 break;
3459 case SND_SOC_DAPM_STREAM_SUSPEND:
3460 case SND_SOC_DAPM_STREAM_RESUME:
3461 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3462 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3463 break;
3464 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003465 }
3466}
3467
Benoit Cousson44ba2642014-07-08 23:19:36 +02003468void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3469{
3470 struct snd_soc_pcm_runtime *rtd = card->rtd;
3471 int i;
3472
3473 /* for each BE DAI link... */
3474 for (i = 0; i < card->num_rtd; i++) {
3475 rtd = &card->rtd[i];
3476
3477 /*
3478 * dynamic FE links have no fixed DAI mapping.
3479 * CODEC<->CODEC links have no direct connection.
3480 */
3481 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3482 continue;
3483
3484 dapm_connect_dai_link_widgets(card, rtd);
3485 }
3486}
3487
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003488static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3489 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003490{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003491 int i;
3492
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003493 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Benoit Cousson44ba2642014-07-08 23:19:36 +02003494 for (i = 0; i < rtd->num_codecs; i++)
3495 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003496
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003497 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003498}
3499
3500/**
3501 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3502 * @rtd: PCM runtime data
3503 * @stream: stream name
3504 * @event: stream event
3505 *
3506 * Sends a stream event to the dapm core. The core then makes any
3507 * necessary widget power changes.
3508 *
3509 * Returns 0 for success else error.
3510 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003511void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3512 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003513{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003514 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003515
Liam Girdwood3cd04342012-03-09 12:02:08 +00003516 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003517 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003518 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003519}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003520
3521/**
Charles Keepax11391102014-02-18 15:22:14 +00003522 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3523 * @dapm: DAPM context
3524 * @pin: pin name
3525 *
3526 * Enables input/output pin and its parents or children widgets iff there is
3527 * a valid audio route and active audio stream.
3528 *
3529 * Requires external locking.
3530 *
3531 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3532 * do any widget power switching.
3533 */
3534int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3535 const char *pin)
3536{
3537 return snd_soc_dapm_set_pin(dapm, pin, 1);
3538}
3539EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3540
3541/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003542 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003543 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003544 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003545 *
Mark Brown74b8f952009-06-06 11:26:15 +01003546 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003547 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003548 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003549 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3550 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003551 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003552int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003553{
Charles Keepax11391102014-02-18 15:22:14 +00003554 int ret;
3555
3556 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3557
3558 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3559
3560 mutex_unlock(&dapm->card->dapm_mutex);
3561
3562 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003563}
Liam Girdwooda5302182008-07-07 13:35:17 +01003564EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003565
3566/**
Charles Keepax11391102014-02-18 15:22:14 +00003567 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3568 * @dapm: DAPM context
3569 * @pin: pin name
3570 *
3571 * Enables input/output pin regardless of any other state. This is
3572 * intended for use with microphone bias supplies used in microphone
3573 * jack detection.
3574 *
3575 * Requires external locking.
3576 *
3577 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3578 * do any widget power switching.
3579 */
3580int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3581 const char *pin)
3582{
3583 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3584
3585 if (!w) {
3586 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3587 return -EINVAL;
3588 }
3589
3590 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3591 w->connected = 1;
3592 w->force = 1;
3593 dapm_mark_dirty(w, "force enable");
3594
3595 return 0;
3596}
3597EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3598
3599/**
Mark Brownda341832010-03-15 19:23:37 +00003600 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003601 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003602 * @pin: pin name
3603 *
3604 * Enables input/output pin regardless of any other state. This is
3605 * intended for use with microphone bias supplies used in microphone
3606 * jack detection.
3607 *
3608 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3609 * do any widget power switching.
3610 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003611int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3612 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003613{
Charles Keepax11391102014-02-18 15:22:14 +00003614 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003615
Charles Keepax11391102014-02-18 15:22:14 +00003616 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003617
Charles Keepax11391102014-02-18 15:22:14 +00003618 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003619
Charles Keepax11391102014-02-18 15:22:14 +00003620 mutex_unlock(&dapm->card->dapm_mutex);
3621
3622 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003623}
3624EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3625
3626/**
Charles Keepax11391102014-02-18 15:22:14 +00003627 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3628 * @dapm: DAPM context
3629 * @pin: pin name
3630 *
3631 * Disables input/output pin and its parents or children widgets.
3632 *
3633 * Requires external locking.
3634 *
3635 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3636 * do any widget power switching.
3637 */
3638int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3639 const char *pin)
3640{
3641 return snd_soc_dapm_set_pin(dapm, pin, 0);
3642}
3643EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3644
3645/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003646 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003647 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003648 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003649 *
Mark Brown74b8f952009-06-06 11:26:15 +01003650 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003651 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003652 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3653 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003654 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003655int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3656 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003657{
Charles Keepax11391102014-02-18 15:22:14 +00003658 int ret;
3659
3660 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3661
3662 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3663
3664 mutex_unlock(&dapm->card->dapm_mutex);
3665
3666 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003667}
3668EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3669
3670/**
Charles Keepax11391102014-02-18 15:22:14 +00003671 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3672 * @dapm: DAPM context
3673 * @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 * Requires external locking.
3682 *
3683 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3684 * do any widget power switching.
3685 */
3686int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3687 const char *pin)
3688{
3689 return snd_soc_dapm_set_pin(dapm, pin, 0);
3690}
3691EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3692
3693/**
Mark Brown5817b522008-09-24 11:23:11 +01003694 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003695 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003696 * @pin: pin name
3697 *
3698 * Marks the specified pin as being not connected, disabling it along
3699 * any parent or child widgets. At present this is identical to
3700 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3701 * additional things such as disabling controls which only affect
3702 * paths through the pin.
3703 *
3704 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3705 * do any widget power switching.
3706 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003707int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003708{
Charles Keepax11391102014-02-18 15:22:14 +00003709 int ret;
3710
3711 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3712
3713 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3714
3715 mutex_unlock(&dapm->card->dapm_mutex);
3716
3717 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003718}
3719EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3720
3721/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003722 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003723 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003724 * @pin: audio signal pin endpoint (or start point)
3725 *
3726 * Get audio pin status - connected or disconnected.
3727 *
3728 * Returns 1 for connected otherwise 0.
3729 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003730int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3731 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003732{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003733 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003734
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003735 if (w)
3736 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003737
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003738 return 0;
3739}
Liam Girdwooda5302182008-07-07 13:35:17 +01003740EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003741
3742/**
Mark Brown1547aba2010-05-07 21:11:40 +01003743 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003744 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003745 * @pin: audio signal pin endpoint (or start point)
3746 *
3747 * Mark the given endpoint or pin as ignoring suspend. When the
3748 * system is disabled a path between two endpoints flagged as ignoring
3749 * suspend will not be disabled. The path must already be enabled via
3750 * normal means at suspend time, it will not be turned on if it was not
3751 * already enabled.
3752 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003753int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3754 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003755{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003756 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003757
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003758 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003759 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003760 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003761 }
3762
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003763 w->ignore_suspend = 1;
3764
3765 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003766}
3767EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3768
Stephen Warren16332812011-11-23 12:42:04 -07003769static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3770 struct snd_soc_dapm_widget *w)
3771{
3772 struct snd_soc_dapm_path *p;
3773
3774 list_for_each_entry(p, &card->paths, list) {
3775 if ((p->source == w) || (p->sink == w)) {
3776 dev_dbg(card->dev,
3777 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3778 p->source->name, p->source->id, p->source->dapm,
3779 p->sink->name, p->sink->id, p->sink->dapm);
3780
3781 /* Connected to something other than the codec */
3782 if (p->source->dapm != p->sink->dapm)
3783 return true;
3784 /*
3785 * Loopback connection from codec external pin to
3786 * codec external pin
3787 */
3788 if (p->sink->id == snd_soc_dapm_input) {
3789 switch (p->source->id) {
3790 case snd_soc_dapm_output:
3791 case snd_soc_dapm_micbias:
3792 return true;
3793 default:
3794 break;
3795 }
3796 }
3797 }
3798 }
3799
3800 return false;
3801}
3802
3803/**
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003804 * snd_soc_dapm_auto_nc_pins - call snd_soc_dapm_nc_pin for unused pins
3805 * @card: The card whose pins should be processed
Stephen Warren16332812011-11-23 12:42:04 -07003806 *
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003807 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the card
3808 * which are unused. Pins are used if they are connected externally to a
3809 * component, whether that be to some other device, or a loop-back connection to
3810 * the component itself.
Stephen Warren16332812011-11-23 12:42:04 -07003811 */
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003812void snd_soc_dapm_auto_nc_pins(struct snd_soc_card *card)
Stephen Warren16332812011-11-23 12:42:04 -07003813{
Stephen Warren16332812011-11-23 12:42:04 -07003814 struct snd_soc_dapm_widget *w;
3815
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003816 dev_dbg(card->dev, "ASoC: Auto NC: DAPMs: card:%p\n", &card->dapm);
Stephen Warren16332812011-11-23 12:42:04 -07003817
3818 list_for_each_entry(w, &card->widgets, list) {
Stephen Warren16332812011-11-23 12:42:04 -07003819 switch (w->id) {
3820 case snd_soc_dapm_input:
3821 case snd_soc_dapm_output:
3822 case snd_soc_dapm_micbias:
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003823 dev_dbg(card->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003824 w->name);
3825 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003826 dev_dbg(card->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003827 "... Not in map; disabling\n");
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02003828 snd_soc_dapm_nc_pin(w->dapm, w->name);
Stephen Warren16332812011-11-23 12:42:04 -07003829 }
3830 break;
3831 default:
3832 break;
3833 }
3834 }
3835}
3836
Mark Brown1547aba2010-05-07 21:11:40 +01003837/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003838 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003839 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003840 *
3841 * Free all dapm widgets and resources.
3842 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003843void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003844{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003845 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003846 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003847 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003848 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003849}
3850EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3851
Xiang Xiao57996352014-03-02 00:04:02 +08003852static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003853{
Liam Girdwood01005a72012-07-06 16:57:05 +01003854 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003855 struct snd_soc_dapm_widget *w;
3856 LIST_HEAD(down_list);
3857 int powerdown = 0;
3858
Liam Girdwood01005a72012-07-06 16:57:05 +01003859 mutex_lock(&card->dapm_mutex);
3860
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003861 list_for_each_entry(w, &dapm->card->widgets, list) {
3862 if (w->dapm != dapm)
3863 continue;
Mark Brown51737472009-06-22 13:16:51 +01003864 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003865 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003866 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003867 powerdown = 1;
3868 }
3869 }
3870
3871 /* If there were no widgets to power down we're already in
3872 * standby.
3873 */
3874 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003875 if (dapm->bias_level == SND_SOC_BIAS_ON)
3876 snd_soc_dapm_set_bias_level(dapm,
3877 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003878 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003879 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3880 snd_soc_dapm_set_bias_level(dapm,
3881 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003882 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003883
3884 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003885}
Mark Brown51737472009-06-22 13:16:51 +01003886
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003887/*
3888 * snd_soc_dapm_shutdown - callback for system shutdown
3889 */
3890void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3891{
Xiang Xiao57996352014-03-02 00:04:02 +08003892 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003893
Xiang Xiao57996352014-03-02 00:04:02 +08003894 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08003895 if (dapm != &card->dapm) {
3896 soc_dapm_shutdown_dapm(dapm);
3897 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3898 snd_soc_dapm_set_bias_level(dapm,
3899 SND_SOC_BIAS_OFF);
3900 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003901 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08003902
3903 soc_dapm_shutdown_dapm(&card->dapm);
3904 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3905 snd_soc_dapm_set_bias_level(&card->dapm,
3906 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01003907}
3908
Richard Purdie2b97eab2006-10-06 18:32:18 +02003909/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003910MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003911MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3912MODULE_LICENSE("GPL");