blob: 6d6ceee447d559e711ba8eccc2e734953585d9ad [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
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200329static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
330{
331 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
332
333 return data->value;
334}
335
336static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
337 unsigned int value)
338{
339 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
340
341 if (data->value == value)
342 return false;
343
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200344 if (data->widget)
345 data->widget->on_val = value;
346
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200347 data->value = value;
348
349 return true;
350}
351
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200352/**
353 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
354 * @kcontrol: The kcontrol
355 */
356struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
357{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200358 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200359}
360EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
361
Liam Girdwood6c120e12012-02-15 15:15:34 +0000362static void dapm_reset(struct snd_soc_card *card)
363{
364 struct snd_soc_dapm_widget *w;
365
Mark Brownf9fa2b12014-03-06 16:49:11 +0800366 lockdep_assert_held(&card->dapm_mutex);
367
Liam Girdwood6c120e12012-02-15 15:15:34 +0000368 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
369
370 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200371 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000372 w->power_checked = false;
373 w->inputs = -1;
374 w->outputs = -1;
375 }
376}
377
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800378static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg,
379 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100380{
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800381 if (w->codec) {
382 *value = snd_soc_read(w->codec, reg);
383 return 0;
384 } else if (w->platform) {
385 *value = snd_soc_platform_read(w->platform, reg);
386 return 0;
387 }
Liam Girdwoodb7950642011-07-04 22:10:52 +0100388
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000389 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100390 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100391}
392
Lars-Peter Clausen30ac6b62014-02-13 14:14:21 +0100393static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg,
394 unsigned int val)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100395{
396 if (w->codec)
397 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100398 else if (w->platform)
399 return snd_soc_platform_write(w->platform, reg, val);
400
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000401 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100402 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100403}
404
Liam Girdwood49575fb52012-03-06 18:16:19 +0000405static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
406{
Mark Browne06ab3b2012-03-06 23:58:22 +0000407 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000408 mutex_lock(&w->codec->mutex);
409 else if (w->platform)
410 mutex_lock(&w->platform->mutex);
411}
412
413static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
414{
Mark Browne06ab3b2012-03-06 23:58:22 +0000415 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000416 mutex_unlock(&w->codec->mutex);
417 else if (w->platform)
418 mutex_unlock(&w->platform->mutex);
419}
420
Mark Browneb270e92013-10-09 13:52:52 +0100421static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
422{
423 if (dapm->codec && dapm->codec->using_regmap)
424 regmap_async_complete(dapm->codec->control_data);
425}
426
Liam Girdwood49575fb52012-03-06 18:16:19 +0000427static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100428 unsigned short reg, unsigned int mask, unsigned int value)
429{
Mark Brown8a713da2011-12-03 12:33:55 +0000430 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100431 unsigned int old, new;
432 int ret;
433
Mark Brown8a713da2011-12-03 12:33:55 +0000434 if (w->codec && w->codec->using_regmap) {
Mark Browneb270e92013-10-09 13:52:52 +0100435 ret = regmap_update_bits_check_async(w->codec->control_data,
436 reg, mask, value,
437 &change);
Mark Brown8a713da2011-12-03 12:33:55 +0000438 if (ret != 0)
439 return ret;
440 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000441 soc_widget_lock(w);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800442 ret = soc_widget_read(w, reg, &old);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000443 if (ret < 0) {
444 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100445 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000446 }
Mark Brown8a713da2011-12-03 12:33:55 +0000447
Mark Brown8a713da2011-12-03 12:33:55 +0000448 new = (old & ~mask) | (value & mask);
449 change = old != new;
450 if (change) {
451 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000452 if (ret < 0) {
453 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000454 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000455 }
Mark Brown8a713da2011-12-03 12:33:55 +0000456 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000457 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100458 }
459
460 return change;
461}
462
Mark Brown452c5ea2009-05-17 21:41:23 +0100463/**
464 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800465 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100466 * @level: level to configure
467 *
468 * Configure the bias (power) levels for the SoC audio device.
469 *
470 * Returns 0 for success else error.
471 */
Mark Browned5a4c42011-02-18 11:12:42 -0800472static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200473 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100474{
Mark Browned5a4c42011-02-18 11:12:42 -0800475 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100476 int ret = 0;
477
Mark Brown84e90932010-11-04 00:07:02 -0400478 trace_snd_soc_bias_level_start(card, level);
479
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000480 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100481 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100482 if (ret != 0)
483 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100484
Mark Browncc4c6702011-06-06 19:03:34 +0100485 if (dapm->codec) {
486 if (dapm->codec->driver->set_bias_level)
487 ret = dapm->codec->driver->set_bias_level(dapm->codec,
488 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100489 else
490 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100491 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100492 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100493 }
Liam Girdwood41231282012-07-06 16:56:16 +0100494
Mark Brown171ec6b2011-06-06 18:15:19 +0100495 if (ret != 0)
496 goto out;
497
498 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100499 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100500out:
Mark Brown84e90932010-11-04 00:07:02 -0400501 trace_snd_soc_bias_level_done(card, level);
502
Mark Brown452c5ea2009-05-17 21:41:23 +0100503 return ret;
504}
505
Mark Brown74b8f952009-06-06 11:26:15 +0100506/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200507static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200508 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
509 struct snd_soc_dapm_path *path, const char *control_name,
510 const struct snd_kcontrol_new *kcontrol)
511{
512 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100513 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200514 int i;
515
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100516 if (e->reg != SND_SOC_NOPM) {
517 soc_widget_read(dest, e->reg, &val);
518 val = (val >> e->shift_l) & e->mask;
519 item = snd_soc_enum_val_to_item(e, val);
520 } else {
521 /* since a virtual mux has no backing registers to
522 * decide which path to connect, it will try to match
523 * with the first enumeration. This is to ensure
524 * that the default mux choice (the first) will be
525 * correctly powered up during initialization.
526 */
527 item = 0;
528 }
529
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100530 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200531 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200532 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200533 list_add(&path->list_sink, &dest->sources);
534 list_add(&path->list_source, &src->sinks);
535 path->name = (char*)e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100536 if (i == item)
537 path->connect = 1;
538 else
539 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200540 return 0;
541 }
542 }
543
544 return -ENODEV;
545}
546
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100547/* set up initial codec paths */
548static void dapm_set_mixer_path_status(struct snd_soc_dapm_widget *w,
549 struct snd_soc_dapm_path *p, int i)
550{
551 struct soc_mixer_control *mc = (struct soc_mixer_control *)
552 w->kcontrol_news[i].private_value;
553 unsigned int reg = mc->reg;
554 unsigned int shift = mc->shift;
555 unsigned int max = mc->max;
556 unsigned int mask = (1 << fls(max)) - 1;
557 unsigned int invert = mc->invert;
558 unsigned int val;
559
560 if (reg != SND_SOC_NOPM) {
561 soc_widget_read(w, reg, &val);
562 val = (val >> shift) & mask;
563 if (invert)
564 val = max - val;
565 p->connect = !!val;
566 } else {
567 p->connect = 0;
568 }
569}
570
Mark Brown74b8f952009-06-06 11:26:15 +0100571/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200572static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200573 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
574 struct snd_soc_dapm_path *path, const char *control_name)
575{
576 int i;
577
578 /* search for mixer kcontrol */
579 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600580 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200581 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200582 list_add(&path->list_sink, &dest->sources);
583 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600584 path->name = dest->kcontrol_news[i].name;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100585 dapm_set_mixer_path_status(dest, path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200586 return 0;
587 }
588 }
589 return -ENODEV;
590}
591
Stephen Warrenaf468002011-04-28 17:38:01 -0600592static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600593 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600594 const struct snd_kcontrol_new *kcontrol_new,
595 struct snd_kcontrol **kcontrol)
596{
597 struct snd_soc_dapm_widget *w;
598 int i;
599
600 *kcontrol = NULL;
601
602 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600603 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
604 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600605 for (i = 0; i < w->num_kcontrols; i++) {
606 if (&w->kcontrol_news[i] == kcontrol_new) {
607 if (w->kcontrols)
608 *kcontrol = w->kcontrols[i];
609 return 1;
610 }
611 }
612 }
613
614 return 0;
615}
616
Stephen Warren85762e72013-03-29 15:40:10 -0600617/*
618 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
619 * create it. Either way, add the widget into the control's widget list
620 */
621static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100622 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200623{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200624 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000625 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000626 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600627 size_t prefix_len;
628 int shared;
629 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600630 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600631 char *long_name;
632 const char *name;
633 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000634
635 if (dapm->codec)
636 prefix = dapm->codec->name_prefix;
637 else
638 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200639
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000640 if (prefix)
641 prefix_len = strlen(prefix) + 1;
642 else
643 prefix_len = 0;
644
Stephen Warren85762e72013-03-29 15:40:10 -0600645 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
646 &kcontrol);
647
Stephen Warren85762e72013-03-29 15:40:10 -0600648 if (!kcontrol) {
649 if (shared) {
650 wname_in_long_name = false;
651 kcname_in_long_name = true;
652 } else {
653 switch (w->id) {
654 case snd_soc_dapm_switch:
655 case snd_soc_dapm_mixer:
656 wname_in_long_name = true;
657 kcname_in_long_name = true;
658 break;
659 case snd_soc_dapm_mixer_named_ctl:
660 wname_in_long_name = false;
661 kcname_in_long_name = true;
662 break;
663 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600664 wname_in_long_name = true;
665 kcname_in_long_name = false;
666 break;
667 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600668 return -EINVAL;
669 }
670 }
671
672 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600673 /*
674 * The control will get a prefix from the control
675 * creation process but we're also using the same
676 * prefix for widgets so cut the prefix off the
677 * front of the widget name.
678 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200679 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600680 w->name + prefix_len,
681 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200682 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200683 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600684
685 name = long_name;
686 } else if (wname_in_long_name) {
687 long_name = NULL;
688 name = w->name + prefix_len;
689 } else {
690 long_name = NULL;
691 name = w->kcontrol_news[kci].name;
692 }
693
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200694 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600695 prefix);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200696 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200697 if (!kcontrol)
698 return -ENOMEM;
699 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200700
701 ret = dapm_kcontrol_data_alloc(w, kcontrol);
702 if (ret) {
703 snd_ctl_free_one(kcontrol);
704 return ret;
705 }
706
Stephen Warren85762e72013-03-29 15:40:10 -0600707 ret = snd_ctl_add(card, kcontrol);
708 if (ret < 0) {
709 dev_err(dapm->dev,
710 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
711 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600712 return ret;
713 }
Stephen Warren85762e72013-03-29 15:40:10 -0600714 }
715
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200716 ret = dapm_kcontrol_add_widget(kcontrol, w);
717 if (ret)
718 return ret;
719
Stephen Warren85762e72013-03-29 15:40:10 -0600720 w->kcontrols[kci] = kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600721
722 return 0;
723}
724
725/* create new dapm mixer control */
726static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
727{
728 int i, ret;
729 struct snd_soc_dapm_path *path;
730
Richard Purdie2b97eab2006-10-06 18:32:18 +0200731 /* add kcontrol */
732 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200733 /* match name */
734 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200735 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600736 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200737 continue;
738
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200739 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200740 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200741 continue;
742 }
743
Mark Brown946d92a2013-08-12 23:28:42 +0100744 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600745 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200746 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100747
748 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200749 }
750 }
Stephen Warren85762e72013-03-29 15:40:10 -0600751
752 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200753}
754
755/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200756static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200757{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200758 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600759 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600760 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200761
Stephen Warrenaf468002011-04-28 17:38:01 -0600762 if (w->num_kcontrols != 1) {
763 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000764 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600765 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200766 return -EINVAL;
767 }
768
Lars-Peter Clausenfe581392013-08-01 18:30:38 +0200769 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600770 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
771 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600772 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200773
Mark Brown946d92a2013-08-12 23:28:42 +0100774 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600775 if (ret < 0)
776 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600777
Richard Purdie2b97eab2006-10-06 18:32:18 +0200778 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200779 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200780
Stephen Warrenaf468002011-04-28 17:38:01 -0600781 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200782}
783
784/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200785static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200786{
Mark Browna6c65732010-03-03 17:45:21 +0000787 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200788 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000789 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200790
Mark Browna6c65732010-03-03 17:45:21 +0000791 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200792}
793
794/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100795static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
796 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200797{
798 struct snd_soc_dapm_path *p;
799
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100800 list_for_each_entry(p, sink, list_source) {
801 if (p->walked) {
802 p->walked = 0;
803 dapm_clear_walk_output(dapm, &p->sink->sinks);
804 }
805 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200806}
807
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100808static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
809 struct list_head *source)
810{
811 struct snd_soc_dapm_path *p;
812
813 list_for_each_entry(p, source, list_sink) {
814 if (p->walked) {
815 p->walked = 0;
816 dapm_clear_walk_input(dapm, &p->source->sources);
817 }
818 }
819}
820
821
Mark Brown99497882010-05-07 20:24:05 +0100822/* We implement power down on suspend by checking the power state of
823 * the ALSA card - when we are suspending the ALSA state for the card
824 * is set to D3.
825 */
826static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
827{
Mark Brown12ea2c72011-03-02 18:17:32 +0000828 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100829
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000830 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100831 case SNDRV_CTL_POWER_D3hot:
832 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100833 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000834 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200835 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100836 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100837 default:
838 return 1;
839 }
840}
841
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100842/* add widget to list if it's not already in the list */
843static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
844 struct snd_soc_dapm_widget *w)
845{
846 struct snd_soc_dapm_widget_list *wlist;
847 int wlistsize, wlistentries, i;
848
849 if (*list == NULL)
850 return -EINVAL;
851
852 wlist = *list;
853
854 /* is this widget already in the list */
855 for (i = 0; i < wlist->num_widgets; i++) {
856 if (wlist->widgets[i] == w)
857 return 0;
858 }
859
860 /* allocate some new space */
861 wlistentries = wlist->num_widgets + 1;
862 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
863 wlistentries * sizeof(struct snd_soc_dapm_widget *);
864 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
865 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000866 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100867 w->name);
868 return -ENOMEM;
869 }
870 wlist = *list;
871
872 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000873 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100874 w->name, wlist->num_widgets);
875
876 wlist->widgets[wlist->num_widgets] = w;
877 wlist->num_widgets++;
878 return 1;
879}
880
Richard Purdie2b97eab2006-10-06 18:32:18 +0200881/*
882 * Recursively check for a completed path to an active or physically connected
883 * output widget. Returns number of complete paths.
884 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100885static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
886 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200887{
888 struct snd_soc_dapm_path *path;
889 int con = 0;
890
Mark Brown024dc072011-10-09 11:52:05 +0100891 if (widget->outputs >= 0)
892 return widget->outputs;
893
Mark Brownde02d072011-09-20 21:43:24 +0100894 DAPM_UPDATE_STAT(widget, path_checks);
895
Mark Brown62ea8742012-01-21 21:14:48 +0000896 switch (widget->id) {
897 case snd_soc_dapm_supply:
898 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200899 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200900 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100901 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000902 default:
903 break;
904 }
Mark Brown246d0a12009-04-22 18:24:55 +0100905
Mark Brown010ff262009-08-17 17:39:22 +0100906 switch (widget->id) {
907 case snd_soc_dapm_adc:
908 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100909 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100910 if (widget->active) {
911 widget->outputs = snd_soc_dapm_suspend_check(widget);
912 return widget->outputs;
913 }
Mark Brown010ff262009-08-17 17:39:22 +0100914 default:
915 break;
916 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200917
918 if (widget->connected) {
919 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100920 if (widget->id == snd_soc_dapm_output && !widget->ext) {
921 widget->outputs = snd_soc_dapm_suspend_check(widget);
922 return widget->outputs;
923 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200924
925 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100926 if (widget->id == snd_soc_dapm_hp ||
927 widget->id == snd_soc_dapm_spk ||
928 (widget->id == snd_soc_dapm_line &&
929 !list_empty(&widget->sources))) {
930 widget->outputs = snd_soc_dapm_suspend_check(widget);
931 return widget->outputs;
932 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200933 }
934
935 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100936 DAPM_UPDATE_STAT(widget, neighbour_checks);
937
Mark Brownbf3a9e12011-06-13 16:42:29 +0100938 if (path->weak)
939 continue;
940
Mark Brown8af294b2013-02-22 17:48:15 +0000941 if (path->walking)
942 return 1;
943
Richard Purdie2b97eab2006-10-06 18:32:18 +0200944 if (path->walked)
945 continue;
946
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100947 trace_snd_soc_dapm_output_path(widget, path);
948
Richard Purdie2b97eab2006-10-06 18:32:18 +0200949 if (path->sink && path->connect) {
950 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000951 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100952
953 /* do we need to add this widget to the list ? */
954 if (list) {
955 int err;
956 err = dapm_list_add_widget(list, path->sink);
957 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000958 dev_err(widget->dapm->dev,
959 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100960 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000961 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100962 return con;
963 }
964 }
965
966 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000967
968 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200969 }
970 }
971
Mark Brown024dc072011-10-09 11:52:05 +0100972 widget->outputs = con;
973
Richard Purdie2b97eab2006-10-06 18:32:18 +0200974 return con;
975}
976
977/*
978 * Recursively check for a completed path to an active or physically connected
979 * input widget. Returns number of complete paths.
980 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100981static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
982 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200983{
984 struct snd_soc_dapm_path *path;
985 int con = 0;
986
Mark Brown024dc072011-10-09 11:52:05 +0100987 if (widget->inputs >= 0)
988 return widget->inputs;
989
Mark Brownde02d072011-09-20 21:43:24 +0100990 DAPM_UPDATE_STAT(widget, path_checks);
991
Mark Brown62ea8742012-01-21 21:14:48 +0000992 switch (widget->id) {
993 case snd_soc_dapm_supply:
994 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200995 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200996 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100997 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000998 default:
999 break;
1000 }
Mark Brown246d0a12009-04-22 18:24:55 +01001001
Richard Purdie2b97eab2006-10-06 18:32:18 +02001002 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +01001003 switch (widget->id) {
1004 case snd_soc_dapm_dac:
1005 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +01001006 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +01001007 if (widget->active) {
1008 widget->inputs = snd_soc_dapm_suspend_check(widget);
1009 return widget->inputs;
1010 }
Mark Brown010ff262009-08-17 17:39:22 +01001011 default:
1012 break;
1013 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001014
1015 if (widget->connected) {
1016 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +01001017 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1018 widget->inputs = snd_soc_dapm_suspend_check(widget);
1019 return widget->inputs;
1020 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001021
1022 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +01001023 if (widget->id == snd_soc_dapm_vmid) {
1024 widget->inputs = snd_soc_dapm_suspend_check(widget);
1025 return widget->inputs;
1026 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001027
1028 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +03001029 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +01001030 (widget->id == snd_soc_dapm_line &&
1031 !list_empty(&widget->sinks))) {
1032 widget->inputs = snd_soc_dapm_suspend_check(widget);
1033 return widget->inputs;
1034 }
1035
Mark Brown1ab97c82011-11-27 16:21:51 +00001036 /* signal generator */
1037 if (widget->id == snd_soc_dapm_siggen) {
1038 widget->inputs = snd_soc_dapm_suspend_check(widget);
1039 return widget->inputs;
1040 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001041 }
1042
1043 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +01001044 DAPM_UPDATE_STAT(widget, neighbour_checks);
1045
Mark Brownbf3a9e12011-06-13 16:42:29 +01001046 if (path->weak)
1047 continue;
1048
Mark Brown8af294b2013-02-22 17:48:15 +00001049 if (path->walking)
1050 return 1;
1051
Richard Purdie2b97eab2006-10-06 18:32:18 +02001052 if (path->walked)
1053 continue;
1054
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001055 trace_snd_soc_dapm_input_path(widget, path);
1056
Richard Purdie2b97eab2006-10-06 18:32:18 +02001057 if (path->source && path->connect) {
1058 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001059 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001060
1061 /* do we need to add this widget to the list ? */
1062 if (list) {
1063 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001064 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001065 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001066 dev_err(widget->dapm->dev,
1067 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001068 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001069 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001070 return con;
1071 }
1072 }
1073
1074 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001075
1076 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001077 }
1078 }
1079
Mark Brown024dc072011-10-09 11:52:05 +01001080 widget->inputs = con;
1081
Richard Purdie2b97eab2006-10-06 18:32:18 +02001082 return con;
1083}
1084
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001085/**
1086 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1087 * @dai: the soc DAI.
1088 * @stream: stream direction.
1089 * @list: list of active widgets for this stream.
1090 *
1091 * Queries DAPM graph as to whether an valid audio stream path exists for
1092 * the initial stream specified by name. This takes into account
1093 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1094 *
1095 * Returns the number of valid paths or negative error.
1096 */
1097int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1098 struct snd_soc_dapm_widget_list **list)
1099{
1100 struct snd_soc_card *card = dai->card;
1101 int paths;
1102
1103 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1104 dapm_reset(card);
1105
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001106 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001107 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001108 dapm_clear_walk_output(&card->dapm,
1109 &dai->playback_widget->sinks);
1110 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001111 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001112 dapm_clear_walk_input(&card->dapm,
1113 &dai->capture_widget->sources);
1114 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001115
1116 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001117 mutex_unlock(&card->dapm_mutex);
1118
1119 return paths;
1120}
1121
Richard Purdie2b97eab2006-10-06 18:32:18 +02001122/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001123 * Handler for generic register modifier widget.
1124 */
1125int dapm_reg_event(struct snd_soc_dapm_widget *w,
1126 struct snd_kcontrol *kcontrol, int event)
1127{
1128 unsigned int val;
1129
1130 if (SND_SOC_DAPM_EVENT_ON(event))
1131 val = w->on_val;
1132 else
1133 val = w->off_val;
1134
Liam Girdwood49575fb52012-03-06 18:16:19 +00001135 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001136 w->mask << w->shift, val << w->shift);
1137
1138 return 0;
1139}
Mark Brown11589412008-07-29 11:42:23 +01001140EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001141
Mark Brown62ea8742012-01-21 21:14:48 +00001142/*
1143 * Handler for regulator supply widget.
1144 */
1145int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1146 struct snd_kcontrol *kcontrol, int event)
1147{
Mark Brownc05b84d2012-09-07 12:57:11 +08001148 int ret;
1149
Mark Browneb270e92013-10-09 13:52:52 +01001150 soc_dapm_async_complete(w->dapm);
1151
Mark Brownc05b84d2012-09-07 12:57:11 +08001152 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001153 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001154 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001155 if (ret != 0)
1156 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001157 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001158 w->name, ret);
1159 }
1160
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001161 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001162 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001163 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001164 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001165 if (ret != 0)
1166 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001167 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001168 w->name, ret);
1169 }
1170
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001171 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001172 }
Mark Brown62ea8742012-01-21 21:14:48 +00001173}
1174EXPORT_SYMBOL_GPL(dapm_regulator_event);
1175
Ola Liljad7e7eb92012-05-24 15:26:25 +02001176/*
1177 * Handler for clock supply widget.
1178 */
1179int dapm_clock_event(struct snd_soc_dapm_widget *w,
1180 struct snd_kcontrol *kcontrol, int event)
1181{
1182 if (!w->clk)
1183 return -EIO;
1184
Mark Browneb270e92013-10-09 13:52:52 +01001185 soc_dapm_async_complete(w->dapm);
1186
Mark Brownec029952012-06-04 08:16:20 +01001187#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001188 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001189 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001190 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001191 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001192 return 0;
1193 }
Mark Brownec029952012-06-04 08:16:20 +01001194#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001195 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001196}
1197EXPORT_SYMBOL_GPL(dapm_clock_event);
1198
Mark Brownd8050022011-09-28 18:28:23 +01001199static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1200{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001201 if (w->power_checked)
1202 return w->new_power;
1203
Mark Brownd8050022011-09-28 18:28:23 +01001204 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001205 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001206 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001207 w->new_power = w->power_check(w);
1208
1209 w->power_checked = true;
1210
1211 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001212}
1213
Mark Browncd0f2d42009-04-20 16:56:59 +01001214/* Generic check to see if a widget should be powered.
1215 */
1216static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1217{
1218 int in, out;
1219
Mark Brownde02d072011-09-20 21:43:24 +01001220 DAPM_UPDATE_STAT(w, power_checks);
1221
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001222 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001223 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001224 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001225 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001226 return out != 0 && in != 0;
1227}
1228
Mark Brown6ea31b92009-04-20 17:15:41 +01001229/* Check to see if an ADC has power */
1230static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1231{
1232 int in;
1233
Mark Brownde02d072011-09-20 21:43:24 +01001234 DAPM_UPDATE_STAT(w, power_checks);
1235
Mark Brown6ea31b92009-04-20 17:15:41 +01001236 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001237 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001238 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001239 return in != 0;
1240 } else {
1241 return dapm_generic_check_power(w);
1242 }
1243}
1244
1245/* Check to see if a DAC has power */
1246static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1247{
1248 int out;
1249
Mark Brownde02d072011-09-20 21:43:24 +01001250 DAPM_UPDATE_STAT(w, power_checks);
1251
Mark Brown6ea31b92009-04-20 17:15:41 +01001252 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001253 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001254 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001255 return out != 0;
1256 } else {
1257 return dapm_generic_check_power(w);
1258 }
1259}
1260
Mark Brown246d0a12009-04-22 18:24:55 +01001261/* Check to see if a power supply is needed */
1262static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1263{
1264 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001265
Mark Brownde02d072011-09-20 21:43:24 +01001266 DAPM_UPDATE_STAT(w, power_checks);
1267
Mark Brown246d0a12009-04-22 18:24:55 +01001268 /* Check if one of our outputs is connected */
1269 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001270 DAPM_UPDATE_STAT(w, neighbour_checks);
1271
Mark Brownbf3a9e12011-06-13 16:42:29 +01001272 if (path->weak)
1273 continue;
1274
Mark Brown215edda2009-09-08 18:59:05 +01001275 if (path->connected &&
1276 !path->connected(path->source, path->sink))
1277 continue;
1278
Mark Brown30173582011-02-11 11:42:19 +00001279 if (!path->sink)
1280 continue;
1281
Mark Brownf68d7e12011-10-04 22:57:50 +01001282 if (dapm_widget_power_check(path->sink))
1283 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001284 }
1285
Mark Brownf68d7e12011-10-04 22:57:50 +01001286 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001287}
1288
Mark Brown35c64bc2011-09-28 18:23:53 +01001289static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1290{
1291 return 1;
1292}
1293
Mark Brown38357ab2009-06-06 19:03:23 +01001294static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1295 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001296 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001297{
Mark Brown828a8422011-01-15 13:14:30 +00001298 int *sort;
1299
1300 if (power_up)
1301 sort = dapm_up_seq;
1302 else
1303 sort = dapm_down_seq;
1304
Mark Brown38357ab2009-06-06 19:03:23 +01001305 if (sort[a->id] != sort[b->id])
1306 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001307 if (a->subseq != b->subseq) {
1308 if (power_up)
1309 return a->subseq - b->subseq;
1310 else
1311 return b->subseq - a->subseq;
1312 }
Mark Brownb22ead22009-06-07 12:51:26 +01001313 if (a->reg != b->reg)
1314 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001315 if (a->dapm != b->dapm)
1316 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001317
Mark Brown38357ab2009-06-06 19:03:23 +01001318 return 0;
1319}
Mark Brown42aa3412009-03-01 19:21:10 +00001320
Mark Brown38357ab2009-06-06 19:03:23 +01001321/* Insert a widget in order into a DAPM power sequence. */
1322static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1323 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001324 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001325{
1326 struct snd_soc_dapm_widget *w;
1327
1328 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001329 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001330 list_add_tail(&new_widget->power_list, &w->power_list);
1331 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001332 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001333
Mark Brown38357ab2009-06-06 19:03:23 +01001334 list_add_tail(&new_widget->power_list, list);
1335}
Mark Brown42aa3412009-03-01 19:21:10 +00001336
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001337static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001338 struct snd_soc_dapm_widget *w, int event)
1339{
Mark Brown68f89ad2010-11-03 23:51:49 -04001340 const char *ev_name;
1341 int power, ret;
1342
1343 switch (event) {
1344 case SND_SOC_DAPM_PRE_PMU:
1345 ev_name = "PRE_PMU";
1346 power = 1;
1347 break;
1348 case SND_SOC_DAPM_POST_PMU:
1349 ev_name = "POST_PMU";
1350 power = 1;
1351 break;
1352 case SND_SOC_DAPM_PRE_PMD:
1353 ev_name = "PRE_PMD";
1354 power = 0;
1355 break;
1356 case SND_SOC_DAPM_POST_PMD:
1357 ev_name = "POST_PMD";
1358 power = 0;
1359 break;
Mark Brown80114122013-02-25 15:14:19 +00001360 case SND_SOC_DAPM_WILL_PMU:
1361 ev_name = "WILL_PMU";
1362 power = 1;
1363 break;
1364 case SND_SOC_DAPM_WILL_PMD:
1365 ev_name = "WILL_PMD";
1366 power = 0;
1367 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001368 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001369 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001370 return;
1371 }
1372
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001373 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001374 return;
1375
1376 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001377 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001378 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001379 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001380 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001381 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001382 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001383 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001384 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001385 ev_name, w->name, ret);
1386 }
1387}
1388
Mark Brownb22ead22009-06-07 12:51:26 +01001389/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001390static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001391 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001392{
Mark Brown68f89ad2010-11-03 23:51:49 -04001393 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001394 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001395 unsigned int value = 0;
1396 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001397
1398 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1399 power_list)->reg;
1400
1401 list_for_each_entry(w, pending, power_list) {
Takashi Iwaibf4edea2013-11-07 18:38:47 +01001402 WARN_ON(reg != w->reg);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001403 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001404
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001405 mask |= w->mask << w->shift;
1406 if (w->power)
1407 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001408 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001409 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001410
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001411 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001412 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1413 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001414
Mark Brown68f89ad2010-11-03 23:51:49 -04001415 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001416 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1417 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001418 }
1419
Mark Brown81628102009-06-07 13:21:24 +01001420 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001421 /* Any widget will do, they should all be updating the
1422 * same register.
1423 */
1424 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1425 power_list);
1426
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001427 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001428 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001429 value, mask, reg, card->pop_time);
1430 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001431 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001432 }
1433
1434 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001435 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1436 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001437 }
Mark Brown42aa3412009-03-01 19:21:10 +00001438}
1439
Mark Brownb22ead22009-06-07 12:51:26 +01001440/* Apply a DAPM power sequence.
1441 *
1442 * We walk over a pre-sorted list of widgets to apply power to. In
1443 * order to minimise the number of writes to the device required
1444 * multiple widgets will be updated in a single write where possible.
1445 * Currently anything that requires more than a single write is not
1446 * handled.
1447 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001448static void dapm_seq_run(struct snd_soc_card *card,
1449 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001450{
1451 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001452 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001453 LIST_HEAD(pending);
1454 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001455 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001456 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001457 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001458 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001459 int *sort;
1460
1461 if (power_up)
1462 sort = dapm_up_seq;
1463 else
1464 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001465
Mark Brownb22ead22009-06-07 12:51:26 +01001466 list_for_each_entry_safe(w, n, list, power_list) {
1467 ret = 0;
1468
1469 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001470 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001471 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001472 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001473 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001474
Mark Brown474b62d2011-01-18 16:14:44 +00001475 if (cur_dapm && cur_dapm->seq_notifier) {
1476 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1477 if (sort[i] == cur_sort)
1478 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001479 i,
1480 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001481 }
1482
Mark Browneb270e92013-10-09 13:52:52 +01001483 if (cur_dapm && w->dapm != cur_dapm)
1484 soc_dapm_async_complete(cur_dapm);
1485
Mark Brownb22ead22009-06-07 12:51:26 +01001486 INIT_LIST_HEAD(&pending);
1487 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001488 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001489 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001490 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001491 }
1492
Mark Brown163cac02009-06-07 10:12:52 +01001493 switch (w->id) {
1494 case snd_soc_dapm_pre:
1495 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001496 list_for_each_entry_safe_continue(w, n, list,
1497 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001498
Mark Brownb22ead22009-06-07 12:51:26 +01001499 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001500 ret = w->event(w,
1501 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001502 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001503 ret = w->event(w,
1504 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001505 break;
1506
1507 case snd_soc_dapm_post:
1508 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001509 list_for_each_entry_safe_continue(w, n, list,
1510 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001511
Mark Brownb22ead22009-06-07 12:51:26 +01001512 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001513 ret = w->event(w,
1514 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001515 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001516 ret = w->event(w,
1517 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001518 break;
1519
Mark Brown163cac02009-06-07 10:12:52 +01001520 default:
Mark Brown81628102009-06-07 13:21:24 +01001521 /* Queue it up for application */
1522 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001523 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001524 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001525 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001526 list_move(&w->power_list, &pending);
1527 break;
Mark Brown163cac02009-06-07 10:12:52 +01001528 }
Mark Brownb22ead22009-06-07 12:51:26 +01001529
1530 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001531 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001532 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001533 }
Mark Brownb22ead22009-06-07 12:51:26 +01001534
1535 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001536 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001537
1538 if (cur_dapm && cur_dapm->seq_notifier) {
1539 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1540 if (sort[i] == cur_sort)
1541 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001542 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001543 }
Mark Browneb270e92013-10-09 13:52:52 +01001544
1545 list_for_each_entry(d, &card->dapm_list, list) {
1546 soc_dapm_async_complete(d);
1547 }
Mark Brown163cac02009-06-07 10:12:52 +01001548}
1549
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001550static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001551{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001552 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001553 struct snd_soc_dapm_widget_list *wlist;
1554 struct snd_soc_dapm_widget *w = NULL;
1555 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001556 int ret;
1557
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001558 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001559 return;
1560
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001561 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001562
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001563 for (wi = 0; wi < wlist->num_widgets; wi++) {
1564 w = wlist->widgets[wi];
1565
1566 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1567 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1568 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001569 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001570 w->name, ret);
1571 }
Mark Brown97404f22010-12-14 16:13:57 +00001572 }
1573
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001574 if (!w)
1575 return;
1576
Liam Girdwood49575fb52012-03-06 18:16:19 +00001577 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001578 update->val);
1579 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001580 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001581 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001582
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001583 for (wi = 0; wi < wlist->num_widgets; wi++) {
1584 w = wlist->widgets[wi];
1585
1586 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1587 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1588 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001589 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001590 w->name, ret);
1591 }
Mark Brown97404f22010-12-14 16:13:57 +00001592 }
1593}
1594
Mark Brown9d0624a2011-02-18 11:49:43 -08001595/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1596 * they're changing state.
1597 */
1598static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1599{
1600 struct snd_soc_dapm_context *d = data;
1601 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001602
Mark Brown56fba412011-06-04 11:25:10 +01001603 /* If we're off and we're not supposed to be go into STANDBY */
1604 if (d->bias_level == SND_SOC_BIAS_OFF &&
1605 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001606 if (d->dev)
1607 pm_runtime_get_sync(d->dev);
1608
Mark Brown9d0624a2011-02-18 11:49:43 -08001609 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1610 if (ret != 0)
1611 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001612 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001613 }
1614
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001615 /* Prepare for a transition to ON or away from ON */
1616 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1617 d->bias_level != SND_SOC_BIAS_ON) ||
1618 (d->target_bias_level != SND_SOC_BIAS_ON &&
1619 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001620 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1621 if (ret != 0)
1622 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001623 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001624 }
1625}
1626
1627/* Async callback run prior to DAPM sequences - brings to their final
1628 * state.
1629 */
1630static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1631{
1632 struct snd_soc_dapm_context *d = data;
1633 int ret;
1634
1635 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001636 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1637 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1638 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001639 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1640 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001641 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001642 ret);
1643 }
1644
1645 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001646 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1647 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001648 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1649 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001650 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1651 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001652
1653 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001654 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001655 }
1656
1657 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001658 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1659 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001660 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1661 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001662 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001663 ret);
1664 }
1665}
Mark Brown97404f22010-12-14 16:13:57 +00001666
Mark Brownfe4fda52011-10-03 22:36:57 +01001667static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1668 bool power, bool connect)
1669{
1670 /* If a connection is being made or broken then that update
1671 * will have marked the peer dirty, otherwise the widgets are
1672 * not connected and this update has no impact. */
1673 if (!connect)
1674 return;
1675
1676 /* If the peer is already in the state we're moving to then we
1677 * won't have an impact on it. */
1678 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001679 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001680}
1681
Mark Brown05623c42011-09-28 17:02:31 +01001682static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1683 struct list_head *up_list,
1684 struct list_head *down_list)
1685{
Mark Browndb432b42011-10-03 21:06:40 +01001686 struct snd_soc_dapm_path *path;
1687
Mark Brown05623c42011-09-28 17:02:31 +01001688 if (w->power == power)
1689 return;
1690
1691 trace_snd_soc_dapm_widget_power(w, power);
1692
Mark Browndb432b42011-10-03 21:06:40 +01001693 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001694 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001695 */
1696 list_for_each_entry(path, &w->sources, list_sink) {
1697 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001698 dapm_widget_set_peer_power(path->source, power,
1699 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001700 }
1701 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001702 switch (w->id) {
1703 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001704 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001705 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001706 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001707 /* Supplies can't affect their outputs, only their inputs */
1708 break;
1709 default:
1710 list_for_each_entry(path, &w->sinks, list_source) {
1711 if (path->sink) {
1712 dapm_widget_set_peer_power(path->sink, power,
1713 path->connect);
1714 }
Mark Browndb432b42011-10-03 21:06:40 +01001715 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001716 break;
Mark Browndb432b42011-10-03 21:06:40 +01001717 }
1718
Mark Brown05623c42011-09-28 17:02:31 +01001719 if (power)
1720 dapm_seq_insert(w, up_list, true);
1721 else
1722 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001723}
1724
Mark Brown7c81beb2011-09-20 22:22:32 +01001725static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1726 struct list_head *up_list,
1727 struct list_head *down_list)
1728{
Mark Brown7c81beb2011-09-20 22:22:32 +01001729 int power;
1730
1731 switch (w->id) {
1732 case snd_soc_dapm_pre:
1733 dapm_seq_insert(w, down_list, false);
1734 break;
1735 case snd_soc_dapm_post:
1736 dapm_seq_insert(w, up_list, true);
1737 break;
1738
1739 default:
Mark Brownd8050022011-09-28 18:28:23 +01001740 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001741
Mark Brown05623c42011-09-28 17:02:31 +01001742 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001743 break;
1744 }
1745}
1746
Mark Brown42aa3412009-03-01 19:21:10 +00001747/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001748 * Scan each dapm widget for complete audio path.
1749 * A complete path is a route that has valid endpoints i.e.:-
1750 *
1751 * o DAC to output pin.
1752 * o Input Pin to ADC.
1753 * o Input pin to Output pin (bypass, sidetone)
1754 * o DAC to ADC (loopback).
1755 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001756static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001757{
1758 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001759 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001760 LIST_HEAD(up_list);
1761 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001762 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001763 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001764
Mark Brownf9fa2b12014-03-06 16:49:11 +08001765 lockdep_assert_held(&card->dapm_mutex);
1766
Mark Brown84e90932010-11-04 00:07:02 -04001767 trace_snd_soc_dapm_start(card);
1768
Mark Brown56fba412011-06-04 11:25:10 +01001769 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001770 if (d->idle_bias_off)
1771 d->target_bias_level = SND_SOC_BIAS_OFF;
1772 else
1773 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001774 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001775
Liam Girdwood6c120e12012-02-15 15:15:34 +00001776 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001777
Mark Brown6d3ddc82009-05-16 17:47:29 +01001778 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001779 * lists indicating if they should be powered up or down. We
1780 * only check widgets that have been flagged as dirty but note
1781 * that new widgets may be added to the dirty list while we
1782 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001783 */
Mark Browndb432b42011-10-03 21:06:40 +01001784 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001785 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001786 }
1787
Mark Brownf9de6d72011-09-28 17:19:47 +01001788 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001789 switch (w->id) {
1790 case snd_soc_dapm_pre:
1791 case snd_soc_dapm_post:
1792 /* These widgets always need to be powered */
1793 break;
1794 default:
1795 list_del_init(&w->dirty);
1796 break;
1797 }
Mark Browndb432b42011-10-03 21:06:40 +01001798
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001799 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001800 d = w->dapm;
1801
1802 /* Supplies and micbiases only bring the
1803 * context up to STANDBY as unless something
1804 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001805 * generally don't require full power. Signal
1806 * generators are virtual pins and have no
1807 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001808 */
1809 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001810 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001811 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001812 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001813 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001814 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001815 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001816 case snd_soc_dapm_micbias:
1817 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1818 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1819 break;
1820 default:
1821 d->target_bias_level = SND_SOC_BIAS_ON;
1822 break;
1823 }
1824 }
1825
1826 }
1827
Mark Brown85a843c2011-09-21 21:29:47 +01001828 /* Force all contexts in the card to the same bias state if
1829 * they're not ground referenced.
1830 */
Mark Brown56fba412011-06-04 11:25:10 +01001831 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001832 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001833 if (d->target_bias_level > bias)
1834 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001835 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001836 if (!d->idle_bias_off)
1837 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001838
Mark Brownde02d072011-09-20 21:43:24 +01001839 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001840
Xiang Xiao17282ba2014-03-02 00:04:03 +08001841 /* Run card bias changes at first */
1842 dapm_pre_sequence_async(&card->dapm, 0);
1843 /* Run other bias changes in parallel */
1844 list_for_each_entry(d, &card->dapm_list, list) {
1845 if (d != &card->dapm)
1846 async_schedule_domain(dapm_pre_sequence_async, d,
1847 &async_domain);
1848 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001849 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001850
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001851 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001852 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001853 }
1854
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001855 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001856 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001857 }
1858
Mark Brown6d3ddc82009-05-16 17:47:29 +01001859 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001860 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001861
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001862 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001863
Mark Brown6d3ddc82009-05-16 17:47:29 +01001864 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001865 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001866
Mark Brown9d0624a2011-02-18 11:49:43 -08001867 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001868 list_for_each_entry(d, &card->dapm_list, list) {
1869 if (d != &card->dapm)
1870 async_schedule_domain(dapm_post_sequence_async, d,
1871 &async_domain);
1872 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001873 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001874 /* Run card bias changes at last */
1875 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001876
Liam Girdwood8078d872012-02-15 15:15:35 +00001877 /* do we need to notify any clients that DAPM event is complete */
1878 list_for_each_entry(d, &card->dapm_list, list) {
1879 if (d->stream_event)
1880 d->stream_event(d, event);
1881 }
1882
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001883 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001884 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001885 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001886
Mark Brown84e90932010-11-04 00:07:02 -04001887 trace_snd_soc_dapm_done(card);
1888
Mark Brown42aa3412009-03-01 19:21:10 +00001889 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001890}
1891
Mark Brown79fb9382009-08-21 16:38:13 +01001892#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001893static ssize_t dapm_widget_power_read_file(struct file *file,
1894 char __user *user_buf,
1895 size_t count, loff_t *ppos)
1896{
1897 struct snd_soc_dapm_widget *w = file->private_data;
1898 char *buf;
1899 int in, out;
1900 ssize_t ret;
1901 struct snd_soc_dapm_path *p = NULL;
1902
1903 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1904 if (!buf)
1905 return -ENOMEM;
1906
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001907 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001908 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001909 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001910 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001911
Mark Brownf13ebad2012-03-03 18:01:01 +00001912 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1913 w->name, w->power ? "On" : "Off",
1914 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001915
Mark Brownd033c362009-12-04 15:25:56 +00001916 if (w->reg >= 0)
1917 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001918 " - R%d(0x%x) mask 0x%x",
1919 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001920
1921 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1922
Mark Brown3eef08b2009-09-14 16:49:00 +01001923 if (w->sname)
1924 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1925 w->sname,
1926 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001927
1928 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001929 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001930 continue;
1931
Mark Brown79fb9382009-08-21 16:38:13 +01001932 if (p->connect)
1933 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001934 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001935 p->name ? p->name : "static",
1936 p->source->name);
1937 }
1938 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001939 if (p->connected && !p->connected(w, p->sink))
1940 continue;
1941
Mark Brown79fb9382009-08-21 16:38:13 +01001942 if (p->connect)
1943 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001944 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001945 p->name ? p->name : "static",
1946 p->sink->name);
1947 }
1948
1949 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1950
1951 kfree(buf);
1952 return ret;
1953}
1954
1955static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001956 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001957 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001958 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001959};
1960
Mark Brownef49e4f2011-04-04 20:48:13 +09001961static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1962 size_t count, loff_t *ppos)
1963{
1964 struct snd_soc_dapm_context *dapm = file->private_data;
1965 char *level;
1966
1967 switch (dapm->bias_level) {
1968 case SND_SOC_BIAS_ON:
1969 level = "On\n";
1970 break;
1971 case SND_SOC_BIAS_PREPARE:
1972 level = "Prepare\n";
1973 break;
1974 case SND_SOC_BIAS_STANDBY:
1975 level = "Standby\n";
1976 break;
1977 case SND_SOC_BIAS_OFF:
1978 level = "Off\n";
1979 break;
1980 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001981 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09001982 level = "Unknown\n";
1983 break;
1984 }
1985
1986 return simple_read_from_buffer(user_buf, count, ppos, level,
1987 strlen(level));
1988}
1989
1990static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001991 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001992 .read = dapm_bias_read_file,
1993 .llseek = default_llseek,
1994};
1995
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001996void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1997 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001998{
Mark Brown79fb9382009-08-21 16:38:13 +01001999 struct dentry *d;
2000
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002001 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2002
2003 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002004 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002005 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002006 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002007 }
Mark Brown79fb9382009-08-21 16:38:13 +01002008
Mark Brownef49e4f2011-04-04 20:48:13 +09002009 d = debugfs_create_file("bias_level", 0444,
2010 dapm->debugfs_dapm, dapm,
2011 &dapm_bias_fops);
2012 if (!d)
2013 dev_warn(dapm->dev,
2014 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002015}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002016
2017static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2018{
2019 struct snd_soc_dapm_context *dapm = w->dapm;
2020 struct dentry *d;
2021
2022 if (!dapm->debugfs_dapm || !w->name)
2023 return;
2024
2025 d = debugfs_create_file(w->name, 0444,
2026 dapm->debugfs_dapm, w,
2027 &dapm_widget_power_fops);
2028 if (!d)
2029 dev_warn(w->dapm->dev,
2030 "ASoC: Failed to create %s debugfs file\n",
2031 w->name);
2032}
2033
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002034static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2035{
2036 debugfs_remove_recursive(dapm->debugfs_dapm);
2037}
2038
Mark Brown79fb9382009-08-21 16:38:13 +01002039#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002040void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2041 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002042{
2043}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002044
2045static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2046{
2047}
2048
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002049static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2050{
2051}
2052
Mark Brown79fb9382009-08-21 16:38:13 +01002053#endif
2054
Richard Purdie2b97eab2006-10-06 18:32:18 +02002055/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002056static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002057 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002058{
2059 struct snd_soc_dapm_path *path;
2060 int found = 0;
2061
Mark Brownf9fa2b12014-03-06 16:49:11 +08002062 lockdep_assert_held(&card->dapm_mutex);
2063
Richard Purdie2b97eab2006-10-06 18:32:18 +02002064 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002065 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002066 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002067 continue;
2068
2069 found = 1;
2070 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002071 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002072 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002073 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002074 } else {
2075 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002076 dapm_mark_dirty(path->source,
2077 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002078 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002079 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002080 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002081 }
2082
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002083 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002084 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002085
Liam Girdwood618dae12012-04-25 12:12:51 +01002086 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002087}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002088
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002089int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002090 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2091 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002092{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002093 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002094 int ret;
2095
Liam Girdwood3cd04342012-03-09 12:02:08 +00002096 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002097 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002098 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002099 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002100 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002101 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002102 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002103 return ret;
2104}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002105EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002106
Milan plzik1b075e32008-01-10 14:39:46 +01002107/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002108static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002109 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002110{
2111 struct snd_soc_dapm_path *path;
2112 int found = 0;
2113
Mark Brownf9fa2b12014-03-06 16:49:11 +08002114 lockdep_assert_held(&card->dapm_mutex);
2115
Richard Purdie2b97eab2006-10-06 18:32:18 +02002116 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002117 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002118 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002119 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002120 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002121 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002122 }
2123
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002124 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002125 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002126
Liam Girdwood618dae12012-04-25 12:12:51 +01002127 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002128}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002129
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002130int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002131 struct snd_kcontrol *kcontrol, int connect,
2132 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002133{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002134 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002135 int ret;
2136
Liam Girdwood3cd04342012-03-09 12:02:08 +00002137 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002138 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002139 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002140 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002141 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002142 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002143 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002144 return ret;
2145}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002146EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002147
2148/* show dapm widget status in sys fs */
2149static ssize_t dapm_widget_show(struct device *dev,
2150 struct device_attribute *attr, char *buf)
2151{
Mark Brown36ae1a92012-01-06 17:12:45 -08002152 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002153 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002154 struct snd_soc_dapm_widget *w;
2155 int count = 0;
2156 char *state = "not set";
2157
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002158 list_for_each_entry(w, &codec->card->widgets, list) {
2159 if (w->dapm != &codec->dapm)
2160 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002161
2162 /* only display widgets that burnm power */
2163 switch (w->id) {
2164 case snd_soc_dapm_hp:
2165 case snd_soc_dapm_mic:
2166 case snd_soc_dapm_spk:
2167 case snd_soc_dapm_line:
2168 case snd_soc_dapm_micbias:
2169 case snd_soc_dapm_dac:
2170 case snd_soc_dapm_adc:
2171 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002172 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002173 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002174 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002175 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002176 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002177 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002178 if (w->name)
2179 count += sprintf(buf + count, "%s: %s\n",
2180 w->name, w->power ? "On":"Off");
2181 break;
2182 default:
2183 break;
2184 }
2185 }
2186
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002187 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002188 case SND_SOC_BIAS_ON:
2189 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002190 break;
Mark Brown0be98982008-05-19 12:31:28 +02002191 case SND_SOC_BIAS_PREPARE:
2192 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002193 break;
Mark Brown0be98982008-05-19 12:31:28 +02002194 case SND_SOC_BIAS_STANDBY:
2195 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002196 break;
Mark Brown0be98982008-05-19 12:31:28 +02002197 case SND_SOC_BIAS_OFF:
2198 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002199 break;
2200 }
2201 count += sprintf(buf + count, "PM State: %s\n", state);
2202
2203 return count;
2204}
2205
2206static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2207
2208int snd_soc_dapm_sys_add(struct device *dev)
2209{
Troy Kisky12ef1932008-10-13 17:42:14 -07002210 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002211}
2212
2213static void snd_soc_dapm_sys_remove(struct device *dev)
2214{
Mark Brownaef90842009-05-16 17:53:16 +01002215 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002216}
2217
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002218static void dapm_free_path(struct snd_soc_dapm_path *path)
2219{
2220 list_del(&path->list_sink);
2221 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002222 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002223 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002224 kfree(path);
2225}
2226
Richard Purdie2b97eab2006-10-06 18:32:18 +02002227/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002228static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002229{
2230 struct snd_soc_dapm_widget *w, *next_w;
2231 struct snd_soc_dapm_path *p, *next_p;
2232
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002233 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2234 if (w->dapm != dapm)
2235 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002236 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002237 /*
2238 * remove source and sink paths associated to this widget.
2239 * While removing the path, remove reference to it from both
2240 * source and sink widgets so that path is removed only once.
2241 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002242 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2243 dapm_free_path(p);
2244
2245 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2246 dapm_free_path(p);
2247
Stephen Warrenfad59882011-04-28 17:37:59 -06002248 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002249 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002250 kfree(w);
2251 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002252}
2253
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002254static struct snd_soc_dapm_widget *dapm_find_widget(
2255 struct snd_soc_dapm_context *dapm, const char *pin,
2256 bool search_other_contexts)
2257{
2258 struct snd_soc_dapm_widget *w;
2259 struct snd_soc_dapm_widget *fallback = NULL;
2260
2261 list_for_each_entry(w, &dapm->card->widgets, list) {
2262 if (!strcmp(w->name, pin)) {
2263 if (w->dapm == dapm)
2264 return w;
2265 else
2266 fallback = w;
2267 }
2268 }
2269
2270 if (search_other_contexts)
2271 return fallback;
2272
2273 return NULL;
2274}
2275
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002276static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002277 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002278{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002279 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002280
Mark Brownf9fa2b12014-03-06 16:49:11 +08002281 dapm_assert_locked(dapm);
2282
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002283 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002284 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002285 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002286 }
2287
Mark Brown1a8b2d92012-02-16 11:50:07 -08002288 if (w->connected != status)
2289 dapm_mark_dirty(w, "pin configuration");
2290
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002291 w->connected = status;
2292 if (status == 0)
2293 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002294
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002295 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002296}
2297
Richard Purdie2b97eab2006-10-06 18:32:18 +02002298/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002299 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2300 * @dapm: DAPM context
2301 *
2302 * Walks all dapm audio paths and powers widgets according to their
2303 * stream or path usage.
2304 *
2305 * Requires external locking.
2306 *
2307 * Returns 0 for success.
2308 */
2309int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2310{
2311 /*
2312 * Suppress early reports (eg, jacks syncing their state) to avoid
2313 * silly DAPM runs during card startup.
2314 */
2315 if (!dapm->card || !dapm->card->instantiated)
2316 return 0;
2317
2318 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2319}
2320EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2321
2322/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002323 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002324 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002325 *
2326 * Walks all dapm audio paths and powers widgets according to their
2327 * stream or path usage.
2328 *
2329 * Returns 0 for success.
2330 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002331int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002332{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002333 int ret;
2334
Liam Girdwood3cd04342012-03-09 12:02:08 +00002335 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002336 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002337 mutex_unlock(&dapm->card->dapm_mutex);
2338 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002339}
Liam Girdwooda5302182008-07-07 13:35:17 +01002340EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002341
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002342static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2343 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2344 const char *control,
2345 int (*connected)(struct snd_soc_dapm_widget *source,
2346 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002347{
2348 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002349 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002350
2351 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2352 if (!path)
2353 return -ENOMEM;
2354
2355 path->source = wsource;
2356 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002357 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002358 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002359 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002360 INIT_LIST_HEAD(&path->list_source);
2361 INIT_LIST_HEAD(&path->list_sink);
2362
2363 /* check for external widgets */
2364 if (wsink->id == snd_soc_dapm_input) {
2365 if (wsource->id == snd_soc_dapm_micbias ||
2366 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002367 wsource->id == snd_soc_dapm_line ||
2368 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002369 wsink->ext = 1;
2370 }
2371 if (wsource->id == snd_soc_dapm_output) {
2372 if (wsink->id == snd_soc_dapm_spk ||
2373 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002374 wsink->id == snd_soc_dapm_line ||
2375 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002376 wsource->ext = 1;
2377 }
2378
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002379 dapm_mark_dirty(wsource, "Route added");
2380 dapm_mark_dirty(wsink, "Route added");
2381
Richard Purdie2b97eab2006-10-06 18:32:18 +02002382 /* connect static paths */
2383 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002384 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002385 list_add(&path->list_sink, &wsink->sources);
2386 list_add(&path->list_source, &wsource->sinks);
2387 path->connect = 1;
2388 return 0;
2389 }
2390
2391 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002392 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002393 case snd_soc_dapm_adc:
2394 case snd_soc_dapm_dac:
2395 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002396 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002397 case snd_soc_dapm_input:
2398 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002399 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002400 case snd_soc_dapm_micbias:
2401 case snd_soc_dapm_vmid:
2402 case snd_soc_dapm_pre:
2403 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002404 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002405 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002406 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002407 case snd_soc_dapm_aif_in:
2408 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002409 case snd_soc_dapm_dai_in:
2410 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002411 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002412 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002413 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002414 list_add(&path->list_sink, &wsink->sources);
2415 list_add(&path->list_source, &wsource->sinks);
2416 path->connect = 1;
2417 return 0;
2418 case snd_soc_dapm_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002419 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002420 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002421 if (ret != 0)
2422 goto err;
2423 break;
2424 case snd_soc_dapm_switch:
2425 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002426 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002427 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002428 if (ret != 0)
2429 goto err;
2430 break;
2431 case snd_soc_dapm_hp:
2432 case snd_soc_dapm_mic:
2433 case snd_soc_dapm_line:
2434 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002435 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002436 list_add(&path->list_sink, &wsink->sources);
2437 list_add(&path->list_source, &wsource->sinks);
2438 path->connect = 0;
2439 return 0;
2440 }
Mark Brownfabd0382012-07-05 17:20:06 +01002441
Richard Purdie2b97eab2006-10-06 18:32:18 +02002442 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002443err:
2444 kfree(path);
2445 return ret;
2446}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002447
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002448static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002449 const struct snd_soc_dapm_route *route,
2450 unsigned int is_prefixed)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002451{
2452 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2453 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2454 const char *sink;
2455 const char *source;
2456 char prefixed_sink[80];
2457 char prefixed_source[80];
2458 int ret;
2459
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002460 if (dapm->codec && dapm->codec->name_prefix && !is_prefixed) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002461 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2462 dapm->codec->name_prefix, route->sink);
2463 sink = prefixed_sink;
2464 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2465 dapm->codec->name_prefix, route->source);
2466 source = prefixed_source;
2467 } else {
2468 sink = route->sink;
2469 source = route->source;
2470 }
2471
2472 /*
2473 * find src and dest widgets over all widgets but favor a widget from
2474 * current DAPM context
2475 */
2476 list_for_each_entry(w, &dapm->card->widgets, list) {
2477 if (!wsink && !(strcmp(w->name, sink))) {
2478 wtsink = w;
2479 if (w->dapm == dapm)
2480 wsink = w;
2481 continue;
2482 }
2483 if (!wsource && !(strcmp(w->name, source))) {
2484 wtsource = w;
2485 if (w->dapm == dapm)
2486 wsource = w;
2487 }
2488 }
2489 /* use widget from another DAPM context if not found from this */
2490 if (!wsink)
2491 wsink = wtsink;
2492 if (!wsource)
2493 wsource = wtsource;
2494
2495 if (wsource == NULL) {
2496 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2497 route->source);
2498 return -ENODEV;
2499 }
2500 if (wsink == NULL) {
2501 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2502 route->sink);
2503 return -ENODEV;
2504 }
2505
2506 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2507 route->connected);
2508 if (ret)
2509 goto err;
2510
2511 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002512err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002513 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002514 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002515 return ret;
2516}
Mark Brown105f1c22008-05-13 14:52:19 +02002517
Mark Brownefcc3c62012-07-05 17:24:19 +01002518static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2519 const struct snd_soc_dapm_route *route)
2520{
2521 struct snd_soc_dapm_path *path, *p;
2522 const char *sink;
2523 const char *source;
2524 char prefixed_sink[80];
2525 char prefixed_source[80];
2526
2527 if (route->control) {
2528 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002529 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002530 return -EINVAL;
2531 }
2532
2533 if (dapm->codec && dapm->codec->name_prefix) {
2534 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2535 dapm->codec->name_prefix, route->sink);
2536 sink = prefixed_sink;
2537 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2538 dapm->codec->name_prefix, route->source);
2539 source = prefixed_source;
2540 } else {
2541 sink = route->sink;
2542 source = route->source;
2543 }
2544
2545 path = NULL;
2546 list_for_each_entry(p, &dapm->card->paths, list) {
2547 if (strcmp(p->source->name, source) != 0)
2548 continue;
2549 if (strcmp(p->sink->name, sink) != 0)
2550 continue;
2551 path = p;
2552 break;
2553 }
2554
2555 if (path) {
2556 dapm_mark_dirty(path->source, "Route removed");
2557 dapm_mark_dirty(path->sink, "Route removed");
2558
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002559 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002560 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002561 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002562 source, sink);
2563 }
2564
2565 return 0;
2566}
2567
Mark Brown105f1c22008-05-13 14:52:19 +02002568/**
Mark Brown105f1c22008-05-13 14:52:19 +02002569 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002570 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002571 * @route: audio routes
2572 * @num: number of routes
2573 *
2574 * Connects 2 dapm widgets together via a named audio path. The sink is
2575 * the widget receiving the audio signal, whilst the source is the sender
2576 * of the audio signal.
2577 *
2578 * Returns 0 for success else error. On error all resources can be freed
2579 * with a call to snd_soc_card_free().
2580 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002581int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002582 const struct snd_soc_dapm_route *route, int num)
2583{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002584 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002585
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002586 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002587 for (i = 0; i < num; i++) {
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002588 r = snd_soc_dapm_add_route(dapm, route, false);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002589 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002590 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2591 route->source,
2592 route->control ? route->control : "direct",
2593 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002594 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002595 }
2596 route++;
2597 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002598 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002599
Dan Carpenter60884c22012-04-13 22:25:43 +03002600 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002601}
2602EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2603
Mark Brownefcc3c62012-07-05 17:24:19 +01002604/**
2605 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2606 * @dapm: DAPM context
2607 * @route: audio routes
2608 * @num: number of routes
2609 *
2610 * Removes routes from the DAPM context.
2611 */
2612int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2613 const struct snd_soc_dapm_route *route, int num)
2614{
2615 int i, ret = 0;
2616
2617 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2618 for (i = 0; i < num; i++) {
2619 snd_soc_dapm_del_route(dapm, route);
2620 route++;
2621 }
2622 mutex_unlock(&dapm->card->dapm_mutex);
2623
2624 return ret;
2625}
2626EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2627
Mark Brownbf3a9e12011-06-13 16:42:29 +01002628static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2629 const struct snd_soc_dapm_route *route)
2630{
2631 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2632 route->source,
2633 true);
2634 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2635 route->sink,
2636 true);
2637 struct snd_soc_dapm_path *path;
2638 int count = 0;
2639
2640 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002641 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002642 route->source);
2643 return -ENODEV;
2644 }
2645
2646 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002647 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002648 route->sink);
2649 return -ENODEV;
2650 }
2651
2652 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002653 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002654 route->source, route->sink);
2655
2656 list_for_each_entry(path, &source->sinks, list_source) {
2657 if (path->sink == sink) {
2658 path->weak = 1;
2659 count++;
2660 }
2661 }
2662
2663 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002664 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002665 route->source, route->sink);
2666 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002667 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002668 count, route->source, route->sink);
2669
2670 return 0;
2671}
2672
2673/**
2674 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2675 * @dapm: DAPM context
2676 * @route: audio routes
2677 * @num: number of routes
2678 *
2679 * Mark existing routes matching those specified in the passed array
2680 * as being weak, meaning that they are ignored for the purpose of
2681 * power decisions. The main intended use case is for sidetone paths
2682 * which couple audio between other independent paths if they are both
2683 * active in order to make the combination work better at the user
2684 * level but which aren't intended to be "used".
2685 *
2686 * Note that CODEC drivers should not use this as sidetone type paths
2687 * can frequently also be used as bypass paths.
2688 */
2689int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2690 const struct snd_soc_dapm_route *route, int num)
2691{
2692 int i, err;
2693 int ret = 0;
2694
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002695 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002696 for (i = 0; i < num; i++) {
2697 err = snd_soc_dapm_weak_route(dapm, route);
2698 if (err)
2699 ret = err;
2700 route++;
2701 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002702 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002703
2704 return ret;
2705}
2706EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2707
Mark Brown105f1c22008-05-13 14:52:19 +02002708/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002709 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002710 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002711 *
2712 * Checks the codec for any new dapm widgets and creates them if found.
2713 *
2714 * Returns 0 for success.
2715 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002716int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002717{
2718 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002719 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002720
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002721 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002722
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002723 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002724 {
2725 if (w->new)
2726 continue;
2727
Stephen Warrenfad59882011-04-28 17:37:59 -06002728 if (w->num_kcontrols) {
2729 w->kcontrols = kzalloc(w->num_kcontrols *
2730 sizeof(struct snd_kcontrol *),
2731 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002732 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002733 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002734 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002735 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002736 }
2737
Richard Purdie2b97eab2006-10-06 18:32:18 +02002738 switch(w->id) {
2739 case snd_soc_dapm_switch:
2740 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002741 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002742 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002743 break;
2744 case snd_soc_dapm_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002745 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002746 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002747 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002748 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002749 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002750 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002751 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002752 break;
2753 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002754
2755 /* Read the initial power state from the device */
2756 if (w->reg >= 0) {
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002757 soc_widget_read(w, w->reg, &val);
2758 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002759 val &= w->mask;
2760 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002761 w->power = 1;
2762 }
2763
Richard Purdie2b97eab2006-10-06 18:32:18 +02002764 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002765
Mark Brown7508b122011-10-05 12:09:12 +01002766 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002767 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002768 }
2769
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002770 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2771 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002772 return 0;
2773}
2774EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2775
2776/**
2777 * snd_soc_dapm_get_volsw - dapm mixer get callback
2778 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002779 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002780 *
2781 * Callback to get the value of a dapm mixer control.
2782 *
2783 * Returns 0 for success.
2784 */
2785int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2786 struct snd_ctl_elem_value *ucontrol)
2787{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002788 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002789 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002790 struct soc_mixer_control *mc =
2791 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002792 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002793 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002794 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002795 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002796 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002797 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002798
2799 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002800 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002801 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002802 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002803
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002804 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002805 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002806 val = (snd_soc_read(codec, reg) >> shift) & mask;
2807 else
2808 val = dapm_kcontrol_get_value(kcontrol);
2809 mutex_unlock(&card->dapm_mutex);
2810
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002811 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002812 ucontrol->value.integer.value[0] = max - val;
2813 else
2814 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002815
2816 return 0;
2817}
2818EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2819
2820/**
2821 * snd_soc_dapm_put_volsw - dapm mixer set callback
2822 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002823 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002824 *
2825 * Callback to set the value of a dapm mixer control.
2826 *
2827 * Returns 0 for success.
2828 */
2829int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2830 struct snd_ctl_elem_value *ucontrol)
2831{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002832 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002833 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002834 struct soc_mixer_control *mc =
2835 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002836 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002837 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002838 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002839 unsigned int mask = (1 << fls(max)) - 1;
2840 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002841 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002842 int connect, change;
2843 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002844 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002845
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002846 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002847 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002848 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002849 kcontrol->id.name);
2850
Richard Purdie2b97eab2006-10-06 18:32:18 +02002851 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002852 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002853
2854 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002855 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002856
Liam Girdwood3cd04342012-03-09 12:02:08 +00002857 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002858
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002859 change = dapm_kcontrol_set_value(kcontrol, val);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002860
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002861 if (reg != SND_SOC_NOPM) {
2862 mask = mask << shift;
2863 val = val << shift;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002864
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002865 change = snd_soc_test_bits(codec, reg, mask, val);
2866 }
2867
Mark Brown97404f22010-12-14 16:13:57 +00002868 if (change) {
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002869 if (reg != SND_SOC_NOPM) {
2870 update.kcontrol = kcontrol;
2871 update.reg = reg;
2872 update.mask = mask;
2873 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002874
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002875 card->update = &update;
2876 }
Mark Brown97404f22010-12-14 16:13:57 +00002877
Nenghua Cao52765972013-12-13 20:13:49 +08002878 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002879
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002880 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002881 }
2882
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002883 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002884
2885 if (ret > 0)
2886 soc_dpcm_runtime_update(card);
2887
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002888 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002889}
2890EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2891
2892/**
2893 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2894 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002895 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002896 *
2897 * Callback to get the value of a dapm enumerated double mixer control.
2898 *
2899 * Returns 0 for success.
2900 */
2901int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2902 struct snd_ctl_elem_value *ucontrol)
2903{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002904 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002905 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002906 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002907
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002908 if (e->reg != SND_SOC_NOPM)
2909 reg_val = snd_soc_read(codec, e->reg);
2910 else
2911 reg_val = dapm_kcontrol_get_value(kcontrol);
2912
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002913 val = (reg_val >> e->shift_l) & e->mask;
2914 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2915 if (e->shift_l != e->shift_r) {
2916 val = (reg_val >> e->shift_r) & e->mask;
2917 val = snd_soc_enum_val_to_item(e, val);
2918 ucontrol->value.enumerated.item[1] = val;
2919 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002920
2921 return 0;
2922}
2923EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2924
2925/**
2926 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2927 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002928 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002929 *
2930 * Callback to set the value of a dapm enumerated double mixer control.
2931 *
2932 * Returns 0 for success.
2933 */
2934int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2935 struct snd_ctl_elem_value *ucontrol)
2936{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002937 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002938 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002939 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002940 unsigned int *item = ucontrol->value.enumerated.item;
2941 unsigned int val, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002942 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002943 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002944 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002945
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002946 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002947 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002948
2949 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002950 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002951 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002952 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002953 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002954 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002955 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002956 }
2957
Liam Girdwood3cd04342012-03-09 12:02:08 +00002958 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002959
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002960 if (e->reg != SND_SOC_NOPM)
2961 change = snd_soc_test_bits(codec, e->reg, mask, val);
2962 else
2963 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002964
Richard Purdie2b97eab2006-10-06 18:32:18 +02002965 if (change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002966 if (e->reg != SND_SOC_NOPM) {
2967 update.kcontrol = kcontrol;
2968 update.reg = e->reg;
2969 update.mask = mask;
2970 update.val = val;
2971 card->update = &update;
2972 }
Mark Brown3a655772009-10-05 17:23:30 +01002973
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002974 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002975
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002976 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002977 }
2978
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002979 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002980
2981 if (ret > 0)
2982 soc_dpcm_runtime_update(card);
2983
Mark Brown97404f22010-12-14 16:13:57 +00002984 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002985}
2986EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2987
2988/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002989 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2990 *
2991 * @kcontrol: mixer control
2992 * @uinfo: control element information
2993 *
2994 * Callback to provide information about a pin switch control.
2995 */
2996int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2997 struct snd_ctl_elem_info *uinfo)
2998{
2999 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3000 uinfo->count = 1;
3001 uinfo->value.integer.min = 0;
3002 uinfo->value.integer.max = 1;
3003
3004 return 0;
3005}
3006EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3007
3008/**
3009 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3010 *
3011 * @kcontrol: mixer control
3012 * @ucontrol: Value
3013 */
3014int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3015 struct snd_ctl_elem_value *ucontrol)
3016{
Mark Brown48a8c392012-02-14 17:11:15 -08003017 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003018 const char *pin = (const char *)kcontrol->private_value;
3019
Liam Girdwood3cd04342012-03-09 12:02:08 +00003020 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003021
3022 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003023 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003024
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003025 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003026
3027 return 0;
3028}
3029EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3030
3031/**
3032 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3033 *
3034 * @kcontrol: mixer control
3035 * @ucontrol: Value
3036 */
3037int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3038 struct snd_ctl_elem_value *ucontrol)
3039{
Mark Brown48a8c392012-02-14 17:11:15 -08003040 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003041 const char *pin = (const char *)kcontrol->private_value;
3042
Mark Brown8b37dbd2009-02-28 21:14:20 +00003043 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003044 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003045 else
Mark Brown48a8c392012-02-14 17:11:15 -08003046 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003047
Mark Brown48a8c392012-02-14 17:11:15 -08003048 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003049 return 0;
3050}
3051EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3052
Mark Brown5ba06fc2012-02-16 11:07:13 -08003053static struct snd_soc_dapm_widget *
3054snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3055 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003056{
3057 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003058 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003059
3060 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003061 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003062
Mark Brown62ea8742012-01-21 21:14:48 +00003063 switch (w->id) {
3064 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003065 w->regulator = devm_regulator_get(dapm->dev, w->name);
3066 if (IS_ERR(w->regulator)) {
3067 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003068 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003069 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003070 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003071 }
Mark Brown8784c772013-01-10 19:33:47 +00003072
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003073 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003074 ret = regulator_allow_bypass(w->regulator, true);
3075 if (ret != 0)
3076 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003077 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003078 w->name, ret);
3079 }
Mark Brown62ea8742012-01-21 21:14:48 +00003080 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003081 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003082#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003083 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003084 if (IS_ERR(w->clk)) {
3085 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003086 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003087 w->name, ret);
3088 return NULL;
3089 }
Mark Brownec029952012-06-04 08:16:20 +01003090#else
3091 return NULL;
3092#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003093 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003094 default:
3095 break;
3096 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003097
Mark Brown88e8b9a2011-03-02 18:18:24 +00003098 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003099 w->name = kasprintf(GFP_KERNEL, "%s %s",
3100 dapm->codec->name_prefix, widget->name);
3101 else
3102 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3103
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003104 if (w->name == NULL) {
3105 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003106 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003107 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003108
Mark Brown7ca3a182011-10-08 14:04:50 +01003109 switch (w->id) {
3110 case snd_soc_dapm_switch:
3111 case snd_soc_dapm_mixer:
3112 case snd_soc_dapm_mixer_named_ctl:
3113 w->power_check = dapm_generic_check_power;
3114 break;
3115 case snd_soc_dapm_mux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003116 w->power_check = dapm_generic_check_power;
3117 break;
Mark Brown46162742013-06-05 19:36:11 +01003118 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003119 w->power_check = dapm_adc_check_power;
3120 break;
Mark Brown46162742013-06-05 19:36:11 +01003121 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003122 w->power_check = dapm_dac_check_power;
3123 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003124 case snd_soc_dapm_adc:
3125 case snd_soc_dapm_aif_out:
3126 case snd_soc_dapm_dac:
3127 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003128 case snd_soc_dapm_pga:
3129 case snd_soc_dapm_out_drv:
3130 case snd_soc_dapm_input:
3131 case snd_soc_dapm_output:
3132 case snd_soc_dapm_micbias:
3133 case snd_soc_dapm_spk:
3134 case snd_soc_dapm_hp:
3135 case snd_soc_dapm_mic:
3136 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003137 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003138 w->power_check = dapm_generic_check_power;
3139 break;
3140 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003141 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003142 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003143 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003144 w->power_check = dapm_supply_check_power;
3145 break;
3146 default:
3147 w->power_check = dapm_always_on_check_power;
3148 break;
3149 }
3150
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003151 w->dapm = dapm;
3152 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003153 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003154 INIT_LIST_HEAD(&w->sources);
3155 INIT_LIST_HEAD(&w->sinks);
3156 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003157 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003158 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003159
3160 /* machine layer set ups unconnected pins and insertions */
3161 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003162 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003163}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003164
3165/**
Mark Brown4ba13272008-05-13 14:51:19 +02003166 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003167 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003168 * @widget: widget array
3169 * @num: number of widgets
3170 *
3171 * Creates new DAPM controls based upon the templates.
3172 *
3173 * Returns 0 for success else error.
3174 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003175int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003176 const struct snd_soc_dapm_widget *widget,
3177 int num)
3178{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003179 struct snd_soc_dapm_widget *w;
3180 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003181 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003182
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003183 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003184 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003185 w = snd_soc_dapm_new_control(dapm, widget);
3186 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003187 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003188 "ASoC: Failed to create DAPM control %s\n",
3189 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003190 ret = -ENOMEM;
3191 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003192 }
Mark Brown4ba13272008-05-13 14:51:19 +02003193 widget++;
3194 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003195 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003196 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003197}
3198EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3199
Mark Brownc74184e2012-04-04 22:12:09 +01003200static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3201 struct snd_kcontrol *kcontrol, int event)
3202{
3203 struct snd_soc_dapm_path *source_p, *sink_p;
3204 struct snd_soc_dai *source, *sink;
3205 const struct snd_soc_pcm_stream *config = w->params;
3206 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003207 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003208 u64 fmt;
3209 int ret;
3210
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003211 if (WARN_ON(!config) ||
3212 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3213 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003214
3215 /* We only support a single source and sink, pick the first */
3216 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3217 list_sink);
3218 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3219 list_source);
3220
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003221 if (WARN_ON(!source_p || !sink_p) ||
3222 WARN_ON(!sink_p->source || !source_p->sink) ||
3223 WARN_ON(!source_p->source || !sink_p->sink))
3224 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003225
3226 source = source_p->source->priv;
3227 sink = sink_p->sink->priv;
3228
3229 /* Be a little careful as we don't want to overflow the mask array */
3230 if (config->formats) {
3231 fmt = ffs(config->formats) - 1;
3232 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003233 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003234 config->formats);
3235 fmt = 0;
3236 }
3237
3238 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003239 params = kzalloc(sizeof(*params), GFP_KERNEL);
3240 if (!params) {
3241 ret = -ENOMEM;
3242 goto out;
3243 }
3244 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003245
Mark Brown9747cec2012-04-26 19:12:21 +01003246 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003247 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003248 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003249 config->rate_max;
3250
Mark Brown9747cec2012-04-26 19:12:21 +01003251 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003252 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003253 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003254 = config->channels_max;
3255
3256 memset(&substream, 0, sizeof(substream));
3257
3258 switch (event) {
3259 case SND_SOC_DAPM_PRE_PMU:
3260 if (source->driver->ops && source->driver->ops->hw_params) {
3261 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3262 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003263 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003264 if (ret != 0) {
3265 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003266 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003267 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003268 }
3269 }
3270
3271 if (sink->driver->ops && sink->driver->ops->hw_params) {
3272 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003273 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003274 sink);
3275 if (ret != 0) {
3276 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003277 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003278 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003279 }
3280 }
3281 break;
3282
3283 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003284 ret = snd_soc_dai_digital_mute(sink, 0,
3285 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003286 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003287 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003288 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003289 break;
3290
3291 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003292 ret = snd_soc_dai_digital_mute(sink, 1,
3293 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003294 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003295 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003296 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003297 break;
3298
3299 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003300 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003301 return -EINVAL;
3302 }
3303
Mark Brown9747cec2012-04-26 19:12:21 +01003304out:
3305 kfree(params);
3306 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003307}
3308
3309int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3310 const struct snd_soc_pcm_stream *params,
3311 struct snd_soc_dapm_widget *source,
3312 struct snd_soc_dapm_widget *sink)
3313{
3314 struct snd_soc_dapm_route routes[2];
3315 struct snd_soc_dapm_widget template;
3316 struct snd_soc_dapm_widget *w;
3317 size_t len;
3318 char *link_name;
3319
3320 len = strlen(source->name) + strlen(sink->name) + 2;
3321 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3322 if (!link_name)
3323 return -ENOMEM;
3324 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3325
3326 memset(&template, 0, sizeof(template));
3327 template.reg = SND_SOC_NOPM;
3328 template.id = snd_soc_dapm_dai_link;
3329 template.name = link_name;
3330 template.event = snd_soc_dai_link_event;
3331 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3332 SND_SOC_DAPM_PRE_PMD;
3333
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003334 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003335
3336 w = snd_soc_dapm_new_control(&card->dapm, &template);
3337 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003338 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003339 link_name);
3340 return -ENOMEM;
3341 }
3342
3343 w->params = params;
3344
3345 memset(&routes, 0, sizeof(routes));
3346
3347 routes[0].source = source->name;
3348 routes[0].sink = link_name;
3349 routes[1].source = link_name;
3350 routes[1].sink = sink->name;
3351
3352 return snd_soc_dapm_add_routes(&card->dapm, routes,
3353 ARRAY_SIZE(routes));
3354}
3355
Mark Brown888df392012-02-16 19:37:51 -08003356int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3357 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003358{
Mark Brown888df392012-02-16 19:37:51 -08003359 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003360 struct snd_soc_dapm_widget *w;
3361
Mark Brown888df392012-02-16 19:37:51 -08003362 WARN_ON(dapm->dev != dai->dev);
3363
3364 memset(&template, 0, sizeof(template));
3365 template.reg = SND_SOC_NOPM;
3366
3367 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003368 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003369 template.name = dai->driver->playback.stream_name;
3370 template.sname = dai->driver->playback.stream_name;
3371
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003372 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003373 template.name);
3374
3375 w = snd_soc_dapm_new_control(dapm, &template);
3376 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003377 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003378 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003379 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003380 }
3381
3382 w->priv = dai;
3383 dai->playback_widget = w;
3384 }
3385
3386 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003387 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003388 template.name = dai->driver->capture.stream_name;
3389 template.sname = dai->driver->capture.stream_name;
3390
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003391 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003392 template.name);
3393
3394 w = snd_soc_dapm_new_control(dapm, &template);
3395 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003396 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003397 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003398 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003399 }
3400
3401 w->priv = dai;
3402 dai->capture_widget = w;
3403 }
3404
3405 return 0;
3406}
3407
3408int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3409{
3410 struct snd_soc_dapm_widget *dai_w, *w;
3411 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003412
3413 /* For each DAI widget... */
3414 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003415 switch (dai_w->id) {
3416 case snd_soc_dapm_dai_in:
3417 case snd_soc_dapm_dai_out:
3418 break;
3419 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003420 continue;
Mark Brown46162742013-06-05 19:36:11 +01003421 }
Mark Brown888df392012-02-16 19:37:51 -08003422
3423 dai = dai_w->priv;
3424
3425 /* ...find all widgets with the same stream and link them */
3426 list_for_each_entry(w, &card->widgets, list) {
3427 if (w->dapm != dai_w->dapm)
3428 continue;
3429
Mark Brown46162742013-06-05 19:36:11 +01003430 switch (w->id) {
3431 case snd_soc_dapm_dai_in:
3432 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003433 continue;
Mark Brown46162742013-06-05 19:36:11 +01003434 default:
3435 break;
3436 }
Mark Brown888df392012-02-16 19:37:51 -08003437
Russell King19c2c5f2013-08-04 20:24:03 +01003438 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003439 continue;
3440
3441 if (dai->driver->playback.stream_name &&
3442 strstr(w->sname,
3443 dai->driver->playback.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003444 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003445 dai->playback_widget->name, w->name);
Mark Brown888df392012-02-16 19:37:51 -08003446
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003447 snd_soc_dapm_add_path(w->dapm,
3448 dai->playback_widget, w, NULL, NULL);
Mark Brown888df392012-02-16 19:37:51 -08003449 }
3450
3451 if (dai->driver->capture.stream_name &&
3452 strstr(w->sname,
3453 dai->driver->capture.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003454 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003455 w->name, dai->capture_widget->name);
Mark Brown888df392012-02-16 19:37:51 -08003456
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003457 snd_soc_dapm_add_path(w->dapm, w,
3458 dai->capture_widget, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003459 }
3460 }
3461 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003462
Mark Brown888df392012-02-16 19:37:51 -08003463 return 0;
3464}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003465
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003466void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3467{
3468 struct snd_soc_pcm_runtime *rtd = card->rtd;
3469 struct snd_soc_dai *cpu_dai, *codec_dai;
3470 struct snd_soc_dapm_route r;
3471 int i;
3472
3473 memset(&r, 0, sizeof(r));
3474
3475 /* for each BE DAI link... */
3476 for (i = 0; i < card->num_rtd; i++) {
3477 rtd = &card->rtd[i];
3478 cpu_dai = rtd->cpu_dai;
3479 codec_dai = rtd->codec_dai;
3480
Lars-Peter Clausenca5106a2014-05-07 16:20:24 +02003481 /*
3482 * dynamic FE links have no fixed DAI mapping.
3483 * CODEC<->CODEC links have no direct connection.
3484 */
3485 if (rtd->dai_link->dynamic || rtd->dai_link->params)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003486 continue;
3487
3488 /* there is no point in connecting BE DAI links with dummies */
3489 if (snd_soc_dai_is_dummy(codec_dai) ||
3490 snd_soc_dai_is_dummy(cpu_dai))
3491 continue;
3492
3493 /* connect BE DAI playback if widgets are valid */
3494 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3495 r.source = cpu_dai->playback_widget->name;
3496 r.sink = codec_dai->playback_widget->name;
3497 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3498 cpu_dai->codec->name, r.source,
3499 codec_dai->platform->name, r.sink);
3500
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003501 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003502 }
3503
3504 /* connect BE DAI capture if widgets are valid */
3505 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3506 r.source = codec_dai->capture_widget->name;
3507 r.sink = cpu_dai->capture_widget->name;
3508 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3509 codec_dai->codec->name, r.source,
3510 cpu_dai->platform->name, r.sink);
3511
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003512 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003513 }
3514
3515 }
3516}
3517
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003518static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3519 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003520{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003521
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003522 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3523 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3524 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003525
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003526 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3527 w_cpu = cpu_dai->playback_widget;
3528 w_codec = codec_dai->playback_widget;
3529 } else {
3530 w_cpu = cpu_dai->capture_widget;
3531 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003532 }
3533
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003534 if (w_cpu) {
3535
3536 dapm_mark_dirty(w_cpu, "stream event");
3537
3538 switch (event) {
3539 case SND_SOC_DAPM_STREAM_START:
3540 w_cpu->active = 1;
3541 break;
3542 case SND_SOC_DAPM_STREAM_STOP:
3543 w_cpu->active = 0;
3544 break;
3545 case SND_SOC_DAPM_STREAM_SUSPEND:
3546 case SND_SOC_DAPM_STREAM_RESUME:
3547 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3548 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3549 break;
3550 }
3551 }
3552
3553 if (w_codec) {
3554
3555 dapm_mark_dirty(w_codec, "stream event");
3556
3557 switch (event) {
3558 case SND_SOC_DAPM_STREAM_START:
3559 w_codec->active = 1;
3560 break;
3561 case SND_SOC_DAPM_STREAM_STOP:
3562 w_codec->active = 0;
3563 break;
3564 case SND_SOC_DAPM_STREAM_SUSPEND:
3565 case SND_SOC_DAPM_STREAM_RESUME:
3566 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3567 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3568 break;
3569 }
3570 }
3571
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003572 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003573}
3574
3575/**
3576 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3577 * @rtd: PCM runtime data
3578 * @stream: stream name
3579 * @event: stream event
3580 *
3581 * Sends a stream event to the dapm core. The core then makes any
3582 * necessary widget power changes.
3583 *
3584 * Returns 0 for success else error.
3585 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003586void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3587 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003588{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003589 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003590
Liam Girdwood3cd04342012-03-09 12:02:08 +00003591 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003592 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003593 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003594}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003595
3596/**
Charles Keepax11391102014-02-18 15:22:14 +00003597 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3598 * @dapm: DAPM context
3599 * @pin: pin name
3600 *
3601 * Enables input/output pin and its parents or children widgets iff there is
3602 * a valid audio route and active audio stream.
3603 *
3604 * Requires external locking.
3605 *
3606 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3607 * do any widget power switching.
3608 */
3609int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3610 const char *pin)
3611{
3612 return snd_soc_dapm_set_pin(dapm, pin, 1);
3613}
3614EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3615
3616/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003617 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003618 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003619 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003620 *
Mark Brown74b8f952009-06-06 11:26:15 +01003621 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003622 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003623 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003624 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3625 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003626 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003627int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003628{
Charles Keepax11391102014-02-18 15:22:14 +00003629 int ret;
3630
3631 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3632
3633 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3634
3635 mutex_unlock(&dapm->card->dapm_mutex);
3636
3637 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003638}
Liam Girdwooda5302182008-07-07 13:35:17 +01003639EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003640
3641/**
Charles Keepax11391102014-02-18 15:22:14 +00003642 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3643 * @dapm: DAPM context
3644 * @pin: pin name
3645 *
3646 * Enables input/output pin regardless of any other state. This is
3647 * intended for use with microphone bias supplies used in microphone
3648 * jack detection.
3649 *
3650 * Requires external locking.
3651 *
3652 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3653 * do any widget power switching.
3654 */
3655int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3656 const char *pin)
3657{
3658 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3659
3660 if (!w) {
3661 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3662 return -EINVAL;
3663 }
3664
3665 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3666 w->connected = 1;
3667 w->force = 1;
3668 dapm_mark_dirty(w, "force enable");
3669
3670 return 0;
3671}
3672EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3673
3674/**
Mark Brownda341832010-03-15 19:23:37 +00003675 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003676 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003677 * @pin: pin name
3678 *
3679 * Enables input/output pin regardless of any other state. This is
3680 * intended for use with microphone bias supplies used in microphone
3681 * jack detection.
3682 *
3683 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3684 * do any widget power switching.
3685 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003686int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3687 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003688{
Charles Keepax11391102014-02-18 15:22:14 +00003689 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003690
Charles Keepax11391102014-02-18 15:22:14 +00003691 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003692
Charles Keepax11391102014-02-18 15:22:14 +00003693 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003694
Charles Keepax11391102014-02-18 15:22:14 +00003695 mutex_unlock(&dapm->card->dapm_mutex);
3696
3697 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003698}
3699EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3700
3701/**
Charles Keepax11391102014-02-18 15:22:14 +00003702 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3703 * @dapm: DAPM context
3704 * @pin: pin name
3705 *
3706 * Disables input/output pin and its parents or children widgets.
3707 *
3708 * Requires external locking.
3709 *
3710 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3711 * do any widget power switching.
3712 */
3713int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3714 const char *pin)
3715{
3716 return snd_soc_dapm_set_pin(dapm, pin, 0);
3717}
3718EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3719
3720/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003721 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003722 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003723 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003724 *
Mark Brown74b8f952009-06-06 11:26:15 +01003725 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003726 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003727 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3728 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003729 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003730int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3731 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003732{
Charles Keepax11391102014-02-18 15:22:14 +00003733 int ret;
3734
3735 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3736
3737 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3738
3739 mutex_unlock(&dapm->card->dapm_mutex);
3740
3741 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003742}
3743EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3744
3745/**
Charles Keepax11391102014-02-18 15:22:14 +00003746 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3747 * @dapm: DAPM context
3748 * @pin: pin name
3749 *
3750 * Marks the specified pin as being not connected, disabling it along
3751 * any parent or child widgets. At present this is identical to
3752 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3753 * additional things such as disabling controls which only affect
3754 * paths through the pin.
3755 *
3756 * Requires external locking.
3757 *
3758 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3759 * do any widget power switching.
3760 */
3761int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3762 const char *pin)
3763{
3764 return snd_soc_dapm_set_pin(dapm, pin, 0);
3765}
3766EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3767
3768/**
Mark Brown5817b522008-09-24 11:23:11 +01003769 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003770 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003771 * @pin: pin name
3772 *
3773 * Marks the specified pin as being not connected, disabling it along
3774 * any parent or child widgets. At present this is identical to
3775 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3776 * additional things such as disabling controls which only affect
3777 * paths through the pin.
3778 *
3779 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3780 * do any widget power switching.
3781 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003782int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003783{
Charles Keepax11391102014-02-18 15:22:14 +00003784 int ret;
3785
3786 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3787
3788 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3789
3790 mutex_unlock(&dapm->card->dapm_mutex);
3791
3792 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003793}
3794EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3795
3796/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003797 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003798 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003799 * @pin: audio signal pin endpoint (or start point)
3800 *
3801 * Get audio pin status - connected or disconnected.
3802 *
3803 * Returns 1 for connected otherwise 0.
3804 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003805int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3806 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003807{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003808 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003809
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003810 if (w)
3811 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003812
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003813 return 0;
3814}
Liam Girdwooda5302182008-07-07 13:35:17 +01003815EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003816
3817/**
Mark Brown1547aba2010-05-07 21:11:40 +01003818 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003819 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003820 * @pin: audio signal pin endpoint (or start point)
3821 *
3822 * Mark the given endpoint or pin as ignoring suspend. When the
3823 * system is disabled a path between two endpoints flagged as ignoring
3824 * suspend will not be disabled. The path must already be enabled via
3825 * normal means at suspend time, it will not be turned on if it was not
3826 * already enabled.
3827 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003828int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3829 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003830{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003831 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003832
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003833 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003834 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003835 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003836 }
3837
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003838 w->ignore_suspend = 1;
3839
3840 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003841}
3842EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3843
Stephen Warren16332812011-11-23 12:42:04 -07003844static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3845 struct snd_soc_dapm_widget *w)
3846{
3847 struct snd_soc_dapm_path *p;
3848
3849 list_for_each_entry(p, &card->paths, list) {
3850 if ((p->source == w) || (p->sink == w)) {
3851 dev_dbg(card->dev,
3852 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3853 p->source->name, p->source->id, p->source->dapm,
3854 p->sink->name, p->sink->id, p->sink->dapm);
3855
3856 /* Connected to something other than the codec */
3857 if (p->source->dapm != p->sink->dapm)
3858 return true;
3859 /*
3860 * Loopback connection from codec external pin to
3861 * codec external pin
3862 */
3863 if (p->sink->id == snd_soc_dapm_input) {
3864 switch (p->source->id) {
3865 case snd_soc_dapm_output:
3866 case snd_soc_dapm_micbias:
3867 return true;
3868 default:
3869 break;
3870 }
3871 }
3872 }
3873 }
3874
3875 return false;
3876}
3877
3878/**
3879 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3880 * @codec: The codec whose pins should be processed
3881 *
3882 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3883 * which are unused. Pins are used if they are connected externally to the
3884 * codec, whether that be to some other device, or a loop-back connection to
3885 * the codec itself.
3886 */
3887void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3888{
3889 struct snd_soc_card *card = codec->card;
3890 struct snd_soc_dapm_context *dapm = &codec->dapm;
3891 struct snd_soc_dapm_widget *w;
3892
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003893 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003894 &card->dapm, &codec->dapm);
3895
3896 list_for_each_entry(w, &card->widgets, list) {
3897 if (w->dapm != dapm)
3898 continue;
3899 switch (w->id) {
3900 case snd_soc_dapm_input:
3901 case snd_soc_dapm_output:
3902 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003903 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003904 w->name);
3905 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003906 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003907 "... Not in map; disabling\n");
3908 snd_soc_dapm_nc_pin(dapm, w->name);
3909 }
3910 break;
3911 default:
3912 break;
3913 }
3914 }
3915}
3916
Mark Brown1547aba2010-05-07 21:11:40 +01003917/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003918 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003919 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003920 *
3921 * Free all dapm widgets and resources.
3922 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003923void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003924{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003925 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003926 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003927 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003928 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003929}
3930EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3931
Xiang Xiao57996352014-03-02 00:04:02 +08003932static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003933{
Liam Girdwood01005a72012-07-06 16:57:05 +01003934 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003935 struct snd_soc_dapm_widget *w;
3936 LIST_HEAD(down_list);
3937 int powerdown = 0;
3938
Liam Girdwood01005a72012-07-06 16:57:05 +01003939 mutex_lock(&card->dapm_mutex);
3940
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003941 list_for_each_entry(w, &dapm->card->widgets, list) {
3942 if (w->dapm != dapm)
3943 continue;
Mark Brown51737472009-06-22 13:16:51 +01003944 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003945 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003946 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003947 powerdown = 1;
3948 }
3949 }
3950
3951 /* If there were no widgets to power down we're already in
3952 * standby.
3953 */
3954 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003955 if (dapm->bias_level == SND_SOC_BIAS_ON)
3956 snd_soc_dapm_set_bias_level(dapm,
3957 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003958 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003959 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3960 snd_soc_dapm_set_bias_level(dapm,
3961 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003962 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003963
3964 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003965}
Mark Brown51737472009-06-22 13:16:51 +01003966
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003967/*
3968 * snd_soc_dapm_shutdown - callback for system shutdown
3969 */
3970void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3971{
Xiang Xiao57996352014-03-02 00:04:02 +08003972 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003973
Xiang Xiao57996352014-03-02 00:04:02 +08003974 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08003975 if (dapm != &card->dapm) {
3976 soc_dapm_shutdown_dapm(dapm);
3977 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3978 snd_soc_dapm_set_bias_level(dapm,
3979 SND_SOC_BIAS_OFF);
3980 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003981 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08003982
3983 soc_dapm_shutdown_dapm(&card->dapm);
3984 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3985 snd_soc_dapm_set_bias_level(&card->dapm,
3986 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01003987}
3988
Richard Purdie2b97eab2006-10-06 18:32:18 +02003989/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003990MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003991MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3992MODULE_LICENSE("GPL");