blob: f23eeeeaafc556ecac544b47f87fa349c712f277 [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,
73 [snd_soc_dapm_virt_mux] = 6,
Mark Brown1dd275b2013-10-09 13:56:37 +010074 [snd_soc_dapm_dac] = 7,
75 [snd_soc_dapm_switch] = 8,
76 [snd_soc_dapm_mixer] = 8,
77 [snd_soc_dapm_mixer_named_ctl] = 8,
78 [snd_soc_dapm_pga] = 9,
79 [snd_soc_dapm_adc] = 10,
80 [snd_soc_dapm_out_drv] = 11,
81 [snd_soc_dapm_hp] = 11,
82 [snd_soc_dapm_spk] = 11,
83 [snd_soc_dapm_line] = 11,
84 [snd_soc_dapm_kcontrol] = 12,
85 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +020086};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000087
Richard Purdie2b97eab2006-10-06 18:32:18 +020088static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010089 [snd_soc_dapm_pre] = 0,
Lars-Peter Clausen57295072013-08-05 11:27:31 +020090 [snd_soc_dapm_kcontrol] = 1,
91 [snd_soc_dapm_adc] = 2,
92 [snd_soc_dapm_hp] = 3,
93 [snd_soc_dapm_spk] = 3,
94 [snd_soc_dapm_line] = 3,
95 [snd_soc_dapm_out_drv] = 3,
Mark Brown38357ab2009-06-06 19:03:23 +010096 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020097 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +010098 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010099 [snd_soc_dapm_mixer] = 5,
100 [snd_soc_dapm_dac] = 6,
101 [snd_soc_dapm_mic] = 7,
102 [snd_soc_dapm_micbias] = 8,
103 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000104 [snd_soc_dapm_virt_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +0100105 [snd_soc_dapm_aif_in] = 10,
106 [snd_soc_dapm_aif_out] = 10,
Mark Brown46162742013-06-05 19:36:11 +0100107 [snd_soc_dapm_dai_in] = 10,
108 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100109 [snd_soc_dapm_dai_link] = 11,
Mark Brownc74184e2012-04-04 22:12:09 +0100110 [snd_soc_dapm_supply] = 12,
Mark Brown1dd275b2013-10-09 13:56:37 +0100111 [snd_soc_dapm_clock_supply] = 13,
112 [snd_soc_dapm_regulator_supply] = 13,
113 [snd_soc_dapm_post] = 14,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200114};
115
Troy Kisky12ef1932008-10-13 17:42:14 -0700116static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100117{
118 if (pop_time)
119 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
120}
121
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200122static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100123{
124 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200125 char *buf;
126
127 if (!pop_time)
128 return;
129
130 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
131 if (buf == NULL)
132 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100133
134 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200135 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100136 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100137 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200138
139 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100140}
141
Mark Browndb432b42011-10-03 21:06:40 +0100142static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
143{
144 return !list_empty(&w->dirty);
145}
146
Mark Brown25c77c52011-10-08 13:36:03 +0100147void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100148{
Mark Brown75c1f892011-10-04 22:28:08 +0100149 if (!dapm_dirty_widget(w)) {
150 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
151 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100152 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100153 }
Mark Browndb432b42011-10-03 21:06:40 +0100154}
Mark Brown25c77c52011-10-08 13:36:03 +0100155EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100156
Mark Browne2d32ff2012-08-31 17:38:32 -0700157void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
158{
159 struct snd_soc_card *card = dapm->card;
160 struct snd_soc_dapm_widget *w;
161
162 mutex_lock(&card->dapm_mutex);
163
164 list_for_each_entry(w, &card->widgets, list) {
165 switch (w->id) {
166 case snd_soc_dapm_input:
167 case snd_soc_dapm_output:
168 dapm_mark_dirty(w, "Rechecking inputs and outputs");
169 break;
170 default:
171 break;
172 }
173 }
174
175 mutex_unlock(&card->dapm_mutex);
176}
177EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
178
Richard Purdie2b97eab2006-10-06 18:32:18 +0200179/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100180static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200181 const struct snd_soc_dapm_widget *_widget)
182{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100183 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200184}
185
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200186struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200187 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200188 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200189 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200190 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200191};
192
193static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
194 struct snd_kcontrol *kcontrol)
195{
196 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200197 struct soc_mixer_control *mc;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200198
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200199 data = kzalloc(sizeof(*data), GFP_KERNEL);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200200 if (!data) {
201 dev_err(widget->dapm->dev,
202 "ASoC: can't allocate kcontrol data for %s\n",
203 widget->name);
204 return -ENOMEM;
205 }
206
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200207 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200208
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200209 switch (widget->id) {
210 case snd_soc_dapm_switch:
211 case snd_soc_dapm_mixer:
212 case snd_soc_dapm_mixer_named_ctl:
213 mc = (struct soc_mixer_control *)kcontrol->private_value;
214
215 if (mc->autodisable) {
216 struct snd_soc_dapm_widget template;
217
218 memset(&template, 0, sizeof(template));
219 template.reg = mc->reg;
220 template.mask = (1 << fls(mc->max)) - 1;
221 template.shift = mc->shift;
222 if (mc->invert)
223 template.off_val = mc->max;
224 else
225 template.off_val = 0;
226 template.on_val = template.off_val;
227 template.id = snd_soc_dapm_kcontrol;
228 template.name = kcontrol->id.name;
229
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200230 data->value = template.on_val;
231
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200232 data->widget = snd_soc_dapm_new_control(widget->dapm,
233 &template);
234 if (!data->widget) {
235 kfree(data);
236 return -ENOMEM;
237 }
238 }
239 break;
240 default:
241 break;
242 }
243
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200244 kcontrol->private_data = data;
245
246 return 0;
247}
248
249static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
250{
251 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200252 kfree(data->widget);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200253 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200254 kfree(data);
255}
256
257static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
258 const struct snd_kcontrol *kcontrol)
259{
260 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
261
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200262 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200263}
264
265static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
266 struct snd_soc_dapm_widget *widget)
267{
268 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200269 struct snd_soc_dapm_widget_list *new_wlist;
270 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200271
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200272 if (data->wlist)
273 n = data->wlist->num_widgets + 1;
274 else
275 n = 1;
276
277 new_wlist = krealloc(data->wlist,
278 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
279 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200280 return -ENOMEM;
281
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200282 new_wlist->widgets[n - 1] = widget;
283 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200284
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200285 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200286
287 return 0;
288}
289
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200290static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
291 struct snd_soc_dapm_path *path)
292{
293 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
294
295 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200296
297 if (data->widget) {
298 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
299 path->source, NULL, NULL);
300 }
301}
302
303static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
304{
305 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
306
307 if (!data->widget)
308 return true;
309
310 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200311}
312
313static struct list_head *dapm_kcontrol_get_path_list(
314 const struct snd_kcontrol *kcontrol)
315{
316 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
317
318 return &data->paths;
319}
320
321#define dapm_kcontrol_for_each_path(path, kcontrol) \
322 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
323 list_kcontrol)
324
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200325static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
326{
327 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
328
329 return data->value;
330}
331
332static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
333 unsigned int value)
334{
335 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
336
337 if (data->value == value)
338 return false;
339
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200340 if (data->widget)
341 data->widget->on_val = value;
342
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200343 data->value = value;
344
345 return true;
346}
347
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200348/**
349 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
350 * @kcontrol: The kcontrol
351 */
352struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
353{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200354 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200355}
356EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
357
Liam Girdwood6c120e12012-02-15 15:15:34 +0000358static void dapm_reset(struct snd_soc_card *card)
359{
360 struct snd_soc_dapm_widget *w;
361
362 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
363
364 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200365 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000366 w->power_checked = false;
367 w->inputs = -1;
368 w->outputs = -1;
369 }
370}
371
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800372static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg,
373 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100374{
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800375 if (w->codec) {
376 *value = snd_soc_read(w->codec, reg);
377 return 0;
378 } else if (w->platform) {
379 *value = snd_soc_platform_read(w->platform, reg);
380 return 0;
381 }
Liam Girdwoodb7950642011-07-04 22:10:52 +0100382
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000383 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100384 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100385}
386
Lars-Peter Clausen30ac6b62014-02-13 14:14:21 +0100387static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg,
388 unsigned int val)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100389{
390 if (w->codec)
391 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100392 else if (w->platform)
393 return snd_soc_platform_write(w->platform, reg, val);
394
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000395 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100396 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100397}
398
Liam Girdwood49575fb52012-03-06 18:16:19 +0000399static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
400{
Mark Browne06ab3b2012-03-06 23:58:22 +0000401 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000402 mutex_lock(&w->codec->mutex);
403 else if (w->platform)
404 mutex_lock(&w->platform->mutex);
405}
406
407static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
408{
Mark Browne06ab3b2012-03-06 23:58:22 +0000409 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000410 mutex_unlock(&w->codec->mutex);
411 else if (w->platform)
412 mutex_unlock(&w->platform->mutex);
413}
414
Mark Browneb270e92013-10-09 13:52:52 +0100415static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
416{
417 if (dapm->codec && dapm->codec->using_regmap)
418 regmap_async_complete(dapm->codec->control_data);
419}
420
Liam Girdwood49575fb52012-03-06 18:16:19 +0000421static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100422 unsigned short reg, unsigned int mask, unsigned int value)
423{
Mark Brown8a713da2011-12-03 12:33:55 +0000424 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100425 unsigned int old, new;
426 int ret;
427
Mark Brown8a713da2011-12-03 12:33:55 +0000428 if (w->codec && w->codec->using_regmap) {
Mark Browneb270e92013-10-09 13:52:52 +0100429 ret = regmap_update_bits_check_async(w->codec->control_data,
430 reg, mask, value,
431 &change);
Mark Brown8a713da2011-12-03 12:33:55 +0000432 if (ret != 0)
433 return ret;
434 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000435 soc_widget_lock(w);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800436 ret = soc_widget_read(w, reg, &old);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000437 if (ret < 0) {
438 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100439 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000440 }
Mark Brown8a713da2011-12-03 12:33:55 +0000441
Mark Brown8a713da2011-12-03 12:33:55 +0000442 new = (old & ~mask) | (value & mask);
443 change = old != new;
444 if (change) {
445 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000446 if (ret < 0) {
447 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000448 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000449 }
Mark Brown8a713da2011-12-03 12:33:55 +0000450 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000451 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100452 }
453
454 return change;
455}
456
Mark Brown452c5ea2009-05-17 21:41:23 +0100457/**
458 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800459 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100460 * @level: level to configure
461 *
462 * Configure the bias (power) levels for the SoC audio device.
463 *
464 * Returns 0 for success else error.
465 */
Mark Browned5a4c42011-02-18 11:12:42 -0800466static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200467 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100468{
Mark Browned5a4c42011-02-18 11:12:42 -0800469 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100470 int ret = 0;
471
Mark Brown84e90932010-11-04 00:07:02 -0400472 trace_snd_soc_bias_level_start(card, level);
473
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000474 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100475 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100476 if (ret != 0)
477 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100478
Mark Browncc4c6702011-06-06 19:03:34 +0100479 if (dapm->codec) {
480 if (dapm->codec->driver->set_bias_level)
481 ret = dapm->codec->driver->set_bias_level(dapm->codec,
482 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100483 else
484 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100485 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100486 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100487 }
Liam Girdwood41231282012-07-06 16:56:16 +0100488
Mark Brown171ec6b2011-06-06 18:15:19 +0100489 if (ret != 0)
490 goto out;
491
492 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100493 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100494out:
Mark Brown84e90932010-11-04 00:07:02 -0400495 trace_snd_soc_bias_level_done(card, level);
496
Mark Brown452c5ea2009-05-17 21:41:23 +0100497 return ret;
498}
499
Richard Purdie2b97eab2006-10-06 18:32:18 +0200500/* set up initial codec paths */
501static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
502 struct snd_soc_dapm_path *p, int i)
503{
504 switch (w->id) {
505 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000506 case snd_soc_dapm_mixer:
507 case snd_soc_dapm_mixer_named_ctl: {
Lars-Peter Clausen30ac6b62014-02-13 14:14:21 +0100508 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100509 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600510 w->kcontrol_news[i].private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +0200511 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400512 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100513 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400514 unsigned int mask = (1 << fls(max)) - 1;
515 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200516
Lars-Peter Clausen249ce132013-10-06 13:43:49 +0200517 if (reg != SND_SOC_NOPM) {
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800518 soc_widget_read(w, reg, &val);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +0200519 val = (val >> shift) & mask;
520 if (invert)
521 val = max - val;
522 p->connect = !!val;
523 } else {
524 p->connect = 0;
525 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200526
Richard Purdie2b97eab2006-10-06 18:32:18 +0200527 }
528 break;
529 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600530 struct soc_enum *e = (struct soc_enum *)
531 w->kcontrol_news[i].private_value;
Lars-Peter Clausen30ac6b62014-02-13 14:14:21 +0100532 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200533
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800534 soc_widget_read(w, e->reg, &val);
Lars-Peter Clausen3727b492014-02-28 08:31:04 +0100535 val = (val >> e->shift_l) & e->mask;
536 item = snd_soc_enum_val_to_item(e, val);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200537
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100538 if (item < e->items && !strcmp(p->name, e->texts[item]))
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200539 p->connect = 1;
540 else
541 p->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200542 }
543 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000544 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600545 struct soc_enum *e = (struct soc_enum *)
546 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000547
548 p->connect = 0;
549 /* since a virtual mux has no backing registers to
550 * decide which path to connect, it will try to match
551 * with the first enumeration. This is to ensure
552 * that the default mux choice (the first) will be
553 * correctly powered up during initialization.
554 */
555 if (!strcmp(p->name, e->texts[0]))
556 p->connect = 1;
557 }
558 break;
Mark Brown56563102011-10-03 22:41:09 +0100559 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200560 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600561 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200562 case snd_soc_dapm_output:
563 case snd_soc_dapm_adc:
564 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000565 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200566 case snd_soc_dapm_dac:
567 case snd_soc_dapm_micbias:
568 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100569 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000570 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200571 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100572 case snd_soc_dapm_aif_in:
573 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100574 case snd_soc_dapm_dai_in:
575 case snd_soc_dapm_dai_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200576 case snd_soc_dapm_hp:
577 case snd_soc_dapm_mic:
578 case snd_soc_dapm_spk:
579 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100580 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200581 case snd_soc_dapm_kcontrol:
Mark Brown56563102011-10-03 22:41:09 +0100582 p->connect = 1;
583 break;
584 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200585 case snd_soc_dapm_pre:
586 case snd_soc_dapm_post:
587 p->connect = 0;
588 break;
589 }
590}
591
Mark Brown74b8f952009-06-06 11:26:15 +0100592/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200593static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200594 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
595 struct snd_soc_dapm_path *path, const char *control_name,
596 const struct snd_kcontrol_new *kcontrol)
597{
598 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
599 int i;
600
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100601 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200602 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200603 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200604 list_add(&path->list_sink, &dest->sources);
605 list_add(&path->list_source, &src->sinks);
606 path->name = (char*)e->texts[i];
607 dapm_set_path_status(dest, path, 0);
608 return 0;
609 }
610 }
611
612 return -ENODEV;
613}
614
Mark Brown74b8f952009-06-06 11:26:15 +0100615/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200616static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200617 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
618 struct snd_soc_dapm_path *path, const char *control_name)
619{
620 int i;
621
622 /* search for mixer kcontrol */
623 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600624 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200625 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200626 list_add(&path->list_sink, &dest->sources);
627 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600628 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200629 dapm_set_path_status(dest, path, i);
630 return 0;
631 }
632 }
633 return -ENODEV;
634}
635
Stephen Warrenaf468002011-04-28 17:38:01 -0600636static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600637 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600638 const struct snd_kcontrol_new *kcontrol_new,
639 struct snd_kcontrol **kcontrol)
640{
641 struct snd_soc_dapm_widget *w;
642 int i;
643
644 *kcontrol = NULL;
645
646 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600647 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
648 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600649 for (i = 0; i < w->num_kcontrols; i++) {
650 if (&w->kcontrol_news[i] == kcontrol_new) {
651 if (w->kcontrols)
652 *kcontrol = w->kcontrols[i];
653 return 1;
654 }
655 }
656 }
657
658 return 0;
659}
660
Stephen Warren85762e72013-03-29 15:40:10 -0600661/*
662 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
663 * create it. Either way, add the widget into the control's widget list
664 */
665static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100666 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200667{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200668 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000669 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000670 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600671 size_t prefix_len;
672 int shared;
673 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600674 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600675 char *long_name;
676 const char *name;
677 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000678
679 if (dapm->codec)
680 prefix = dapm->codec->name_prefix;
681 else
682 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200683
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000684 if (prefix)
685 prefix_len = strlen(prefix) + 1;
686 else
687 prefix_len = 0;
688
Stephen Warren85762e72013-03-29 15:40:10 -0600689 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
690 &kcontrol);
691
Stephen Warren85762e72013-03-29 15:40:10 -0600692 if (!kcontrol) {
693 if (shared) {
694 wname_in_long_name = false;
695 kcname_in_long_name = true;
696 } else {
697 switch (w->id) {
698 case snd_soc_dapm_switch:
699 case snd_soc_dapm_mixer:
700 wname_in_long_name = true;
701 kcname_in_long_name = true;
702 break;
703 case snd_soc_dapm_mixer_named_ctl:
704 wname_in_long_name = false;
705 kcname_in_long_name = true;
706 break;
707 case snd_soc_dapm_mux:
708 case snd_soc_dapm_virt_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600709 wname_in_long_name = true;
710 kcname_in_long_name = false;
711 break;
712 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600713 return -EINVAL;
714 }
715 }
716
717 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600718 /*
719 * The control will get a prefix from the control
720 * creation process but we're also using the same
721 * prefix for widgets so cut the prefix off the
722 * front of the widget name.
723 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200724 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600725 w->name + prefix_len,
726 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200727 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200728 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600729
730 name = long_name;
731 } else if (wname_in_long_name) {
732 long_name = NULL;
733 name = w->name + prefix_len;
734 } else {
735 long_name = NULL;
736 name = w->kcontrol_news[kci].name;
737 }
738
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200739 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600740 prefix);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200741 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200742 if (!kcontrol)
743 return -ENOMEM;
744 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200745
746 ret = dapm_kcontrol_data_alloc(w, kcontrol);
747 if (ret) {
748 snd_ctl_free_one(kcontrol);
749 return ret;
750 }
751
Stephen Warren85762e72013-03-29 15:40:10 -0600752 ret = snd_ctl_add(card, kcontrol);
753 if (ret < 0) {
754 dev_err(dapm->dev,
755 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
756 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600757 return ret;
758 }
Stephen Warren85762e72013-03-29 15:40:10 -0600759 }
760
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200761 ret = dapm_kcontrol_add_widget(kcontrol, w);
762 if (ret)
763 return ret;
764
Stephen Warren85762e72013-03-29 15:40:10 -0600765 w->kcontrols[kci] = kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600766
767 return 0;
768}
769
770/* create new dapm mixer control */
771static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
772{
773 int i, ret;
774 struct snd_soc_dapm_path *path;
775
Richard Purdie2b97eab2006-10-06 18:32:18 +0200776 /* add kcontrol */
777 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200778 /* match name */
779 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200780 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600781 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200782 continue;
783
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200784 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200785 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200786 continue;
787 }
788
Mark Brown946d92a2013-08-12 23:28:42 +0100789 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600790 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200791 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100792
793 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200794 }
795 }
Stephen Warren85762e72013-03-29 15:40:10 -0600796
797 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200798}
799
800/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200801static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200802{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200803 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600804 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600805 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200806
Stephen Warrenaf468002011-04-28 17:38:01 -0600807 if (w->num_kcontrols != 1) {
808 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000809 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600810 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200811 return -EINVAL;
812 }
813
Lars-Peter Clausenfe581392013-08-01 18:30:38 +0200814 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600815 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
816 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600817 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200818
Mark Brown946d92a2013-08-12 23:28:42 +0100819 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600820 if (ret < 0)
821 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600822
Richard Purdie2b97eab2006-10-06 18:32:18 +0200823 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200824 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200825
Stephen Warrenaf468002011-04-28 17:38:01 -0600826 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200827}
828
829/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200830static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200831{
Mark Browna6c65732010-03-03 17:45:21 +0000832 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200833 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000834 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200835
Mark Browna6c65732010-03-03 17:45:21 +0000836 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200837}
838
839/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100840static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
841 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200842{
843 struct snd_soc_dapm_path *p;
844
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100845 list_for_each_entry(p, sink, list_source) {
846 if (p->walked) {
847 p->walked = 0;
848 dapm_clear_walk_output(dapm, &p->sink->sinks);
849 }
850 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200851}
852
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100853static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
854 struct list_head *source)
855{
856 struct snd_soc_dapm_path *p;
857
858 list_for_each_entry(p, source, list_sink) {
859 if (p->walked) {
860 p->walked = 0;
861 dapm_clear_walk_input(dapm, &p->source->sources);
862 }
863 }
864}
865
866
Mark Brown99497882010-05-07 20:24:05 +0100867/* We implement power down on suspend by checking the power state of
868 * the ALSA card - when we are suspending the ALSA state for the card
869 * is set to D3.
870 */
871static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
872{
Mark Brown12ea2c72011-03-02 18:17:32 +0000873 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100874
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000875 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100876 case SNDRV_CTL_POWER_D3hot:
877 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100878 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000879 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200880 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100881 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100882 default:
883 return 1;
884 }
885}
886
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100887/* add widget to list if it's not already in the list */
888static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
889 struct snd_soc_dapm_widget *w)
890{
891 struct snd_soc_dapm_widget_list *wlist;
892 int wlistsize, wlistentries, i;
893
894 if (*list == NULL)
895 return -EINVAL;
896
897 wlist = *list;
898
899 /* is this widget already in the list */
900 for (i = 0; i < wlist->num_widgets; i++) {
901 if (wlist->widgets[i] == w)
902 return 0;
903 }
904
905 /* allocate some new space */
906 wlistentries = wlist->num_widgets + 1;
907 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
908 wlistentries * sizeof(struct snd_soc_dapm_widget *);
909 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
910 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000911 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100912 w->name);
913 return -ENOMEM;
914 }
915 wlist = *list;
916
917 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000918 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100919 w->name, wlist->num_widgets);
920
921 wlist->widgets[wlist->num_widgets] = w;
922 wlist->num_widgets++;
923 return 1;
924}
925
Richard Purdie2b97eab2006-10-06 18:32:18 +0200926/*
927 * Recursively check for a completed path to an active or physically connected
928 * output widget. Returns number of complete paths.
929 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100930static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
931 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200932{
933 struct snd_soc_dapm_path *path;
934 int con = 0;
935
Mark Brown024dc072011-10-09 11:52:05 +0100936 if (widget->outputs >= 0)
937 return widget->outputs;
938
Mark Brownde02d072011-09-20 21:43:24 +0100939 DAPM_UPDATE_STAT(widget, path_checks);
940
Mark Brown62ea8742012-01-21 21:14:48 +0000941 switch (widget->id) {
942 case snd_soc_dapm_supply:
943 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200944 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200945 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100946 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000947 default:
948 break;
949 }
Mark Brown246d0a12009-04-22 18:24:55 +0100950
Mark Brown010ff262009-08-17 17:39:22 +0100951 switch (widget->id) {
952 case snd_soc_dapm_adc:
953 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100954 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100955 if (widget->active) {
956 widget->outputs = snd_soc_dapm_suspend_check(widget);
957 return widget->outputs;
958 }
Mark Brown010ff262009-08-17 17:39:22 +0100959 default:
960 break;
961 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200962
963 if (widget->connected) {
964 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100965 if (widget->id == snd_soc_dapm_output && !widget->ext) {
966 widget->outputs = snd_soc_dapm_suspend_check(widget);
967 return widget->outputs;
968 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200969
970 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100971 if (widget->id == snd_soc_dapm_hp ||
972 widget->id == snd_soc_dapm_spk ||
973 (widget->id == snd_soc_dapm_line &&
974 !list_empty(&widget->sources))) {
975 widget->outputs = snd_soc_dapm_suspend_check(widget);
976 return widget->outputs;
977 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200978 }
979
980 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100981 DAPM_UPDATE_STAT(widget, neighbour_checks);
982
Mark Brownbf3a9e12011-06-13 16:42:29 +0100983 if (path->weak)
984 continue;
985
Mark Brown8af294b2013-02-22 17:48:15 +0000986 if (path->walking)
987 return 1;
988
Richard Purdie2b97eab2006-10-06 18:32:18 +0200989 if (path->walked)
990 continue;
991
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100992 trace_snd_soc_dapm_output_path(widget, path);
993
Richard Purdie2b97eab2006-10-06 18:32:18 +0200994 if (path->sink && path->connect) {
995 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000996 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100997
998 /* do we need to add this widget to the list ? */
999 if (list) {
1000 int err;
1001 err = dapm_list_add_widget(list, path->sink);
1002 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001003 dev_err(widget->dapm->dev,
1004 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001005 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001006 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001007 return con;
1008 }
1009 }
1010
1011 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001012
1013 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001014 }
1015 }
1016
Mark Brown024dc072011-10-09 11:52:05 +01001017 widget->outputs = con;
1018
Richard Purdie2b97eab2006-10-06 18:32:18 +02001019 return con;
1020}
1021
1022/*
1023 * Recursively check for a completed path to an active or physically connected
1024 * input widget. Returns number of complete paths.
1025 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001026static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1027 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001028{
1029 struct snd_soc_dapm_path *path;
1030 int con = 0;
1031
Mark Brown024dc072011-10-09 11:52:05 +01001032 if (widget->inputs >= 0)
1033 return widget->inputs;
1034
Mark Brownde02d072011-09-20 21:43:24 +01001035 DAPM_UPDATE_STAT(widget, path_checks);
1036
Mark Brown62ea8742012-01-21 21:14:48 +00001037 switch (widget->id) {
1038 case snd_soc_dapm_supply:
1039 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001040 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001041 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +01001042 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +00001043 default:
1044 break;
1045 }
Mark Brown246d0a12009-04-22 18:24:55 +01001046
Richard Purdie2b97eab2006-10-06 18:32:18 +02001047 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +01001048 switch (widget->id) {
1049 case snd_soc_dapm_dac:
1050 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +01001051 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +01001052 if (widget->active) {
1053 widget->inputs = snd_soc_dapm_suspend_check(widget);
1054 return widget->inputs;
1055 }
Mark Brown010ff262009-08-17 17:39:22 +01001056 default:
1057 break;
1058 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001059
1060 if (widget->connected) {
1061 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +01001062 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1063 widget->inputs = snd_soc_dapm_suspend_check(widget);
1064 return widget->inputs;
1065 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001066
1067 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +01001068 if (widget->id == snd_soc_dapm_vmid) {
1069 widget->inputs = snd_soc_dapm_suspend_check(widget);
1070 return widget->inputs;
1071 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001072
1073 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +03001074 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +01001075 (widget->id == snd_soc_dapm_line &&
1076 !list_empty(&widget->sinks))) {
1077 widget->inputs = snd_soc_dapm_suspend_check(widget);
1078 return widget->inputs;
1079 }
1080
Mark Brown1ab97c82011-11-27 16:21:51 +00001081 /* signal generator */
1082 if (widget->id == snd_soc_dapm_siggen) {
1083 widget->inputs = snd_soc_dapm_suspend_check(widget);
1084 return widget->inputs;
1085 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001086 }
1087
1088 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +01001089 DAPM_UPDATE_STAT(widget, neighbour_checks);
1090
Mark Brownbf3a9e12011-06-13 16:42:29 +01001091 if (path->weak)
1092 continue;
1093
Mark Brown8af294b2013-02-22 17:48:15 +00001094 if (path->walking)
1095 return 1;
1096
Richard Purdie2b97eab2006-10-06 18:32:18 +02001097 if (path->walked)
1098 continue;
1099
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001100 trace_snd_soc_dapm_input_path(widget, path);
1101
Richard Purdie2b97eab2006-10-06 18:32:18 +02001102 if (path->source && path->connect) {
1103 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001104 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001105
1106 /* do we need to add this widget to the list ? */
1107 if (list) {
1108 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001109 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001110 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001111 dev_err(widget->dapm->dev,
1112 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001113 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001114 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001115 return con;
1116 }
1117 }
1118
1119 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001120
1121 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001122 }
1123 }
1124
Mark Brown024dc072011-10-09 11:52:05 +01001125 widget->inputs = con;
1126
Richard Purdie2b97eab2006-10-06 18:32:18 +02001127 return con;
1128}
1129
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001130/**
1131 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1132 * @dai: the soc DAI.
1133 * @stream: stream direction.
1134 * @list: list of active widgets for this stream.
1135 *
1136 * Queries DAPM graph as to whether an valid audio stream path exists for
1137 * the initial stream specified by name. This takes into account
1138 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1139 *
1140 * Returns the number of valid paths or negative error.
1141 */
1142int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1143 struct snd_soc_dapm_widget_list **list)
1144{
1145 struct snd_soc_card *card = dai->card;
1146 int paths;
1147
1148 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1149 dapm_reset(card);
1150
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001151 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001152 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001153 dapm_clear_walk_output(&card->dapm,
1154 &dai->playback_widget->sinks);
1155 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001156 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001157 dapm_clear_walk_input(&card->dapm,
1158 &dai->capture_widget->sources);
1159 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001160
1161 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001162 mutex_unlock(&card->dapm_mutex);
1163
1164 return paths;
1165}
1166
Richard Purdie2b97eab2006-10-06 18:32:18 +02001167/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001168 * Handler for generic register modifier widget.
1169 */
1170int dapm_reg_event(struct snd_soc_dapm_widget *w,
1171 struct snd_kcontrol *kcontrol, int event)
1172{
1173 unsigned int val;
1174
1175 if (SND_SOC_DAPM_EVENT_ON(event))
1176 val = w->on_val;
1177 else
1178 val = w->off_val;
1179
Liam Girdwood49575fb52012-03-06 18:16:19 +00001180 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001181 w->mask << w->shift, val << w->shift);
1182
1183 return 0;
1184}
Mark Brown11589412008-07-29 11:42:23 +01001185EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001186
Mark Brown62ea8742012-01-21 21:14:48 +00001187/*
1188 * Handler for regulator supply widget.
1189 */
1190int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1191 struct snd_kcontrol *kcontrol, int event)
1192{
Mark Brownc05b84d2012-09-07 12:57:11 +08001193 int ret;
1194
Mark Browneb270e92013-10-09 13:52:52 +01001195 soc_dapm_async_complete(w->dapm);
1196
Mark Brownc05b84d2012-09-07 12:57:11 +08001197 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001198 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001199 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001200 if (ret != 0)
1201 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001202 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001203 w->name, ret);
1204 }
1205
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001206 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001207 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001208 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001209 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001210 if (ret != 0)
1211 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001212 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001213 w->name, ret);
1214 }
1215
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001216 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001217 }
Mark Brown62ea8742012-01-21 21:14:48 +00001218}
1219EXPORT_SYMBOL_GPL(dapm_regulator_event);
1220
Ola Liljad7e7eb92012-05-24 15:26:25 +02001221/*
1222 * Handler for clock supply widget.
1223 */
1224int dapm_clock_event(struct snd_soc_dapm_widget *w,
1225 struct snd_kcontrol *kcontrol, int event)
1226{
1227 if (!w->clk)
1228 return -EIO;
1229
Mark Browneb270e92013-10-09 13:52:52 +01001230 soc_dapm_async_complete(w->dapm);
1231
Mark Brownec029952012-06-04 08:16:20 +01001232#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001233 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001234 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001235 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001236 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001237 return 0;
1238 }
Mark Brownec029952012-06-04 08:16:20 +01001239#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001240 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001241}
1242EXPORT_SYMBOL_GPL(dapm_clock_event);
1243
Mark Brownd8050022011-09-28 18:28:23 +01001244static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1245{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001246 if (w->power_checked)
1247 return w->new_power;
1248
Mark Brownd8050022011-09-28 18:28:23 +01001249 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001250 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001251 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001252 w->new_power = w->power_check(w);
1253
1254 w->power_checked = true;
1255
1256 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001257}
1258
Mark Browncd0f2d42009-04-20 16:56:59 +01001259/* Generic check to see if a widget should be powered.
1260 */
1261static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1262{
1263 int in, out;
1264
Mark Brownde02d072011-09-20 21:43:24 +01001265 DAPM_UPDATE_STAT(w, power_checks);
1266
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001267 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001268 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001269 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001270 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001271 return out != 0 && in != 0;
1272}
1273
Mark Brown6ea31b92009-04-20 17:15:41 +01001274/* Check to see if an ADC has power */
1275static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1276{
1277 int in;
1278
Mark Brownde02d072011-09-20 21:43:24 +01001279 DAPM_UPDATE_STAT(w, power_checks);
1280
Mark Brown6ea31b92009-04-20 17:15:41 +01001281 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001282 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001283 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001284 return in != 0;
1285 } else {
1286 return dapm_generic_check_power(w);
1287 }
1288}
1289
1290/* Check to see if a DAC has power */
1291static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1292{
1293 int out;
1294
Mark Brownde02d072011-09-20 21:43:24 +01001295 DAPM_UPDATE_STAT(w, power_checks);
1296
Mark Brown6ea31b92009-04-20 17:15:41 +01001297 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001298 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001299 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001300 return out != 0;
1301 } else {
1302 return dapm_generic_check_power(w);
1303 }
1304}
1305
Mark Brown246d0a12009-04-22 18:24:55 +01001306/* Check to see if a power supply is needed */
1307static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1308{
1309 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001310
Mark Brownde02d072011-09-20 21:43:24 +01001311 DAPM_UPDATE_STAT(w, power_checks);
1312
Mark Brown246d0a12009-04-22 18:24:55 +01001313 /* Check if one of our outputs is connected */
1314 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001315 DAPM_UPDATE_STAT(w, neighbour_checks);
1316
Mark Brownbf3a9e12011-06-13 16:42:29 +01001317 if (path->weak)
1318 continue;
1319
Mark Brown215edda2009-09-08 18:59:05 +01001320 if (path->connected &&
1321 !path->connected(path->source, path->sink))
1322 continue;
1323
Mark Brown30173582011-02-11 11:42:19 +00001324 if (!path->sink)
1325 continue;
1326
Mark Brownf68d7e12011-10-04 22:57:50 +01001327 if (dapm_widget_power_check(path->sink))
1328 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001329 }
1330
Mark Brownf68d7e12011-10-04 22:57:50 +01001331 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001332}
1333
Mark Brown35c64bc2011-09-28 18:23:53 +01001334static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1335{
1336 return 1;
1337}
1338
Mark Brown38357ab2009-06-06 19:03:23 +01001339static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1340 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001341 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001342{
Mark Brown828a8422011-01-15 13:14:30 +00001343 int *sort;
1344
1345 if (power_up)
1346 sort = dapm_up_seq;
1347 else
1348 sort = dapm_down_seq;
1349
Mark Brown38357ab2009-06-06 19:03:23 +01001350 if (sort[a->id] != sort[b->id])
1351 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001352 if (a->subseq != b->subseq) {
1353 if (power_up)
1354 return a->subseq - b->subseq;
1355 else
1356 return b->subseq - a->subseq;
1357 }
Mark Brownb22ead22009-06-07 12:51:26 +01001358 if (a->reg != b->reg)
1359 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001360 if (a->dapm != b->dapm)
1361 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001362
Mark Brown38357ab2009-06-06 19:03:23 +01001363 return 0;
1364}
Mark Brown42aa3412009-03-01 19:21:10 +00001365
Mark Brown38357ab2009-06-06 19:03:23 +01001366/* Insert a widget in order into a DAPM power sequence. */
1367static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1368 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001369 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001370{
1371 struct snd_soc_dapm_widget *w;
1372
1373 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001374 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001375 list_add_tail(&new_widget->power_list, &w->power_list);
1376 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001377 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001378
Mark Brown38357ab2009-06-06 19:03:23 +01001379 list_add_tail(&new_widget->power_list, list);
1380}
Mark Brown42aa3412009-03-01 19:21:10 +00001381
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001382static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001383 struct snd_soc_dapm_widget *w, int event)
1384{
Mark Brown68f89ad2010-11-03 23:51:49 -04001385 const char *ev_name;
1386 int power, ret;
1387
1388 switch (event) {
1389 case SND_SOC_DAPM_PRE_PMU:
1390 ev_name = "PRE_PMU";
1391 power = 1;
1392 break;
1393 case SND_SOC_DAPM_POST_PMU:
1394 ev_name = "POST_PMU";
1395 power = 1;
1396 break;
1397 case SND_SOC_DAPM_PRE_PMD:
1398 ev_name = "PRE_PMD";
1399 power = 0;
1400 break;
1401 case SND_SOC_DAPM_POST_PMD:
1402 ev_name = "POST_PMD";
1403 power = 0;
1404 break;
Mark Brown80114122013-02-25 15:14:19 +00001405 case SND_SOC_DAPM_WILL_PMU:
1406 ev_name = "WILL_PMU";
1407 power = 1;
1408 break;
1409 case SND_SOC_DAPM_WILL_PMD:
1410 ev_name = "WILL_PMD";
1411 power = 0;
1412 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001413 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001414 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001415 return;
1416 }
1417
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001418 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001419 return;
1420
1421 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001422 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001423 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001424 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001425 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001426 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001427 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001428 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001429 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001430 ev_name, w->name, ret);
1431 }
1432}
1433
Mark Brownb22ead22009-06-07 12:51:26 +01001434/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001435static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001436 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001437{
Mark Brown68f89ad2010-11-03 23:51:49 -04001438 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001439 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001440 unsigned int value = 0;
1441 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001442
1443 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1444 power_list)->reg;
1445
1446 list_for_each_entry(w, pending, power_list) {
Takashi Iwaibf4edea2013-11-07 18:38:47 +01001447 WARN_ON(reg != w->reg);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001448 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001449
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001450 mask |= w->mask << w->shift;
1451 if (w->power)
1452 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001453 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001454 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001455
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001456 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001457 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1458 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001459
Mark Brown68f89ad2010-11-03 23:51:49 -04001460 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001461 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1462 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001463 }
1464
Mark Brown81628102009-06-07 13:21:24 +01001465 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001466 /* Any widget will do, they should all be updating the
1467 * same register.
1468 */
1469 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1470 power_list);
1471
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001472 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001473 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001474 value, mask, reg, card->pop_time);
1475 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001476 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001477 }
1478
1479 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001480 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1481 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001482 }
Mark Brown42aa3412009-03-01 19:21:10 +00001483}
1484
Mark Brownb22ead22009-06-07 12:51:26 +01001485/* Apply a DAPM power sequence.
1486 *
1487 * We walk over a pre-sorted list of widgets to apply power to. In
1488 * order to minimise the number of writes to the device required
1489 * multiple widgets will be updated in a single write where possible.
1490 * Currently anything that requires more than a single write is not
1491 * handled.
1492 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001493static void dapm_seq_run(struct snd_soc_card *card,
1494 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001495{
1496 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001497 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001498 LIST_HEAD(pending);
1499 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001500 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001501 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001502 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001503 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001504 int *sort;
1505
1506 if (power_up)
1507 sort = dapm_up_seq;
1508 else
1509 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001510
Mark Brownb22ead22009-06-07 12:51:26 +01001511 list_for_each_entry_safe(w, n, list, power_list) {
1512 ret = 0;
1513
1514 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001515 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001516 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001517 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001518 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001519
Mark Brown474b62d2011-01-18 16:14:44 +00001520 if (cur_dapm && cur_dapm->seq_notifier) {
1521 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1522 if (sort[i] == cur_sort)
1523 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001524 i,
1525 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001526 }
1527
Mark Browneb270e92013-10-09 13:52:52 +01001528 if (cur_dapm && w->dapm != cur_dapm)
1529 soc_dapm_async_complete(cur_dapm);
1530
Mark Brownb22ead22009-06-07 12:51:26 +01001531 INIT_LIST_HEAD(&pending);
1532 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001533 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001534 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001535 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001536 }
1537
Mark Brown163cac02009-06-07 10:12:52 +01001538 switch (w->id) {
1539 case snd_soc_dapm_pre:
1540 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001541 list_for_each_entry_safe_continue(w, n, list,
1542 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001543
Mark Brownb22ead22009-06-07 12:51:26 +01001544 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001545 ret = w->event(w,
1546 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001547 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001548 ret = w->event(w,
1549 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001550 break;
1551
1552 case snd_soc_dapm_post:
1553 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001554 list_for_each_entry_safe_continue(w, n, list,
1555 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001556
Mark Brownb22ead22009-06-07 12:51:26 +01001557 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001558 ret = w->event(w,
1559 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001560 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001561 ret = w->event(w,
1562 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001563 break;
1564
Mark Brown163cac02009-06-07 10:12:52 +01001565 default:
Mark Brown81628102009-06-07 13:21:24 +01001566 /* Queue it up for application */
1567 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001568 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001569 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001570 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001571 list_move(&w->power_list, &pending);
1572 break;
Mark Brown163cac02009-06-07 10:12:52 +01001573 }
Mark Brownb22ead22009-06-07 12:51:26 +01001574
1575 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001576 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001577 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001578 }
Mark Brownb22ead22009-06-07 12:51:26 +01001579
1580 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001581 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001582
1583 if (cur_dapm && cur_dapm->seq_notifier) {
1584 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1585 if (sort[i] == cur_sort)
1586 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001587 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001588 }
Mark Browneb270e92013-10-09 13:52:52 +01001589
1590 list_for_each_entry(d, &card->dapm_list, list) {
1591 soc_dapm_async_complete(d);
1592 }
Mark Brown163cac02009-06-07 10:12:52 +01001593}
1594
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001595static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001596{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001597 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001598 struct snd_soc_dapm_widget_list *wlist;
1599 struct snd_soc_dapm_widget *w = NULL;
1600 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001601 int ret;
1602
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001603 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001604 return;
1605
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001606 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001607
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001608 for (wi = 0; wi < wlist->num_widgets; wi++) {
1609 w = wlist->widgets[wi];
1610
1611 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1612 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1613 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001614 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001615 w->name, ret);
1616 }
Mark Brown97404f22010-12-14 16:13:57 +00001617 }
1618
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001619 if (!w)
1620 return;
1621
Liam Girdwood49575fb52012-03-06 18:16:19 +00001622 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001623 update->val);
1624 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001625 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001626 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001627
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001628 for (wi = 0; wi < wlist->num_widgets; wi++) {
1629 w = wlist->widgets[wi];
1630
1631 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1632 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1633 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001634 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001635 w->name, ret);
1636 }
Mark Brown97404f22010-12-14 16:13:57 +00001637 }
1638}
1639
Mark Brown9d0624a2011-02-18 11:49:43 -08001640/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1641 * they're changing state.
1642 */
1643static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1644{
1645 struct snd_soc_dapm_context *d = data;
1646 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001647
Mark Brown56fba412011-06-04 11:25:10 +01001648 /* If we're off and we're not supposed to be go into STANDBY */
1649 if (d->bias_level == SND_SOC_BIAS_OFF &&
1650 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001651 if (d->dev)
1652 pm_runtime_get_sync(d->dev);
1653
Mark Brown9d0624a2011-02-18 11:49:43 -08001654 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1655 if (ret != 0)
1656 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001657 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001658 }
1659
Mark Brown56fba412011-06-04 11:25:10 +01001660 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1661 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001662 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1663 if (ret != 0)
1664 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001665 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001666 }
1667}
1668
1669/* Async callback run prior to DAPM sequences - brings to their final
1670 * state.
1671 */
1672static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1673{
1674 struct snd_soc_dapm_context *d = data;
1675 int ret;
1676
1677 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001678 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1679 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1680 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001681 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1682 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001683 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001684 ret);
1685 }
1686
1687 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001688 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1689 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001690 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1691 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001692 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1693 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001694
1695 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001696 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001697 }
1698
1699 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001700 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1701 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001702 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1703 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001704 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001705 ret);
1706 }
1707}
Mark Brown97404f22010-12-14 16:13:57 +00001708
Mark Brownfe4fda52011-10-03 22:36:57 +01001709static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1710 bool power, bool connect)
1711{
1712 /* If a connection is being made or broken then that update
1713 * will have marked the peer dirty, otherwise the widgets are
1714 * not connected and this update has no impact. */
1715 if (!connect)
1716 return;
1717
1718 /* If the peer is already in the state we're moving to then we
1719 * won't have an impact on it. */
1720 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001721 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001722}
1723
Mark Brown05623c42011-09-28 17:02:31 +01001724static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1725 struct list_head *up_list,
1726 struct list_head *down_list)
1727{
Mark Browndb432b42011-10-03 21:06:40 +01001728 struct snd_soc_dapm_path *path;
1729
Mark Brown05623c42011-09-28 17:02:31 +01001730 if (w->power == power)
1731 return;
1732
1733 trace_snd_soc_dapm_widget_power(w, power);
1734
Mark Browndb432b42011-10-03 21:06:40 +01001735 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001736 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001737 */
1738 list_for_each_entry(path, &w->sources, list_sink) {
1739 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001740 dapm_widget_set_peer_power(path->source, power,
1741 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001742 }
1743 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001744 switch (w->id) {
1745 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001746 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001747 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001748 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001749 /* Supplies can't affect their outputs, only their inputs */
1750 break;
1751 default:
1752 list_for_each_entry(path, &w->sinks, list_source) {
1753 if (path->sink) {
1754 dapm_widget_set_peer_power(path->sink, power,
1755 path->connect);
1756 }
Mark Browndb432b42011-10-03 21:06:40 +01001757 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001758 break;
Mark Browndb432b42011-10-03 21:06:40 +01001759 }
1760
Mark Brown05623c42011-09-28 17:02:31 +01001761 if (power)
1762 dapm_seq_insert(w, up_list, true);
1763 else
1764 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001765}
1766
Mark Brown7c81beb2011-09-20 22:22:32 +01001767static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1768 struct list_head *up_list,
1769 struct list_head *down_list)
1770{
Mark Brown7c81beb2011-09-20 22:22:32 +01001771 int power;
1772
1773 switch (w->id) {
1774 case snd_soc_dapm_pre:
1775 dapm_seq_insert(w, down_list, false);
1776 break;
1777 case snd_soc_dapm_post:
1778 dapm_seq_insert(w, up_list, true);
1779 break;
1780
1781 default:
Mark Brownd8050022011-09-28 18:28:23 +01001782 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001783
Mark Brown05623c42011-09-28 17:02:31 +01001784 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001785 break;
1786 }
1787}
1788
Mark Brown42aa3412009-03-01 19:21:10 +00001789/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001790 * Scan each dapm widget for complete audio path.
1791 * A complete path is a route that has valid endpoints i.e.:-
1792 *
1793 * o DAC to output pin.
1794 * o Input Pin to ADC.
1795 * o Input pin to Output pin (bypass, sidetone)
1796 * o DAC to ADC (loopback).
1797 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001798static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001799{
1800 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001801 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001802 LIST_HEAD(up_list);
1803 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001804 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001805 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001806
Mark Brown84e90932010-11-04 00:07:02 -04001807 trace_snd_soc_dapm_start(card);
1808
Mark Brown56fba412011-06-04 11:25:10 +01001809 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001810 if (d->idle_bias_off)
1811 d->target_bias_level = SND_SOC_BIAS_OFF;
1812 else
1813 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001814 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001815
Liam Girdwood6c120e12012-02-15 15:15:34 +00001816 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001817
Mark Brown6d3ddc82009-05-16 17:47:29 +01001818 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001819 * lists indicating if they should be powered up or down. We
1820 * only check widgets that have been flagged as dirty but note
1821 * that new widgets may be added to the dirty list while we
1822 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001823 */
Mark Browndb432b42011-10-03 21:06:40 +01001824 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001825 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001826 }
1827
Mark Brownf9de6d72011-09-28 17:19:47 +01001828 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001829 switch (w->id) {
1830 case snd_soc_dapm_pre:
1831 case snd_soc_dapm_post:
1832 /* These widgets always need to be powered */
1833 break;
1834 default:
1835 list_del_init(&w->dirty);
1836 break;
1837 }
Mark Browndb432b42011-10-03 21:06:40 +01001838
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001839 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001840 d = w->dapm;
1841
1842 /* Supplies and micbiases only bring the
1843 * context up to STANDBY as unless something
1844 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001845 * generally don't require full power. Signal
1846 * generators are virtual pins and have no
1847 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001848 */
1849 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001850 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001851 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001852 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001853 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001854 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001855 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001856 case snd_soc_dapm_micbias:
1857 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1858 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1859 break;
1860 default:
1861 d->target_bias_level = SND_SOC_BIAS_ON;
1862 break;
1863 }
1864 }
1865
1866 }
1867
Mark Brown85a843c2011-09-21 21:29:47 +01001868 /* Force all contexts in the card to the same bias state if
1869 * they're not ground referenced.
1870 */
Mark Brown56fba412011-06-04 11:25:10 +01001871 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001872 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001873 if (d->target_bias_level > bias)
1874 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001875 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001876 if (!d->idle_bias_off)
1877 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001878
Mark Brownde02d072011-09-20 21:43:24 +01001879 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001880
Mark Brown9d0624a2011-02-18 11:49:43 -08001881 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001882 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001883 async_schedule_domain(dapm_pre_sequence_async, d,
1884 &async_domain);
1885 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001886
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001887 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001888 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001889 }
1890
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001891 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001892 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001893 }
1894
Mark Brown6d3ddc82009-05-16 17:47:29 +01001895 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001896 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001897
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001898 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001899
Mark Brown6d3ddc82009-05-16 17:47:29 +01001900 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001901 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001902
Mark Brown9d0624a2011-02-18 11:49:43 -08001903 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001904 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001905 async_schedule_domain(dapm_post_sequence_async, d,
1906 &async_domain);
1907 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001908
Liam Girdwood8078d872012-02-15 15:15:35 +00001909 /* do we need to notify any clients that DAPM event is complete */
1910 list_for_each_entry(d, &card->dapm_list, list) {
1911 if (d->stream_event)
1912 d->stream_event(d, event);
1913 }
1914
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001915 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001916 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001917 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001918
Mark Brown84e90932010-11-04 00:07:02 -04001919 trace_snd_soc_dapm_done(card);
1920
Mark Brown42aa3412009-03-01 19:21:10 +00001921 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001922}
1923
Mark Brown79fb9382009-08-21 16:38:13 +01001924#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001925static ssize_t dapm_widget_power_read_file(struct file *file,
1926 char __user *user_buf,
1927 size_t count, loff_t *ppos)
1928{
1929 struct snd_soc_dapm_widget *w = file->private_data;
1930 char *buf;
1931 int in, out;
1932 ssize_t ret;
1933 struct snd_soc_dapm_path *p = NULL;
1934
1935 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1936 if (!buf)
1937 return -ENOMEM;
1938
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001939 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001940 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001941 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001942 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001943
Mark Brownf13ebad2012-03-03 18:01:01 +00001944 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1945 w->name, w->power ? "On" : "Off",
1946 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001947
Mark Brownd033c362009-12-04 15:25:56 +00001948 if (w->reg >= 0)
1949 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001950 " - R%d(0x%x) mask 0x%x",
1951 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001952
1953 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1954
Mark Brown3eef08b2009-09-14 16:49:00 +01001955 if (w->sname)
1956 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1957 w->sname,
1958 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001959
1960 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001961 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001962 continue;
1963
Mark Brown79fb9382009-08-21 16:38:13 +01001964 if (p->connect)
1965 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001966 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001967 p->name ? p->name : "static",
1968 p->source->name);
1969 }
1970 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001971 if (p->connected && !p->connected(w, p->sink))
1972 continue;
1973
Mark Brown79fb9382009-08-21 16:38:13 +01001974 if (p->connect)
1975 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001976 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001977 p->name ? p->name : "static",
1978 p->sink->name);
1979 }
1980
1981 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1982
1983 kfree(buf);
1984 return ret;
1985}
1986
1987static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001988 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001989 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001990 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001991};
1992
Mark Brownef49e4f2011-04-04 20:48:13 +09001993static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1994 size_t count, loff_t *ppos)
1995{
1996 struct snd_soc_dapm_context *dapm = file->private_data;
1997 char *level;
1998
1999 switch (dapm->bias_level) {
2000 case SND_SOC_BIAS_ON:
2001 level = "On\n";
2002 break;
2003 case SND_SOC_BIAS_PREPARE:
2004 level = "Prepare\n";
2005 break;
2006 case SND_SOC_BIAS_STANDBY:
2007 level = "Standby\n";
2008 break;
2009 case SND_SOC_BIAS_OFF:
2010 level = "Off\n";
2011 break;
2012 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002013 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002014 level = "Unknown\n";
2015 break;
2016 }
2017
2018 return simple_read_from_buffer(user_buf, count, ppos, level,
2019 strlen(level));
2020}
2021
2022static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002023 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002024 .read = dapm_bias_read_file,
2025 .llseek = default_llseek,
2026};
2027
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002028void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2029 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002030{
Mark Brown79fb9382009-08-21 16:38:13 +01002031 struct dentry *d;
2032
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002033 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2034
2035 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002036 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002037 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002038 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002039 }
Mark Brown79fb9382009-08-21 16:38:13 +01002040
Mark Brownef49e4f2011-04-04 20:48:13 +09002041 d = debugfs_create_file("bias_level", 0444,
2042 dapm->debugfs_dapm, dapm,
2043 &dapm_bias_fops);
2044 if (!d)
2045 dev_warn(dapm->dev,
2046 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002047}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002048
2049static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2050{
2051 struct snd_soc_dapm_context *dapm = w->dapm;
2052 struct dentry *d;
2053
2054 if (!dapm->debugfs_dapm || !w->name)
2055 return;
2056
2057 d = debugfs_create_file(w->name, 0444,
2058 dapm->debugfs_dapm, w,
2059 &dapm_widget_power_fops);
2060 if (!d)
2061 dev_warn(w->dapm->dev,
2062 "ASoC: Failed to create %s debugfs file\n",
2063 w->name);
2064}
2065
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002066static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2067{
2068 debugfs_remove_recursive(dapm->debugfs_dapm);
2069}
2070
Mark Brown79fb9382009-08-21 16:38:13 +01002071#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002072void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2073 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002074{
2075}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002076
2077static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2078{
2079}
2080
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002081static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2082{
2083}
2084
Mark Brown79fb9382009-08-21 16:38:13 +01002085#endif
2086
Richard Purdie2b97eab2006-10-06 18:32:18 +02002087/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002088static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002089 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002090{
2091 struct snd_soc_dapm_path *path;
2092 int found = 0;
2093
Richard Purdie2b97eab2006-10-06 18:32:18 +02002094 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002095 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002096 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002097 continue;
2098
2099 found = 1;
2100 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002101 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002102 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002103 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002104 } else {
2105 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002106 dapm_mark_dirty(path->source,
2107 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002108 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002109 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002110 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002111 }
2112
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002113 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002114 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002115
Liam Girdwood618dae12012-04-25 12:12:51 +01002116 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002117}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002118
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002119int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002120 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2121 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002122{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002123 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002124 int ret;
2125
Liam Girdwood3cd04342012-03-09 12:02:08 +00002126 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002127 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002128 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002129 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002130 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002131 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002132 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002133 return ret;
2134}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002135EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002136
Milan plzik1b075e32008-01-10 14:39:46 +01002137/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002138static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002139 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002140{
2141 struct snd_soc_dapm_path *path;
2142 int found = 0;
2143
Richard Purdie2b97eab2006-10-06 18:32:18 +02002144 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002145 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002146 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002147 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002148 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002149 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002150 }
2151
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002152 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002153 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002154
Liam Girdwood618dae12012-04-25 12:12:51 +01002155 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002156}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002157
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002158int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002159 struct snd_kcontrol *kcontrol, int connect,
2160 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002161{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002162 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002163 int ret;
2164
Liam Girdwood3cd04342012-03-09 12:02:08 +00002165 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002166 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002167 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002168 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002169 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002170 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002171 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002172 return ret;
2173}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002174EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002175
2176/* show dapm widget status in sys fs */
2177static ssize_t dapm_widget_show(struct device *dev,
2178 struct device_attribute *attr, char *buf)
2179{
Mark Brown36ae1a92012-01-06 17:12:45 -08002180 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002181 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002182 struct snd_soc_dapm_widget *w;
2183 int count = 0;
2184 char *state = "not set";
2185
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002186 list_for_each_entry(w, &codec->card->widgets, list) {
2187 if (w->dapm != &codec->dapm)
2188 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002189
2190 /* only display widgets that burnm power */
2191 switch (w->id) {
2192 case snd_soc_dapm_hp:
2193 case snd_soc_dapm_mic:
2194 case snd_soc_dapm_spk:
2195 case snd_soc_dapm_line:
2196 case snd_soc_dapm_micbias:
2197 case snd_soc_dapm_dac:
2198 case snd_soc_dapm_adc:
2199 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002200 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002201 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002202 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002203 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002204 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002205 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002206 if (w->name)
2207 count += sprintf(buf + count, "%s: %s\n",
2208 w->name, w->power ? "On":"Off");
2209 break;
2210 default:
2211 break;
2212 }
2213 }
2214
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002215 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002216 case SND_SOC_BIAS_ON:
2217 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002218 break;
Mark Brown0be98982008-05-19 12:31:28 +02002219 case SND_SOC_BIAS_PREPARE:
2220 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002221 break;
Mark Brown0be98982008-05-19 12:31:28 +02002222 case SND_SOC_BIAS_STANDBY:
2223 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002224 break;
Mark Brown0be98982008-05-19 12:31:28 +02002225 case SND_SOC_BIAS_OFF:
2226 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002227 break;
2228 }
2229 count += sprintf(buf + count, "PM State: %s\n", state);
2230
2231 return count;
2232}
2233
2234static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2235
2236int snd_soc_dapm_sys_add(struct device *dev)
2237{
Troy Kisky12ef1932008-10-13 17:42:14 -07002238 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002239}
2240
2241static void snd_soc_dapm_sys_remove(struct device *dev)
2242{
Mark Brownaef90842009-05-16 17:53:16 +01002243 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244}
2245
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002246static void dapm_free_path(struct snd_soc_dapm_path *path)
2247{
2248 list_del(&path->list_sink);
2249 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002250 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002251 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002252 kfree(path);
2253}
2254
Richard Purdie2b97eab2006-10-06 18:32:18 +02002255/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002256static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002257{
2258 struct snd_soc_dapm_widget *w, *next_w;
2259 struct snd_soc_dapm_path *p, *next_p;
2260
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002261 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2262 if (w->dapm != dapm)
2263 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002264 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002265 /*
2266 * remove source and sink paths associated to this widget.
2267 * While removing the path, remove reference to it from both
2268 * source and sink widgets so that path is removed only once.
2269 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002270 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2271 dapm_free_path(p);
2272
2273 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2274 dapm_free_path(p);
2275
Stephen Warrenfad59882011-04-28 17:37:59 -06002276 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002277 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002278 kfree(w);
2279 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002280}
2281
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002282static struct snd_soc_dapm_widget *dapm_find_widget(
2283 struct snd_soc_dapm_context *dapm, const char *pin,
2284 bool search_other_contexts)
2285{
2286 struct snd_soc_dapm_widget *w;
2287 struct snd_soc_dapm_widget *fallback = NULL;
2288
2289 list_for_each_entry(w, &dapm->card->widgets, list) {
2290 if (!strcmp(w->name, pin)) {
2291 if (w->dapm == dapm)
2292 return w;
2293 else
2294 fallback = w;
2295 }
2296 }
2297
2298 if (search_other_contexts)
2299 return fallback;
2300
2301 return NULL;
2302}
2303
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002304static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002305 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002306{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002307 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002308
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002309 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002310 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002311 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002312 }
2313
Mark Brown1a8b2d92012-02-16 11:50:07 -08002314 if (w->connected != status)
2315 dapm_mark_dirty(w, "pin configuration");
2316
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002317 w->connected = status;
2318 if (status == 0)
2319 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002320
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002321 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002322}
2323
Richard Purdie2b97eab2006-10-06 18:32:18 +02002324/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002325 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2326 * @dapm: DAPM context
2327 *
2328 * Walks all dapm audio paths and powers widgets according to their
2329 * stream or path usage.
2330 *
2331 * Requires external locking.
2332 *
2333 * Returns 0 for success.
2334 */
2335int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2336{
2337 /*
2338 * Suppress early reports (eg, jacks syncing their state) to avoid
2339 * silly DAPM runs during card startup.
2340 */
2341 if (!dapm->card || !dapm->card->instantiated)
2342 return 0;
2343
2344 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2345}
2346EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2347
2348/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002349 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002350 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002351 *
2352 * Walks all dapm audio paths and powers widgets according to their
2353 * stream or path usage.
2354 *
2355 * Returns 0 for success.
2356 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002357int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002358{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002359 int ret;
2360
Liam Girdwood3cd04342012-03-09 12:02:08 +00002361 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002362 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002363 mutex_unlock(&dapm->card->dapm_mutex);
2364 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002365}
Liam Girdwooda5302182008-07-07 13:35:17 +01002366EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002367
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002368static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2369 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2370 const char *control,
2371 int (*connected)(struct snd_soc_dapm_widget *source,
2372 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002373{
2374 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002375 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002376
2377 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2378 if (!path)
2379 return -ENOMEM;
2380
2381 path->source = wsource;
2382 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002383 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002384 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002385 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002386 INIT_LIST_HEAD(&path->list_source);
2387 INIT_LIST_HEAD(&path->list_sink);
2388
2389 /* check for external widgets */
2390 if (wsink->id == snd_soc_dapm_input) {
2391 if (wsource->id == snd_soc_dapm_micbias ||
2392 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002393 wsource->id == snd_soc_dapm_line ||
2394 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002395 wsink->ext = 1;
2396 }
2397 if (wsource->id == snd_soc_dapm_output) {
2398 if (wsink->id == snd_soc_dapm_spk ||
2399 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002400 wsink->id == snd_soc_dapm_line ||
2401 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002402 wsource->ext = 1;
2403 }
2404
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002405 dapm_mark_dirty(wsource, "Route added");
2406 dapm_mark_dirty(wsink, "Route added");
2407
Richard Purdie2b97eab2006-10-06 18:32:18 +02002408 /* connect static paths */
2409 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002410 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002411 list_add(&path->list_sink, &wsink->sources);
2412 list_add(&path->list_source, &wsource->sinks);
2413 path->connect = 1;
2414 return 0;
2415 }
2416
2417 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002418 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002419 case snd_soc_dapm_adc:
2420 case snd_soc_dapm_dac:
2421 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002422 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002423 case snd_soc_dapm_input:
2424 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002425 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002426 case snd_soc_dapm_micbias:
2427 case snd_soc_dapm_vmid:
2428 case snd_soc_dapm_pre:
2429 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002430 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002431 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002432 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002433 case snd_soc_dapm_aif_in:
2434 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002435 case snd_soc_dapm_dai_in:
2436 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002437 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002438 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002439 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002440 list_add(&path->list_sink, &wsink->sources);
2441 list_add(&path->list_source, &wsource->sinks);
2442 path->connect = 1;
2443 return 0;
2444 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002445 case snd_soc_dapm_virt_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002446 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002447 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002448 if (ret != 0)
2449 goto err;
2450 break;
2451 case snd_soc_dapm_switch:
2452 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002453 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002454 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002455 if (ret != 0)
2456 goto err;
2457 break;
2458 case snd_soc_dapm_hp:
2459 case snd_soc_dapm_mic:
2460 case snd_soc_dapm_line:
2461 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002462 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002463 list_add(&path->list_sink, &wsink->sources);
2464 list_add(&path->list_source, &wsource->sinks);
2465 path->connect = 0;
2466 return 0;
2467 }
Mark Brownfabd0382012-07-05 17:20:06 +01002468
Richard Purdie2b97eab2006-10-06 18:32:18 +02002469 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002470err:
2471 kfree(path);
2472 return ret;
2473}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002474
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002475static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002476 const struct snd_soc_dapm_route *route,
2477 unsigned int is_prefixed)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002478{
2479 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2480 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2481 const char *sink;
2482 const char *source;
2483 char prefixed_sink[80];
2484 char prefixed_source[80];
2485 int ret;
2486
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002487 if (dapm->codec && dapm->codec->name_prefix && !is_prefixed) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002488 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2489 dapm->codec->name_prefix, route->sink);
2490 sink = prefixed_sink;
2491 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2492 dapm->codec->name_prefix, route->source);
2493 source = prefixed_source;
2494 } else {
2495 sink = route->sink;
2496 source = route->source;
2497 }
2498
2499 /*
2500 * find src and dest widgets over all widgets but favor a widget from
2501 * current DAPM context
2502 */
2503 list_for_each_entry(w, &dapm->card->widgets, list) {
2504 if (!wsink && !(strcmp(w->name, sink))) {
2505 wtsink = w;
2506 if (w->dapm == dapm)
2507 wsink = w;
2508 continue;
2509 }
2510 if (!wsource && !(strcmp(w->name, source))) {
2511 wtsource = w;
2512 if (w->dapm == dapm)
2513 wsource = w;
2514 }
2515 }
2516 /* use widget from another DAPM context if not found from this */
2517 if (!wsink)
2518 wsink = wtsink;
2519 if (!wsource)
2520 wsource = wtsource;
2521
2522 if (wsource == NULL) {
2523 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2524 route->source);
2525 return -ENODEV;
2526 }
2527 if (wsink == NULL) {
2528 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2529 route->sink);
2530 return -ENODEV;
2531 }
2532
2533 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2534 route->connected);
2535 if (ret)
2536 goto err;
2537
2538 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002539err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002540 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002541 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002542 return ret;
2543}
Mark Brown105f1c22008-05-13 14:52:19 +02002544
Mark Brownefcc3c62012-07-05 17:24:19 +01002545static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2546 const struct snd_soc_dapm_route *route)
2547{
2548 struct snd_soc_dapm_path *path, *p;
2549 const char *sink;
2550 const char *source;
2551 char prefixed_sink[80];
2552 char prefixed_source[80];
2553
2554 if (route->control) {
2555 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002556 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002557 return -EINVAL;
2558 }
2559
2560 if (dapm->codec && dapm->codec->name_prefix) {
2561 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2562 dapm->codec->name_prefix, route->sink);
2563 sink = prefixed_sink;
2564 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2565 dapm->codec->name_prefix, route->source);
2566 source = prefixed_source;
2567 } else {
2568 sink = route->sink;
2569 source = route->source;
2570 }
2571
2572 path = NULL;
2573 list_for_each_entry(p, &dapm->card->paths, list) {
2574 if (strcmp(p->source->name, source) != 0)
2575 continue;
2576 if (strcmp(p->sink->name, sink) != 0)
2577 continue;
2578 path = p;
2579 break;
2580 }
2581
2582 if (path) {
2583 dapm_mark_dirty(path->source, "Route removed");
2584 dapm_mark_dirty(path->sink, "Route removed");
2585
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002586 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002587 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002588 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002589 source, sink);
2590 }
2591
2592 return 0;
2593}
2594
Mark Brown105f1c22008-05-13 14:52:19 +02002595/**
Mark Brown105f1c22008-05-13 14:52:19 +02002596 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002597 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002598 * @route: audio routes
2599 * @num: number of routes
2600 *
2601 * Connects 2 dapm widgets together via a named audio path. The sink is
2602 * the widget receiving the audio signal, whilst the source is the sender
2603 * of the audio signal.
2604 *
2605 * Returns 0 for success else error. On error all resources can be freed
2606 * with a call to snd_soc_card_free().
2607 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002608int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002609 const struct snd_soc_dapm_route *route, int num)
2610{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002611 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002612
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002613 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002614 for (i = 0; i < num; i++) {
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002615 r = snd_soc_dapm_add_route(dapm, route, false);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002616 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002617 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2618 route->source,
2619 route->control ? route->control : "direct",
2620 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002621 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002622 }
2623 route++;
2624 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002625 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002626
Dan Carpenter60884c22012-04-13 22:25:43 +03002627 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002628}
2629EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2630
Mark Brownefcc3c62012-07-05 17:24:19 +01002631/**
2632 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2633 * @dapm: DAPM context
2634 * @route: audio routes
2635 * @num: number of routes
2636 *
2637 * Removes routes from the DAPM context.
2638 */
2639int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2640 const struct snd_soc_dapm_route *route, int num)
2641{
2642 int i, ret = 0;
2643
2644 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2645 for (i = 0; i < num; i++) {
2646 snd_soc_dapm_del_route(dapm, route);
2647 route++;
2648 }
2649 mutex_unlock(&dapm->card->dapm_mutex);
2650
2651 return ret;
2652}
2653EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2654
Mark Brownbf3a9e12011-06-13 16:42:29 +01002655static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2656 const struct snd_soc_dapm_route *route)
2657{
2658 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2659 route->source,
2660 true);
2661 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2662 route->sink,
2663 true);
2664 struct snd_soc_dapm_path *path;
2665 int count = 0;
2666
2667 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002668 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002669 route->source);
2670 return -ENODEV;
2671 }
2672
2673 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002674 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002675 route->sink);
2676 return -ENODEV;
2677 }
2678
2679 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002680 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002681 route->source, route->sink);
2682
2683 list_for_each_entry(path, &source->sinks, list_source) {
2684 if (path->sink == sink) {
2685 path->weak = 1;
2686 count++;
2687 }
2688 }
2689
2690 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002691 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002692 route->source, route->sink);
2693 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002694 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002695 count, route->source, route->sink);
2696
2697 return 0;
2698}
2699
2700/**
2701 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2702 * @dapm: DAPM context
2703 * @route: audio routes
2704 * @num: number of routes
2705 *
2706 * Mark existing routes matching those specified in the passed array
2707 * as being weak, meaning that they are ignored for the purpose of
2708 * power decisions. The main intended use case is for sidetone paths
2709 * which couple audio between other independent paths if they are both
2710 * active in order to make the combination work better at the user
2711 * level but which aren't intended to be "used".
2712 *
2713 * Note that CODEC drivers should not use this as sidetone type paths
2714 * can frequently also be used as bypass paths.
2715 */
2716int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2717 const struct snd_soc_dapm_route *route, int num)
2718{
2719 int i, err;
2720 int ret = 0;
2721
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002722 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002723 for (i = 0; i < num; i++) {
2724 err = snd_soc_dapm_weak_route(dapm, route);
2725 if (err)
2726 ret = err;
2727 route++;
2728 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002729 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002730
2731 return ret;
2732}
2733EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2734
Mark Brown105f1c22008-05-13 14:52:19 +02002735/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002736 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002737 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002738 *
2739 * Checks the codec for any new dapm widgets and creates them if found.
2740 *
2741 * Returns 0 for success.
2742 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002743int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002744{
2745 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002746 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002747
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002748 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002749
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002750 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002751 {
2752 if (w->new)
2753 continue;
2754
Stephen Warrenfad59882011-04-28 17:37:59 -06002755 if (w->num_kcontrols) {
2756 w->kcontrols = kzalloc(w->num_kcontrols *
2757 sizeof(struct snd_kcontrol *),
2758 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002759 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002760 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002761 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002762 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002763 }
2764
Richard Purdie2b97eab2006-10-06 18:32:18 +02002765 switch(w->id) {
2766 case snd_soc_dapm_switch:
2767 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002768 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002769 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002770 break;
2771 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002772 case snd_soc_dapm_virt_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002773 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002774 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002775 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002776 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002777 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002778 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002779 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002780 break;
2781 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002782
2783 /* Read the initial power state from the device */
2784 if (w->reg >= 0) {
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002785 soc_widget_read(w, w->reg, &val);
2786 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002787 val &= w->mask;
2788 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002789 w->power = 1;
2790 }
2791
Richard Purdie2b97eab2006-10-06 18:32:18 +02002792 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002793
Mark Brown7508b122011-10-05 12:09:12 +01002794 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002795 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002796 }
2797
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002798 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2799 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002800 return 0;
2801}
2802EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2803
2804/**
2805 * snd_soc_dapm_get_volsw - dapm mixer get callback
2806 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002807 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002808 *
2809 * Callback to get the value of a dapm mixer control.
2810 *
2811 * Returns 0 for success.
2812 */
2813int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2814 struct snd_ctl_elem_value *ucontrol)
2815{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002816 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002817 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002818 struct soc_mixer_control *mc =
2819 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002820 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002821 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002822 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002823 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002824 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002825 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002826
2827 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002828 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002829 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002830 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002831
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002832 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002833 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002834 val = (snd_soc_read(codec, reg) >> shift) & mask;
2835 else
2836 val = dapm_kcontrol_get_value(kcontrol);
2837 mutex_unlock(&card->dapm_mutex);
2838
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002839 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002840 ucontrol->value.integer.value[0] = max - val;
2841 else
2842 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002843
2844 return 0;
2845}
2846EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2847
2848/**
2849 * snd_soc_dapm_put_volsw - dapm mixer set callback
2850 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002851 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002852 *
2853 * Callback to set the value of a dapm mixer control.
2854 *
2855 * Returns 0 for success.
2856 */
2857int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2858 struct snd_ctl_elem_value *ucontrol)
2859{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002860 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002861 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002862 struct soc_mixer_control *mc =
2863 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002864 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002865 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002866 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002867 unsigned int mask = (1 << fls(max)) - 1;
2868 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002869 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002870 int connect, change;
2871 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002872 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002873
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002874 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002875 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002876 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002877 kcontrol->id.name);
2878
Richard Purdie2b97eab2006-10-06 18:32:18 +02002879 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002880 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002881
2882 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002883 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002884
Liam Girdwood3cd04342012-03-09 12:02:08 +00002885 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002886
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002887 change = dapm_kcontrol_set_value(kcontrol, val);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002888
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002889 if (reg != SND_SOC_NOPM) {
2890 mask = mask << shift;
2891 val = val << shift;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002892
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002893 change = snd_soc_test_bits(codec, reg, mask, val);
2894 }
2895
Mark Brown97404f22010-12-14 16:13:57 +00002896 if (change) {
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002897 if (reg != SND_SOC_NOPM) {
2898 update.kcontrol = kcontrol;
2899 update.reg = reg;
2900 update.mask = mask;
2901 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002902
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002903 card->update = &update;
2904 }
Mark Brown97404f22010-12-14 16:13:57 +00002905
Nenghua Cao52765972013-12-13 20:13:49 +08002906 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002907
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002908 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002909 }
2910
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002911 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002912
2913 if (ret > 0)
2914 soc_dpcm_runtime_update(card);
2915
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002916 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002917}
2918EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2919
2920/**
2921 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2922 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002923 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002924 *
2925 * Callback to get the value of a dapm enumerated double mixer control.
2926 *
2927 * Returns 0 for success.
2928 */
2929int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2930 struct snd_ctl_elem_value *ucontrol)
2931{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002932 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002933 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002934 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002935
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002936 reg_val = snd_soc_read(codec, e->reg);
2937 val = (reg_val >> e->shift_l) & e->mask;
2938 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2939 if (e->shift_l != e->shift_r) {
2940 val = (reg_val >> e->shift_r) & e->mask;
2941 val = snd_soc_enum_val_to_item(e, val);
2942 ucontrol->value.enumerated.item[1] = val;
2943 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002944
2945 return 0;
2946}
2947EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2948
2949/**
2950 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2951 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002952 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002953 *
2954 * Callback to set the value of a dapm enumerated double mixer control.
2955 *
2956 * Returns 0 for success.
2957 */
2958int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2959 struct snd_ctl_elem_value *ucontrol)
2960{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002961 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002962 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002963 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002964 unsigned int *item = ucontrol->value.enumerated.item;
2965 unsigned int val, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002966 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002967 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002968 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002969
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002970 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002971 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002972
2973 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002974 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002975 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002976 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002977 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002978 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002979 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002980 }
2981
Liam Girdwood3cd04342012-03-09 12:02:08 +00002982 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002983
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002984 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002985 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002986 update.kcontrol = kcontrol;
2987 update.reg = e->reg;
2988 update.mask = mask;
2989 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002990 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002991
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002992 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002993
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002994 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002995 }
2996
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002997 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002998
2999 if (ret > 0)
3000 soc_dpcm_runtime_update(card);
3001
Mark Brown97404f22010-12-14 16:13:57 +00003002 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003003}
3004EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3005
3006/**
Mark Brownd2b247a2009-10-06 15:21:04 +01003007 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
3008 * @kcontrol: mixer control
3009 * @ucontrol: control element information
3010 *
3011 * Returns 0 for success.
3012 */
3013int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
3014 struct snd_ctl_elem_value *ucontrol)
3015{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003016 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
Mark Brownd2b247a2009-10-06 15:21:04 +01003017 return 0;
3018}
3019EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
3020
3021/**
3022 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
3023 * @kcontrol: mixer control
3024 * @ucontrol: control element information
3025 *
3026 * Returns 0 for success.
3027 */
3028int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
3029 struct snd_ctl_elem_value *ucontrol)
3030{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003031 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003032 struct snd_soc_card *card = codec->card;
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003033 unsigned int value;
Mark Brownd2b247a2009-10-06 15:21:04 +01003034 struct soc_enum *e =
3035 (struct soc_enum *)kcontrol->private_value;
3036 int change;
Nenghua Cao52765972013-12-13 20:13:49 +08003037 int ret = 0;
Mark Brownd2b247a2009-10-06 15:21:04 +01003038
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01003039 if (ucontrol->value.enumerated.item[0] >= e->items)
Mark Brownd2b247a2009-10-06 15:21:04 +01003040 return -EINVAL;
3041
Liam Girdwood3cd04342012-03-09 12:02:08 +00003042 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01003043
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003044 value = ucontrol->value.enumerated.item[0];
3045 change = dapm_kcontrol_set_value(kcontrol, value);
3046 if (change)
Nenghua Cao52765972013-12-13 20:13:49 +08003047 ret = soc_dapm_mux_update_power(card, kcontrol, value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003048
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003049 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003050
3051 if (ret > 0)
3052 soc_dpcm_runtime_update(card);
3053
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003054 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01003055}
3056EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3057
3058/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003059 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3060 *
3061 * @kcontrol: mixer control
3062 * @uinfo: control element information
3063 *
3064 * Callback to provide information about a pin switch control.
3065 */
3066int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3067 struct snd_ctl_elem_info *uinfo)
3068{
3069 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3070 uinfo->count = 1;
3071 uinfo->value.integer.min = 0;
3072 uinfo->value.integer.max = 1;
3073
3074 return 0;
3075}
3076EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3077
3078/**
3079 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3080 *
3081 * @kcontrol: mixer control
3082 * @ucontrol: Value
3083 */
3084int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3085 struct snd_ctl_elem_value *ucontrol)
3086{
Mark Brown48a8c392012-02-14 17:11:15 -08003087 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003088 const char *pin = (const char *)kcontrol->private_value;
3089
Liam Girdwood3cd04342012-03-09 12:02:08 +00003090 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003091
3092 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003093 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003094
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003095 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003096
3097 return 0;
3098}
3099EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3100
3101/**
3102 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3103 *
3104 * @kcontrol: mixer control
3105 * @ucontrol: Value
3106 */
3107int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3108 struct snd_ctl_elem_value *ucontrol)
3109{
Mark Brown48a8c392012-02-14 17:11:15 -08003110 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003111 const char *pin = (const char *)kcontrol->private_value;
3112
Mark Brown8b37dbd2009-02-28 21:14:20 +00003113 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003114 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003115 else
Mark Brown48a8c392012-02-14 17:11:15 -08003116 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003117
Mark Brown48a8c392012-02-14 17:11:15 -08003118 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003119 return 0;
3120}
3121EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3122
Mark Brown5ba06fc2012-02-16 11:07:13 -08003123static struct snd_soc_dapm_widget *
3124snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3125 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003126{
3127 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003128 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003129
3130 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003131 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003132
Mark Brown62ea8742012-01-21 21:14:48 +00003133 switch (w->id) {
3134 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003135 w->regulator = devm_regulator_get(dapm->dev, w->name);
3136 if (IS_ERR(w->regulator)) {
3137 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003138 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003139 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003140 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003141 }
Mark Brown8784c772013-01-10 19:33:47 +00003142
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003143 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003144 ret = regulator_allow_bypass(w->regulator, true);
3145 if (ret != 0)
3146 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003147 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003148 w->name, ret);
3149 }
Mark Brown62ea8742012-01-21 21:14:48 +00003150 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003151 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003152#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003153 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003154 if (IS_ERR(w->clk)) {
3155 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003156 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003157 w->name, ret);
3158 return NULL;
3159 }
Mark Brownec029952012-06-04 08:16:20 +01003160#else
3161 return NULL;
3162#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003163 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003164 default:
3165 break;
3166 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003167
Mark Brown88e8b9a2011-03-02 18:18:24 +00003168 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003169 w->name = kasprintf(GFP_KERNEL, "%s %s",
3170 dapm->codec->name_prefix, widget->name);
3171 else
3172 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3173
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003174 if (w->name == NULL) {
3175 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003176 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003177 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003178
Mark Brown7ca3a182011-10-08 14:04:50 +01003179 switch (w->id) {
3180 case snd_soc_dapm_switch:
3181 case snd_soc_dapm_mixer:
3182 case snd_soc_dapm_mixer_named_ctl:
3183 w->power_check = dapm_generic_check_power;
3184 break;
3185 case snd_soc_dapm_mux:
3186 case snd_soc_dapm_virt_mux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003187 w->power_check = dapm_generic_check_power;
3188 break;
Mark Brown46162742013-06-05 19:36:11 +01003189 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003190 w->power_check = dapm_adc_check_power;
3191 break;
Mark Brown46162742013-06-05 19:36:11 +01003192 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003193 w->power_check = dapm_dac_check_power;
3194 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003195 case snd_soc_dapm_adc:
3196 case snd_soc_dapm_aif_out:
3197 case snd_soc_dapm_dac:
3198 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003199 case snd_soc_dapm_pga:
3200 case snd_soc_dapm_out_drv:
3201 case snd_soc_dapm_input:
3202 case snd_soc_dapm_output:
3203 case snd_soc_dapm_micbias:
3204 case snd_soc_dapm_spk:
3205 case snd_soc_dapm_hp:
3206 case snd_soc_dapm_mic:
3207 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003208 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003209 w->power_check = dapm_generic_check_power;
3210 break;
3211 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003212 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003213 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003214 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003215 w->power_check = dapm_supply_check_power;
3216 break;
3217 default:
3218 w->power_check = dapm_always_on_check_power;
3219 break;
3220 }
3221
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003222 w->dapm = dapm;
3223 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003224 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003225 INIT_LIST_HEAD(&w->sources);
3226 INIT_LIST_HEAD(&w->sinks);
3227 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003228 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003229 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003230
3231 /* machine layer set ups unconnected pins and insertions */
3232 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003233 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003234}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003235
3236/**
Mark Brown4ba13272008-05-13 14:51:19 +02003237 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003238 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003239 * @widget: widget array
3240 * @num: number of widgets
3241 *
3242 * Creates new DAPM controls based upon the templates.
3243 *
3244 * Returns 0 for success else error.
3245 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003246int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003247 const struct snd_soc_dapm_widget *widget,
3248 int num)
3249{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003250 struct snd_soc_dapm_widget *w;
3251 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003252 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003253
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003254 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003255 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003256 w = snd_soc_dapm_new_control(dapm, widget);
3257 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003258 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003259 "ASoC: Failed to create DAPM control %s\n",
3260 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003261 ret = -ENOMEM;
3262 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003263 }
Mark Brown4ba13272008-05-13 14:51:19 +02003264 widget++;
3265 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003266 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003267 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003268}
3269EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3270
Mark Brownc74184e2012-04-04 22:12:09 +01003271static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3272 struct snd_kcontrol *kcontrol, int event)
3273{
3274 struct snd_soc_dapm_path *source_p, *sink_p;
3275 struct snd_soc_dai *source, *sink;
3276 const struct snd_soc_pcm_stream *config = w->params;
3277 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003278 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003279 u64 fmt;
3280 int ret;
3281
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003282 if (WARN_ON(!config) ||
3283 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3284 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003285
3286 /* We only support a single source and sink, pick the first */
3287 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3288 list_sink);
3289 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3290 list_source);
3291
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003292 if (WARN_ON(!source_p || !sink_p) ||
3293 WARN_ON(!sink_p->source || !source_p->sink) ||
3294 WARN_ON(!source_p->source || !sink_p->sink))
3295 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003296
3297 source = source_p->source->priv;
3298 sink = sink_p->sink->priv;
3299
3300 /* Be a little careful as we don't want to overflow the mask array */
3301 if (config->formats) {
3302 fmt = ffs(config->formats) - 1;
3303 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003304 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003305 config->formats);
3306 fmt = 0;
3307 }
3308
3309 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003310 params = kzalloc(sizeof(*params), GFP_KERNEL);
3311 if (!params) {
3312 ret = -ENOMEM;
3313 goto out;
3314 }
3315 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003316
Mark Brown9747cec2012-04-26 19:12:21 +01003317 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003318 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003319 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003320 config->rate_max;
3321
Mark Brown9747cec2012-04-26 19:12:21 +01003322 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003323 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003324 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003325 = config->channels_max;
3326
3327 memset(&substream, 0, sizeof(substream));
3328
3329 switch (event) {
3330 case SND_SOC_DAPM_PRE_PMU:
3331 if (source->driver->ops && source->driver->ops->hw_params) {
3332 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3333 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003334 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003335 if (ret != 0) {
3336 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003337 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003338 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003339 }
3340 }
3341
3342 if (sink->driver->ops && sink->driver->ops->hw_params) {
3343 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003344 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003345 sink);
3346 if (ret != 0) {
3347 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003348 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003349 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003350 }
3351 }
3352 break;
3353
3354 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003355 ret = snd_soc_dai_digital_mute(sink, 0,
3356 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003357 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003358 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003359 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003360 break;
3361
3362 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003363 ret = snd_soc_dai_digital_mute(sink, 1,
3364 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003365 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003366 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003367 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003368 break;
3369
3370 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003371 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003372 return -EINVAL;
3373 }
3374
Mark Brown9747cec2012-04-26 19:12:21 +01003375out:
3376 kfree(params);
3377 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003378}
3379
3380int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3381 const struct snd_soc_pcm_stream *params,
3382 struct snd_soc_dapm_widget *source,
3383 struct snd_soc_dapm_widget *sink)
3384{
3385 struct snd_soc_dapm_route routes[2];
3386 struct snd_soc_dapm_widget template;
3387 struct snd_soc_dapm_widget *w;
3388 size_t len;
3389 char *link_name;
3390
3391 len = strlen(source->name) + strlen(sink->name) + 2;
3392 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3393 if (!link_name)
3394 return -ENOMEM;
3395 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3396
3397 memset(&template, 0, sizeof(template));
3398 template.reg = SND_SOC_NOPM;
3399 template.id = snd_soc_dapm_dai_link;
3400 template.name = link_name;
3401 template.event = snd_soc_dai_link_event;
3402 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3403 SND_SOC_DAPM_PRE_PMD;
3404
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003405 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003406
3407 w = snd_soc_dapm_new_control(&card->dapm, &template);
3408 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003409 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003410 link_name);
3411 return -ENOMEM;
3412 }
3413
3414 w->params = params;
3415
3416 memset(&routes, 0, sizeof(routes));
3417
3418 routes[0].source = source->name;
3419 routes[0].sink = link_name;
3420 routes[1].source = link_name;
3421 routes[1].sink = sink->name;
3422
3423 return snd_soc_dapm_add_routes(&card->dapm, routes,
3424 ARRAY_SIZE(routes));
3425}
3426
Mark Brown888df392012-02-16 19:37:51 -08003427int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3428 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003429{
Mark Brown888df392012-02-16 19:37:51 -08003430 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003431 struct snd_soc_dapm_widget *w;
3432
Mark Brown888df392012-02-16 19:37:51 -08003433 WARN_ON(dapm->dev != dai->dev);
3434
3435 memset(&template, 0, sizeof(template));
3436 template.reg = SND_SOC_NOPM;
3437
3438 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003439 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003440 template.name = dai->driver->playback.stream_name;
3441 template.sname = dai->driver->playback.stream_name;
3442
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003443 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003444 template.name);
3445
3446 w = snd_soc_dapm_new_control(dapm, &template);
3447 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003448 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003449 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003450 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003451 }
3452
3453 w->priv = dai;
3454 dai->playback_widget = w;
3455 }
3456
3457 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003458 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003459 template.name = dai->driver->capture.stream_name;
3460 template.sname = dai->driver->capture.stream_name;
3461
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003462 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003463 template.name);
3464
3465 w = snd_soc_dapm_new_control(dapm, &template);
3466 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003467 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003468 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003469 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003470 }
3471
3472 w->priv = dai;
3473 dai->capture_widget = w;
3474 }
3475
3476 return 0;
3477}
3478
3479int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3480{
3481 struct snd_soc_dapm_widget *dai_w, *w;
3482 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003483
3484 /* For each DAI widget... */
3485 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003486 switch (dai_w->id) {
3487 case snd_soc_dapm_dai_in:
3488 case snd_soc_dapm_dai_out:
3489 break;
3490 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003491 continue;
Mark Brown46162742013-06-05 19:36:11 +01003492 }
Mark Brown888df392012-02-16 19:37:51 -08003493
3494 dai = dai_w->priv;
3495
3496 /* ...find all widgets with the same stream and link them */
3497 list_for_each_entry(w, &card->widgets, list) {
3498 if (w->dapm != dai_w->dapm)
3499 continue;
3500
Mark Brown46162742013-06-05 19:36:11 +01003501 switch (w->id) {
3502 case snd_soc_dapm_dai_in:
3503 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003504 continue;
Mark Brown46162742013-06-05 19:36:11 +01003505 default:
3506 break;
3507 }
Mark Brown888df392012-02-16 19:37:51 -08003508
Russell King19c2c5f2013-08-04 20:24:03 +01003509 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003510 continue;
3511
3512 if (dai->driver->playback.stream_name &&
3513 strstr(w->sname,
3514 dai->driver->playback.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003515 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003516 dai->playback_widget->name, w->name);
Mark Brown888df392012-02-16 19:37:51 -08003517
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003518 snd_soc_dapm_add_path(w->dapm,
3519 dai->playback_widget, w, NULL, NULL);
Mark Brown888df392012-02-16 19:37:51 -08003520 }
3521
3522 if (dai->driver->capture.stream_name &&
3523 strstr(w->sname,
3524 dai->driver->capture.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003525 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003526 w->name, dai->capture_widget->name);
Mark Brown888df392012-02-16 19:37:51 -08003527
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003528 snd_soc_dapm_add_path(w->dapm, w,
3529 dai->capture_widget, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003530 }
3531 }
3532 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003533
Mark Brown888df392012-02-16 19:37:51 -08003534 return 0;
3535}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003536
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003537void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3538{
3539 struct snd_soc_pcm_runtime *rtd = card->rtd;
3540 struct snd_soc_dai *cpu_dai, *codec_dai;
3541 struct snd_soc_dapm_route r;
3542 int i;
3543
3544 memset(&r, 0, sizeof(r));
3545
3546 /* for each BE DAI link... */
3547 for (i = 0; i < card->num_rtd; i++) {
3548 rtd = &card->rtd[i];
3549 cpu_dai = rtd->cpu_dai;
3550 codec_dai = rtd->codec_dai;
3551
3552 /* dynamic FE links have no fixed DAI mapping */
3553 if (rtd->dai_link->dynamic)
3554 continue;
3555
3556 /* there is no point in connecting BE DAI links with dummies */
3557 if (snd_soc_dai_is_dummy(codec_dai) ||
3558 snd_soc_dai_is_dummy(cpu_dai))
3559 continue;
3560
3561 /* connect BE DAI playback if widgets are valid */
3562 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3563 r.source = cpu_dai->playback_widget->name;
3564 r.sink = codec_dai->playback_widget->name;
3565 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3566 cpu_dai->codec->name, r.source,
3567 codec_dai->platform->name, r.sink);
3568
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003569 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003570 }
3571
3572 /* connect BE DAI capture if widgets are valid */
3573 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3574 r.source = codec_dai->capture_widget->name;
3575 r.sink = cpu_dai->capture_widget->name;
3576 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3577 codec_dai->codec->name, r.source,
3578 cpu_dai->platform->name, r.sink);
3579
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003580 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003581 }
3582
3583 }
3584}
3585
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003586static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3587 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003588{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003589
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003590 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3591 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3592 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003593
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003594 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3595 w_cpu = cpu_dai->playback_widget;
3596 w_codec = codec_dai->playback_widget;
3597 } else {
3598 w_cpu = cpu_dai->capture_widget;
3599 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003600 }
3601
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003602 if (w_cpu) {
3603
3604 dapm_mark_dirty(w_cpu, "stream event");
3605
3606 switch (event) {
3607 case SND_SOC_DAPM_STREAM_START:
3608 w_cpu->active = 1;
3609 break;
3610 case SND_SOC_DAPM_STREAM_STOP:
3611 w_cpu->active = 0;
3612 break;
3613 case SND_SOC_DAPM_STREAM_SUSPEND:
3614 case SND_SOC_DAPM_STREAM_RESUME:
3615 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3616 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3617 break;
3618 }
3619 }
3620
3621 if (w_codec) {
3622
3623 dapm_mark_dirty(w_codec, "stream event");
3624
3625 switch (event) {
3626 case SND_SOC_DAPM_STREAM_START:
3627 w_codec->active = 1;
3628 break;
3629 case SND_SOC_DAPM_STREAM_STOP:
3630 w_codec->active = 0;
3631 break;
3632 case SND_SOC_DAPM_STREAM_SUSPEND:
3633 case SND_SOC_DAPM_STREAM_RESUME:
3634 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3635 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3636 break;
3637 }
3638 }
3639
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003640 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003641}
3642
3643/**
3644 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3645 * @rtd: PCM runtime data
3646 * @stream: stream name
3647 * @event: stream event
3648 *
3649 * Sends a stream event to the dapm core. The core then makes any
3650 * necessary widget power changes.
3651 *
3652 * Returns 0 for success else error.
3653 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003654void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3655 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003656{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003657 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003658
Liam Girdwood3cd04342012-03-09 12:02:08 +00003659 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003660 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003661 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003662}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003663
3664/**
Charles Keepax11391102014-02-18 15:22:14 +00003665 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3666 * @dapm: DAPM context
3667 * @pin: pin name
3668 *
3669 * Enables input/output pin and its parents or children widgets iff there is
3670 * a valid audio route and active audio stream.
3671 *
3672 * Requires external locking.
3673 *
3674 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3675 * do any widget power switching.
3676 */
3677int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3678 const char *pin)
3679{
3680 return snd_soc_dapm_set_pin(dapm, pin, 1);
3681}
3682EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3683
3684/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003685 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003686 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003687 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003688 *
Mark Brown74b8f952009-06-06 11:26:15 +01003689 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003690 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003691 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003692 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3693 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003694 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003695int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003696{
Charles Keepax11391102014-02-18 15:22:14 +00003697 int ret;
3698
3699 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3700
3701 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3702
3703 mutex_unlock(&dapm->card->dapm_mutex);
3704
3705 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003706}
Liam Girdwooda5302182008-07-07 13:35:17 +01003707EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003708
3709/**
Charles Keepax11391102014-02-18 15:22:14 +00003710 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3711 * @dapm: DAPM context
3712 * @pin: pin name
3713 *
3714 * Enables input/output pin regardless of any other state. This is
3715 * intended for use with microphone bias supplies used in microphone
3716 * jack detection.
3717 *
3718 * Requires external locking.
3719 *
3720 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3721 * do any widget power switching.
3722 */
3723int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3724 const char *pin)
3725{
3726 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3727
3728 if (!w) {
3729 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3730 return -EINVAL;
3731 }
3732
3733 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3734 w->connected = 1;
3735 w->force = 1;
3736 dapm_mark_dirty(w, "force enable");
3737
3738 return 0;
3739}
3740EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3741
3742/**
Mark Brownda341832010-03-15 19:23:37 +00003743 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003744 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003745 * @pin: pin name
3746 *
3747 * Enables input/output pin regardless of any other state. This is
3748 * intended for use with microphone bias supplies used in microphone
3749 * jack detection.
3750 *
3751 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3752 * do any widget power switching.
3753 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003754int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3755 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003756{
Charles Keepax11391102014-02-18 15:22:14 +00003757 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003758
Charles Keepax11391102014-02-18 15:22:14 +00003759 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003760
Charles Keepax11391102014-02-18 15:22:14 +00003761 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003762
Charles Keepax11391102014-02-18 15:22:14 +00003763 mutex_unlock(&dapm->card->dapm_mutex);
3764
3765 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003766}
3767EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3768
3769/**
Charles Keepax11391102014-02-18 15:22:14 +00003770 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3771 * @dapm: DAPM context
3772 * @pin: pin name
3773 *
3774 * Disables input/output pin and its parents or children widgets.
3775 *
3776 * Requires external locking.
3777 *
3778 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3779 * do any widget power switching.
3780 */
3781int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3782 const char *pin)
3783{
3784 return snd_soc_dapm_set_pin(dapm, pin, 0);
3785}
3786EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3787
3788/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003789 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003790 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003791 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003792 *
Mark Brown74b8f952009-06-06 11:26:15 +01003793 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003794 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003795 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3796 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003797 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003798int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3799 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003800{
Charles Keepax11391102014-02-18 15:22:14 +00003801 int ret;
3802
3803 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3804
3805 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3806
3807 mutex_unlock(&dapm->card->dapm_mutex);
3808
3809 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003810}
3811EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3812
3813/**
Charles Keepax11391102014-02-18 15:22:14 +00003814 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3815 * @dapm: DAPM context
3816 * @pin: pin name
3817 *
3818 * Marks the specified pin as being not connected, disabling it along
3819 * any parent or child widgets. At present this is identical to
3820 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3821 * additional things such as disabling controls which only affect
3822 * paths through the pin.
3823 *
3824 * Requires external locking.
3825 *
3826 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3827 * do any widget power switching.
3828 */
3829int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3830 const char *pin)
3831{
3832 return snd_soc_dapm_set_pin(dapm, pin, 0);
3833}
3834EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3835
3836/**
Mark Brown5817b522008-09-24 11:23:11 +01003837 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003838 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003839 * @pin: pin name
3840 *
3841 * Marks the specified pin as being not connected, disabling it along
3842 * any parent or child widgets. At present this is identical to
3843 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3844 * additional things such as disabling controls which only affect
3845 * paths through the pin.
3846 *
3847 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3848 * do any widget power switching.
3849 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003850int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003851{
Charles Keepax11391102014-02-18 15:22:14 +00003852 int ret;
3853
3854 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3855
3856 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3857
3858 mutex_unlock(&dapm->card->dapm_mutex);
3859
3860 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003861}
3862EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3863
3864/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003865 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003866 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003867 * @pin: audio signal pin endpoint (or start point)
3868 *
3869 * Get audio pin status - connected or disconnected.
3870 *
3871 * Returns 1 for connected otherwise 0.
3872 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003873int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3874 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003875{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003876 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003877
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003878 if (w)
3879 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003880
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003881 return 0;
3882}
Liam Girdwooda5302182008-07-07 13:35:17 +01003883EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003884
3885/**
Mark Brown1547aba2010-05-07 21:11:40 +01003886 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003887 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003888 * @pin: audio signal pin endpoint (or start point)
3889 *
3890 * Mark the given endpoint or pin as ignoring suspend. When the
3891 * system is disabled a path between two endpoints flagged as ignoring
3892 * suspend will not be disabled. The path must already be enabled via
3893 * normal means at suspend time, it will not be turned on if it was not
3894 * already enabled.
3895 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003896int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3897 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003898{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003899 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003900
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003901 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003902 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003903 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003904 }
3905
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003906 w->ignore_suspend = 1;
3907
3908 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003909}
3910EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3911
Stephen Warren16332812011-11-23 12:42:04 -07003912static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3913 struct snd_soc_dapm_widget *w)
3914{
3915 struct snd_soc_dapm_path *p;
3916
3917 list_for_each_entry(p, &card->paths, list) {
3918 if ((p->source == w) || (p->sink == w)) {
3919 dev_dbg(card->dev,
3920 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3921 p->source->name, p->source->id, p->source->dapm,
3922 p->sink->name, p->sink->id, p->sink->dapm);
3923
3924 /* Connected to something other than the codec */
3925 if (p->source->dapm != p->sink->dapm)
3926 return true;
3927 /*
3928 * Loopback connection from codec external pin to
3929 * codec external pin
3930 */
3931 if (p->sink->id == snd_soc_dapm_input) {
3932 switch (p->source->id) {
3933 case snd_soc_dapm_output:
3934 case snd_soc_dapm_micbias:
3935 return true;
3936 default:
3937 break;
3938 }
3939 }
3940 }
3941 }
3942
3943 return false;
3944}
3945
3946/**
3947 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3948 * @codec: The codec whose pins should be processed
3949 *
3950 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3951 * which are unused. Pins are used if they are connected externally to the
3952 * codec, whether that be to some other device, or a loop-back connection to
3953 * the codec itself.
3954 */
3955void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3956{
3957 struct snd_soc_card *card = codec->card;
3958 struct snd_soc_dapm_context *dapm = &codec->dapm;
3959 struct snd_soc_dapm_widget *w;
3960
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003961 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003962 &card->dapm, &codec->dapm);
3963
3964 list_for_each_entry(w, &card->widgets, list) {
3965 if (w->dapm != dapm)
3966 continue;
3967 switch (w->id) {
3968 case snd_soc_dapm_input:
3969 case snd_soc_dapm_output:
3970 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003971 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003972 w->name);
3973 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003974 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003975 "... Not in map; disabling\n");
3976 snd_soc_dapm_nc_pin(dapm, w->name);
3977 }
3978 break;
3979 default:
3980 break;
3981 }
3982 }
3983}
3984
Mark Brown1547aba2010-05-07 21:11:40 +01003985/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003986 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003987 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003988 *
3989 * Free all dapm widgets and resources.
3990 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003991void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003992{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003993 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003994 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003995 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003996 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003997}
3998EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3999
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004000static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004001{
Liam Girdwood01005a72012-07-06 16:57:05 +01004002 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004003 struct snd_soc_dapm_widget *w;
4004 LIST_HEAD(down_list);
4005 int powerdown = 0;
4006
Liam Girdwood01005a72012-07-06 16:57:05 +01004007 mutex_lock(&card->dapm_mutex);
4008
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004009 list_for_each_entry(w, &dapm->card->widgets, list) {
4010 if (w->dapm != dapm)
4011 continue;
Mark Brown51737472009-06-22 13:16:51 +01004012 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004013 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01004014 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004015 powerdown = 1;
4016 }
4017 }
4018
4019 /* If there were no widgets to power down we're already in
4020 * standby.
4021 */
4022 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004023 if (dapm->bias_level == SND_SOC_BIAS_ON)
4024 snd_soc_dapm_set_bias_level(dapm,
4025 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004026 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004027 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4028 snd_soc_dapm_set_bias_level(dapm,
4029 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004030 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004031
4032 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004033}
Mark Brown51737472009-06-22 13:16:51 +01004034
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004035/*
4036 * snd_soc_dapm_shutdown - callback for system shutdown
4037 */
4038void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4039{
4040 struct snd_soc_codec *codec;
4041
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06004042 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004043 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00004044 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4045 snd_soc_dapm_set_bias_level(&codec->dapm,
4046 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004047 }
Mark Brown51737472009-06-22 13:16:51 +01004048}
4049
Richard Purdie2b97eab2006-10-06 18:32:18 +02004050/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004051MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004052MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4053MODULE_LICENSE("GPL");