blob: 3651a37bb2c738911fa4630e6ed39b2f79715795 [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
Troy Kisky12ef1932008-10-13 17:42:14 -0700114static void pop_wait(u32 pop_time)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100115{
116 if (pop_time)
117 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
118}
119
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200120static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100121{
122 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200123 char *buf;
124
125 if (!pop_time)
126 return;
127
128 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
129 if (buf == NULL)
130 return;
Mark Brown15e4c72f2008-07-02 11:51:20 +0100131
132 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200133 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100134 dev_info(dev, "%s", buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100135 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200136
137 kfree(buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100138}
139
Mark Browndb432b42011-10-03 21:06:40 +0100140static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
141{
142 return !list_empty(&w->dirty);
143}
144
Mark Brown25c77c52011-10-08 13:36:03 +0100145void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100146{
Mark Brown75c1f892011-10-04 22:28:08 +0100147 if (!dapm_dirty_widget(w)) {
148 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
149 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100150 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100151 }
Mark Browndb432b42011-10-03 21:06:40 +0100152}
Mark Brown25c77c52011-10-08 13:36:03 +0100153EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100154
Mark Browne2d32ff2012-08-31 17:38:32 -0700155void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
156{
157 struct snd_soc_card *card = dapm->card;
158 struct snd_soc_dapm_widget *w;
159
160 mutex_lock(&card->dapm_mutex);
161
162 list_for_each_entry(w, &card->widgets, list) {
163 switch (w->id) {
164 case snd_soc_dapm_input:
165 case snd_soc_dapm_output:
166 dapm_mark_dirty(w, "Rechecking inputs and outputs");
167 break;
168 default:
169 break;
170 }
171 }
172
173 mutex_unlock(&card->dapm_mutex);
174}
175EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
176
Richard Purdie2b97eab2006-10-06 18:32:18 +0200177/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100178static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200179 const struct snd_soc_dapm_widget *_widget)
180{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100181 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200182}
183
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200184struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200185 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200186 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200187 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200188 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200189};
190
191static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
192 struct snd_kcontrol *kcontrol)
193{
194 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200195 struct soc_mixer_control *mc;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200196
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200197 data = kzalloc(sizeof(*data), GFP_KERNEL);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200198 if (!data) {
199 dev_err(widget->dapm->dev,
200 "ASoC: can't allocate kcontrol data for %s\n",
201 widget->name);
202 return -ENOMEM;
203 }
204
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200205 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200206
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200207 switch (widget->id) {
208 case snd_soc_dapm_switch:
209 case snd_soc_dapm_mixer:
210 case snd_soc_dapm_mixer_named_ctl:
211 mc = (struct soc_mixer_control *)kcontrol->private_value;
212
213 if (mc->autodisable) {
214 struct snd_soc_dapm_widget template;
215
216 memset(&template, 0, sizeof(template));
217 template.reg = mc->reg;
218 template.mask = (1 << fls(mc->max)) - 1;
219 template.shift = mc->shift;
220 if (mc->invert)
221 template.off_val = mc->max;
222 else
223 template.off_val = 0;
224 template.on_val = template.off_val;
225 template.id = snd_soc_dapm_kcontrol;
226 template.name = kcontrol->id.name;
227
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200228 data->value = template.on_val;
229
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200230 data->widget = snd_soc_dapm_new_control(widget->dapm,
231 &template);
232 if (!data->widget) {
233 kfree(data);
234 return -ENOMEM;
235 }
236 }
237 break;
238 default:
239 break;
240 }
241
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200242 kcontrol->private_data = data;
243
244 return 0;
245}
246
247static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
248{
249 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200250 kfree(data->widget);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200251 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200252 kfree(data);
253}
254
255static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
256 const struct snd_kcontrol *kcontrol)
257{
258 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
259
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200260 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200261}
262
263static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
264 struct snd_soc_dapm_widget *widget)
265{
266 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200267 struct snd_soc_dapm_widget_list *new_wlist;
268 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200269
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200270 if (data->wlist)
271 n = data->wlist->num_widgets + 1;
272 else
273 n = 1;
274
275 new_wlist = krealloc(data->wlist,
276 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
277 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200278 return -ENOMEM;
279
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200280 new_wlist->widgets[n - 1] = widget;
281 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200282
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200283 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200284
285 return 0;
286}
287
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200288static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
289 struct snd_soc_dapm_path *path)
290{
291 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
292
293 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200294
295 if (data->widget) {
296 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
297 path->source, NULL, NULL);
298 }
299}
300
301static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
302{
303 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
304
305 if (!data->widget)
306 return true;
307
308 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200309}
310
311static struct list_head *dapm_kcontrol_get_path_list(
312 const struct snd_kcontrol *kcontrol)
313{
314 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
315
316 return &data->paths;
317}
318
319#define dapm_kcontrol_for_each_path(path, kcontrol) \
320 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
321 list_kcontrol)
322
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200323static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
324{
325 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
326
327 return data->value;
328}
329
330static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
331 unsigned int value)
332{
333 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
334
335 if (data->value == value)
336 return false;
337
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200338 if (data->widget)
339 data->widget->on_val = value;
340
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200341 data->value = value;
342
343 return true;
344}
345
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200346/**
347 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
348 * @kcontrol: The kcontrol
349 */
350struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
351{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200352 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200353}
354EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
355
Liam Girdwood6c120e12012-02-15 15:15:34 +0000356static void dapm_reset(struct snd_soc_card *card)
357{
358 struct snd_soc_dapm_widget *w;
359
360 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
361
362 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200363 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000364 w->power_checked = false;
365 w->inputs = -1;
366 w->outputs = -1;
367 }
368}
369
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800370static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg,
371 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100372{
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800373 if (w->codec) {
374 *value = snd_soc_read(w->codec, reg);
375 return 0;
376 } else if (w->platform) {
377 *value = snd_soc_platform_read(w->platform, reg);
378 return 0;
379 }
Liam Girdwoodb7950642011-07-04 22:10:52 +0100380
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000381 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100382 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100383}
384
Lars-Peter Clausen30ac6b62014-02-13 14:14:21 +0100385static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg,
386 unsigned int val)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100387{
388 if (w->codec)
389 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100390 else if (w->platform)
391 return snd_soc_platform_write(w->platform, reg, val);
392
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000393 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100394 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100395}
396
Liam Girdwood49575fb52012-03-06 18:16:19 +0000397static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
398{
Mark Browne06ab3b2012-03-06 23:58:22 +0000399 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000400 mutex_lock(&w->codec->mutex);
401 else if (w->platform)
402 mutex_lock(&w->platform->mutex);
403}
404
405static inline void soc_widget_unlock(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_unlock(&w->codec->mutex);
409 else if (w->platform)
410 mutex_unlock(&w->platform->mutex);
411}
412
Mark Browneb270e92013-10-09 13:52:52 +0100413static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
414{
415 if (dapm->codec && dapm->codec->using_regmap)
416 regmap_async_complete(dapm->codec->control_data);
417}
418
Liam Girdwood49575fb52012-03-06 18:16:19 +0000419static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100420 unsigned short reg, unsigned int mask, unsigned int value)
421{
Mark Brown8a713da2011-12-03 12:33:55 +0000422 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100423 unsigned int old, new;
424 int ret;
425
Mark Brown8a713da2011-12-03 12:33:55 +0000426 if (w->codec && w->codec->using_regmap) {
Mark Browneb270e92013-10-09 13:52:52 +0100427 ret = regmap_update_bits_check_async(w->codec->control_data,
428 reg, mask, value,
429 &change);
Mark Brown8a713da2011-12-03 12:33:55 +0000430 if (ret != 0)
431 return ret;
432 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000433 soc_widget_lock(w);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800434 ret = soc_widget_read(w, reg, &old);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000435 if (ret < 0) {
436 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100437 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000438 }
Mark Brown8a713da2011-12-03 12:33:55 +0000439
Mark Brown8a713da2011-12-03 12:33:55 +0000440 new = (old & ~mask) | (value & mask);
441 change = old != new;
442 if (change) {
443 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000444 if (ret < 0) {
445 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000446 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000447 }
Mark Brown8a713da2011-12-03 12:33:55 +0000448 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000449 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100450 }
451
452 return change;
453}
454
Mark Brown452c5ea2009-05-17 21:41:23 +0100455/**
456 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800457 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100458 * @level: level to configure
459 *
460 * Configure the bias (power) levels for the SoC audio device.
461 *
462 * Returns 0 for success else error.
463 */
Mark Browned5a4c42011-02-18 11:12:42 -0800464static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200465 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100466{
Mark Browned5a4c42011-02-18 11:12:42 -0800467 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100468 int ret = 0;
469
Mark Brown84e90932010-11-04 00:07:02 -0400470 trace_snd_soc_bias_level_start(card, level);
471
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000472 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100473 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100474 if (ret != 0)
475 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100476
Mark Browncc4c6702011-06-06 19:03:34 +0100477 if (dapm->codec) {
478 if (dapm->codec->driver->set_bias_level)
479 ret = dapm->codec->driver->set_bias_level(dapm->codec,
480 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100481 else
482 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100483 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100484 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100485 }
Liam Girdwood41231282012-07-06 16:56:16 +0100486
Mark Brown171ec6b2011-06-06 18:15:19 +0100487 if (ret != 0)
488 goto out;
489
490 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100491 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100492out:
Mark Brown84e90932010-11-04 00:07:02 -0400493 trace_snd_soc_bias_level_done(card, level);
494
Mark Brown452c5ea2009-05-17 21:41:23 +0100495 return ret;
496}
497
Mark Brown74b8f952009-06-06 11:26:15 +0100498/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200499static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200500 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
501 struct snd_soc_dapm_path *path, const char *control_name,
502 const struct snd_kcontrol_new *kcontrol)
503{
504 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100505 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200506 int i;
507
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100508 if (e->reg != SND_SOC_NOPM) {
509 soc_widget_read(dest, e->reg, &val);
510 val = (val >> e->shift_l) & e->mask;
511 item = snd_soc_enum_val_to_item(e, val);
512 } else {
513 /* since a virtual mux has no backing registers to
514 * decide which path to connect, it will try to match
515 * with the first enumeration. This is to ensure
516 * that the default mux choice (the first) will be
517 * correctly powered up during initialization.
518 */
519 item = 0;
520 }
521
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100522 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200523 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200524 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200525 list_add(&path->list_sink, &dest->sources);
526 list_add(&path->list_source, &src->sinks);
527 path->name = (char*)e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100528 if (i == item)
529 path->connect = 1;
530 else
531 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200532 return 0;
533 }
534 }
535
536 return -ENODEV;
537}
538
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100539/* set up initial codec paths */
540static void dapm_set_mixer_path_status(struct snd_soc_dapm_widget *w,
541 struct snd_soc_dapm_path *p, int i)
542{
543 struct soc_mixer_control *mc = (struct soc_mixer_control *)
544 w->kcontrol_news[i].private_value;
545 unsigned int reg = mc->reg;
546 unsigned int shift = mc->shift;
547 unsigned int max = mc->max;
548 unsigned int mask = (1 << fls(max)) - 1;
549 unsigned int invert = mc->invert;
550 unsigned int val;
551
552 if (reg != SND_SOC_NOPM) {
553 soc_widget_read(w, reg, &val);
554 val = (val >> shift) & mask;
555 if (invert)
556 val = max - val;
557 p->connect = !!val;
558 } else {
559 p->connect = 0;
560 }
561}
562
Mark Brown74b8f952009-06-06 11:26:15 +0100563/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200564static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200565 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
566 struct snd_soc_dapm_path *path, const char *control_name)
567{
568 int i;
569
570 /* search for mixer kcontrol */
571 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600572 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200573 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200574 list_add(&path->list_sink, &dest->sources);
575 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600576 path->name = dest->kcontrol_news[i].name;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100577 dapm_set_mixer_path_status(dest, path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200578 return 0;
579 }
580 }
581 return -ENODEV;
582}
583
Stephen Warrenaf468002011-04-28 17:38:01 -0600584static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600585 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600586 const struct snd_kcontrol_new *kcontrol_new,
587 struct snd_kcontrol **kcontrol)
588{
589 struct snd_soc_dapm_widget *w;
590 int i;
591
592 *kcontrol = NULL;
593
594 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600595 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
596 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600597 for (i = 0; i < w->num_kcontrols; i++) {
598 if (&w->kcontrol_news[i] == kcontrol_new) {
599 if (w->kcontrols)
600 *kcontrol = w->kcontrols[i];
601 return 1;
602 }
603 }
604 }
605
606 return 0;
607}
608
Stephen Warren85762e72013-03-29 15:40:10 -0600609/*
610 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
611 * create it. Either way, add the widget into the control's widget list
612 */
613static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100614 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200615{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200616 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000617 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000618 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600619 size_t prefix_len;
620 int shared;
621 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600622 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600623 char *long_name;
624 const char *name;
625 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000626
627 if (dapm->codec)
628 prefix = dapm->codec->name_prefix;
629 else
630 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200631
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000632 if (prefix)
633 prefix_len = strlen(prefix) + 1;
634 else
635 prefix_len = 0;
636
Stephen Warren85762e72013-03-29 15:40:10 -0600637 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
638 &kcontrol);
639
Stephen Warren85762e72013-03-29 15:40:10 -0600640 if (!kcontrol) {
641 if (shared) {
642 wname_in_long_name = false;
643 kcname_in_long_name = true;
644 } else {
645 switch (w->id) {
646 case snd_soc_dapm_switch:
647 case snd_soc_dapm_mixer:
648 wname_in_long_name = true;
649 kcname_in_long_name = true;
650 break;
651 case snd_soc_dapm_mixer_named_ctl:
652 wname_in_long_name = false;
653 kcname_in_long_name = true;
654 break;
655 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600656 wname_in_long_name = true;
657 kcname_in_long_name = false;
658 break;
659 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600660 return -EINVAL;
661 }
662 }
663
664 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600665 /*
666 * The control will get a prefix from the control
667 * creation process but we're also using the same
668 * prefix for widgets so cut the prefix off the
669 * front of the widget name.
670 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200671 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600672 w->name + prefix_len,
673 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200674 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200675 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600676
677 name = long_name;
678 } else if (wname_in_long_name) {
679 long_name = NULL;
680 name = w->name + prefix_len;
681 } else {
682 long_name = NULL;
683 name = w->kcontrol_news[kci].name;
684 }
685
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200686 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600687 prefix);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200688 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200689 if (!kcontrol)
690 return -ENOMEM;
691 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200692
693 ret = dapm_kcontrol_data_alloc(w, kcontrol);
694 if (ret) {
695 snd_ctl_free_one(kcontrol);
696 return ret;
697 }
698
Stephen Warren85762e72013-03-29 15:40:10 -0600699 ret = snd_ctl_add(card, kcontrol);
700 if (ret < 0) {
701 dev_err(dapm->dev,
702 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
703 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600704 return ret;
705 }
Stephen Warren85762e72013-03-29 15:40:10 -0600706 }
707
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200708 ret = dapm_kcontrol_add_widget(kcontrol, w);
709 if (ret)
710 return ret;
711
Stephen Warren85762e72013-03-29 15:40:10 -0600712 w->kcontrols[kci] = kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600713
714 return 0;
715}
716
717/* create new dapm mixer control */
718static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
719{
720 int i, ret;
721 struct snd_soc_dapm_path *path;
722
Richard Purdie2b97eab2006-10-06 18:32:18 +0200723 /* add kcontrol */
724 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200725 /* match name */
726 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200727 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600728 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200729 continue;
730
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200731 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200732 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200733 continue;
734 }
735
Mark Brown946d92a2013-08-12 23:28:42 +0100736 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600737 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200738 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100739
740 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200741 }
742 }
Stephen Warren85762e72013-03-29 15:40:10 -0600743
744 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200745}
746
747/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200748static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200749{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200750 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600751 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600752 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200753
Stephen Warrenaf468002011-04-28 17:38:01 -0600754 if (w->num_kcontrols != 1) {
755 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000756 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600757 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200758 return -EINVAL;
759 }
760
Lars-Peter Clausenfe581392013-08-01 18:30:38 +0200761 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600762 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
763 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600764 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200765
Mark Brown946d92a2013-08-12 23:28:42 +0100766 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600767 if (ret < 0)
768 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600769
Richard Purdie2b97eab2006-10-06 18:32:18 +0200770 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200771 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200772
Stephen Warrenaf468002011-04-28 17:38:01 -0600773 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200774}
775
776/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200777static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200778{
Mark Browna6c65732010-03-03 17:45:21 +0000779 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200780 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000781 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200782
Mark Browna6c65732010-03-03 17:45:21 +0000783 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200784}
785
786/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100787static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
788 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200789{
790 struct snd_soc_dapm_path *p;
791
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100792 list_for_each_entry(p, sink, list_source) {
793 if (p->walked) {
794 p->walked = 0;
795 dapm_clear_walk_output(dapm, &p->sink->sinks);
796 }
797 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200798}
799
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100800static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
801 struct list_head *source)
802{
803 struct snd_soc_dapm_path *p;
804
805 list_for_each_entry(p, source, list_sink) {
806 if (p->walked) {
807 p->walked = 0;
808 dapm_clear_walk_input(dapm, &p->source->sources);
809 }
810 }
811}
812
813
Mark Brown9949788b2010-05-07 20:24:05 +0100814/* We implement power down on suspend by checking the power state of
815 * the ALSA card - when we are suspending the ALSA state for the card
816 * is set to D3.
817 */
818static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
819{
Mark Brown12ea2c72011-03-02 18:17:32 +0000820 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100821
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000822 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100823 case SNDRV_CTL_POWER_D3hot:
824 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100825 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000826 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200827 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100828 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100829 default:
830 return 1;
831 }
832}
833
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100834/* add widget to list if it's not already in the list */
835static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
836 struct snd_soc_dapm_widget *w)
837{
838 struct snd_soc_dapm_widget_list *wlist;
839 int wlistsize, wlistentries, i;
840
841 if (*list == NULL)
842 return -EINVAL;
843
844 wlist = *list;
845
846 /* is this widget already in the list */
847 for (i = 0; i < wlist->num_widgets; i++) {
848 if (wlist->widgets[i] == w)
849 return 0;
850 }
851
852 /* allocate some new space */
853 wlistentries = wlist->num_widgets + 1;
854 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
855 wlistentries * sizeof(struct snd_soc_dapm_widget *);
856 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
857 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000858 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100859 w->name);
860 return -ENOMEM;
861 }
862 wlist = *list;
863
864 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000865 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100866 w->name, wlist->num_widgets);
867
868 wlist->widgets[wlist->num_widgets] = w;
869 wlist->num_widgets++;
870 return 1;
871}
872
Richard Purdie2b97eab2006-10-06 18:32:18 +0200873/*
874 * Recursively check for a completed path to an active or physically connected
875 * output widget. Returns number of complete paths.
876 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100877static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
878 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200879{
880 struct snd_soc_dapm_path *path;
881 int con = 0;
882
Mark Brown024dc072011-10-09 11:52:05 +0100883 if (widget->outputs >= 0)
884 return widget->outputs;
885
Mark Brownde02d072011-09-20 21:43:24 +0100886 DAPM_UPDATE_STAT(widget, path_checks);
887
Mark Brown62ea8742012-01-21 21:14:48 +0000888 switch (widget->id) {
889 case snd_soc_dapm_supply:
890 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200891 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200892 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100893 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000894 default:
895 break;
896 }
Mark Brown246d0a12009-04-22 18:24:55 +0100897
Mark Brown010ff262009-08-17 17:39:22 +0100898 switch (widget->id) {
899 case snd_soc_dapm_adc:
900 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100901 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100902 if (widget->active) {
903 widget->outputs = snd_soc_dapm_suspend_check(widget);
904 return widget->outputs;
905 }
Mark Brown010ff262009-08-17 17:39:22 +0100906 default:
907 break;
908 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200909
910 if (widget->connected) {
911 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100912 if (widget->id == snd_soc_dapm_output && !widget->ext) {
913 widget->outputs = snd_soc_dapm_suspend_check(widget);
914 return widget->outputs;
915 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200916
917 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100918 if (widget->id == snd_soc_dapm_hp ||
919 widget->id == snd_soc_dapm_spk ||
920 (widget->id == snd_soc_dapm_line &&
921 !list_empty(&widget->sources))) {
922 widget->outputs = snd_soc_dapm_suspend_check(widget);
923 return widget->outputs;
924 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200925 }
926
927 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100928 DAPM_UPDATE_STAT(widget, neighbour_checks);
929
Mark Brownbf3a9e12011-06-13 16:42:29 +0100930 if (path->weak)
931 continue;
932
Mark Brown8af294b2013-02-22 17:48:15 +0000933 if (path->walking)
934 return 1;
935
Richard Purdie2b97eab2006-10-06 18:32:18 +0200936 if (path->walked)
937 continue;
938
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100939 trace_snd_soc_dapm_output_path(widget, path);
940
Richard Purdie2b97eab2006-10-06 18:32:18 +0200941 if (path->sink && path->connect) {
942 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000943 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100944
945 /* do we need to add this widget to the list ? */
946 if (list) {
947 int err;
948 err = dapm_list_add_widget(list, path->sink);
949 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000950 dev_err(widget->dapm->dev,
951 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100952 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000953 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100954 return con;
955 }
956 }
957
958 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000959
960 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200961 }
962 }
963
Mark Brown024dc072011-10-09 11:52:05 +0100964 widget->outputs = con;
965
Richard Purdie2b97eab2006-10-06 18:32:18 +0200966 return con;
967}
968
969/*
970 * Recursively check for a completed path to an active or physically connected
971 * input widget. Returns number of complete paths.
972 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100973static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
974 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200975{
976 struct snd_soc_dapm_path *path;
977 int con = 0;
978
Mark Brown024dc072011-10-09 11:52:05 +0100979 if (widget->inputs >= 0)
980 return widget->inputs;
981
Mark Brownde02d072011-09-20 21:43:24 +0100982 DAPM_UPDATE_STAT(widget, path_checks);
983
Mark Brown62ea8742012-01-21 21:14:48 +0000984 switch (widget->id) {
985 case snd_soc_dapm_supply:
986 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200987 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200988 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100989 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000990 default:
991 break;
992 }
Mark Brown246d0a12009-04-22 18:24:55 +0100993
Richard Purdie2b97eab2006-10-06 18:32:18 +0200994 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100995 switch (widget->id) {
996 case snd_soc_dapm_dac:
997 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +0100998 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +0100999 if (widget->active) {
1000 widget->inputs = snd_soc_dapm_suspend_check(widget);
1001 return widget->inputs;
1002 }
Mark Brown010ff262009-08-17 17:39:22 +01001003 default:
1004 break;
1005 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001006
1007 if (widget->connected) {
1008 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +01001009 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1010 widget->inputs = snd_soc_dapm_suspend_check(widget);
1011 return widget->inputs;
1012 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001013
1014 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +01001015 if (widget->id == snd_soc_dapm_vmid) {
1016 widget->inputs = snd_soc_dapm_suspend_check(widget);
1017 return widget->inputs;
1018 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001019
1020 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +03001021 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +01001022 (widget->id == snd_soc_dapm_line &&
1023 !list_empty(&widget->sinks))) {
1024 widget->inputs = snd_soc_dapm_suspend_check(widget);
1025 return widget->inputs;
1026 }
1027
Mark Brown1ab97c82011-11-27 16:21:51 +00001028 /* signal generator */
1029 if (widget->id == snd_soc_dapm_siggen) {
1030 widget->inputs = snd_soc_dapm_suspend_check(widget);
1031 return widget->inputs;
1032 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001033 }
1034
1035 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +01001036 DAPM_UPDATE_STAT(widget, neighbour_checks);
1037
Mark Brownbf3a9e12011-06-13 16:42:29 +01001038 if (path->weak)
1039 continue;
1040
Mark Brown8af294b2013-02-22 17:48:15 +00001041 if (path->walking)
1042 return 1;
1043
Richard Purdie2b97eab2006-10-06 18:32:18 +02001044 if (path->walked)
1045 continue;
1046
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001047 trace_snd_soc_dapm_input_path(widget, path);
1048
Richard Purdie2b97eab2006-10-06 18:32:18 +02001049 if (path->source && path->connect) {
1050 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001051 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001052
1053 /* do we need to add this widget to the list ? */
1054 if (list) {
1055 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001056 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001057 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001058 dev_err(widget->dapm->dev,
1059 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001060 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001061 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001062 return con;
1063 }
1064 }
1065
1066 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001067
1068 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001069 }
1070 }
1071
Mark Brown024dc072011-10-09 11:52:05 +01001072 widget->inputs = con;
1073
Richard Purdie2b97eab2006-10-06 18:32:18 +02001074 return con;
1075}
1076
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001077/**
1078 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1079 * @dai: the soc DAI.
1080 * @stream: stream direction.
1081 * @list: list of active widgets for this stream.
1082 *
1083 * Queries DAPM graph as to whether an valid audio stream path exists for
1084 * the initial stream specified by name. This takes into account
1085 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1086 *
1087 * Returns the number of valid paths or negative error.
1088 */
1089int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1090 struct snd_soc_dapm_widget_list **list)
1091{
1092 struct snd_soc_card *card = dai->card;
1093 int paths;
1094
1095 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1096 dapm_reset(card);
1097
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001098 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001099 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001100 dapm_clear_walk_output(&card->dapm,
1101 &dai->playback_widget->sinks);
1102 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001103 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001104 dapm_clear_walk_input(&card->dapm,
1105 &dai->capture_widget->sources);
1106 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001107
1108 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001109 mutex_unlock(&card->dapm_mutex);
1110
1111 return paths;
1112}
1113
Richard Purdie2b97eab2006-10-06 18:32:18 +02001114/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001115 * Handler for generic register modifier widget.
1116 */
1117int dapm_reg_event(struct snd_soc_dapm_widget *w,
1118 struct snd_kcontrol *kcontrol, int event)
1119{
1120 unsigned int val;
1121
1122 if (SND_SOC_DAPM_EVENT_ON(event))
1123 val = w->on_val;
1124 else
1125 val = w->off_val;
1126
Liam Girdwood49575fb52012-03-06 18:16:19 +00001127 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001128 w->mask << w->shift, val << w->shift);
1129
1130 return 0;
1131}
Mark Brown11589412008-07-29 11:42:23 +01001132EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001133
Mark Brown62ea8742012-01-21 21:14:48 +00001134/*
1135 * Handler for regulator supply widget.
1136 */
1137int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1138 struct snd_kcontrol *kcontrol, int event)
1139{
Mark Brownc05b84d2012-09-07 12:57:11 +08001140 int ret;
1141
Mark Browneb270e92013-10-09 13:52:52 +01001142 soc_dapm_async_complete(w->dapm);
1143
Mark Brownc05b84d2012-09-07 12:57:11 +08001144 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001145 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001146 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001147 if (ret != 0)
1148 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001149 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001150 w->name, ret);
1151 }
1152
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001153 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001154 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001155 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001156 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001157 if (ret != 0)
1158 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001159 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001160 w->name, ret);
1161 }
1162
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001163 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001164 }
Mark Brown62ea8742012-01-21 21:14:48 +00001165}
1166EXPORT_SYMBOL_GPL(dapm_regulator_event);
1167
Ola Liljad7e7eb92012-05-24 15:26:25 +02001168/*
1169 * Handler for clock supply widget.
1170 */
1171int dapm_clock_event(struct snd_soc_dapm_widget *w,
1172 struct snd_kcontrol *kcontrol, int event)
1173{
1174 if (!w->clk)
1175 return -EIO;
1176
Mark Browneb270e92013-10-09 13:52:52 +01001177 soc_dapm_async_complete(w->dapm);
1178
Mark Brownec029952012-06-04 08:16:20 +01001179#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001180 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001181 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001182 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001183 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001184 return 0;
1185 }
Mark Brownec029952012-06-04 08:16:20 +01001186#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001187 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001188}
1189EXPORT_SYMBOL_GPL(dapm_clock_event);
1190
Mark Brownd805002b2011-09-28 18:28:23 +01001191static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1192{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001193 if (w->power_checked)
1194 return w->new_power;
1195
Mark Brownd805002b2011-09-28 18:28:23 +01001196 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001197 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +01001198 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001199 w->new_power = w->power_check(w);
1200
1201 w->power_checked = true;
1202
1203 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +01001204}
1205
Mark Browncd0f2d42009-04-20 16:56:59 +01001206/* Generic check to see if a widget should be powered.
1207 */
1208static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1209{
1210 int in, out;
1211
Mark Brownde02d072011-09-20 21:43:24 +01001212 DAPM_UPDATE_STAT(w, power_checks);
1213
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001214 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001215 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001216 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001217 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001218 return out != 0 && in != 0;
1219}
1220
Mark Brown6ea31b92009-04-20 17:15:41 +01001221/* Check to see if an ADC has power */
1222static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1223{
1224 int in;
1225
Mark Brownde02d072011-09-20 21:43:24 +01001226 DAPM_UPDATE_STAT(w, power_checks);
1227
Mark Brown6ea31b92009-04-20 17:15:41 +01001228 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001229 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001230 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001231 return in != 0;
1232 } else {
1233 return dapm_generic_check_power(w);
1234 }
1235}
1236
1237/* Check to see if a DAC has power */
1238static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1239{
1240 int out;
1241
Mark Brownde02d072011-09-20 21:43:24 +01001242 DAPM_UPDATE_STAT(w, power_checks);
1243
Mark Brown6ea31b92009-04-20 17:15:41 +01001244 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001245 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001246 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001247 return out != 0;
1248 } else {
1249 return dapm_generic_check_power(w);
1250 }
1251}
1252
Mark Brown246d0a12009-04-22 18:24:55 +01001253/* Check to see if a power supply is needed */
1254static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1255{
1256 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001257
Mark Brownde02d072011-09-20 21:43:24 +01001258 DAPM_UPDATE_STAT(w, power_checks);
1259
Mark Brown246d0a12009-04-22 18:24:55 +01001260 /* Check if one of our outputs is connected */
1261 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001262 DAPM_UPDATE_STAT(w, neighbour_checks);
1263
Mark Brownbf3a9e12011-06-13 16:42:29 +01001264 if (path->weak)
1265 continue;
1266
Mark Brown215edda2009-09-08 18:59:05 +01001267 if (path->connected &&
1268 !path->connected(path->source, path->sink))
1269 continue;
1270
Mark Brown30173582011-02-11 11:42:19 +00001271 if (!path->sink)
1272 continue;
1273
Mark Brownf68d7e12011-10-04 22:57:50 +01001274 if (dapm_widget_power_check(path->sink))
1275 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001276 }
1277
Mark Brownf68d7e12011-10-04 22:57:50 +01001278 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001279}
1280
Mark Brown35c64bc2011-09-28 18:23:53 +01001281static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1282{
1283 return 1;
1284}
1285
Mark Brown38357ab2009-06-06 19:03:23 +01001286static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1287 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001288 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001289{
Mark Brown828a8422011-01-15 13:14:30 +00001290 int *sort;
1291
1292 if (power_up)
1293 sort = dapm_up_seq;
1294 else
1295 sort = dapm_down_seq;
1296
Mark Brown38357ab2009-06-06 19:03:23 +01001297 if (sort[a->id] != sort[b->id])
1298 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001299 if (a->subseq != b->subseq) {
1300 if (power_up)
1301 return a->subseq - b->subseq;
1302 else
1303 return b->subseq - a->subseq;
1304 }
Mark Brownb22ead22009-06-07 12:51:26 +01001305 if (a->reg != b->reg)
1306 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001307 if (a->dapm != b->dapm)
1308 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001309
Mark Brown38357ab2009-06-06 19:03:23 +01001310 return 0;
1311}
Mark Brown42aa3412009-03-01 19:21:10 +00001312
Mark Brown38357ab2009-06-06 19:03:23 +01001313/* Insert a widget in order into a DAPM power sequence. */
1314static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1315 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001316 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001317{
1318 struct snd_soc_dapm_widget *w;
1319
1320 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001321 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001322 list_add_tail(&new_widget->power_list, &w->power_list);
1323 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001324 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001325
Mark Brown38357ab2009-06-06 19:03:23 +01001326 list_add_tail(&new_widget->power_list, list);
1327}
Mark Brown42aa3412009-03-01 19:21:10 +00001328
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001329static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001330 struct snd_soc_dapm_widget *w, int event)
1331{
Mark Brown68f89ad2010-11-03 23:51:49 -04001332 const char *ev_name;
1333 int power, ret;
1334
1335 switch (event) {
1336 case SND_SOC_DAPM_PRE_PMU:
1337 ev_name = "PRE_PMU";
1338 power = 1;
1339 break;
1340 case SND_SOC_DAPM_POST_PMU:
1341 ev_name = "POST_PMU";
1342 power = 1;
1343 break;
1344 case SND_SOC_DAPM_PRE_PMD:
1345 ev_name = "PRE_PMD";
1346 power = 0;
1347 break;
1348 case SND_SOC_DAPM_POST_PMD:
1349 ev_name = "POST_PMD";
1350 power = 0;
1351 break;
Mark Brown80114122013-02-25 15:14:19 +00001352 case SND_SOC_DAPM_WILL_PMU:
1353 ev_name = "WILL_PMU";
1354 power = 1;
1355 break;
1356 case SND_SOC_DAPM_WILL_PMD:
1357 ev_name = "WILL_PMD";
1358 power = 0;
1359 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001360 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001361 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001362 return;
1363 }
1364
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001365 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001366 return;
1367
1368 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001369 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001370 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001371 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001372 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001373 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001374 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001375 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001376 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001377 ev_name, w->name, ret);
1378 }
1379}
1380
Mark Brownb22ead22009-06-07 12:51:26 +01001381/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001382static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001383 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001384{
Mark Brown68f89ad2010-11-03 23:51:49 -04001385 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001386 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001387 unsigned int value = 0;
1388 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001389
1390 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1391 power_list)->reg;
1392
1393 list_for_each_entry(w, pending, power_list) {
Takashi Iwaibf4edea2013-11-07 18:38:47 +01001394 WARN_ON(reg != w->reg);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001395 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001396
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001397 mask |= w->mask << w->shift;
1398 if (w->power)
1399 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001400 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001401 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001402
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001403 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001404 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1405 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001406
Mark Brown68f89ad2010-11-03 23:51:49 -04001407 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001408 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1409 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001410 }
1411
Mark Brown81628102009-06-07 13:21:24 +01001412 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001413 /* Any widget will do, they should all be updating the
1414 * same register.
1415 */
1416 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1417 power_list);
1418
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001419 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001420 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001421 value, mask, reg, card->pop_time);
1422 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001423 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001424 }
1425
1426 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001427 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1428 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001429 }
Mark Brown42aa3412009-03-01 19:21:10 +00001430}
1431
Mark Brownb22ead22009-06-07 12:51:26 +01001432/* Apply a DAPM power sequence.
1433 *
1434 * We walk over a pre-sorted list of widgets to apply power to. In
1435 * order to minimise the number of writes to the device required
1436 * multiple widgets will be updated in a single write where possible.
1437 * Currently anything that requires more than a single write is not
1438 * handled.
1439 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001440static void dapm_seq_run(struct snd_soc_card *card,
1441 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001442{
1443 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001444 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001445 LIST_HEAD(pending);
1446 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001447 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001448 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001449 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001450 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001451 int *sort;
1452
1453 if (power_up)
1454 sort = dapm_up_seq;
1455 else
1456 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001457
Mark Brownb22ead22009-06-07 12:51:26 +01001458 list_for_each_entry_safe(w, n, list, power_list) {
1459 ret = 0;
1460
1461 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001462 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001463 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001464 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001465 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001466
Mark Brown474b62d2011-01-18 16:14:44 +00001467 if (cur_dapm && cur_dapm->seq_notifier) {
1468 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1469 if (sort[i] == cur_sort)
1470 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001471 i,
1472 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001473 }
1474
Mark Browneb270e92013-10-09 13:52:52 +01001475 if (cur_dapm && w->dapm != cur_dapm)
1476 soc_dapm_async_complete(cur_dapm);
1477
Mark Brownb22ead22009-06-07 12:51:26 +01001478 INIT_LIST_HEAD(&pending);
1479 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001480 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001481 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001482 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001483 }
1484
Mark Brown163cac02009-06-07 10:12:52 +01001485 switch (w->id) {
1486 case snd_soc_dapm_pre:
1487 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001488 list_for_each_entry_safe_continue(w, n, list,
1489 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001490
Mark Brownb22ead22009-06-07 12:51:26 +01001491 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001492 ret = w->event(w,
1493 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001494 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001495 ret = w->event(w,
1496 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001497 break;
1498
1499 case snd_soc_dapm_post:
1500 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001501 list_for_each_entry_safe_continue(w, n, list,
1502 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001503
Mark Brownb22ead22009-06-07 12:51:26 +01001504 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001505 ret = w->event(w,
1506 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001507 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001508 ret = w->event(w,
1509 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001510 break;
1511
Mark Brown163cac02009-06-07 10:12:52 +01001512 default:
Mark Brown81628102009-06-07 13:21:24 +01001513 /* Queue it up for application */
1514 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001515 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001516 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001517 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001518 list_move(&w->power_list, &pending);
1519 break;
Mark Brown163cac02009-06-07 10:12:52 +01001520 }
Mark Brownb22ead22009-06-07 12:51:26 +01001521
1522 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001523 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001524 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001525 }
Mark Brownb22ead22009-06-07 12:51:26 +01001526
1527 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001528 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001529
1530 if (cur_dapm && cur_dapm->seq_notifier) {
1531 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1532 if (sort[i] == cur_sort)
1533 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001534 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001535 }
Mark Browneb270e92013-10-09 13:52:52 +01001536
1537 list_for_each_entry(d, &card->dapm_list, list) {
1538 soc_dapm_async_complete(d);
1539 }
Mark Brown163cac02009-06-07 10:12:52 +01001540}
1541
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001542static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001543{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001544 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001545 struct snd_soc_dapm_widget_list *wlist;
1546 struct snd_soc_dapm_widget *w = NULL;
1547 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001548 int ret;
1549
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001550 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001551 return;
1552
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001553 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001554
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001555 for (wi = 0; wi < wlist->num_widgets; wi++) {
1556 w = wlist->widgets[wi];
1557
1558 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1559 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1560 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001561 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001562 w->name, ret);
1563 }
Mark Brown97404f22010-12-14 16:13:57 +00001564 }
1565
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001566 if (!w)
1567 return;
1568
Liam Girdwood49575fb52012-03-06 18:16:19 +00001569 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001570 update->val);
1571 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001572 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001573 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001574
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001575 for (wi = 0; wi < wlist->num_widgets; wi++) {
1576 w = wlist->widgets[wi];
1577
1578 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1579 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1580 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001581 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001582 w->name, ret);
1583 }
Mark Brown97404f22010-12-14 16:13:57 +00001584 }
1585}
1586
Mark Brown9d0624a2011-02-18 11:49:43 -08001587/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1588 * they're changing state.
1589 */
1590static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1591{
1592 struct snd_soc_dapm_context *d = data;
1593 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001594
Mark Brown56fba412011-06-04 11:25:10 +01001595 /* If we're off and we're not supposed to be go into STANDBY */
1596 if (d->bias_level == SND_SOC_BIAS_OFF &&
1597 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001598 if (d->dev)
1599 pm_runtime_get_sync(d->dev);
1600
Mark Brown9d0624a2011-02-18 11:49:43 -08001601 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1602 if (ret != 0)
1603 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001604 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001605 }
1606
Mark Brown56fba412011-06-04 11:25:10 +01001607 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1608 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001609 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1610 if (ret != 0)
1611 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001612 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001613 }
1614}
1615
1616/* Async callback run prior to DAPM sequences - brings to their final
1617 * state.
1618 */
1619static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1620{
1621 struct snd_soc_dapm_context *d = data;
1622 int ret;
1623
1624 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001625 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1626 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1627 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001628 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1629 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001630 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001631 ret);
1632 }
1633
1634 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001635 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1636 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001637 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1638 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001639 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1640 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001641
1642 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001643 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001644 }
1645
1646 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001647 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1648 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001649 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1650 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001651 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001652 ret);
1653 }
1654}
Mark Brown97404f22010-12-14 16:13:57 +00001655
Mark Brownfe4fda52011-10-03 22:36:57 +01001656static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1657 bool power, bool connect)
1658{
1659 /* If a connection is being made or broken then that update
1660 * will have marked the peer dirty, otherwise the widgets are
1661 * not connected and this update has no impact. */
1662 if (!connect)
1663 return;
1664
1665 /* If the peer is already in the state we're moving to then we
1666 * won't have an impact on it. */
1667 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001668 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001669}
1670
Mark Brown05623c42011-09-28 17:02:31 +01001671static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1672 struct list_head *up_list,
1673 struct list_head *down_list)
1674{
Mark Browndb432b42011-10-03 21:06:40 +01001675 struct snd_soc_dapm_path *path;
1676
Mark Brown05623c42011-09-28 17:02:31 +01001677 if (w->power == power)
1678 return;
1679
1680 trace_snd_soc_dapm_widget_power(w, power);
1681
Mark Browndb432b42011-10-03 21:06:40 +01001682 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001683 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001684 */
1685 list_for_each_entry(path, &w->sources, list_sink) {
1686 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001687 dapm_widget_set_peer_power(path->source, power,
1688 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001689 }
1690 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001691 switch (w->id) {
1692 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001693 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001694 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001695 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001696 /* Supplies can't affect their outputs, only their inputs */
1697 break;
1698 default:
1699 list_for_each_entry(path, &w->sinks, list_source) {
1700 if (path->sink) {
1701 dapm_widget_set_peer_power(path->sink, power,
1702 path->connect);
1703 }
Mark Browndb432b42011-10-03 21:06:40 +01001704 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001705 break;
Mark Browndb432b42011-10-03 21:06:40 +01001706 }
1707
Mark Brown05623c42011-09-28 17:02:31 +01001708 if (power)
1709 dapm_seq_insert(w, up_list, true);
1710 else
1711 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001712}
1713
Mark Brown7c81beb2011-09-20 22:22:32 +01001714static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1715 struct list_head *up_list,
1716 struct list_head *down_list)
1717{
Mark Brown7c81beb2011-09-20 22:22:32 +01001718 int power;
1719
1720 switch (w->id) {
1721 case snd_soc_dapm_pre:
1722 dapm_seq_insert(w, down_list, false);
1723 break;
1724 case snd_soc_dapm_post:
1725 dapm_seq_insert(w, up_list, true);
1726 break;
1727
1728 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001729 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001730
Mark Brown05623c42011-09-28 17:02:31 +01001731 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001732 break;
1733 }
1734}
1735
Mark Brown42aa3412009-03-01 19:21:10 +00001736/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001737 * Scan each dapm widget for complete audio path.
1738 * A complete path is a route that has valid endpoints i.e.:-
1739 *
1740 * o DAC to output pin.
1741 * o Input Pin to ADC.
1742 * o Input pin to Output pin (bypass, sidetone)
1743 * o DAC to ADC (loopback).
1744 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001745static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001746{
1747 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001748 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001749 LIST_HEAD(up_list);
1750 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001751 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001752 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001753
Mark Brown84e90932010-11-04 00:07:02 -04001754 trace_snd_soc_dapm_start(card);
1755
Mark Brown56fba412011-06-04 11:25:10 +01001756 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001757 if (d->idle_bias_off)
1758 d->target_bias_level = SND_SOC_BIAS_OFF;
1759 else
1760 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001761 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001762
Liam Girdwood6c120e12012-02-15 15:15:34 +00001763 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001764
Mark Brown6d3ddc82009-05-16 17:47:29 +01001765 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001766 * lists indicating if they should be powered up or down. We
1767 * only check widgets that have been flagged as dirty but note
1768 * that new widgets may be added to the dirty list while we
1769 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001770 */
Mark Browndb432b42011-10-03 21:06:40 +01001771 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001772 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001773 }
1774
Mark Brownf9de6d72011-09-28 17:19:47 +01001775 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001776 switch (w->id) {
1777 case snd_soc_dapm_pre:
1778 case snd_soc_dapm_post:
1779 /* These widgets always need to be powered */
1780 break;
1781 default:
1782 list_del_init(&w->dirty);
1783 break;
1784 }
Mark Browndb432b42011-10-03 21:06:40 +01001785
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001786 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001787 d = w->dapm;
1788
1789 /* Supplies and micbiases only bring the
1790 * context up to STANDBY as unless something
1791 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001792 * generally don't require full power. Signal
1793 * generators are virtual pins and have no
1794 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001795 */
1796 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001797 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001798 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001799 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001800 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001801 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001802 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001803 case snd_soc_dapm_micbias:
1804 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1805 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1806 break;
1807 default:
1808 d->target_bias_level = SND_SOC_BIAS_ON;
1809 break;
1810 }
1811 }
1812
1813 }
1814
Mark Brown85a843c2011-09-21 21:29:47 +01001815 /* Force all contexts in the card to the same bias state if
1816 * they're not ground referenced.
1817 */
Mark Brown56fba412011-06-04 11:25:10 +01001818 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001819 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001820 if (d->target_bias_level > bias)
1821 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001822 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001823 if (!d->idle_bias_off)
1824 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001825
Mark Brownde02d072011-09-20 21:43:24 +01001826 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001827
Mark Brown9d0624a2011-02-18 11:49:43 -08001828 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001829 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001830 async_schedule_domain(dapm_pre_sequence_async, d,
1831 &async_domain);
1832 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001833
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001834 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001835 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001836 }
1837
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001838 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001839 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001840 }
1841
Mark Brown6d3ddc82009-05-16 17:47:29 +01001842 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001843 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001844
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001845 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001846
Mark Brown6d3ddc82009-05-16 17:47:29 +01001847 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001848 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001849
Mark Brown9d0624a2011-02-18 11:49:43 -08001850 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001851 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001852 async_schedule_domain(dapm_post_sequence_async, d,
1853 &async_domain);
1854 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001855
Liam Girdwood8078d872012-02-15 15:15:35 +00001856 /* do we need to notify any clients that DAPM event is complete */
1857 list_for_each_entry(d, &card->dapm_list, list) {
1858 if (d->stream_event)
1859 d->stream_event(d, event);
1860 }
1861
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001862 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001863 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001864 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001865
Mark Brown84e90932010-11-04 00:07:02 -04001866 trace_snd_soc_dapm_done(card);
1867
Mark Brown42aa3412009-03-01 19:21:10 +00001868 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001869}
1870
Mark Brown79fb9382009-08-21 16:38:13 +01001871#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001872static ssize_t dapm_widget_power_read_file(struct file *file,
1873 char __user *user_buf,
1874 size_t count, loff_t *ppos)
1875{
1876 struct snd_soc_dapm_widget *w = file->private_data;
1877 char *buf;
1878 int in, out;
1879 ssize_t ret;
1880 struct snd_soc_dapm_path *p = NULL;
1881
1882 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1883 if (!buf)
1884 return -ENOMEM;
1885
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001886 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001887 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001888 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001889 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001890
Mark Brownf13ebad2012-03-03 18:01:01 +00001891 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1892 w->name, w->power ? "On" : "Off",
1893 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001894
Mark Brownd033c362009-12-04 15:25:56 +00001895 if (w->reg >= 0)
1896 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001897 " - R%d(0x%x) mask 0x%x",
1898 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001899
1900 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1901
Mark Brown3eef08b2009-09-14 16:49:00 +01001902 if (w->sname)
1903 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1904 w->sname,
1905 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001906
1907 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001908 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001909 continue;
1910
Mark Brown79fb9382009-08-21 16:38:13 +01001911 if (p->connect)
1912 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001913 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001914 p->name ? p->name : "static",
1915 p->source->name);
1916 }
1917 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001918 if (p->connected && !p->connected(w, p->sink))
1919 continue;
1920
Mark Brown79fb9382009-08-21 16:38:13 +01001921 if (p->connect)
1922 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001923 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001924 p->name ? p->name : "static",
1925 p->sink->name);
1926 }
1927
1928 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1929
1930 kfree(buf);
1931 return ret;
1932}
1933
1934static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001935 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001936 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001937 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001938};
1939
Mark Brownef49e4f2011-04-04 20:48:13 +09001940static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1941 size_t count, loff_t *ppos)
1942{
1943 struct snd_soc_dapm_context *dapm = file->private_data;
1944 char *level;
1945
1946 switch (dapm->bias_level) {
1947 case SND_SOC_BIAS_ON:
1948 level = "On\n";
1949 break;
1950 case SND_SOC_BIAS_PREPARE:
1951 level = "Prepare\n";
1952 break;
1953 case SND_SOC_BIAS_STANDBY:
1954 level = "Standby\n";
1955 break;
1956 case SND_SOC_BIAS_OFF:
1957 level = "Off\n";
1958 break;
1959 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001960 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09001961 level = "Unknown\n";
1962 break;
1963 }
1964
1965 return simple_read_from_buffer(user_buf, count, ppos, level,
1966 strlen(level));
1967}
1968
1969static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001970 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001971 .read = dapm_bias_read_file,
1972 .llseek = default_llseek,
1973};
1974
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001975void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1976 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001977{
Mark Brown79fb9382009-08-21 16:38:13 +01001978 struct dentry *d;
1979
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001980 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1981
1982 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001983 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001984 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001985 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001986 }
Mark Brown79fb9382009-08-21 16:38:13 +01001987
Mark Brownef49e4f2011-04-04 20:48:13 +09001988 d = debugfs_create_file("bias_level", 0444,
1989 dapm->debugfs_dapm, dapm,
1990 &dapm_bias_fops);
1991 if (!d)
1992 dev_warn(dapm->dev,
1993 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001994}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001995
1996static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1997{
1998 struct snd_soc_dapm_context *dapm = w->dapm;
1999 struct dentry *d;
2000
2001 if (!dapm->debugfs_dapm || !w->name)
2002 return;
2003
2004 d = debugfs_create_file(w->name, 0444,
2005 dapm->debugfs_dapm, w,
2006 &dapm_widget_power_fops);
2007 if (!d)
2008 dev_warn(w->dapm->dev,
2009 "ASoC: Failed to create %s debugfs file\n",
2010 w->name);
2011}
2012
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002013static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2014{
2015 debugfs_remove_recursive(dapm->debugfs_dapm);
2016}
2017
Mark Brown79fb9382009-08-21 16:38:13 +01002018#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002019void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2020 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002021{
2022}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002023
2024static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2025{
2026}
2027
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002028static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2029{
2030}
2031
Mark Brown79fb9382009-08-21 16:38:13 +01002032#endif
2033
Richard Purdie2b97eab2006-10-06 18:32:18 +02002034/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002035static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002036 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002037{
2038 struct snd_soc_dapm_path *path;
2039 int found = 0;
2040
Richard Purdie2b97eab2006-10-06 18:32:18 +02002041 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002042 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002043 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002044 continue;
2045
2046 found = 1;
2047 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002048 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002049 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002050 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002051 } else {
2052 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002053 dapm_mark_dirty(path->source,
2054 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002055 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002056 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002057 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002058 }
2059
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002060 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002061 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002062
Liam Girdwood618dae12012-04-25 12:12:51 +01002063 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002064}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002065
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002066int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002067 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2068 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002069{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002070 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002071 int ret;
2072
Liam Girdwood3cd04342012-03-09 12:02:08 +00002073 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002074 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002075 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002076 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002077 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002078 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002079 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002080 return ret;
2081}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002082EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002083
Milan plzik1b075e32008-01-10 14:39:46 +01002084/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002085static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002086 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002087{
2088 struct snd_soc_dapm_path *path;
2089 int found = 0;
2090
Richard Purdie2b97eab2006-10-06 18:32:18 +02002091 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002092 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002093 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002094 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002095 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002096 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002097 }
2098
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002099 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002100 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002101
Liam Girdwood618dae12012-04-25 12:12:51 +01002102 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002103}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002104
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002105int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002106 struct snd_kcontrol *kcontrol, int connect,
2107 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002108{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002109 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002110 int ret;
2111
Liam Girdwood3cd04342012-03-09 12:02:08 +00002112 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002113 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002114 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002115 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002116 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002117 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002118 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002119 return ret;
2120}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002121EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002122
2123/* show dapm widget status in sys fs */
2124static ssize_t dapm_widget_show(struct device *dev,
2125 struct device_attribute *attr, char *buf)
2126{
Mark Brown36ae1a92012-01-06 17:12:45 -08002127 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002128 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002129 struct snd_soc_dapm_widget *w;
2130 int count = 0;
2131 char *state = "not set";
2132
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002133 list_for_each_entry(w, &codec->card->widgets, list) {
2134 if (w->dapm != &codec->dapm)
2135 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002136
2137 /* only display widgets that burnm power */
2138 switch (w->id) {
2139 case snd_soc_dapm_hp:
2140 case snd_soc_dapm_mic:
2141 case snd_soc_dapm_spk:
2142 case snd_soc_dapm_line:
2143 case snd_soc_dapm_micbias:
2144 case snd_soc_dapm_dac:
2145 case snd_soc_dapm_adc:
2146 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002147 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002148 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002149 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002150 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002151 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002152 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002153 if (w->name)
2154 count += sprintf(buf + count, "%s: %s\n",
2155 w->name, w->power ? "On":"Off");
2156 break;
2157 default:
2158 break;
2159 }
2160 }
2161
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002162 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002163 case SND_SOC_BIAS_ON:
2164 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002165 break;
Mark Brown0be98982008-05-19 12:31:28 +02002166 case SND_SOC_BIAS_PREPARE:
2167 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002168 break;
Mark Brown0be98982008-05-19 12:31:28 +02002169 case SND_SOC_BIAS_STANDBY:
2170 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002171 break;
Mark Brown0be98982008-05-19 12:31:28 +02002172 case SND_SOC_BIAS_OFF:
2173 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002174 break;
2175 }
2176 count += sprintf(buf + count, "PM State: %s\n", state);
2177
2178 return count;
2179}
2180
2181static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2182
2183int snd_soc_dapm_sys_add(struct device *dev)
2184{
Troy Kisky12ef1932008-10-13 17:42:14 -07002185 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002186}
2187
2188static void snd_soc_dapm_sys_remove(struct device *dev)
2189{
Mark Brownaef90842009-05-16 17:53:16 +01002190 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002191}
2192
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002193static void dapm_free_path(struct snd_soc_dapm_path *path)
2194{
2195 list_del(&path->list_sink);
2196 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002197 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002198 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002199 kfree(path);
2200}
2201
Richard Purdie2b97eab2006-10-06 18:32:18 +02002202/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002203static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002204{
2205 struct snd_soc_dapm_widget *w, *next_w;
2206 struct snd_soc_dapm_path *p, *next_p;
2207
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002208 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2209 if (w->dapm != dapm)
2210 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002211 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002212 /*
2213 * remove source and sink paths associated to this widget.
2214 * While removing the path, remove reference to it from both
2215 * source and sink widgets so that path is removed only once.
2216 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002217 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2218 dapm_free_path(p);
2219
2220 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2221 dapm_free_path(p);
2222
Stephen Warrenfad59882011-04-28 17:37:59 -06002223 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002224 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002225 kfree(w);
2226 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002227}
2228
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002229static struct snd_soc_dapm_widget *dapm_find_widget(
2230 struct snd_soc_dapm_context *dapm, const char *pin,
2231 bool search_other_contexts)
2232{
2233 struct snd_soc_dapm_widget *w;
2234 struct snd_soc_dapm_widget *fallback = NULL;
2235
2236 list_for_each_entry(w, &dapm->card->widgets, list) {
2237 if (!strcmp(w->name, pin)) {
2238 if (w->dapm == dapm)
2239 return w;
2240 else
2241 fallback = w;
2242 }
2243 }
2244
2245 if (search_other_contexts)
2246 return fallback;
2247
2248 return NULL;
2249}
2250
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002251static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002252 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002253{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002254 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002255
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002256 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002257 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002258 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002259 }
2260
Mark Brown1a8b2d92012-02-16 11:50:07 -08002261 if (w->connected != status)
2262 dapm_mark_dirty(w, "pin configuration");
2263
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002264 w->connected = status;
2265 if (status == 0)
2266 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002267
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002268 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002269}
2270
Richard Purdie2b97eab2006-10-06 18:32:18 +02002271/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002272 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2273 * @dapm: DAPM context
2274 *
2275 * Walks all dapm audio paths and powers widgets according to their
2276 * stream or path usage.
2277 *
2278 * Requires external locking.
2279 *
2280 * Returns 0 for success.
2281 */
2282int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2283{
2284 /*
2285 * Suppress early reports (eg, jacks syncing their state) to avoid
2286 * silly DAPM runs during card startup.
2287 */
2288 if (!dapm->card || !dapm->card->instantiated)
2289 return 0;
2290
2291 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2292}
2293EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2294
2295/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002296 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002297 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002298 *
2299 * Walks all dapm audio paths and powers widgets according to their
2300 * stream or path usage.
2301 *
2302 * Returns 0 for success.
2303 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002304int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002305{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002306 int ret;
2307
Liam Girdwood3cd04342012-03-09 12:02:08 +00002308 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002309 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002310 mutex_unlock(&dapm->card->dapm_mutex);
2311 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002312}
Liam Girdwooda5302182008-07-07 13:35:17 +01002313EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002314
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002315static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2316 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2317 const char *control,
2318 int (*connected)(struct snd_soc_dapm_widget *source,
2319 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002320{
2321 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002322 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002323
2324 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2325 if (!path)
2326 return -ENOMEM;
2327
2328 path->source = wsource;
2329 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002330 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002331 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002332 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002333 INIT_LIST_HEAD(&path->list_source);
2334 INIT_LIST_HEAD(&path->list_sink);
2335
2336 /* check for external widgets */
2337 if (wsink->id == snd_soc_dapm_input) {
2338 if (wsource->id == snd_soc_dapm_micbias ||
2339 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002340 wsource->id == snd_soc_dapm_line ||
2341 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002342 wsink->ext = 1;
2343 }
2344 if (wsource->id == snd_soc_dapm_output) {
2345 if (wsink->id == snd_soc_dapm_spk ||
2346 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002347 wsink->id == snd_soc_dapm_line ||
2348 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002349 wsource->ext = 1;
2350 }
2351
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002352 dapm_mark_dirty(wsource, "Route added");
2353 dapm_mark_dirty(wsink, "Route added");
2354
Richard Purdie2b97eab2006-10-06 18:32:18 +02002355 /* connect static paths */
2356 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002357 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002358 list_add(&path->list_sink, &wsink->sources);
2359 list_add(&path->list_source, &wsource->sinks);
2360 path->connect = 1;
2361 return 0;
2362 }
2363
2364 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002365 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002366 case snd_soc_dapm_adc:
2367 case snd_soc_dapm_dac:
2368 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002369 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002370 case snd_soc_dapm_input:
2371 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002372 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002373 case snd_soc_dapm_micbias:
2374 case snd_soc_dapm_vmid:
2375 case snd_soc_dapm_pre:
2376 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002377 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002378 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002379 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002380 case snd_soc_dapm_aif_in:
2381 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002382 case snd_soc_dapm_dai_in:
2383 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002384 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002385 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002386 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002387 list_add(&path->list_sink, &wsink->sources);
2388 list_add(&path->list_source, &wsource->sinks);
2389 path->connect = 1;
2390 return 0;
2391 case snd_soc_dapm_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002392 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002393 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002394 if (ret != 0)
2395 goto err;
2396 break;
2397 case snd_soc_dapm_switch:
2398 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002399 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002400 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002401 if (ret != 0)
2402 goto err;
2403 break;
2404 case snd_soc_dapm_hp:
2405 case snd_soc_dapm_mic:
2406 case snd_soc_dapm_line:
2407 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002408 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002409 list_add(&path->list_sink, &wsink->sources);
2410 list_add(&path->list_source, &wsource->sinks);
2411 path->connect = 0;
2412 return 0;
2413 }
Mark Brownfabd0382012-07-05 17:20:06 +01002414
Richard Purdie2b97eab2006-10-06 18:32:18 +02002415 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002416err:
2417 kfree(path);
2418 return ret;
2419}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002420
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002421static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002422 const struct snd_soc_dapm_route *route,
2423 unsigned int is_prefixed)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002424{
2425 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2426 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2427 const char *sink;
2428 const char *source;
2429 char prefixed_sink[80];
2430 char prefixed_source[80];
2431 int ret;
2432
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002433 if (dapm->codec && dapm->codec->name_prefix && !is_prefixed) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002434 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2435 dapm->codec->name_prefix, route->sink);
2436 sink = prefixed_sink;
2437 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2438 dapm->codec->name_prefix, route->source);
2439 source = prefixed_source;
2440 } else {
2441 sink = route->sink;
2442 source = route->source;
2443 }
2444
2445 /*
2446 * find src and dest widgets over all widgets but favor a widget from
2447 * current DAPM context
2448 */
2449 list_for_each_entry(w, &dapm->card->widgets, list) {
2450 if (!wsink && !(strcmp(w->name, sink))) {
2451 wtsink = w;
2452 if (w->dapm == dapm)
2453 wsink = w;
2454 continue;
2455 }
2456 if (!wsource && !(strcmp(w->name, source))) {
2457 wtsource = w;
2458 if (w->dapm == dapm)
2459 wsource = w;
2460 }
2461 }
2462 /* use widget from another DAPM context if not found from this */
2463 if (!wsink)
2464 wsink = wtsink;
2465 if (!wsource)
2466 wsource = wtsource;
2467
2468 if (wsource == NULL) {
2469 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2470 route->source);
2471 return -ENODEV;
2472 }
2473 if (wsink == NULL) {
2474 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2475 route->sink);
2476 return -ENODEV;
2477 }
2478
2479 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2480 route->connected);
2481 if (ret)
2482 goto err;
2483
2484 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002485err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002486 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002487 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002488 return ret;
2489}
Mark Brown105f1c22008-05-13 14:52:19 +02002490
Mark Brownefcc3c62012-07-05 17:24:19 +01002491static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2492 const struct snd_soc_dapm_route *route)
2493{
2494 struct snd_soc_dapm_path *path, *p;
2495 const char *sink;
2496 const char *source;
2497 char prefixed_sink[80];
2498 char prefixed_source[80];
2499
2500 if (route->control) {
2501 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002502 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002503 return -EINVAL;
2504 }
2505
2506 if (dapm->codec && dapm->codec->name_prefix) {
2507 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2508 dapm->codec->name_prefix, route->sink);
2509 sink = prefixed_sink;
2510 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2511 dapm->codec->name_prefix, route->source);
2512 source = prefixed_source;
2513 } else {
2514 sink = route->sink;
2515 source = route->source;
2516 }
2517
2518 path = NULL;
2519 list_for_each_entry(p, &dapm->card->paths, list) {
2520 if (strcmp(p->source->name, source) != 0)
2521 continue;
2522 if (strcmp(p->sink->name, sink) != 0)
2523 continue;
2524 path = p;
2525 break;
2526 }
2527
2528 if (path) {
2529 dapm_mark_dirty(path->source, "Route removed");
2530 dapm_mark_dirty(path->sink, "Route removed");
2531
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002532 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002533 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002534 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002535 source, sink);
2536 }
2537
2538 return 0;
2539}
2540
Mark Brown105f1c22008-05-13 14:52:19 +02002541/**
Mark Brown105f1c22008-05-13 14:52:19 +02002542 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002543 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002544 * @route: audio routes
2545 * @num: number of routes
2546 *
2547 * Connects 2 dapm widgets together via a named audio path. The sink is
2548 * the widget receiving the audio signal, whilst the source is the sender
2549 * of the audio signal.
2550 *
2551 * Returns 0 for success else error. On error all resources can be freed
2552 * with a call to snd_soc_card_free().
2553 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002554int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002555 const struct snd_soc_dapm_route *route, int num)
2556{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002557 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002558
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002559 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002560 for (i = 0; i < num; i++) {
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002561 r = snd_soc_dapm_add_route(dapm, route, false);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002562 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002563 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2564 route->source,
2565 route->control ? route->control : "direct",
2566 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002567 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002568 }
2569 route++;
2570 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002571 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002572
Dan Carpenter60884c22012-04-13 22:25:43 +03002573 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002574}
2575EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2576
Mark Brownefcc3c62012-07-05 17:24:19 +01002577/**
2578 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2579 * @dapm: DAPM context
2580 * @route: audio routes
2581 * @num: number of routes
2582 *
2583 * Removes routes from the DAPM context.
2584 */
2585int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2586 const struct snd_soc_dapm_route *route, int num)
2587{
2588 int i, ret = 0;
2589
2590 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2591 for (i = 0; i < num; i++) {
2592 snd_soc_dapm_del_route(dapm, route);
2593 route++;
2594 }
2595 mutex_unlock(&dapm->card->dapm_mutex);
2596
2597 return ret;
2598}
2599EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2600
Mark Brownbf3a9e12011-06-13 16:42:29 +01002601static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2602 const struct snd_soc_dapm_route *route)
2603{
2604 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2605 route->source,
2606 true);
2607 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2608 route->sink,
2609 true);
2610 struct snd_soc_dapm_path *path;
2611 int count = 0;
2612
2613 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002614 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002615 route->source);
2616 return -ENODEV;
2617 }
2618
2619 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002620 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002621 route->sink);
2622 return -ENODEV;
2623 }
2624
2625 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002626 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002627 route->source, route->sink);
2628
2629 list_for_each_entry(path, &source->sinks, list_source) {
2630 if (path->sink == sink) {
2631 path->weak = 1;
2632 count++;
2633 }
2634 }
2635
2636 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002637 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002638 route->source, route->sink);
2639 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002640 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002641 count, route->source, route->sink);
2642
2643 return 0;
2644}
2645
2646/**
2647 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2648 * @dapm: DAPM context
2649 * @route: audio routes
2650 * @num: number of routes
2651 *
2652 * Mark existing routes matching those specified in the passed array
2653 * as being weak, meaning that they are ignored for the purpose of
2654 * power decisions. The main intended use case is for sidetone paths
2655 * which couple audio between other independent paths if they are both
2656 * active in order to make the combination work better at the user
2657 * level but which aren't intended to be "used".
2658 *
2659 * Note that CODEC drivers should not use this as sidetone type paths
2660 * can frequently also be used as bypass paths.
2661 */
2662int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2663 const struct snd_soc_dapm_route *route, int num)
2664{
2665 int i, err;
2666 int ret = 0;
2667
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002668 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002669 for (i = 0; i < num; i++) {
2670 err = snd_soc_dapm_weak_route(dapm, route);
2671 if (err)
2672 ret = err;
2673 route++;
2674 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002675 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002676
2677 return ret;
2678}
2679EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2680
Mark Brown105f1c22008-05-13 14:52:19 +02002681/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002682 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002683 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002684 *
2685 * Checks the codec for any new dapm widgets and creates them if found.
2686 *
2687 * Returns 0 for success.
2688 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002689int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002690{
2691 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002692 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002693
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002694 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002695
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002696 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002697 {
2698 if (w->new)
2699 continue;
2700
Stephen Warrenfad59882011-04-28 17:37:59 -06002701 if (w->num_kcontrols) {
2702 w->kcontrols = kzalloc(w->num_kcontrols *
2703 sizeof(struct snd_kcontrol *),
2704 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002705 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002706 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002707 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002708 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002709 }
2710
Richard Purdie2b97eab2006-10-06 18:32:18 +02002711 switch(w->id) {
2712 case snd_soc_dapm_switch:
2713 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002714 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002715 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002716 break;
2717 case snd_soc_dapm_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002718 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002719 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002720 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002721 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002722 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002723 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002724 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002725 break;
2726 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002727
2728 /* Read the initial power state from the device */
2729 if (w->reg >= 0) {
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002730 soc_widget_read(w, w->reg, &val);
2731 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002732 val &= w->mask;
2733 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002734 w->power = 1;
2735 }
2736
Richard Purdie2b97eab2006-10-06 18:32:18 +02002737 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002738
Mark Brown7508b122011-10-05 12:09:12 +01002739 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002740 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002741 }
2742
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002743 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2744 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002745 return 0;
2746}
2747EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2748
2749/**
2750 * snd_soc_dapm_get_volsw - dapm mixer get callback
2751 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002752 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002753 *
2754 * Callback to get the value of a dapm mixer control.
2755 *
2756 * Returns 0 for success.
2757 */
2758int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2759 struct snd_ctl_elem_value *ucontrol)
2760{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002761 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002762 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002763 struct soc_mixer_control *mc =
2764 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002765 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002766 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002767 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002768 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002769 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002770 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002771
2772 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002773 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002774 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002775 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002776
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002777 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002778 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002779 val = (snd_soc_read(codec, reg) >> shift) & mask;
2780 else
2781 val = dapm_kcontrol_get_value(kcontrol);
2782 mutex_unlock(&card->dapm_mutex);
2783
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002784 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002785 ucontrol->value.integer.value[0] = max - val;
2786 else
2787 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002788
2789 return 0;
2790}
2791EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2792
2793/**
2794 * snd_soc_dapm_put_volsw - dapm mixer set callback
2795 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002796 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002797 *
2798 * Callback to set the value of a dapm mixer control.
2799 *
2800 * Returns 0 for success.
2801 */
2802int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2803 struct snd_ctl_elem_value *ucontrol)
2804{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002805 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002806 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002807 struct soc_mixer_control *mc =
2808 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002809 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002810 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002811 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002812 unsigned int mask = (1 << fls(max)) - 1;
2813 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002814 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002815 int connect, change;
2816 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002817 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002818
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002819 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002820 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002821 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002822 kcontrol->id.name);
2823
Richard Purdie2b97eab2006-10-06 18:32:18 +02002824 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002825 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002826
2827 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002828 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002829
Liam Girdwood3cd04342012-03-09 12:02:08 +00002830 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002831
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002832 change = dapm_kcontrol_set_value(kcontrol, val);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002833
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002834 if (reg != SND_SOC_NOPM) {
2835 mask = mask << shift;
2836 val = val << shift;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002837
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002838 change = snd_soc_test_bits(codec, reg, mask, val);
2839 }
2840
Mark Brown97404f22010-12-14 16:13:57 +00002841 if (change) {
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002842 if (reg != SND_SOC_NOPM) {
2843 update.kcontrol = kcontrol;
2844 update.reg = reg;
2845 update.mask = mask;
2846 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002847
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002848 card->update = &update;
2849 }
Mark Brown97404f22010-12-14 16:13:57 +00002850
Nenghua Cao52765972013-12-13 20:13:49 +08002851 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002852
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002853 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002854 }
2855
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002856 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002857
2858 if (ret > 0)
2859 soc_dpcm_runtime_update(card);
2860
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002861 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002862}
2863EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2864
2865/**
2866 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2867 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002868 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002869 *
2870 * Callback to get the value of a dapm enumerated double mixer control.
2871 *
2872 * Returns 0 for success.
2873 */
2874int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2875 struct snd_ctl_elem_value *ucontrol)
2876{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002877 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002878 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002879 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002880
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002881 if (e->reg != SND_SOC_NOPM)
2882 reg_val = snd_soc_read(codec, e->reg);
2883 else
2884 reg_val = dapm_kcontrol_get_value(kcontrol);
2885
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002886 val = (reg_val >> e->shift_l) & e->mask;
2887 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2888 if (e->shift_l != e->shift_r) {
2889 val = (reg_val >> e->shift_r) & e->mask;
2890 val = snd_soc_enum_val_to_item(e, val);
2891 ucontrol->value.enumerated.item[1] = val;
2892 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002893
2894 return 0;
2895}
2896EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2897
2898/**
2899 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2900 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002901 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002902 *
2903 * Callback to set the value of a dapm enumerated double mixer control.
2904 *
2905 * Returns 0 for success.
2906 */
2907int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2908 struct snd_ctl_elem_value *ucontrol)
2909{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002910 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002911 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002912 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002913 unsigned int *item = ucontrol->value.enumerated.item;
2914 unsigned int val, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002915 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002916 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002917 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002918
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002919 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002920 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002921
2922 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002923 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002924 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002925 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002926 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002927 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002928 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002929 }
2930
Liam Girdwood3cd04342012-03-09 12:02:08 +00002931 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002932
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002933 if (e->reg != SND_SOC_NOPM)
2934 change = snd_soc_test_bits(codec, e->reg, mask, val);
2935 else
2936 change = dapm_kcontrol_set_value(kcontrol, val);
2937
Stephen Warrenfafd2172011-04-28 17:38:00 -06002938 if (change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002939 if (e->reg != SND_SOC_NOPM) {
2940 update.kcontrol = kcontrol;
2941 update.reg = e->reg;
2942 update.mask = mask;
2943 update.val = val;
2944 card->update = &update;
2945 }
Mark Brown97404f22010-12-14 16:13:57 +00002946
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002947 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002948
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002949 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002950 }
2951
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002952 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002953
2954 if (ret > 0)
2955 soc_dpcm_runtime_update(card);
2956
Mark Brown97404f22010-12-14 16:13:57 +00002957 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002958}
2959EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2960
2961/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002962 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2963 *
2964 * @kcontrol: mixer control
2965 * @uinfo: control element information
2966 *
2967 * Callback to provide information about a pin switch control.
2968 */
2969int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2970 struct snd_ctl_elem_info *uinfo)
2971{
2972 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2973 uinfo->count = 1;
2974 uinfo->value.integer.min = 0;
2975 uinfo->value.integer.max = 1;
2976
2977 return 0;
2978}
2979EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2980
2981/**
2982 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2983 *
2984 * @kcontrol: mixer control
2985 * @ucontrol: Value
2986 */
2987int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2988 struct snd_ctl_elem_value *ucontrol)
2989{
Mark Brown48a8c392012-02-14 17:11:15 -08002990 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002991 const char *pin = (const char *)kcontrol->private_value;
2992
Liam Girdwood3cd04342012-03-09 12:02:08 +00002993 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002994
2995 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002996 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002997
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002998 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002999
3000 return 0;
3001}
3002EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3003
3004/**
3005 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3006 *
3007 * @kcontrol: mixer control
3008 * @ucontrol: Value
3009 */
3010int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3011 struct snd_ctl_elem_value *ucontrol)
3012{
Mark Brown48a8c392012-02-14 17:11:15 -08003013 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003014 const char *pin = (const char *)kcontrol->private_value;
3015
Mark Brown8b37dbd2009-02-28 21:14:20 +00003016 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003017 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003018 else
Mark Brown48a8c392012-02-14 17:11:15 -08003019 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003020
Mark Brown48a8c392012-02-14 17:11:15 -08003021 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003022 return 0;
3023}
3024EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3025
Mark Brown5ba06fc2012-02-16 11:07:13 -08003026static struct snd_soc_dapm_widget *
3027snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3028 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003029{
3030 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003031 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003032
3033 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003034 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003035
Mark Brown62ea8742012-01-21 21:14:48 +00003036 switch (w->id) {
3037 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003038 w->regulator = devm_regulator_get(dapm->dev, w->name);
3039 if (IS_ERR(w->regulator)) {
3040 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003041 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003042 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003043 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003044 }
Mark Brown8784c772013-01-10 19:33:47 +00003045
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003046 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003047 ret = regulator_allow_bypass(w->regulator, true);
3048 if (ret != 0)
3049 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003050 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003051 w->name, ret);
3052 }
Mark Brown62ea8742012-01-21 21:14:48 +00003053 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003054 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003055#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003056 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003057 if (IS_ERR(w->clk)) {
3058 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003059 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003060 w->name, ret);
3061 return NULL;
3062 }
Mark Brownec029952012-06-04 08:16:20 +01003063#else
3064 return NULL;
3065#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003066 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003067 default:
3068 break;
3069 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003070
Mark Brown88e8b9a2011-03-02 18:18:24 +00003071 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003072 w->name = kasprintf(GFP_KERNEL, "%s %s",
3073 dapm->codec->name_prefix, widget->name);
3074 else
3075 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3076
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003077 if (w->name == NULL) {
3078 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003079 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003080 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003081
Mark Brown7ca3a182011-10-08 14:04:50 +01003082 switch (w->id) {
3083 case snd_soc_dapm_switch:
3084 case snd_soc_dapm_mixer:
3085 case snd_soc_dapm_mixer_named_ctl:
3086 w->power_check = dapm_generic_check_power;
3087 break;
3088 case snd_soc_dapm_mux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003089 w->power_check = dapm_generic_check_power;
3090 break;
Mark Brown46162742013-06-05 19:36:11 +01003091 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003092 w->power_check = dapm_adc_check_power;
3093 break;
Mark Brown46162742013-06-05 19:36:11 +01003094 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003095 w->power_check = dapm_dac_check_power;
3096 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003097 case snd_soc_dapm_adc:
3098 case snd_soc_dapm_aif_out:
3099 case snd_soc_dapm_dac:
3100 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003101 case snd_soc_dapm_pga:
3102 case snd_soc_dapm_out_drv:
3103 case snd_soc_dapm_input:
3104 case snd_soc_dapm_output:
3105 case snd_soc_dapm_micbias:
3106 case snd_soc_dapm_spk:
3107 case snd_soc_dapm_hp:
3108 case snd_soc_dapm_mic:
3109 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003110 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003111 w->power_check = dapm_generic_check_power;
3112 break;
3113 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003114 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003115 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003116 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003117 w->power_check = dapm_supply_check_power;
3118 break;
3119 default:
3120 w->power_check = dapm_always_on_check_power;
3121 break;
3122 }
3123
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003124 w->dapm = dapm;
3125 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003126 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003127 INIT_LIST_HEAD(&w->sources);
3128 INIT_LIST_HEAD(&w->sinks);
3129 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003130 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003131 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003132
3133 /* machine layer set ups unconnected pins and insertions */
3134 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003135 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003136}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003137
3138/**
Mark Brown4ba13272008-05-13 14:51:19 +02003139 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003140 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003141 * @widget: widget array
3142 * @num: number of widgets
3143 *
3144 * Creates new DAPM controls based upon the templates.
3145 *
3146 * Returns 0 for success else error.
3147 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003148int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003149 const struct snd_soc_dapm_widget *widget,
3150 int num)
3151{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003152 struct snd_soc_dapm_widget *w;
3153 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003154 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003155
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003156 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003157 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003158 w = snd_soc_dapm_new_control(dapm, widget);
3159 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003160 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003161 "ASoC: Failed to create DAPM control %s\n",
3162 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003163 ret = -ENOMEM;
3164 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003165 }
Mark Brown4ba13272008-05-13 14:51:19 +02003166 widget++;
3167 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003168 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003169 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003170}
3171EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3172
Mark Brownc74184e2012-04-04 22:12:09 +01003173static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3174 struct snd_kcontrol *kcontrol, int event)
3175{
3176 struct snd_soc_dapm_path *source_p, *sink_p;
3177 struct snd_soc_dai *source, *sink;
3178 const struct snd_soc_pcm_stream *config = w->params;
3179 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003180 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003181 u64 fmt;
3182 int ret;
3183
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003184 if (WARN_ON(!config) ||
3185 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3186 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003187
3188 /* We only support a single source and sink, pick the first */
3189 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3190 list_sink);
3191 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3192 list_source);
3193
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003194 if (WARN_ON(!source_p || !sink_p) ||
3195 WARN_ON(!sink_p->source || !source_p->sink) ||
3196 WARN_ON(!source_p->source || !sink_p->sink))
3197 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003198
3199 source = source_p->source->priv;
3200 sink = sink_p->sink->priv;
3201
3202 /* Be a little careful as we don't want to overflow the mask array */
3203 if (config->formats) {
3204 fmt = ffs(config->formats) - 1;
3205 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003206 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003207 config->formats);
3208 fmt = 0;
3209 }
3210
3211 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003212 params = kzalloc(sizeof(*params), GFP_KERNEL);
3213 if (!params) {
3214 ret = -ENOMEM;
3215 goto out;
3216 }
3217 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003218
Mark Brown9747cec2012-04-26 19:12:21 +01003219 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003220 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003221 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003222 config->rate_max;
3223
Mark Brown9747cec2012-04-26 19:12:21 +01003224 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003225 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003226 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003227 = config->channels_max;
3228
3229 memset(&substream, 0, sizeof(substream));
3230
3231 switch (event) {
3232 case SND_SOC_DAPM_PRE_PMU:
3233 if (source->driver->ops && source->driver->ops->hw_params) {
3234 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3235 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003236 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003237 if (ret != 0) {
3238 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003239 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003240 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003241 }
3242 }
3243
3244 if (sink->driver->ops && sink->driver->ops->hw_params) {
3245 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003246 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003247 sink);
3248 if (ret != 0) {
3249 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003250 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003251 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003252 }
3253 }
3254 break;
3255
3256 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003257 ret = snd_soc_dai_digital_mute(sink, 0,
3258 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003259 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003260 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003261 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003262 break;
3263
3264 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003265 ret = snd_soc_dai_digital_mute(sink, 1,
3266 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003267 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003268 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003269 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003270 break;
3271
3272 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003273 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003274 return -EINVAL;
3275 }
3276
Mark Brown9747cec2012-04-26 19:12:21 +01003277out:
3278 kfree(params);
3279 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003280}
3281
3282int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3283 const struct snd_soc_pcm_stream *params,
3284 struct snd_soc_dapm_widget *source,
3285 struct snd_soc_dapm_widget *sink)
3286{
3287 struct snd_soc_dapm_route routes[2];
3288 struct snd_soc_dapm_widget template;
3289 struct snd_soc_dapm_widget *w;
3290 size_t len;
3291 char *link_name;
3292
3293 len = strlen(source->name) + strlen(sink->name) + 2;
3294 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3295 if (!link_name)
3296 return -ENOMEM;
3297 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3298
3299 memset(&template, 0, sizeof(template));
3300 template.reg = SND_SOC_NOPM;
3301 template.id = snd_soc_dapm_dai_link;
3302 template.name = link_name;
3303 template.event = snd_soc_dai_link_event;
3304 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3305 SND_SOC_DAPM_PRE_PMD;
3306
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003307 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003308
3309 w = snd_soc_dapm_new_control(&card->dapm, &template);
3310 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003311 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003312 link_name);
3313 return -ENOMEM;
3314 }
3315
3316 w->params = params;
3317
3318 memset(&routes, 0, sizeof(routes));
3319
3320 routes[0].source = source->name;
3321 routes[0].sink = link_name;
3322 routes[1].source = link_name;
3323 routes[1].sink = sink->name;
3324
3325 return snd_soc_dapm_add_routes(&card->dapm, routes,
3326 ARRAY_SIZE(routes));
3327}
3328
Mark Brown888df392012-02-16 19:37:51 -08003329int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3330 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003331{
Mark Brown888df392012-02-16 19:37:51 -08003332 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003333 struct snd_soc_dapm_widget *w;
3334
Mark Brown888df392012-02-16 19:37:51 -08003335 WARN_ON(dapm->dev != dai->dev);
3336
3337 memset(&template, 0, sizeof(template));
3338 template.reg = SND_SOC_NOPM;
3339
3340 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003341 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003342 template.name = dai->driver->playback.stream_name;
3343 template.sname = dai->driver->playback.stream_name;
3344
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003345 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003346 template.name);
3347
3348 w = snd_soc_dapm_new_control(dapm, &template);
3349 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003350 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003351 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003352 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003353 }
3354
3355 w->priv = dai;
3356 dai->playback_widget = w;
3357 }
3358
3359 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003360 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003361 template.name = dai->driver->capture.stream_name;
3362 template.sname = dai->driver->capture.stream_name;
3363
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003364 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003365 template.name);
3366
3367 w = snd_soc_dapm_new_control(dapm, &template);
3368 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003369 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003370 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003371 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003372 }
3373
3374 w->priv = dai;
3375 dai->capture_widget = w;
3376 }
3377
3378 return 0;
3379}
3380
3381int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3382{
3383 struct snd_soc_dapm_widget *dai_w, *w;
3384 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003385
3386 /* For each DAI widget... */
3387 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003388 switch (dai_w->id) {
3389 case snd_soc_dapm_dai_in:
3390 case snd_soc_dapm_dai_out:
3391 break;
3392 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003393 continue;
Mark Brown46162742013-06-05 19:36:11 +01003394 }
Mark Brown888df392012-02-16 19:37:51 -08003395
3396 dai = dai_w->priv;
3397
3398 /* ...find all widgets with the same stream and link them */
3399 list_for_each_entry(w, &card->widgets, list) {
3400 if (w->dapm != dai_w->dapm)
3401 continue;
3402
Mark Brown46162742013-06-05 19:36:11 +01003403 switch (w->id) {
3404 case snd_soc_dapm_dai_in:
3405 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003406 continue;
Mark Brown46162742013-06-05 19:36:11 +01003407 default:
3408 break;
3409 }
Mark Brown888df392012-02-16 19:37:51 -08003410
Russell King19c2c5f2013-08-04 20:24:03 +01003411 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003412 continue;
3413
3414 if (dai->driver->playback.stream_name &&
3415 strstr(w->sname,
3416 dai->driver->playback.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003417 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003418 dai->playback_widget->name, w->name);
Mark Brown888df392012-02-16 19:37:51 -08003419
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003420 snd_soc_dapm_add_path(w->dapm,
3421 dai->playback_widget, w, NULL, NULL);
Mark Brown888df392012-02-16 19:37:51 -08003422 }
3423
3424 if (dai->driver->capture.stream_name &&
3425 strstr(w->sname,
3426 dai->driver->capture.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003427 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003428 w->name, dai->capture_widget->name);
Mark Brown888df392012-02-16 19:37:51 -08003429
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003430 snd_soc_dapm_add_path(w->dapm, w,
3431 dai->capture_widget, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003432 }
3433 }
3434 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003435
Mark Brown888df392012-02-16 19:37:51 -08003436 return 0;
3437}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003438
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003439void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3440{
3441 struct snd_soc_pcm_runtime *rtd = card->rtd;
3442 struct snd_soc_dai *cpu_dai, *codec_dai;
3443 struct snd_soc_dapm_route r;
3444 int i;
3445
3446 memset(&r, 0, sizeof(r));
3447
3448 /* for each BE DAI link... */
3449 for (i = 0; i < card->num_rtd; i++) {
3450 rtd = &card->rtd[i];
3451 cpu_dai = rtd->cpu_dai;
3452 codec_dai = rtd->codec_dai;
3453
3454 /* dynamic FE links have no fixed DAI mapping */
3455 if (rtd->dai_link->dynamic)
3456 continue;
3457
3458 /* there is no point in connecting BE DAI links with dummies */
3459 if (snd_soc_dai_is_dummy(codec_dai) ||
3460 snd_soc_dai_is_dummy(cpu_dai))
3461 continue;
3462
3463 /* connect BE DAI playback if widgets are valid */
3464 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3465 r.source = cpu_dai->playback_widget->name;
3466 r.sink = codec_dai->playback_widget->name;
3467 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3468 cpu_dai->codec->name, r.source,
3469 codec_dai->platform->name, r.sink);
3470
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003471 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003472 }
3473
3474 /* connect BE DAI capture if widgets are valid */
3475 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3476 r.source = codec_dai->capture_widget->name;
3477 r.sink = cpu_dai->capture_widget->name;
3478 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3479 codec_dai->codec->name, r.source,
3480 cpu_dai->platform->name, r.sink);
3481
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003482 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003483 }
3484
3485 }
3486}
3487
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003488static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3489 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003490{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003491
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003492 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3493 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3494 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003495
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003496 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3497 w_cpu = cpu_dai->playback_widget;
3498 w_codec = codec_dai->playback_widget;
3499 } else {
3500 w_cpu = cpu_dai->capture_widget;
3501 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003502 }
3503
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003504 if (w_cpu) {
3505
3506 dapm_mark_dirty(w_cpu, "stream event");
3507
3508 switch (event) {
3509 case SND_SOC_DAPM_STREAM_START:
3510 w_cpu->active = 1;
3511 break;
3512 case SND_SOC_DAPM_STREAM_STOP:
3513 w_cpu->active = 0;
3514 break;
3515 case SND_SOC_DAPM_STREAM_SUSPEND:
3516 case SND_SOC_DAPM_STREAM_RESUME:
3517 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3518 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3519 break;
3520 }
3521 }
3522
3523 if (w_codec) {
3524
3525 dapm_mark_dirty(w_codec, "stream event");
3526
3527 switch (event) {
3528 case SND_SOC_DAPM_STREAM_START:
3529 w_codec->active = 1;
3530 break;
3531 case SND_SOC_DAPM_STREAM_STOP:
3532 w_codec->active = 0;
3533 break;
3534 case SND_SOC_DAPM_STREAM_SUSPEND:
3535 case SND_SOC_DAPM_STREAM_RESUME:
3536 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3537 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3538 break;
3539 }
3540 }
3541
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003542 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003543}
3544
3545/**
3546 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3547 * @rtd: PCM runtime data
3548 * @stream: stream name
3549 * @event: stream event
3550 *
3551 * Sends a stream event to the dapm core. The core then makes any
3552 * necessary widget power changes.
3553 *
3554 * Returns 0 for success else error.
3555 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003556void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3557 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003558{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003559 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003560
Liam Girdwood3cd04342012-03-09 12:02:08 +00003561 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003562 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003563 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003564}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003565
3566/**
Charles Keepax11391102014-02-18 15:22:14 +00003567 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3568 * @dapm: DAPM context
3569 * @pin: pin name
3570 *
3571 * Enables input/output pin and its parents or children widgets iff there is
3572 * a valid audio route and active audio stream.
3573 *
3574 * Requires external locking.
3575 *
3576 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3577 * do any widget power switching.
3578 */
3579int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3580 const char *pin)
3581{
3582 return snd_soc_dapm_set_pin(dapm, pin, 1);
3583}
3584EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3585
3586/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003587 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003588 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003589 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003590 *
Mark Brown74b8f952009-06-06 11:26:15 +01003591 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003592 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003593 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003594 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3595 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003596 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003597int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003598{
Charles Keepax11391102014-02-18 15:22:14 +00003599 int ret;
3600
3601 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3602
3603 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3604
3605 mutex_unlock(&dapm->card->dapm_mutex);
3606
3607 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003608}
Liam Girdwooda5302182008-07-07 13:35:17 +01003609EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003610
3611/**
Charles Keepax11391102014-02-18 15:22:14 +00003612 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3613 * @dapm: DAPM context
3614 * @pin: pin name
3615 *
3616 * Enables input/output pin regardless of any other state. This is
3617 * intended for use with microphone bias supplies used in microphone
3618 * jack detection.
3619 *
3620 * Requires external locking.
3621 *
3622 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3623 * do any widget power switching.
3624 */
3625int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3626 const char *pin)
3627{
3628 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3629
3630 if (!w) {
3631 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3632 return -EINVAL;
3633 }
3634
3635 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3636 w->connected = 1;
3637 w->force = 1;
3638 dapm_mark_dirty(w, "force enable");
3639
3640 return 0;
3641}
3642EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3643
3644/**
Mark Brownda341832010-03-15 19:23:37 +00003645 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003646 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003647 * @pin: pin name
3648 *
3649 * Enables input/output pin regardless of any other state. This is
3650 * intended for use with microphone bias supplies used in microphone
3651 * jack detection.
3652 *
3653 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3654 * do any widget power switching.
3655 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003656int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3657 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003658{
Charles Keepax11391102014-02-18 15:22:14 +00003659 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003660
Charles Keepax11391102014-02-18 15:22:14 +00003661 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003662
Charles Keepax11391102014-02-18 15:22:14 +00003663 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003664
Charles Keepax11391102014-02-18 15:22:14 +00003665 mutex_unlock(&dapm->card->dapm_mutex);
3666
3667 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003668}
3669EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3670
3671/**
Charles Keepax11391102014-02-18 15:22:14 +00003672 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3673 * @dapm: DAPM context
3674 * @pin: pin name
3675 *
3676 * Disables input/output pin and its parents or children widgets.
3677 *
3678 * Requires external locking.
3679 *
3680 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3681 * do any widget power switching.
3682 */
3683int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3684 const char *pin)
3685{
3686 return snd_soc_dapm_set_pin(dapm, pin, 0);
3687}
3688EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3689
3690/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003691 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003692 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003693 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003694 *
Mark Brown74b8f952009-06-06 11:26:15 +01003695 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003696 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003697 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3698 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003699 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003700int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3701 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003702{
Charles Keepax11391102014-02-18 15:22:14 +00003703 int ret;
3704
3705 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3706
3707 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3708
3709 mutex_unlock(&dapm->card->dapm_mutex);
3710
3711 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003712}
3713EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3714
3715/**
Charles Keepax11391102014-02-18 15:22:14 +00003716 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3717 * @dapm: DAPM context
3718 * @pin: pin name
3719 *
3720 * Marks the specified pin as being not connected, disabling it along
3721 * any parent or child widgets. At present this is identical to
3722 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3723 * additional things such as disabling controls which only affect
3724 * paths through the pin.
3725 *
3726 * Requires external locking.
3727 *
3728 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3729 * do any widget power switching.
3730 */
3731int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3732 const char *pin)
3733{
3734 return snd_soc_dapm_set_pin(dapm, pin, 0);
3735}
3736EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3737
3738/**
Mark Brown5817b522008-09-24 11:23:11 +01003739 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003740 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003741 * @pin: pin name
3742 *
3743 * Marks the specified pin as being not connected, disabling it along
3744 * any parent or child widgets. At present this is identical to
3745 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3746 * additional things such as disabling controls which only affect
3747 * paths through the pin.
3748 *
3749 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3750 * do any widget power switching.
3751 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003752int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003753{
Charles Keepax11391102014-02-18 15:22:14 +00003754 int ret;
3755
3756 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3757
3758 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3759
3760 mutex_unlock(&dapm->card->dapm_mutex);
3761
3762 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003763}
3764EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3765
3766/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003767 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003768 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003769 * @pin: audio signal pin endpoint (or start point)
3770 *
3771 * Get audio pin status - connected or disconnected.
3772 *
3773 * Returns 1 for connected otherwise 0.
3774 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003775int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3776 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003777{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003778 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003779
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003780 if (w)
3781 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003782
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003783 return 0;
3784}
Liam Girdwooda5302182008-07-07 13:35:17 +01003785EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003786
3787/**
Mark Brown1547aba2010-05-07 21:11:40 +01003788 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003789 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003790 * @pin: audio signal pin endpoint (or start point)
3791 *
3792 * Mark the given endpoint or pin as ignoring suspend. When the
3793 * system is disabled a path between two endpoints flagged as ignoring
3794 * suspend will not be disabled. The path must already be enabled via
3795 * normal means at suspend time, it will not be turned on if it was not
3796 * already enabled.
3797 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003798int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3799 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003800{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003801 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003802
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003803 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003804 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003805 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003806 }
3807
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003808 w->ignore_suspend = 1;
3809
3810 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003811}
3812EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3813
Stephen Warren16332812011-11-23 12:42:04 -07003814static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3815 struct snd_soc_dapm_widget *w)
3816{
3817 struct snd_soc_dapm_path *p;
3818
3819 list_for_each_entry(p, &card->paths, list) {
3820 if ((p->source == w) || (p->sink == w)) {
3821 dev_dbg(card->dev,
3822 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3823 p->source->name, p->source->id, p->source->dapm,
3824 p->sink->name, p->sink->id, p->sink->dapm);
3825
3826 /* Connected to something other than the codec */
3827 if (p->source->dapm != p->sink->dapm)
3828 return true;
3829 /*
3830 * Loopback connection from codec external pin to
3831 * codec external pin
3832 */
3833 if (p->sink->id == snd_soc_dapm_input) {
3834 switch (p->source->id) {
3835 case snd_soc_dapm_output:
3836 case snd_soc_dapm_micbias:
3837 return true;
3838 default:
3839 break;
3840 }
3841 }
3842 }
3843 }
3844
3845 return false;
3846}
3847
3848/**
3849 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3850 * @codec: The codec whose pins should be processed
3851 *
3852 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3853 * which are unused. Pins are used if they are connected externally to the
3854 * codec, whether that be to some other device, or a loop-back connection to
3855 * the codec itself.
3856 */
3857void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3858{
3859 struct snd_soc_card *card = codec->card;
3860 struct snd_soc_dapm_context *dapm = &codec->dapm;
3861 struct snd_soc_dapm_widget *w;
3862
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003863 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003864 &card->dapm, &codec->dapm);
3865
3866 list_for_each_entry(w, &card->widgets, list) {
3867 if (w->dapm != dapm)
3868 continue;
3869 switch (w->id) {
3870 case snd_soc_dapm_input:
3871 case snd_soc_dapm_output:
3872 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003873 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003874 w->name);
3875 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003876 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003877 "... Not in map; disabling\n");
3878 snd_soc_dapm_nc_pin(dapm, w->name);
3879 }
3880 break;
3881 default:
3882 break;
3883 }
3884 }
3885}
3886
Mark Brown1547aba2010-05-07 21:11:40 +01003887/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003888 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003889 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003890 *
3891 * Free all dapm widgets and resources.
3892 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003893void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003894{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003895 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003896 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003897 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003898 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003899}
3900EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3901
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003902static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003903{
Liam Girdwood01005a72012-07-06 16:57:05 +01003904 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003905 struct snd_soc_dapm_widget *w;
3906 LIST_HEAD(down_list);
3907 int powerdown = 0;
3908
Liam Girdwood01005a72012-07-06 16:57:05 +01003909 mutex_lock(&card->dapm_mutex);
3910
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003911 list_for_each_entry(w, &dapm->card->widgets, list) {
3912 if (w->dapm != dapm)
3913 continue;
Mark Brown51737472009-06-22 13:16:51 +01003914 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003915 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003916 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003917 powerdown = 1;
3918 }
3919 }
3920
3921 /* If there were no widgets to power down we're already in
3922 * standby.
3923 */
3924 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003925 if (dapm->bias_level == SND_SOC_BIAS_ON)
3926 snd_soc_dapm_set_bias_level(dapm,
3927 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003928 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003929 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3930 snd_soc_dapm_set_bias_level(dapm,
3931 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003932 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003933
3934 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003935}
Mark Brown51737472009-06-22 13:16:51 +01003936
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003937/*
3938 * snd_soc_dapm_shutdown - callback for system shutdown
3939 */
3940void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3941{
3942 struct snd_soc_codec *codec;
3943
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003944 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003945 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003946 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3947 snd_soc_dapm_set_bias_level(&codec->dapm,
3948 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003949 }
Mark Brown51737472009-06-22 13:16:51 +01003950}
3951
Richard Purdie2b97eab2006-10-06 18:32:18 +02003952/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003953MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003954MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3955MODULE_LICENSE("GPL");