blob: d856e7c4c631ab36e4e79c5fe0d52910a4563b28 [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,
74 [snd_soc_dapm_value_mux] = 6,
75 [snd_soc_dapm_dac] = 7,
76 [snd_soc_dapm_switch] = 8,
77 [snd_soc_dapm_mixer] = 8,
78 [snd_soc_dapm_mixer_named_ctl] = 8,
79 [snd_soc_dapm_pga] = 9,
80 [snd_soc_dapm_adc] = 10,
81 [snd_soc_dapm_out_drv] = 11,
82 [snd_soc_dapm_hp] = 11,
83 [snd_soc_dapm_spk] = 11,
84 [snd_soc_dapm_line] = 11,
85 [snd_soc_dapm_kcontrol] = 12,
86 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +020087};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000088
Richard Purdie2b97eab2006-10-06 18:32:18 +020089static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010090 [snd_soc_dapm_pre] = 0,
Lars-Peter Clausen57295072013-08-05 11:27:31 +020091 [snd_soc_dapm_kcontrol] = 1,
92 [snd_soc_dapm_adc] = 2,
93 [snd_soc_dapm_hp] = 3,
94 [snd_soc_dapm_spk] = 3,
95 [snd_soc_dapm_line] = 3,
96 [snd_soc_dapm_out_drv] = 3,
Mark Brown38357ab2009-06-06 19:03:23 +010097 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020098 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +010099 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +0100100 [snd_soc_dapm_mixer] = 5,
101 [snd_soc_dapm_dac] = 6,
102 [snd_soc_dapm_mic] = 7,
103 [snd_soc_dapm_micbias] = 8,
104 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000105 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +0100106 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +0100107 [snd_soc_dapm_aif_in] = 10,
108 [snd_soc_dapm_aif_out] = 10,
Mark Brown46162742013-06-05 19:36:11 +0100109 [snd_soc_dapm_dai_in] = 10,
110 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100111 [snd_soc_dapm_dai_link] = 11,
Mark Brownc74184e2012-04-04 22:12:09 +0100112 [snd_soc_dapm_supply] = 12,
Mark Brown1dd275b2013-10-09 13:56:37 +0100113 [snd_soc_dapm_clock_supply] = 13,
114 [snd_soc_dapm_regulator_supply] = 13,
115 [snd_soc_dapm_post] = 14,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200116};
117
Troy Kisky12ef1932008-10-13 17:42:14 -0700118static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100119{
120 if (pop_time)
121 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
122}
123
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200124static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100125{
126 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200127 char *buf;
128
129 if (!pop_time)
130 return;
131
132 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
133 if (buf == NULL)
134 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100135
136 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200137 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100138 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100139 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200140
141 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100142}
143
Mark Browndb432b42011-10-03 21:06:40 +0100144static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
145{
146 return !list_empty(&w->dirty);
147}
148
Mark Brown25c77c52011-10-08 13:36:03 +0100149void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100150{
Mark Brown75c1f892011-10-04 22:28:08 +0100151 if (!dapm_dirty_widget(w)) {
152 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
153 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100154 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100155 }
Mark Browndb432b42011-10-03 21:06:40 +0100156}
Mark Brown25c77c52011-10-08 13:36:03 +0100157EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100158
Mark Browne2d32ff2012-08-31 17:38:32 -0700159void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
160{
161 struct snd_soc_card *card = dapm->card;
162 struct snd_soc_dapm_widget *w;
163
164 mutex_lock(&card->dapm_mutex);
165
166 list_for_each_entry(w, &card->widgets, list) {
167 switch (w->id) {
168 case snd_soc_dapm_input:
169 case snd_soc_dapm_output:
170 dapm_mark_dirty(w, "Rechecking inputs and outputs");
171 break;
172 default:
173 break;
174 }
175 }
176
177 mutex_unlock(&card->dapm_mutex);
178}
179EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
180
Richard Purdie2b97eab2006-10-06 18:32:18 +0200181/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100182static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200183 const struct snd_soc_dapm_widget *_widget)
184{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100185 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200186}
187
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200188struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200189 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200190 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200191 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200192 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200193};
194
195static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
196 struct snd_kcontrol *kcontrol)
197{
198 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200199 struct soc_mixer_control *mc;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200200
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200201 data = kzalloc(sizeof(*data), GFP_KERNEL);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200202 if (!data) {
203 dev_err(widget->dapm->dev,
204 "ASoC: can't allocate kcontrol data for %s\n",
205 widget->name);
206 return -ENOMEM;
207 }
208
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200209 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200210
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200211 switch (widget->id) {
212 case snd_soc_dapm_switch:
213 case snd_soc_dapm_mixer:
214 case snd_soc_dapm_mixer_named_ctl:
215 mc = (struct soc_mixer_control *)kcontrol->private_value;
216
217 if (mc->autodisable) {
218 struct snd_soc_dapm_widget template;
219
220 memset(&template, 0, sizeof(template));
221 template.reg = mc->reg;
222 template.mask = (1 << fls(mc->max)) - 1;
223 template.shift = mc->shift;
224 if (mc->invert)
225 template.off_val = mc->max;
226 else
227 template.off_val = 0;
228 template.on_val = template.off_val;
229 template.id = snd_soc_dapm_kcontrol;
230 template.name = kcontrol->id.name;
231
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200232 data->value = template.on_val;
233
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200234 data->widget = snd_soc_dapm_new_control(widget->dapm,
235 &template);
236 if (!data->widget) {
237 kfree(data);
238 return -ENOMEM;
239 }
240 }
241 break;
242 default:
243 break;
244 }
245
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200246 kcontrol->private_data = data;
247
248 return 0;
249}
250
251static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
252{
253 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200254 kfree(data->widget);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200255 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200256 kfree(data);
257}
258
259static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
260 const struct snd_kcontrol *kcontrol)
261{
262 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
263
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200264 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200265}
266
267static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
268 struct snd_soc_dapm_widget *widget)
269{
270 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200271 struct snd_soc_dapm_widget_list *new_wlist;
272 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200273
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200274 if (data->wlist)
275 n = data->wlist->num_widgets + 1;
276 else
277 n = 1;
278
279 new_wlist = krealloc(data->wlist,
280 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
281 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200282 return -ENOMEM;
283
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200284 new_wlist->widgets[n - 1] = widget;
285 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200286
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200287 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200288
289 return 0;
290}
291
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200292static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
293 struct snd_soc_dapm_path *path)
294{
295 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
296
297 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200298
299 if (data->widget) {
300 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
301 path->source, NULL, NULL);
302 }
303}
304
305static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
306{
307 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
308
309 if (!data->widget)
310 return true;
311
312 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200313}
314
315static struct list_head *dapm_kcontrol_get_path_list(
316 const struct snd_kcontrol *kcontrol)
317{
318 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
319
320 return &data->paths;
321}
322
323#define dapm_kcontrol_for_each_path(path, kcontrol) \
324 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
325 list_kcontrol)
326
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200327static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
328{
329 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
330
331 return data->value;
332}
333
334static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
335 unsigned int value)
336{
337 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
338
339 if (data->value == value)
340 return false;
341
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200342 if (data->widget)
343 data->widget->on_val = value;
344
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200345 data->value = value;
346
347 return true;
348}
349
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200350/**
351 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
352 * @kcontrol: The kcontrol
353 */
354struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
355{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200356 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200357}
358EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
359
Liam Girdwood6c120e12012-02-15 15:15:34 +0000360static void dapm_reset(struct snd_soc_card *card)
361{
362 struct snd_soc_dapm_widget *w;
363
364 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
365
366 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200367 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000368 w->power_checked = false;
369 w->inputs = -1;
370 w->outputs = -1;
371 }
372}
373
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800374static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg,
375 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100376{
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800377 if (w->codec) {
378 *value = snd_soc_read(w->codec, reg);
379 return 0;
380 } else if (w->platform) {
381 *value = snd_soc_platform_read(w->platform, reg);
382 return 0;
383 }
Liam Girdwoodb7950642011-07-04 22:10:52 +0100384
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000385 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100386 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100387}
388
Lars-Peter Clausen30ac6b62014-02-13 14:14:21 +0100389static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg,
390 unsigned int val)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100391{
392 if (w->codec)
393 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100394 else if (w->platform)
395 return snd_soc_platform_write(w->platform, reg, val);
396
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000397 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100398 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100399}
400
Liam Girdwood49575fb52012-03-06 18:16:19 +0000401static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
402{
Mark Browne06ab3b2012-03-06 23:58:22 +0000403 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000404 mutex_lock(&w->codec->mutex);
405 else if (w->platform)
406 mutex_lock(&w->platform->mutex);
407}
408
409static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
410{
Mark Browne06ab3b2012-03-06 23:58:22 +0000411 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000412 mutex_unlock(&w->codec->mutex);
413 else if (w->platform)
414 mutex_unlock(&w->platform->mutex);
415}
416
Mark Browneb270e92013-10-09 13:52:52 +0100417static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
418{
419 if (dapm->codec && dapm->codec->using_regmap)
420 regmap_async_complete(dapm->codec->control_data);
421}
422
Liam Girdwood49575fb52012-03-06 18:16:19 +0000423static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100424 unsigned short reg, unsigned int mask, unsigned int value)
425{
Mark Brown8a713da2011-12-03 12:33:55 +0000426 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100427 unsigned int old, new;
428 int ret;
429
Mark Brown8a713da2011-12-03 12:33:55 +0000430 if (w->codec && w->codec->using_regmap) {
Mark Browneb270e92013-10-09 13:52:52 +0100431 ret = regmap_update_bits_check_async(w->codec->control_data,
432 reg, mask, value,
433 &change);
Mark Brown8a713da2011-12-03 12:33:55 +0000434 if (ret != 0)
435 return ret;
436 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000437 soc_widget_lock(w);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800438 ret = soc_widget_read(w, reg, &old);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000439 if (ret < 0) {
440 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100441 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000442 }
Mark Brown8a713da2011-12-03 12:33:55 +0000443
Mark Brown8a713da2011-12-03 12:33:55 +0000444 new = (old & ~mask) | (value & mask);
445 change = old != new;
446 if (change) {
447 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000448 if (ret < 0) {
449 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000450 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000451 }
Mark Brown8a713da2011-12-03 12:33:55 +0000452 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000453 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100454 }
455
456 return change;
457}
458
Mark Brown452c5ea2009-05-17 21:41:23 +0100459/**
460 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800461 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100462 * @level: level to configure
463 *
464 * Configure the bias (power) levels for the SoC audio device.
465 *
466 * Returns 0 for success else error.
467 */
Mark Browned5a4c42011-02-18 11:12:42 -0800468static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200469 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100470{
Mark Browned5a4c42011-02-18 11:12:42 -0800471 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100472 int ret = 0;
473
Mark Brown84e90932010-11-04 00:07:02 -0400474 trace_snd_soc_bias_level_start(card, level);
475
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000476 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100477 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100478 if (ret != 0)
479 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100480
Mark Browncc4c6702011-06-06 19:03:34 +0100481 if (dapm->codec) {
482 if (dapm->codec->driver->set_bias_level)
483 ret = dapm->codec->driver->set_bias_level(dapm->codec,
484 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100485 else
486 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100487 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100488 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100489 }
Liam Girdwood41231282012-07-06 16:56:16 +0100490
Mark Brown171ec6b2011-06-06 18:15:19 +0100491 if (ret != 0)
492 goto out;
493
494 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100495 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100496out:
Mark Brown84e90932010-11-04 00:07:02 -0400497 trace_snd_soc_bias_level_done(card, level);
498
Mark Brown452c5ea2009-05-17 21:41:23 +0100499 return ret;
500}
501
Richard Purdie2b97eab2006-10-06 18:32:18 +0200502/* set up initial codec paths */
503static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
504 struct snd_soc_dapm_path *p, int i)
505{
506 switch (w->id) {
507 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000508 case snd_soc_dapm_mixer:
509 case snd_soc_dapm_mixer_named_ctl: {
Lars-Peter Clausen30ac6b62014-02-13 14:14:21 +0100510 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100511 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600512 w->kcontrol_news[i].private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +0200513 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400514 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100515 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400516 unsigned int mask = (1 << fls(max)) - 1;
517 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200518
Lars-Peter Clausen249ce132013-10-06 13:43:49 +0200519 if (reg != SND_SOC_NOPM) {
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800520 soc_widget_read(w, reg, &val);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +0200521 val = (val >> shift) & mask;
522 if (invert)
523 val = max - val;
524 p->connect = !!val;
525 } else {
526 p->connect = 0;
527 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200528
Richard Purdie2b97eab2006-10-06 18:32:18 +0200529 }
530 break;
531 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600532 struct soc_enum *e = (struct soc_enum *)
533 w->kcontrol_news[i].private_value;
Lars-Peter Clausen30ac6b62014-02-13 14:14:21 +0100534 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200535
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800536 soc_widget_read(w, e->reg, &val);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200537 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200538
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200539 if (item < e->max && !strcmp(p->name, e->texts[item]))
540 p->connect = 1;
541 else
542 p->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200543 }
544 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000545 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600546 struct soc_enum *e = (struct soc_enum *)
547 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000548
549 p->connect = 0;
550 /* since a virtual mux has no backing registers to
551 * decide which path to connect, it will try to match
552 * with the first enumeration. This is to ensure
553 * that the default mux choice (the first) will be
554 * correctly powered up during initialization.
555 */
556 if (!strcmp(p->name, e->texts[0]))
557 p->connect = 1;
558 }
559 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200560 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200561 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600562 w->kcontrol_news[i].private_value;
Lars-Peter Clausen30ac6b62014-02-13 14:14:21 +0100563 unsigned int val, item;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200564
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800565 soc_widget_read(w, e->reg, &val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200566 val = (val >> e->shift_l) & e->mask;
567 for (item = 0; item < e->max; item++) {
568 if (val == e->values[item])
569 break;
570 }
571
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200572 if (item < e->max && !strcmp(p->name, e->texts[item]))
573 p->connect = 1;
574 else
575 p->connect = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200576 }
577 break;
Mark Brown56563102011-10-03 22:41:09 +0100578 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200579 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600580 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200581 case snd_soc_dapm_output:
582 case snd_soc_dapm_adc:
583 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000584 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200585 case snd_soc_dapm_dac:
586 case snd_soc_dapm_micbias:
587 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100588 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000589 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200590 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100591 case snd_soc_dapm_aif_in:
592 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100593 case snd_soc_dapm_dai_in:
594 case snd_soc_dapm_dai_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200595 case snd_soc_dapm_hp:
596 case snd_soc_dapm_mic:
597 case snd_soc_dapm_spk:
598 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100599 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200600 case snd_soc_dapm_kcontrol:
Mark Brown56563102011-10-03 22:41:09 +0100601 p->connect = 1;
602 break;
603 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200604 case snd_soc_dapm_pre:
605 case snd_soc_dapm_post:
606 p->connect = 0;
607 break;
608 }
609}
610
Mark Brown74b8f952009-06-06 11:26:15 +0100611/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200612static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200613 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
614 struct snd_soc_dapm_path *path, const char *control_name,
615 const struct snd_kcontrol_new *kcontrol)
616{
617 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
618 int i;
619
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100620 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200621 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200622 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200623 list_add(&path->list_sink, &dest->sources);
624 list_add(&path->list_source, &src->sinks);
625 path->name = (char*)e->texts[i];
626 dapm_set_path_status(dest, path, 0);
627 return 0;
628 }
629 }
630
631 return -ENODEV;
632}
633
Mark Brown74b8f952009-06-06 11:26:15 +0100634/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200635static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200636 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
637 struct snd_soc_dapm_path *path, const char *control_name)
638{
639 int i;
640
641 /* search for mixer kcontrol */
642 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600643 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200644 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200645 list_add(&path->list_sink, &dest->sources);
646 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600647 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200648 dapm_set_path_status(dest, path, i);
649 return 0;
650 }
651 }
652 return -ENODEV;
653}
654
Stephen Warrenaf468002011-04-28 17:38:01 -0600655static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600656 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600657 const struct snd_kcontrol_new *kcontrol_new,
658 struct snd_kcontrol **kcontrol)
659{
660 struct snd_soc_dapm_widget *w;
661 int i;
662
663 *kcontrol = NULL;
664
665 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600666 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
667 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600668 for (i = 0; i < w->num_kcontrols; i++) {
669 if (&w->kcontrol_news[i] == kcontrol_new) {
670 if (w->kcontrols)
671 *kcontrol = w->kcontrols[i];
672 return 1;
673 }
674 }
675 }
676
677 return 0;
678}
679
Stephen Warren85762e72013-03-29 15:40:10 -0600680/*
681 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
682 * create it. Either way, add the widget into the control's widget list
683 */
684static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100685 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200686{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200687 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000688 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000689 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600690 size_t prefix_len;
691 int shared;
692 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600693 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600694 char *long_name;
695 const char *name;
696 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000697
698 if (dapm->codec)
699 prefix = dapm->codec->name_prefix;
700 else
701 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200702
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000703 if (prefix)
704 prefix_len = strlen(prefix) + 1;
705 else
706 prefix_len = 0;
707
Stephen Warren85762e72013-03-29 15:40:10 -0600708 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
709 &kcontrol);
710
Stephen Warren85762e72013-03-29 15:40:10 -0600711 if (!kcontrol) {
712 if (shared) {
713 wname_in_long_name = false;
714 kcname_in_long_name = true;
715 } else {
716 switch (w->id) {
717 case snd_soc_dapm_switch:
718 case snd_soc_dapm_mixer:
719 wname_in_long_name = true;
720 kcname_in_long_name = true;
721 break;
722 case snd_soc_dapm_mixer_named_ctl:
723 wname_in_long_name = false;
724 kcname_in_long_name = true;
725 break;
726 case snd_soc_dapm_mux:
727 case snd_soc_dapm_virt_mux:
728 case snd_soc_dapm_value_mux:
729 wname_in_long_name = true;
730 kcname_in_long_name = false;
731 break;
732 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600733 return -EINVAL;
734 }
735 }
736
737 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600738 /*
739 * The control will get a prefix from the control
740 * creation process but we're also using the same
741 * prefix for widgets so cut the prefix off the
742 * front of the widget name.
743 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200744 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600745 w->name + prefix_len,
746 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200747 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200748 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600749
750 name = long_name;
751 } else if (wname_in_long_name) {
752 long_name = NULL;
753 name = w->name + prefix_len;
754 } else {
755 long_name = NULL;
756 name = w->kcontrol_news[kci].name;
757 }
758
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200759 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600760 prefix);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200761 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200762 if (!kcontrol)
763 return -ENOMEM;
764 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200765
766 ret = dapm_kcontrol_data_alloc(w, kcontrol);
767 if (ret) {
768 snd_ctl_free_one(kcontrol);
769 return ret;
770 }
771
Stephen Warren85762e72013-03-29 15:40:10 -0600772 ret = snd_ctl_add(card, kcontrol);
773 if (ret < 0) {
774 dev_err(dapm->dev,
775 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
776 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600777 return ret;
778 }
Stephen Warren85762e72013-03-29 15:40:10 -0600779 }
780
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200781 ret = dapm_kcontrol_add_widget(kcontrol, w);
782 if (ret)
783 return ret;
784
Stephen Warren85762e72013-03-29 15:40:10 -0600785 w->kcontrols[kci] = kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600786
787 return 0;
788}
789
790/* create new dapm mixer control */
791static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
792{
793 int i, ret;
794 struct snd_soc_dapm_path *path;
795
Richard Purdie2b97eab2006-10-06 18:32:18 +0200796 /* add kcontrol */
797 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200798 /* match name */
799 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200800 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600801 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200802 continue;
803
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200804 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200805 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200806 continue;
807 }
808
Mark Brown946d92a2013-08-12 23:28:42 +0100809 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600810 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200811 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100812
813 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200814 }
815 }
Stephen Warren85762e72013-03-29 15:40:10 -0600816
817 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200818}
819
820/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200821static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200822{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200823 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600824 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600825 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200826
Stephen Warrenaf468002011-04-28 17:38:01 -0600827 if (w->num_kcontrols != 1) {
828 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000829 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600830 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200831 return -EINVAL;
832 }
833
Lars-Peter Clausenfe581392013-08-01 18:30:38 +0200834 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600835 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
836 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600837 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200838
Mark Brown946d92a2013-08-12 23:28:42 +0100839 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600840 if (ret < 0)
841 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600842
Richard Purdie2b97eab2006-10-06 18:32:18 +0200843 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200844 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200845
Stephen Warrenaf468002011-04-28 17:38:01 -0600846 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200847}
848
849/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200850static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200851{
Mark Browna6c65732010-03-03 17:45:21 +0000852 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200853 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000854 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200855
Mark Browna6c65732010-03-03 17:45:21 +0000856 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200857}
858
859/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100860static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
861 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200862{
863 struct snd_soc_dapm_path *p;
864
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100865 list_for_each_entry(p, sink, list_source) {
866 if (p->walked) {
867 p->walked = 0;
868 dapm_clear_walk_output(dapm, &p->sink->sinks);
869 }
870 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200871}
872
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100873static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
874 struct list_head *source)
875{
876 struct snd_soc_dapm_path *p;
877
878 list_for_each_entry(p, source, list_sink) {
879 if (p->walked) {
880 p->walked = 0;
881 dapm_clear_walk_input(dapm, &p->source->sources);
882 }
883 }
884}
885
886
Mark Brown99497882010-05-07 20:24:05 +0100887/* We implement power down on suspend by checking the power state of
888 * the ALSA card - when we are suspending the ALSA state for the card
889 * is set to D3.
890 */
891static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
892{
Mark Brown12ea2c72011-03-02 18:17:32 +0000893 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100894
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000895 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100896 case SNDRV_CTL_POWER_D3hot:
897 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100898 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000899 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200900 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100901 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100902 default:
903 return 1;
904 }
905}
906
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100907/* add widget to list if it's not already in the list */
908static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
909 struct snd_soc_dapm_widget *w)
910{
911 struct snd_soc_dapm_widget_list *wlist;
912 int wlistsize, wlistentries, i;
913
914 if (*list == NULL)
915 return -EINVAL;
916
917 wlist = *list;
918
919 /* is this widget already in the list */
920 for (i = 0; i < wlist->num_widgets; i++) {
921 if (wlist->widgets[i] == w)
922 return 0;
923 }
924
925 /* allocate some new space */
926 wlistentries = wlist->num_widgets + 1;
927 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
928 wlistentries * sizeof(struct snd_soc_dapm_widget *);
929 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
930 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000931 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100932 w->name);
933 return -ENOMEM;
934 }
935 wlist = *list;
936
937 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000938 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100939 w->name, wlist->num_widgets);
940
941 wlist->widgets[wlist->num_widgets] = w;
942 wlist->num_widgets++;
943 return 1;
944}
945
Richard Purdie2b97eab2006-10-06 18:32:18 +0200946/*
947 * Recursively check for a completed path to an active or physically connected
948 * output widget. Returns number of complete paths.
949 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100950static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
951 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200952{
953 struct snd_soc_dapm_path *path;
954 int con = 0;
955
Mark Brown024dc072011-10-09 11:52:05 +0100956 if (widget->outputs >= 0)
957 return widget->outputs;
958
Mark Brownde02d072011-09-20 21:43:24 +0100959 DAPM_UPDATE_STAT(widget, path_checks);
960
Mark Brown62ea8742012-01-21 21:14:48 +0000961 switch (widget->id) {
962 case snd_soc_dapm_supply:
963 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200964 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200965 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100966 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000967 default:
968 break;
969 }
Mark Brown246d0a12009-04-22 18:24:55 +0100970
Mark Brown010ff262009-08-17 17:39:22 +0100971 switch (widget->id) {
972 case snd_soc_dapm_adc:
973 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100974 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100975 if (widget->active) {
976 widget->outputs = snd_soc_dapm_suspend_check(widget);
977 return widget->outputs;
978 }
Mark Brown010ff262009-08-17 17:39:22 +0100979 default:
980 break;
981 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200982
983 if (widget->connected) {
984 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100985 if (widget->id == snd_soc_dapm_output && !widget->ext) {
986 widget->outputs = snd_soc_dapm_suspend_check(widget);
987 return widget->outputs;
988 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200989
990 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100991 if (widget->id == snd_soc_dapm_hp ||
992 widget->id == snd_soc_dapm_spk ||
993 (widget->id == snd_soc_dapm_line &&
994 !list_empty(&widget->sources))) {
995 widget->outputs = snd_soc_dapm_suspend_check(widget);
996 return widget->outputs;
997 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200998 }
999
1000 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +01001001 DAPM_UPDATE_STAT(widget, neighbour_checks);
1002
Mark Brownbf3a9e12011-06-13 16:42:29 +01001003 if (path->weak)
1004 continue;
1005
Mark Brown8af294b2013-02-22 17:48:15 +00001006 if (path->walking)
1007 return 1;
1008
Richard Purdie2b97eab2006-10-06 18:32:18 +02001009 if (path->walked)
1010 continue;
1011
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001012 trace_snd_soc_dapm_output_path(widget, path);
1013
Richard Purdie2b97eab2006-10-06 18:32:18 +02001014 if (path->sink && path->connect) {
1015 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001016 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001017
1018 /* do we need to add this widget to the list ? */
1019 if (list) {
1020 int err;
1021 err = dapm_list_add_widget(list, path->sink);
1022 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001023 dev_err(widget->dapm->dev,
1024 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001025 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001026 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001027 return con;
1028 }
1029 }
1030
1031 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001032
1033 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001034 }
1035 }
1036
Mark Brown024dc072011-10-09 11:52:05 +01001037 widget->outputs = con;
1038
Richard Purdie2b97eab2006-10-06 18:32:18 +02001039 return con;
1040}
1041
1042/*
1043 * Recursively check for a completed path to an active or physically connected
1044 * input widget. Returns number of complete paths.
1045 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001046static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1047 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001048{
1049 struct snd_soc_dapm_path *path;
1050 int con = 0;
1051
Mark Brown024dc072011-10-09 11:52:05 +01001052 if (widget->inputs >= 0)
1053 return widget->inputs;
1054
Mark Brownde02d072011-09-20 21:43:24 +01001055 DAPM_UPDATE_STAT(widget, path_checks);
1056
Mark Brown62ea8742012-01-21 21:14:48 +00001057 switch (widget->id) {
1058 case snd_soc_dapm_supply:
1059 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001060 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001061 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +01001062 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +00001063 default:
1064 break;
1065 }
Mark Brown246d0a12009-04-22 18:24:55 +01001066
Richard Purdie2b97eab2006-10-06 18:32:18 +02001067 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +01001068 switch (widget->id) {
1069 case snd_soc_dapm_dac:
1070 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +01001071 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +01001072 if (widget->active) {
1073 widget->inputs = snd_soc_dapm_suspend_check(widget);
1074 return widget->inputs;
1075 }
Mark Brown010ff262009-08-17 17:39:22 +01001076 default:
1077 break;
1078 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001079
1080 if (widget->connected) {
1081 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +01001082 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1083 widget->inputs = snd_soc_dapm_suspend_check(widget);
1084 return widget->inputs;
1085 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001086
1087 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +01001088 if (widget->id == snd_soc_dapm_vmid) {
1089 widget->inputs = snd_soc_dapm_suspend_check(widget);
1090 return widget->inputs;
1091 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001092
1093 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +03001094 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +01001095 (widget->id == snd_soc_dapm_line &&
1096 !list_empty(&widget->sinks))) {
1097 widget->inputs = snd_soc_dapm_suspend_check(widget);
1098 return widget->inputs;
1099 }
1100
Mark Brown1ab97c82011-11-27 16:21:51 +00001101 /* signal generator */
1102 if (widget->id == snd_soc_dapm_siggen) {
1103 widget->inputs = snd_soc_dapm_suspend_check(widget);
1104 return widget->inputs;
1105 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001106 }
1107
1108 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +01001109 DAPM_UPDATE_STAT(widget, neighbour_checks);
1110
Mark Brownbf3a9e12011-06-13 16:42:29 +01001111 if (path->weak)
1112 continue;
1113
Mark Brown8af294b2013-02-22 17:48:15 +00001114 if (path->walking)
1115 return 1;
1116
Richard Purdie2b97eab2006-10-06 18:32:18 +02001117 if (path->walked)
1118 continue;
1119
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001120 trace_snd_soc_dapm_input_path(widget, path);
1121
Richard Purdie2b97eab2006-10-06 18:32:18 +02001122 if (path->source && path->connect) {
1123 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001124 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001125
1126 /* do we need to add this widget to the list ? */
1127 if (list) {
1128 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001129 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001130 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001131 dev_err(widget->dapm->dev,
1132 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001133 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001134 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001135 return con;
1136 }
1137 }
1138
1139 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001140
1141 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001142 }
1143 }
1144
Mark Brown024dc072011-10-09 11:52:05 +01001145 widget->inputs = con;
1146
Richard Purdie2b97eab2006-10-06 18:32:18 +02001147 return con;
1148}
1149
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001150/**
1151 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1152 * @dai: the soc DAI.
1153 * @stream: stream direction.
1154 * @list: list of active widgets for this stream.
1155 *
1156 * Queries DAPM graph as to whether an valid audio stream path exists for
1157 * the initial stream specified by name. This takes into account
1158 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1159 *
1160 * Returns the number of valid paths or negative error.
1161 */
1162int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1163 struct snd_soc_dapm_widget_list **list)
1164{
1165 struct snd_soc_card *card = dai->card;
1166 int paths;
1167
1168 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1169 dapm_reset(card);
1170
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001171 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001172 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001173 dapm_clear_walk_output(&card->dapm,
1174 &dai->playback_widget->sinks);
1175 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001176 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001177 dapm_clear_walk_input(&card->dapm,
1178 &dai->capture_widget->sources);
1179 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001180
1181 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001182 mutex_unlock(&card->dapm_mutex);
1183
1184 return paths;
1185}
1186
Richard Purdie2b97eab2006-10-06 18:32:18 +02001187/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001188 * Handler for generic register modifier widget.
1189 */
1190int dapm_reg_event(struct snd_soc_dapm_widget *w,
1191 struct snd_kcontrol *kcontrol, int event)
1192{
1193 unsigned int val;
1194
1195 if (SND_SOC_DAPM_EVENT_ON(event))
1196 val = w->on_val;
1197 else
1198 val = w->off_val;
1199
Liam Girdwood49575fb52012-03-06 18:16:19 +00001200 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001201 w->mask << w->shift, val << w->shift);
1202
1203 return 0;
1204}
Mark Brown11589412008-07-29 11:42:23 +01001205EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001206
Mark Brown62ea8742012-01-21 21:14:48 +00001207/*
1208 * Handler for regulator supply widget.
1209 */
1210int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1211 struct snd_kcontrol *kcontrol, int event)
1212{
Mark Brownc05b84d2012-09-07 12:57:11 +08001213 int ret;
1214
Mark Browneb270e92013-10-09 13:52:52 +01001215 soc_dapm_async_complete(w->dapm);
1216
Mark Brownc05b84d2012-09-07 12:57:11 +08001217 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001218 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001219 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001220 if (ret != 0)
1221 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001222 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001223 w->name, ret);
1224 }
1225
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001226 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001227 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001228 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001229 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001230 if (ret != 0)
1231 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001232 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001233 w->name, ret);
1234 }
1235
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001236 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001237 }
Mark Brown62ea8742012-01-21 21:14:48 +00001238}
1239EXPORT_SYMBOL_GPL(dapm_regulator_event);
1240
Ola Liljad7e7eb92012-05-24 15:26:25 +02001241/*
1242 * Handler for clock supply widget.
1243 */
1244int dapm_clock_event(struct snd_soc_dapm_widget *w,
1245 struct snd_kcontrol *kcontrol, int event)
1246{
1247 if (!w->clk)
1248 return -EIO;
1249
Mark Browneb270e92013-10-09 13:52:52 +01001250 soc_dapm_async_complete(w->dapm);
1251
Mark Brownec029952012-06-04 08:16:20 +01001252#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001253 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001254 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001255 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001256 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001257 return 0;
1258 }
Mark Brownec029952012-06-04 08:16:20 +01001259#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001260 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001261}
1262EXPORT_SYMBOL_GPL(dapm_clock_event);
1263
Mark Brownd8050022011-09-28 18:28:23 +01001264static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1265{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001266 if (w->power_checked)
1267 return w->new_power;
1268
Mark Brownd8050022011-09-28 18:28:23 +01001269 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001270 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001271 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001272 w->new_power = w->power_check(w);
1273
1274 w->power_checked = true;
1275
1276 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001277}
1278
Mark Browncd0f2d42009-04-20 16:56:59 +01001279/* Generic check to see if a widget should be powered.
1280 */
1281static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1282{
1283 int in, out;
1284
Mark Brownde02d072011-09-20 21:43:24 +01001285 DAPM_UPDATE_STAT(w, power_checks);
1286
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001287 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001288 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001289 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001290 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001291 return out != 0 && in != 0;
1292}
1293
Mark Brown6ea31b92009-04-20 17:15:41 +01001294/* Check to see if an ADC has power */
1295static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1296{
1297 int in;
1298
Mark Brownde02d072011-09-20 21:43:24 +01001299 DAPM_UPDATE_STAT(w, power_checks);
1300
Mark Brown6ea31b92009-04-20 17:15:41 +01001301 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001302 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001303 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001304 return in != 0;
1305 } else {
1306 return dapm_generic_check_power(w);
1307 }
1308}
1309
1310/* Check to see if a DAC has power */
1311static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1312{
1313 int out;
1314
Mark Brownde02d072011-09-20 21:43:24 +01001315 DAPM_UPDATE_STAT(w, power_checks);
1316
Mark Brown6ea31b92009-04-20 17:15:41 +01001317 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001318 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001319 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001320 return out != 0;
1321 } else {
1322 return dapm_generic_check_power(w);
1323 }
1324}
1325
Mark Brown246d0a12009-04-22 18:24:55 +01001326/* Check to see if a power supply is needed */
1327static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1328{
1329 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001330
Mark Brownde02d072011-09-20 21:43:24 +01001331 DAPM_UPDATE_STAT(w, power_checks);
1332
Mark Brown246d0a12009-04-22 18:24:55 +01001333 /* Check if one of our outputs is connected */
1334 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001335 DAPM_UPDATE_STAT(w, neighbour_checks);
1336
Mark Brownbf3a9e12011-06-13 16:42:29 +01001337 if (path->weak)
1338 continue;
1339
Mark Brown215edda2009-09-08 18:59:05 +01001340 if (path->connected &&
1341 !path->connected(path->source, path->sink))
1342 continue;
1343
Mark Brown30173582011-02-11 11:42:19 +00001344 if (!path->sink)
1345 continue;
1346
Mark Brownf68d7e12011-10-04 22:57:50 +01001347 if (dapm_widget_power_check(path->sink))
1348 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001349 }
1350
Mark Brownf68d7e12011-10-04 22:57:50 +01001351 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001352}
1353
Mark Brown35c64bc2011-09-28 18:23:53 +01001354static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1355{
1356 return 1;
1357}
1358
Mark Brown38357ab2009-06-06 19:03:23 +01001359static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1360 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001361 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001362{
Mark Brown828a8422011-01-15 13:14:30 +00001363 int *sort;
1364
1365 if (power_up)
1366 sort = dapm_up_seq;
1367 else
1368 sort = dapm_down_seq;
1369
Mark Brown38357ab2009-06-06 19:03:23 +01001370 if (sort[a->id] != sort[b->id])
1371 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001372 if (a->subseq != b->subseq) {
1373 if (power_up)
1374 return a->subseq - b->subseq;
1375 else
1376 return b->subseq - a->subseq;
1377 }
Mark Brownb22ead22009-06-07 12:51:26 +01001378 if (a->reg != b->reg)
1379 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001380 if (a->dapm != b->dapm)
1381 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001382
Mark Brown38357ab2009-06-06 19:03:23 +01001383 return 0;
1384}
Mark Brown42aa3412009-03-01 19:21:10 +00001385
Mark Brown38357ab2009-06-06 19:03:23 +01001386/* Insert a widget in order into a DAPM power sequence. */
1387static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1388 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001389 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001390{
1391 struct snd_soc_dapm_widget *w;
1392
1393 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001394 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001395 list_add_tail(&new_widget->power_list, &w->power_list);
1396 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001397 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001398
Mark Brown38357ab2009-06-06 19:03:23 +01001399 list_add_tail(&new_widget->power_list, list);
1400}
Mark Brown42aa3412009-03-01 19:21:10 +00001401
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001402static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001403 struct snd_soc_dapm_widget *w, int event)
1404{
Mark Brown68f89ad2010-11-03 23:51:49 -04001405 const char *ev_name;
1406 int power, ret;
1407
1408 switch (event) {
1409 case SND_SOC_DAPM_PRE_PMU:
1410 ev_name = "PRE_PMU";
1411 power = 1;
1412 break;
1413 case SND_SOC_DAPM_POST_PMU:
1414 ev_name = "POST_PMU";
1415 power = 1;
1416 break;
1417 case SND_SOC_DAPM_PRE_PMD:
1418 ev_name = "PRE_PMD";
1419 power = 0;
1420 break;
1421 case SND_SOC_DAPM_POST_PMD:
1422 ev_name = "POST_PMD";
1423 power = 0;
1424 break;
Mark Brown80114122013-02-25 15:14:19 +00001425 case SND_SOC_DAPM_WILL_PMU:
1426 ev_name = "WILL_PMU";
1427 power = 1;
1428 break;
1429 case SND_SOC_DAPM_WILL_PMD:
1430 ev_name = "WILL_PMD";
1431 power = 0;
1432 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001433 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001434 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001435 return;
1436 }
1437
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001438 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001439 return;
1440
1441 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001442 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001443 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001444 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001445 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001446 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001447 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001448 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001449 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001450 ev_name, w->name, ret);
1451 }
1452}
1453
Mark Brownb22ead22009-06-07 12:51:26 +01001454/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001455static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001456 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001457{
Mark Brown68f89ad2010-11-03 23:51:49 -04001458 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001459 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001460 unsigned int value = 0;
1461 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001462
1463 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1464 power_list)->reg;
1465
1466 list_for_each_entry(w, pending, power_list) {
Takashi Iwaibf4edea2013-11-07 18:38:47 +01001467 WARN_ON(reg != w->reg);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001468 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001469
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001470 mask |= w->mask << w->shift;
1471 if (w->power)
1472 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001473 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001474 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001475
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001476 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001477 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1478 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001479
Mark Brown68f89ad2010-11-03 23:51:49 -04001480 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001481 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1482 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001483 }
1484
Mark Brown81628102009-06-07 13:21:24 +01001485 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001486 /* Any widget will do, they should all be updating the
1487 * same register.
1488 */
1489 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1490 power_list);
1491
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001492 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001493 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001494 value, mask, reg, card->pop_time);
1495 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001496 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001497 }
1498
1499 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001500 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1501 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001502 }
Mark Brown42aa3412009-03-01 19:21:10 +00001503}
1504
Mark Brownb22ead22009-06-07 12:51:26 +01001505/* Apply a DAPM power sequence.
1506 *
1507 * We walk over a pre-sorted list of widgets to apply power to. In
1508 * order to minimise the number of writes to the device required
1509 * multiple widgets will be updated in a single write where possible.
1510 * Currently anything that requires more than a single write is not
1511 * handled.
1512 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001513static void dapm_seq_run(struct snd_soc_card *card,
1514 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001515{
1516 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001517 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001518 LIST_HEAD(pending);
1519 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001520 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001521 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001522 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001523 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001524 int *sort;
1525
1526 if (power_up)
1527 sort = dapm_up_seq;
1528 else
1529 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001530
Mark Brownb22ead22009-06-07 12:51:26 +01001531 list_for_each_entry_safe(w, n, list, power_list) {
1532 ret = 0;
1533
1534 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001535 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001536 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001537 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001538 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001539
Mark Brown474b62d2011-01-18 16:14:44 +00001540 if (cur_dapm && cur_dapm->seq_notifier) {
1541 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1542 if (sort[i] == cur_sort)
1543 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001544 i,
1545 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001546 }
1547
Mark Browneb270e92013-10-09 13:52:52 +01001548 if (cur_dapm && w->dapm != cur_dapm)
1549 soc_dapm_async_complete(cur_dapm);
1550
Mark Brownb22ead22009-06-07 12:51:26 +01001551 INIT_LIST_HEAD(&pending);
1552 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001553 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001554 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001555 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001556 }
1557
Mark Brown163cac02009-06-07 10:12:52 +01001558 switch (w->id) {
1559 case snd_soc_dapm_pre:
1560 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001561 list_for_each_entry_safe_continue(w, n, list,
1562 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001563
Mark Brownb22ead22009-06-07 12:51:26 +01001564 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001565 ret = w->event(w,
1566 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001567 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001568 ret = w->event(w,
1569 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001570 break;
1571
1572 case snd_soc_dapm_post:
1573 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001574 list_for_each_entry_safe_continue(w, n, list,
1575 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001576
Mark Brownb22ead22009-06-07 12:51:26 +01001577 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001578 ret = w->event(w,
1579 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001580 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001581 ret = w->event(w,
1582 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001583 break;
1584
Mark Brown163cac02009-06-07 10:12:52 +01001585 default:
Mark Brown81628102009-06-07 13:21:24 +01001586 /* Queue it up for application */
1587 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001588 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001589 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001590 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001591 list_move(&w->power_list, &pending);
1592 break;
Mark Brown163cac02009-06-07 10:12:52 +01001593 }
Mark Brownb22ead22009-06-07 12:51:26 +01001594
1595 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001596 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001597 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001598 }
Mark Brownb22ead22009-06-07 12:51:26 +01001599
1600 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001601 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001602
1603 if (cur_dapm && cur_dapm->seq_notifier) {
1604 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1605 if (sort[i] == cur_sort)
1606 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001607 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001608 }
Mark Browneb270e92013-10-09 13:52:52 +01001609
1610 list_for_each_entry(d, &card->dapm_list, list) {
1611 soc_dapm_async_complete(d);
1612 }
Mark Brown163cac02009-06-07 10:12:52 +01001613}
1614
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001615static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001616{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001617 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001618 struct snd_soc_dapm_widget_list *wlist;
1619 struct snd_soc_dapm_widget *w = NULL;
1620 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001621 int ret;
1622
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001623 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001624 return;
1625
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001626 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
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_PRE_REG)) {
1632 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1633 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001634 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-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
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001639 if (!w)
1640 return;
1641
Liam Girdwood49575fb52012-03-06 18:16:19 +00001642 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001643 update->val);
1644 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001645 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001646 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001647
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001648 for (wi = 0; wi < wlist->num_widgets; wi++) {
1649 w = wlist->widgets[wi];
1650
1651 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1652 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1653 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001654 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001655 w->name, ret);
1656 }
Mark Brown97404f22010-12-14 16:13:57 +00001657 }
1658}
1659
Mark Brown9d0624a2011-02-18 11:49:43 -08001660/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1661 * they're changing state.
1662 */
1663static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1664{
1665 struct snd_soc_dapm_context *d = data;
1666 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001667
Mark Brown56fba412011-06-04 11:25:10 +01001668 /* If we're off and we're not supposed to be go into STANDBY */
1669 if (d->bias_level == SND_SOC_BIAS_OFF &&
1670 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001671 if (d->dev)
1672 pm_runtime_get_sync(d->dev);
1673
Mark Brown9d0624a2011-02-18 11:49:43 -08001674 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1675 if (ret != 0)
1676 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001677 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001678 }
1679
Mark Brown56fba412011-06-04 11:25:10 +01001680 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1681 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001682 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1683 if (ret != 0)
1684 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001685 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001686 }
1687}
1688
1689/* Async callback run prior to DAPM sequences - brings to their final
1690 * state.
1691 */
1692static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1693{
1694 struct snd_soc_dapm_context *d = data;
1695 int ret;
1696
1697 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001698 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1699 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1700 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001701 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1702 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001703 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001704 ret);
1705 }
1706
1707 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001708 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1709 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001710 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1711 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001712 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1713 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001714
1715 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001716 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001717 }
1718
1719 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001720 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1721 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001722 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1723 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001724 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001725 ret);
1726 }
1727}
Mark Brown97404f22010-12-14 16:13:57 +00001728
Mark Brownfe4fda52011-10-03 22:36:57 +01001729static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1730 bool power, bool connect)
1731{
1732 /* If a connection is being made or broken then that update
1733 * will have marked the peer dirty, otherwise the widgets are
1734 * not connected and this update has no impact. */
1735 if (!connect)
1736 return;
1737
1738 /* If the peer is already in the state we're moving to then we
1739 * won't have an impact on it. */
1740 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001741 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001742}
1743
Mark Brown05623c42011-09-28 17:02:31 +01001744static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1745 struct list_head *up_list,
1746 struct list_head *down_list)
1747{
Mark Browndb432b42011-10-03 21:06:40 +01001748 struct snd_soc_dapm_path *path;
1749
Mark Brown05623c42011-09-28 17:02:31 +01001750 if (w->power == power)
1751 return;
1752
1753 trace_snd_soc_dapm_widget_power(w, power);
1754
Mark Browndb432b42011-10-03 21:06:40 +01001755 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001756 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001757 */
1758 list_for_each_entry(path, &w->sources, list_sink) {
1759 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001760 dapm_widget_set_peer_power(path->source, power,
1761 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001762 }
1763 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001764 switch (w->id) {
1765 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001766 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001767 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001768 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001769 /* Supplies can't affect their outputs, only their inputs */
1770 break;
1771 default:
1772 list_for_each_entry(path, &w->sinks, list_source) {
1773 if (path->sink) {
1774 dapm_widget_set_peer_power(path->sink, power,
1775 path->connect);
1776 }
Mark Browndb432b42011-10-03 21:06:40 +01001777 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001778 break;
Mark Browndb432b42011-10-03 21:06:40 +01001779 }
1780
Mark Brown05623c42011-09-28 17:02:31 +01001781 if (power)
1782 dapm_seq_insert(w, up_list, true);
1783 else
1784 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001785}
1786
Mark Brown7c81beb2011-09-20 22:22:32 +01001787static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1788 struct list_head *up_list,
1789 struct list_head *down_list)
1790{
Mark Brown7c81beb2011-09-20 22:22:32 +01001791 int power;
1792
1793 switch (w->id) {
1794 case snd_soc_dapm_pre:
1795 dapm_seq_insert(w, down_list, false);
1796 break;
1797 case snd_soc_dapm_post:
1798 dapm_seq_insert(w, up_list, true);
1799 break;
1800
1801 default:
Mark Brownd8050022011-09-28 18:28:23 +01001802 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001803
Mark Brown05623c42011-09-28 17:02:31 +01001804 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001805 break;
1806 }
1807}
1808
Mark Brown42aa3412009-03-01 19:21:10 +00001809/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001810 * Scan each dapm widget for complete audio path.
1811 * A complete path is a route that has valid endpoints i.e.:-
1812 *
1813 * o DAC to output pin.
1814 * o Input Pin to ADC.
1815 * o Input pin to Output pin (bypass, sidetone)
1816 * o DAC to ADC (loopback).
1817 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001818static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001819{
1820 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001821 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001822 LIST_HEAD(up_list);
1823 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001824 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001825 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001826
Mark Brown84e90932010-11-04 00:07:02 -04001827 trace_snd_soc_dapm_start(card);
1828
Mark Brown56fba412011-06-04 11:25:10 +01001829 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001830 if (d->idle_bias_off)
1831 d->target_bias_level = SND_SOC_BIAS_OFF;
1832 else
1833 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001834 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001835
Liam Girdwood6c120e12012-02-15 15:15:34 +00001836 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001837
Mark Brown6d3ddc82009-05-16 17:47:29 +01001838 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001839 * lists indicating if they should be powered up or down. We
1840 * only check widgets that have been flagged as dirty but note
1841 * that new widgets may be added to the dirty list while we
1842 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001843 */
Mark Browndb432b42011-10-03 21:06:40 +01001844 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001845 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001846 }
1847
Mark Brownf9de6d72011-09-28 17:19:47 +01001848 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001849 switch (w->id) {
1850 case snd_soc_dapm_pre:
1851 case snd_soc_dapm_post:
1852 /* These widgets always need to be powered */
1853 break;
1854 default:
1855 list_del_init(&w->dirty);
1856 break;
1857 }
Mark Browndb432b42011-10-03 21:06:40 +01001858
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001859 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001860 d = w->dapm;
1861
1862 /* Supplies and micbiases only bring the
1863 * context up to STANDBY as unless something
1864 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001865 * generally don't require full power. Signal
1866 * generators are virtual pins and have no
1867 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001868 */
1869 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001870 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001871 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001872 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001873 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001874 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001875 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001876 case snd_soc_dapm_micbias:
1877 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1878 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1879 break;
1880 default:
1881 d->target_bias_level = SND_SOC_BIAS_ON;
1882 break;
1883 }
1884 }
1885
1886 }
1887
Mark Brown85a843c2011-09-21 21:29:47 +01001888 /* Force all contexts in the card to the same bias state if
1889 * they're not ground referenced.
1890 */
Mark Brown56fba412011-06-04 11:25:10 +01001891 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001892 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001893 if (d->target_bias_level > bias)
1894 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001895 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001896 if (!d->idle_bias_off)
1897 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001898
Mark Brownde02d072011-09-20 21:43:24 +01001899 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001900
Mark Brown9d0624a2011-02-18 11:49:43 -08001901 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001902 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001903 async_schedule_domain(dapm_pre_sequence_async, d,
1904 &async_domain);
1905 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001906
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001907 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001908 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001909 }
1910
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001911 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001912 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001913 }
1914
Mark Brown6d3ddc82009-05-16 17:47:29 +01001915 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001916 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001917
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001918 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001919
Mark Brown6d3ddc82009-05-16 17:47:29 +01001920 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001921 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001922
Mark Brown9d0624a2011-02-18 11:49:43 -08001923 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001924 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001925 async_schedule_domain(dapm_post_sequence_async, d,
1926 &async_domain);
1927 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001928
Liam Girdwood8078d872012-02-15 15:15:35 +00001929 /* do we need to notify any clients that DAPM event is complete */
1930 list_for_each_entry(d, &card->dapm_list, list) {
1931 if (d->stream_event)
1932 d->stream_event(d, event);
1933 }
1934
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001935 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001936 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001937 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001938
Mark Brown84e90932010-11-04 00:07:02 -04001939 trace_snd_soc_dapm_done(card);
1940
Mark Brown42aa3412009-03-01 19:21:10 +00001941 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001942}
1943
Mark Brown79fb9382009-08-21 16:38:13 +01001944#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001945static ssize_t dapm_widget_power_read_file(struct file *file,
1946 char __user *user_buf,
1947 size_t count, loff_t *ppos)
1948{
1949 struct snd_soc_dapm_widget *w = file->private_data;
1950 char *buf;
1951 int in, out;
1952 ssize_t ret;
1953 struct snd_soc_dapm_path *p = NULL;
1954
1955 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1956 if (!buf)
1957 return -ENOMEM;
1958
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001959 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001960 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001961 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001962 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001963
Mark Brownf13ebad2012-03-03 18:01:01 +00001964 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1965 w->name, w->power ? "On" : "Off",
1966 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001967
Mark Brownd033c362009-12-04 15:25:56 +00001968 if (w->reg >= 0)
1969 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001970 " - R%d(0x%x) mask 0x%x",
1971 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001972
1973 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1974
Mark Brown3eef08b2009-09-14 16:49:00 +01001975 if (w->sname)
1976 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1977 w->sname,
1978 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001979
1980 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001981 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001982 continue;
1983
Mark Brown79fb9382009-08-21 16:38:13 +01001984 if (p->connect)
1985 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001986 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001987 p->name ? p->name : "static",
1988 p->source->name);
1989 }
1990 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001991 if (p->connected && !p->connected(w, p->sink))
1992 continue;
1993
Mark Brown79fb9382009-08-21 16:38:13 +01001994 if (p->connect)
1995 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001996 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001997 p->name ? p->name : "static",
1998 p->sink->name);
1999 }
2000
2001 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2002
2003 kfree(buf);
2004 return ret;
2005}
2006
2007static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002008 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002009 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002010 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002011};
2012
Mark Brownef49e4f2011-04-04 20:48:13 +09002013static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2014 size_t count, loff_t *ppos)
2015{
2016 struct snd_soc_dapm_context *dapm = file->private_data;
2017 char *level;
2018
2019 switch (dapm->bias_level) {
2020 case SND_SOC_BIAS_ON:
2021 level = "On\n";
2022 break;
2023 case SND_SOC_BIAS_PREPARE:
2024 level = "Prepare\n";
2025 break;
2026 case SND_SOC_BIAS_STANDBY:
2027 level = "Standby\n";
2028 break;
2029 case SND_SOC_BIAS_OFF:
2030 level = "Off\n";
2031 break;
2032 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002033 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002034 level = "Unknown\n";
2035 break;
2036 }
2037
2038 return simple_read_from_buffer(user_buf, count, ppos, level,
2039 strlen(level));
2040}
2041
2042static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002043 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002044 .read = dapm_bias_read_file,
2045 .llseek = default_llseek,
2046};
2047
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002048void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2049 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002050{
Mark Brown79fb9382009-08-21 16:38:13 +01002051 struct dentry *d;
2052
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002053 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2054
2055 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002056 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002057 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002058 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002059 }
Mark Brown79fb9382009-08-21 16:38:13 +01002060
Mark Brownef49e4f2011-04-04 20:48:13 +09002061 d = debugfs_create_file("bias_level", 0444,
2062 dapm->debugfs_dapm, dapm,
2063 &dapm_bias_fops);
2064 if (!d)
2065 dev_warn(dapm->dev,
2066 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002067}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002068
2069static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2070{
2071 struct snd_soc_dapm_context *dapm = w->dapm;
2072 struct dentry *d;
2073
2074 if (!dapm->debugfs_dapm || !w->name)
2075 return;
2076
2077 d = debugfs_create_file(w->name, 0444,
2078 dapm->debugfs_dapm, w,
2079 &dapm_widget_power_fops);
2080 if (!d)
2081 dev_warn(w->dapm->dev,
2082 "ASoC: Failed to create %s debugfs file\n",
2083 w->name);
2084}
2085
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002086static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2087{
2088 debugfs_remove_recursive(dapm->debugfs_dapm);
2089}
2090
Mark Brown79fb9382009-08-21 16:38:13 +01002091#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002092void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2093 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002094{
2095}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002096
2097static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2098{
2099}
2100
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002101static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2102{
2103}
2104
Mark Brown79fb9382009-08-21 16:38:13 +01002105#endif
2106
Richard Purdie2b97eab2006-10-06 18:32:18 +02002107/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002108static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002109 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002110{
2111 struct snd_soc_dapm_path *path;
2112 int found = 0;
2113
Richard Purdie2b97eab2006-10-06 18:32:18 +02002114 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002115 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002116 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002117 continue;
2118
2119 found = 1;
2120 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002121 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002122 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002123 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002124 } else {
2125 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002126 dapm_mark_dirty(path->source,
2127 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002128 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002129 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002130 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002131 }
2132
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002133 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002134 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002135
Liam Girdwood618dae12012-04-25 12:12:51 +01002136 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002137}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002138
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002139int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002140 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2141 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002142{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002143 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002144 int ret;
2145
Liam Girdwood3cd04342012-03-09 12:02:08 +00002146 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002147 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002148 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002149 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002150 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002151 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002152 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002153 return ret;
2154}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002155EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002156
Milan plzik1b075e32008-01-10 14:39:46 +01002157/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002158static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002159 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002160{
2161 struct snd_soc_dapm_path *path;
2162 int found = 0;
2163
Richard Purdie2b97eab2006-10-06 18:32:18 +02002164 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002165 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002166 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002167 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002168 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002169 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002170 }
2171
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002172 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002173 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002174
Liam Girdwood618dae12012-04-25 12:12:51 +01002175 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002176}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002177
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002178int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002179 struct snd_kcontrol *kcontrol, int connect,
2180 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002181{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002182 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002183 int ret;
2184
Liam Girdwood3cd04342012-03-09 12:02:08 +00002185 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002186 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002187 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002188 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002189 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002190 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002191 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002192 return ret;
2193}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002194EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002195
2196/* show dapm widget status in sys fs */
2197static ssize_t dapm_widget_show(struct device *dev,
2198 struct device_attribute *attr, char *buf)
2199{
Mark Brown36ae1a92012-01-06 17:12:45 -08002200 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002201 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002202 struct snd_soc_dapm_widget *w;
2203 int count = 0;
2204 char *state = "not set";
2205
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002206 list_for_each_entry(w, &codec->card->widgets, list) {
2207 if (w->dapm != &codec->dapm)
2208 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002209
2210 /* only display widgets that burnm power */
2211 switch (w->id) {
2212 case snd_soc_dapm_hp:
2213 case snd_soc_dapm_mic:
2214 case snd_soc_dapm_spk:
2215 case snd_soc_dapm_line:
2216 case snd_soc_dapm_micbias:
2217 case snd_soc_dapm_dac:
2218 case snd_soc_dapm_adc:
2219 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002220 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002221 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002222 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002223 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002224 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002225 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002226 if (w->name)
2227 count += sprintf(buf + count, "%s: %s\n",
2228 w->name, w->power ? "On":"Off");
2229 break;
2230 default:
2231 break;
2232 }
2233 }
2234
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002235 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002236 case SND_SOC_BIAS_ON:
2237 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002238 break;
Mark Brown0be98982008-05-19 12:31:28 +02002239 case SND_SOC_BIAS_PREPARE:
2240 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002241 break;
Mark Brown0be98982008-05-19 12:31:28 +02002242 case SND_SOC_BIAS_STANDBY:
2243 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244 break;
Mark Brown0be98982008-05-19 12:31:28 +02002245 case SND_SOC_BIAS_OFF:
2246 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002247 break;
2248 }
2249 count += sprintf(buf + count, "PM State: %s\n", state);
2250
2251 return count;
2252}
2253
2254static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2255
2256int snd_soc_dapm_sys_add(struct device *dev)
2257{
Troy Kisky12ef1932008-10-13 17:42:14 -07002258 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002259}
2260
2261static void snd_soc_dapm_sys_remove(struct device *dev)
2262{
Mark Brownaef90842009-05-16 17:53:16 +01002263 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002264}
2265
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002266static void dapm_free_path(struct snd_soc_dapm_path *path)
2267{
2268 list_del(&path->list_sink);
2269 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002270 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002271 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002272 kfree(path);
2273}
2274
Richard Purdie2b97eab2006-10-06 18:32:18 +02002275/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002276static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002277{
2278 struct snd_soc_dapm_widget *w, *next_w;
2279 struct snd_soc_dapm_path *p, *next_p;
2280
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002281 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2282 if (w->dapm != dapm)
2283 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002284 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002285 /*
2286 * remove source and sink paths associated to this widget.
2287 * While removing the path, remove reference to it from both
2288 * source and sink widgets so that path is removed only once.
2289 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002290 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2291 dapm_free_path(p);
2292
2293 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2294 dapm_free_path(p);
2295
Stephen Warrenfad59882011-04-28 17:37:59 -06002296 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002297 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002298 kfree(w);
2299 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002300}
2301
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002302static struct snd_soc_dapm_widget *dapm_find_widget(
2303 struct snd_soc_dapm_context *dapm, const char *pin,
2304 bool search_other_contexts)
2305{
2306 struct snd_soc_dapm_widget *w;
2307 struct snd_soc_dapm_widget *fallback = NULL;
2308
2309 list_for_each_entry(w, &dapm->card->widgets, list) {
2310 if (!strcmp(w->name, pin)) {
2311 if (w->dapm == dapm)
2312 return w;
2313 else
2314 fallback = w;
2315 }
2316 }
2317
2318 if (search_other_contexts)
2319 return fallback;
2320
2321 return NULL;
2322}
2323
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002324static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002325 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002326{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002327 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002328
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002329 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002330 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002331 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002332 }
2333
Mark Brown1a8b2d92012-02-16 11:50:07 -08002334 if (w->connected != status)
2335 dapm_mark_dirty(w, "pin configuration");
2336
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002337 w->connected = status;
2338 if (status == 0)
2339 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002340
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002341 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002342}
2343
Richard Purdie2b97eab2006-10-06 18:32:18 +02002344/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002345 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2346 * @dapm: DAPM context
2347 *
2348 * Walks all dapm audio paths and powers widgets according to their
2349 * stream or path usage.
2350 *
2351 * Requires external locking.
2352 *
2353 * Returns 0 for success.
2354 */
2355int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2356{
2357 /*
2358 * Suppress early reports (eg, jacks syncing their state) to avoid
2359 * silly DAPM runs during card startup.
2360 */
2361 if (!dapm->card || !dapm->card->instantiated)
2362 return 0;
2363
2364 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2365}
2366EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2367
2368/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002369 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002370 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002371 *
2372 * Walks all dapm audio paths and powers widgets according to their
2373 * stream or path usage.
2374 *
2375 * Returns 0 for success.
2376 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002377int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002378{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002379 int ret;
2380
Liam Girdwood3cd04342012-03-09 12:02:08 +00002381 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002382 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002383 mutex_unlock(&dapm->card->dapm_mutex);
2384 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002385}
Liam Girdwooda5302182008-07-07 13:35:17 +01002386EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002387
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002388static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2389 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2390 const char *control,
2391 int (*connected)(struct snd_soc_dapm_widget *source,
2392 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002393{
2394 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002395 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002396
2397 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2398 if (!path)
2399 return -ENOMEM;
2400
2401 path->source = wsource;
2402 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002403 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002404 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002405 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002406 INIT_LIST_HEAD(&path->list_source);
2407 INIT_LIST_HEAD(&path->list_sink);
2408
2409 /* check for external widgets */
2410 if (wsink->id == snd_soc_dapm_input) {
2411 if (wsource->id == snd_soc_dapm_micbias ||
2412 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002413 wsource->id == snd_soc_dapm_line ||
2414 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002415 wsink->ext = 1;
2416 }
2417 if (wsource->id == snd_soc_dapm_output) {
2418 if (wsink->id == snd_soc_dapm_spk ||
2419 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002420 wsink->id == snd_soc_dapm_line ||
2421 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002422 wsource->ext = 1;
2423 }
2424
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002425 dapm_mark_dirty(wsource, "Route added");
2426 dapm_mark_dirty(wsink, "Route added");
2427
Richard Purdie2b97eab2006-10-06 18:32:18 +02002428 /* connect static paths */
2429 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002430 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002431 list_add(&path->list_sink, &wsink->sources);
2432 list_add(&path->list_source, &wsource->sinks);
2433 path->connect = 1;
2434 return 0;
2435 }
2436
2437 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002438 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002439 case snd_soc_dapm_adc:
2440 case snd_soc_dapm_dac:
2441 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002442 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002443 case snd_soc_dapm_input:
2444 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002445 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002446 case snd_soc_dapm_micbias:
2447 case snd_soc_dapm_vmid:
2448 case snd_soc_dapm_pre:
2449 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002450 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002451 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002452 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002453 case snd_soc_dapm_aif_in:
2454 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002455 case snd_soc_dapm_dai_in:
2456 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002457 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002458 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002459 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002460 list_add(&path->list_sink, &wsink->sources);
2461 list_add(&path->list_source, &wsource->sinks);
2462 path->connect = 1;
2463 return 0;
2464 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002465 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002466 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002467 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002468 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002469 if (ret != 0)
2470 goto err;
2471 break;
2472 case snd_soc_dapm_switch:
2473 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002474 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002475 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002476 if (ret != 0)
2477 goto err;
2478 break;
2479 case snd_soc_dapm_hp:
2480 case snd_soc_dapm_mic:
2481 case snd_soc_dapm_line:
2482 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002483 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002484 list_add(&path->list_sink, &wsink->sources);
2485 list_add(&path->list_source, &wsource->sinks);
2486 path->connect = 0;
2487 return 0;
2488 }
Mark Brownfabd0382012-07-05 17:20:06 +01002489
Richard Purdie2b97eab2006-10-06 18:32:18 +02002490 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002491err:
2492 kfree(path);
2493 return ret;
2494}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002495
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002496static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002497 const struct snd_soc_dapm_route *route,
2498 unsigned int is_prefixed)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002499{
2500 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2501 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2502 const char *sink;
2503 const char *source;
2504 char prefixed_sink[80];
2505 char prefixed_source[80];
2506 int ret;
2507
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002508 if (dapm->codec && dapm->codec->name_prefix && !is_prefixed) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002509 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2510 dapm->codec->name_prefix, route->sink);
2511 sink = prefixed_sink;
2512 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2513 dapm->codec->name_prefix, route->source);
2514 source = prefixed_source;
2515 } else {
2516 sink = route->sink;
2517 source = route->source;
2518 }
2519
2520 /*
2521 * find src and dest widgets over all widgets but favor a widget from
2522 * current DAPM context
2523 */
2524 list_for_each_entry(w, &dapm->card->widgets, list) {
2525 if (!wsink && !(strcmp(w->name, sink))) {
2526 wtsink = w;
2527 if (w->dapm == dapm)
2528 wsink = w;
2529 continue;
2530 }
2531 if (!wsource && !(strcmp(w->name, source))) {
2532 wtsource = w;
2533 if (w->dapm == dapm)
2534 wsource = w;
2535 }
2536 }
2537 /* use widget from another DAPM context if not found from this */
2538 if (!wsink)
2539 wsink = wtsink;
2540 if (!wsource)
2541 wsource = wtsource;
2542
2543 if (wsource == NULL) {
2544 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2545 route->source);
2546 return -ENODEV;
2547 }
2548 if (wsink == NULL) {
2549 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2550 route->sink);
2551 return -ENODEV;
2552 }
2553
2554 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2555 route->connected);
2556 if (ret)
2557 goto err;
2558
2559 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002560err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002561 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002562 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002563 return ret;
2564}
Mark Brown105f1c22008-05-13 14:52:19 +02002565
Mark Brownefcc3c62012-07-05 17:24:19 +01002566static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2567 const struct snd_soc_dapm_route *route)
2568{
2569 struct snd_soc_dapm_path *path, *p;
2570 const char *sink;
2571 const char *source;
2572 char prefixed_sink[80];
2573 char prefixed_source[80];
2574
2575 if (route->control) {
2576 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002577 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002578 return -EINVAL;
2579 }
2580
2581 if (dapm->codec && dapm->codec->name_prefix) {
2582 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2583 dapm->codec->name_prefix, route->sink);
2584 sink = prefixed_sink;
2585 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2586 dapm->codec->name_prefix, route->source);
2587 source = prefixed_source;
2588 } else {
2589 sink = route->sink;
2590 source = route->source;
2591 }
2592
2593 path = NULL;
2594 list_for_each_entry(p, &dapm->card->paths, list) {
2595 if (strcmp(p->source->name, source) != 0)
2596 continue;
2597 if (strcmp(p->sink->name, sink) != 0)
2598 continue;
2599 path = p;
2600 break;
2601 }
2602
2603 if (path) {
2604 dapm_mark_dirty(path->source, "Route removed");
2605 dapm_mark_dirty(path->sink, "Route removed");
2606
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002607 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002608 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002609 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002610 source, sink);
2611 }
2612
2613 return 0;
2614}
2615
Mark Brown105f1c22008-05-13 14:52:19 +02002616/**
Mark Brown105f1c22008-05-13 14:52:19 +02002617 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002618 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002619 * @route: audio routes
2620 * @num: number of routes
2621 *
2622 * Connects 2 dapm widgets together via a named audio path. The sink is
2623 * the widget receiving the audio signal, whilst the source is the sender
2624 * of the audio signal.
2625 *
2626 * Returns 0 for success else error. On error all resources can be freed
2627 * with a call to snd_soc_card_free().
2628 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002629int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002630 const struct snd_soc_dapm_route *route, int num)
2631{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002632 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002633
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002634 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002635 for (i = 0; i < num; i++) {
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002636 r = snd_soc_dapm_add_route(dapm, route, false);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002637 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002638 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2639 route->source,
2640 route->control ? route->control : "direct",
2641 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002642 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002643 }
2644 route++;
2645 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002646 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002647
Dan Carpenter60884c22012-04-13 22:25:43 +03002648 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002649}
2650EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2651
Mark Brownefcc3c62012-07-05 17:24:19 +01002652/**
2653 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2654 * @dapm: DAPM context
2655 * @route: audio routes
2656 * @num: number of routes
2657 *
2658 * Removes routes from the DAPM context.
2659 */
2660int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2661 const struct snd_soc_dapm_route *route, int num)
2662{
2663 int i, ret = 0;
2664
2665 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2666 for (i = 0; i < num; i++) {
2667 snd_soc_dapm_del_route(dapm, route);
2668 route++;
2669 }
2670 mutex_unlock(&dapm->card->dapm_mutex);
2671
2672 return ret;
2673}
2674EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2675
Mark Brownbf3a9e12011-06-13 16:42:29 +01002676static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2677 const struct snd_soc_dapm_route *route)
2678{
2679 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2680 route->source,
2681 true);
2682 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2683 route->sink,
2684 true);
2685 struct snd_soc_dapm_path *path;
2686 int count = 0;
2687
2688 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002689 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002690 route->source);
2691 return -ENODEV;
2692 }
2693
2694 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002695 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002696 route->sink);
2697 return -ENODEV;
2698 }
2699
2700 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002701 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002702 route->source, route->sink);
2703
2704 list_for_each_entry(path, &source->sinks, list_source) {
2705 if (path->sink == sink) {
2706 path->weak = 1;
2707 count++;
2708 }
2709 }
2710
2711 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002712 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002713 route->source, route->sink);
2714 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002715 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002716 count, route->source, route->sink);
2717
2718 return 0;
2719}
2720
2721/**
2722 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2723 * @dapm: DAPM context
2724 * @route: audio routes
2725 * @num: number of routes
2726 *
2727 * Mark existing routes matching those specified in the passed array
2728 * as being weak, meaning that they are ignored for the purpose of
2729 * power decisions. The main intended use case is for sidetone paths
2730 * which couple audio between other independent paths if they are both
2731 * active in order to make the combination work better at the user
2732 * level but which aren't intended to be "used".
2733 *
2734 * Note that CODEC drivers should not use this as sidetone type paths
2735 * can frequently also be used as bypass paths.
2736 */
2737int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2738 const struct snd_soc_dapm_route *route, int num)
2739{
2740 int i, err;
2741 int ret = 0;
2742
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002743 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002744 for (i = 0; i < num; i++) {
2745 err = snd_soc_dapm_weak_route(dapm, route);
2746 if (err)
2747 ret = err;
2748 route++;
2749 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002750 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002751
2752 return ret;
2753}
2754EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2755
Mark Brown105f1c22008-05-13 14:52:19 +02002756/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002757 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002758 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002759 *
2760 * Checks the codec for any new dapm widgets and creates them if found.
2761 *
2762 * Returns 0 for success.
2763 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002764int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002765{
2766 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002767 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002768
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002769 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002770
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002771 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002772 {
2773 if (w->new)
2774 continue;
2775
Stephen Warrenfad59882011-04-28 17:37:59 -06002776 if (w->num_kcontrols) {
2777 w->kcontrols = kzalloc(w->num_kcontrols *
2778 sizeof(struct snd_kcontrol *),
2779 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002780 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002781 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002782 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002783 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002784 }
2785
Richard Purdie2b97eab2006-10-06 18:32:18 +02002786 switch(w->id) {
2787 case snd_soc_dapm_switch:
2788 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002789 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002790 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002791 break;
2792 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002793 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002794 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002795 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002796 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002797 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002798 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002799 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002800 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002801 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002802 break;
2803 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002804
2805 /* Read the initial power state from the device */
2806 if (w->reg >= 0) {
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002807 soc_widget_read(w, w->reg, &val);
2808 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002809 val &= w->mask;
2810 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002811 w->power = 1;
2812 }
2813
Richard Purdie2b97eab2006-10-06 18:32:18 +02002814 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002815
Mark Brown7508b122011-10-05 12:09:12 +01002816 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002817 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002818 }
2819
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002820 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2821 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002822 return 0;
2823}
2824EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2825
2826/**
2827 * snd_soc_dapm_get_volsw - dapm mixer get callback
2828 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002829 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002830 *
2831 * Callback to get the value of a dapm mixer control.
2832 *
2833 * Returns 0 for success.
2834 */
2835int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2836 struct snd_ctl_elem_value *ucontrol)
2837{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002838 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002839 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002840 struct soc_mixer_control *mc =
2841 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002842 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002843 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002844 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002845 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002846 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002847 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002848
2849 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002850 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002851 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002852 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002853
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002854 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002855 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002856 val = (snd_soc_read(codec, reg) >> shift) & mask;
2857 else
2858 val = dapm_kcontrol_get_value(kcontrol);
2859 mutex_unlock(&card->dapm_mutex);
2860
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002861 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002862 ucontrol->value.integer.value[0] = max - val;
2863 else
2864 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002865
2866 return 0;
2867}
2868EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2869
2870/**
2871 * snd_soc_dapm_put_volsw - dapm mixer set callback
2872 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002873 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002874 *
2875 * Callback to set the value of a dapm mixer control.
2876 *
2877 * Returns 0 for success.
2878 */
2879int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2880 struct snd_ctl_elem_value *ucontrol)
2881{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002882 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002883 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002884 struct soc_mixer_control *mc =
2885 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002886 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002887 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002888 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002889 unsigned int mask = (1 << fls(max)) - 1;
2890 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002891 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002892 int connect, change;
2893 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002894 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002895
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002896 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002897 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002898 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002899 kcontrol->id.name);
2900
Richard Purdie2b97eab2006-10-06 18:32:18 +02002901 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002902 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002903
2904 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002905 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002906
Liam Girdwood3cd04342012-03-09 12:02:08 +00002907 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002908
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002909 change = dapm_kcontrol_set_value(kcontrol, val);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002910
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002911 if (reg != SND_SOC_NOPM) {
2912 mask = mask << shift;
2913 val = val << shift;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002914
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002915 change = snd_soc_test_bits(codec, reg, mask, val);
2916 }
2917
Mark Brown97404f22010-12-14 16:13:57 +00002918 if (change) {
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002919 if (reg != SND_SOC_NOPM) {
2920 update.kcontrol = kcontrol;
2921 update.reg = reg;
2922 update.mask = mask;
2923 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002924
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002925 card->update = &update;
2926 }
Mark Brown97404f22010-12-14 16:13:57 +00002927
Nenghua Cao52765972013-12-13 20:13:49 +08002928 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002929
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002930 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002931 }
2932
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002933 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002934
2935 if (ret > 0)
2936 soc_dpcm_runtime_update(card);
2937
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002938 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002939}
2940EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2941
2942/**
2943 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2944 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002945 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002946 *
2947 * Callback to get the value of a dapm enumerated double mixer control.
2948 *
2949 * Returns 0 for success.
2950 */
2951int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2952 struct snd_ctl_elem_value *ucontrol)
2953{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002954 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002955 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002956 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002957
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002958 val = snd_soc_read(codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002959 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002960 if (e->shift_l != e->shift_r)
2961 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002962 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002963
2964 return 0;
2965}
2966EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2967
2968/**
2969 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2970 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002971 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002972 *
2973 * Callback to set the value of a dapm enumerated double mixer control.
2974 *
2975 * Returns 0 for success.
2976 */
2977int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2978 struct snd_ctl_elem_value *ucontrol)
2979{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002980 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002981 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002982 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002983 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002984 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002985 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002986 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002987
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002988 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002989 return -EINVAL;
2990 mux = ucontrol->value.enumerated.item[0];
2991 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002992 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002993 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002994 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002995 return -EINVAL;
2996 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002997 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002998 }
2999
Liam Girdwood3cd04342012-03-09 12:02:08 +00003000 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003001
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003002 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003003 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02003004 update.kcontrol = kcontrol;
3005 update.reg = e->reg;
3006 update.mask = mask;
3007 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003008 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00003009
Nenghua Cao52765972013-12-13 20:13:49 +08003010 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003011
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003012 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003013 }
3014
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003015 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003016
3017 if (ret > 0)
3018 soc_dpcm_runtime_update(card);
3019
Mark Brown97404f22010-12-14 16:13:57 +00003020 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003021}
3022EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3023
3024/**
Mark Brownd2b247a2009-10-06 15:21:04 +01003025 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
3026 * @kcontrol: mixer control
3027 * @ucontrol: control element information
3028 *
3029 * Returns 0 for success.
3030 */
3031int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
3032 struct snd_ctl_elem_value *ucontrol)
3033{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003034 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
Mark Brownd2b247a2009-10-06 15:21:04 +01003035 return 0;
3036}
3037EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
3038
3039/**
3040 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
3041 * @kcontrol: mixer control
3042 * @ucontrol: control element information
3043 *
3044 * Returns 0 for success.
3045 */
3046int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
3047 struct snd_ctl_elem_value *ucontrol)
3048{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003049 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003050 struct snd_soc_card *card = codec->card;
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003051 unsigned int value;
Mark Brownd2b247a2009-10-06 15:21:04 +01003052 struct soc_enum *e =
3053 (struct soc_enum *)kcontrol->private_value;
3054 int change;
Nenghua Cao52765972013-12-13 20:13:49 +08003055 int ret = 0;
Mark Brownd2b247a2009-10-06 15:21:04 +01003056
3057 if (ucontrol->value.enumerated.item[0] >= e->max)
3058 return -EINVAL;
3059
Liam Girdwood3cd04342012-03-09 12:02:08 +00003060 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01003061
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003062 value = ucontrol->value.enumerated.item[0];
3063 change = dapm_kcontrol_set_value(kcontrol, value);
3064 if (change)
Nenghua Cao52765972013-12-13 20:13:49 +08003065 ret = soc_dapm_mux_update_power(card, kcontrol, value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003066
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003067 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003068
3069 if (ret > 0)
3070 soc_dpcm_runtime_update(card);
3071
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003072 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01003073}
3074EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3075
3076/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003077 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3078 * callback
3079 * @kcontrol: mixer control
3080 * @ucontrol: control element information
3081 *
3082 * Callback to get the value of a dapm semi enumerated double mixer control.
3083 *
3084 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3085 * used for handling bitfield coded enumeration for example.
3086 *
3087 * Returns 0 for success.
3088 */
3089int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
3090 struct snd_ctl_elem_value *ucontrol)
3091{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003092 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02003093 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003094 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003095
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003096 reg_val = snd_soc_read(codec, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003097 val = (reg_val >> e->shift_l) & e->mask;
3098 for (mux = 0; mux < e->max; mux++) {
3099 if (val == e->values[mux])
3100 break;
3101 }
3102 ucontrol->value.enumerated.item[0] = mux;
3103 if (e->shift_l != e->shift_r) {
3104 val = (reg_val >> e->shift_r) & e->mask;
3105 for (mux = 0; mux < e->max; mux++) {
3106 if (val == e->values[mux])
3107 break;
3108 }
3109 ucontrol->value.enumerated.item[1] = mux;
3110 }
3111
3112 return 0;
3113}
3114EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
3115
3116/**
3117 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3118 * callback
3119 * @kcontrol: mixer control
3120 * @ucontrol: control element information
3121 *
3122 * Callback to set the value of a dapm semi enumerated double mixer control.
3123 *
3124 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3125 * used for handling bitfield coded enumeration for example.
3126 *
3127 * Returns 0 for success.
3128 */
3129int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
3130 struct snd_ctl_elem_value *ucontrol)
3131{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003132 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003133 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02003134 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01003135 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003136 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00003137 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003138 int ret = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003139
3140 if (ucontrol->value.enumerated.item[0] > e->max - 1)
3141 return -EINVAL;
3142 mux = ucontrol->value.enumerated.item[0];
3143 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
3144 mask = e->mask << e->shift_l;
3145 if (e->shift_l != e->shift_r) {
3146 if (ucontrol->value.enumerated.item[1] > e->max - 1)
3147 return -EINVAL;
3148 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
3149 mask |= e->mask << e->shift_r;
3150 }
3151
Liam Girdwood3cd04342012-03-09 12:02:08 +00003152 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003153
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003154 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003155 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02003156 update.kcontrol = kcontrol;
3157 update.reg = e->reg;
3158 update.mask = mask;
3159 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003160 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00003161
Nenghua Cao52765972013-12-13 20:13:49 +08003162 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003163
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003164 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003165 }
3166
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003167 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003168
3169 if (ret > 0)
3170 soc_dpcm_runtime_update(card);
3171
Mark Brown97404f22010-12-14 16:13:57 +00003172 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003173}
3174EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3175
3176/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003177 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3178 *
3179 * @kcontrol: mixer control
3180 * @uinfo: control element information
3181 *
3182 * Callback to provide information about a pin switch control.
3183 */
3184int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3185 struct snd_ctl_elem_info *uinfo)
3186{
3187 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3188 uinfo->count = 1;
3189 uinfo->value.integer.min = 0;
3190 uinfo->value.integer.max = 1;
3191
3192 return 0;
3193}
3194EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3195
3196/**
3197 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3198 *
3199 * @kcontrol: mixer control
3200 * @ucontrol: Value
3201 */
3202int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3203 struct snd_ctl_elem_value *ucontrol)
3204{
Mark Brown48a8c392012-02-14 17:11:15 -08003205 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003206 const char *pin = (const char *)kcontrol->private_value;
3207
Liam Girdwood3cd04342012-03-09 12:02:08 +00003208 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003209
3210 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003211 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003212
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003213 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003214
3215 return 0;
3216}
3217EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3218
3219/**
3220 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3221 *
3222 * @kcontrol: mixer control
3223 * @ucontrol: Value
3224 */
3225int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3226 struct snd_ctl_elem_value *ucontrol)
3227{
Mark Brown48a8c392012-02-14 17:11:15 -08003228 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003229 const char *pin = (const char *)kcontrol->private_value;
3230
Mark Brown8b37dbd2009-02-28 21:14:20 +00003231 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003232 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003233 else
Mark Brown48a8c392012-02-14 17:11:15 -08003234 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003235
Mark Brown48a8c392012-02-14 17:11:15 -08003236 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003237 return 0;
3238}
3239EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3240
Mark Brown5ba06fc2012-02-16 11:07:13 -08003241static struct snd_soc_dapm_widget *
3242snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3243 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003244{
3245 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003246 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003247
3248 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003249 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003250
Mark Brown62ea8742012-01-21 21:14:48 +00003251 switch (w->id) {
3252 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003253 w->regulator = devm_regulator_get(dapm->dev, w->name);
3254 if (IS_ERR(w->regulator)) {
3255 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003256 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003257 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003258 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003259 }
Mark Brown8784c772013-01-10 19:33:47 +00003260
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003261 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003262 ret = regulator_allow_bypass(w->regulator, true);
3263 if (ret != 0)
3264 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003265 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003266 w->name, ret);
3267 }
Mark Brown62ea8742012-01-21 21:14:48 +00003268 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003269 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003270#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003271 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003272 if (IS_ERR(w->clk)) {
3273 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003274 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003275 w->name, ret);
3276 return NULL;
3277 }
Mark Brownec029952012-06-04 08:16:20 +01003278#else
3279 return NULL;
3280#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003281 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003282 default:
3283 break;
3284 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003285
Mark Brown88e8b9a2011-03-02 18:18:24 +00003286 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003287 w->name = kasprintf(GFP_KERNEL, "%s %s",
3288 dapm->codec->name_prefix, widget->name);
3289 else
3290 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3291
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003292 if (w->name == NULL) {
3293 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003294 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003295 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003296
Mark Brown7ca3a182011-10-08 14:04:50 +01003297 switch (w->id) {
3298 case snd_soc_dapm_switch:
3299 case snd_soc_dapm_mixer:
3300 case snd_soc_dapm_mixer_named_ctl:
3301 w->power_check = dapm_generic_check_power;
3302 break;
3303 case snd_soc_dapm_mux:
3304 case snd_soc_dapm_virt_mux:
3305 case snd_soc_dapm_value_mux:
3306 w->power_check = dapm_generic_check_power;
3307 break;
Mark Brown46162742013-06-05 19:36:11 +01003308 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003309 w->power_check = dapm_adc_check_power;
3310 break;
Mark Brown46162742013-06-05 19:36:11 +01003311 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003312 w->power_check = dapm_dac_check_power;
3313 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003314 case snd_soc_dapm_adc:
3315 case snd_soc_dapm_aif_out:
3316 case snd_soc_dapm_dac:
3317 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003318 case snd_soc_dapm_pga:
3319 case snd_soc_dapm_out_drv:
3320 case snd_soc_dapm_input:
3321 case snd_soc_dapm_output:
3322 case snd_soc_dapm_micbias:
3323 case snd_soc_dapm_spk:
3324 case snd_soc_dapm_hp:
3325 case snd_soc_dapm_mic:
3326 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003327 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003328 w->power_check = dapm_generic_check_power;
3329 break;
3330 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003331 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003332 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003333 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003334 w->power_check = dapm_supply_check_power;
3335 break;
3336 default:
3337 w->power_check = dapm_always_on_check_power;
3338 break;
3339 }
3340
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003341 w->dapm = dapm;
3342 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003343 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003344 INIT_LIST_HEAD(&w->sources);
3345 INIT_LIST_HEAD(&w->sinks);
3346 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003347 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003348 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003349
3350 /* machine layer set ups unconnected pins and insertions */
3351 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003352 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003353}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003354
3355/**
Mark Brown4ba13272008-05-13 14:51:19 +02003356 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003357 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003358 * @widget: widget array
3359 * @num: number of widgets
3360 *
3361 * Creates new DAPM controls based upon the templates.
3362 *
3363 * Returns 0 for success else error.
3364 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003365int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003366 const struct snd_soc_dapm_widget *widget,
3367 int num)
3368{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003369 struct snd_soc_dapm_widget *w;
3370 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003371 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003372
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003373 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003374 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003375 w = snd_soc_dapm_new_control(dapm, widget);
3376 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003377 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003378 "ASoC: Failed to create DAPM control %s\n",
3379 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003380 ret = -ENOMEM;
3381 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003382 }
Mark Brown4ba13272008-05-13 14:51:19 +02003383 widget++;
3384 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003385 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003386 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003387}
3388EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3389
Mark Brownc74184e2012-04-04 22:12:09 +01003390static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3391 struct snd_kcontrol *kcontrol, int event)
3392{
3393 struct snd_soc_dapm_path *source_p, *sink_p;
3394 struct snd_soc_dai *source, *sink;
3395 const struct snd_soc_pcm_stream *config = w->params;
3396 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003397 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003398 u64 fmt;
3399 int ret;
3400
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003401 if (WARN_ON(!config) ||
3402 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3403 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003404
3405 /* We only support a single source and sink, pick the first */
3406 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3407 list_sink);
3408 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3409 list_source);
3410
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003411 if (WARN_ON(!source_p || !sink_p) ||
3412 WARN_ON(!sink_p->source || !source_p->sink) ||
3413 WARN_ON(!source_p->source || !sink_p->sink))
3414 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003415
3416 source = source_p->source->priv;
3417 sink = sink_p->sink->priv;
3418
3419 /* Be a little careful as we don't want to overflow the mask array */
3420 if (config->formats) {
3421 fmt = ffs(config->formats) - 1;
3422 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003423 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003424 config->formats);
3425 fmt = 0;
3426 }
3427
3428 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003429 params = kzalloc(sizeof(*params), GFP_KERNEL);
3430 if (!params) {
3431 ret = -ENOMEM;
3432 goto out;
3433 }
3434 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003435
Mark Brown9747cec2012-04-26 19:12:21 +01003436 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003437 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003438 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003439 config->rate_max;
3440
Mark Brown9747cec2012-04-26 19:12:21 +01003441 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003442 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003443 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003444 = config->channels_max;
3445
3446 memset(&substream, 0, sizeof(substream));
3447
3448 switch (event) {
3449 case SND_SOC_DAPM_PRE_PMU:
3450 if (source->driver->ops && source->driver->ops->hw_params) {
3451 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3452 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003453 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003454 if (ret != 0) {
3455 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003456 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003457 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003458 }
3459 }
3460
3461 if (sink->driver->ops && sink->driver->ops->hw_params) {
3462 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003463 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003464 sink);
3465 if (ret != 0) {
3466 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003467 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003468 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003469 }
3470 }
3471 break;
3472
3473 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003474 ret = snd_soc_dai_digital_mute(sink, 0,
3475 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003476 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003477 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003478 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003479 break;
3480
3481 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003482 ret = snd_soc_dai_digital_mute(sink, 1,
3483 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003484 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003485 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003486 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003487 break;
3488
3489 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003490 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003491 return -EINVAL;
3492 }
3493
Mark Brown9747cec2012-04-26 19:12:21 +01003494out:
3495 kfree(params);
3496 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003497}
3498
3499int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3500 const struct snd_soc_pcm_stream *params,
3501 struct snd_soc_dapm_widget *source,
3502 struct snd_soc_dapm_widget *sink)
3503{
3504 struct snd_soc_dapm_route routes[2];
3505 struct snd_soc_dapm_widget template;
3506 struct snd_soc_dapm_widget *w;
3507 size_t len;
3508 char *link_name;
3509
3510 len = strlen(source->name) + strlen(sink->name) + 2;
3511 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3512 if (!link_name)
3513 return -ENOMEM;
3514 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3515
3516 memset(&template, 0, sizeof(template));
3517 template.reg = SND_SOC_NOPM;
3518 template.id = snd_soc_dapm_dai_link;
3519 template.name = link_name;
3520 template.event = snd_soc_dai_link_event;
3521 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3522 SND_SOC_DAPM_PRE_PMD;
3523
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003524 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003525
3526 w = snd_soc_dapm_new_control(&card->dapm, &template);
3527 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003528 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003529 link_name);
3530 return -ENOMEM;
3531 }
3532
3533 w->params = params;
3534
3535 memset(&routes, 0, sizeof(routes));
3536
3537 routes[0].source = source->name;
3538 routes[0].sink = link_name;
3539 routes[1].source = link_name;
3540 routes[1].sink = sink->name;
3541
3542 return snd_soc_dapm_add_routes(&card->dapm, routes,
3543 ARRAY_SIZE(routes));
3544}
3545
Mark Brown888df392012-02-16 19:37:51 -08003546int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3547 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003548{
Mark Brown888df392012-02-16 19:37:51 -08003549 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003550 struct snd_soc_dapm_widget *w;
3551
Mark Brown888df392012-02-16 19:37:51 -08003552 WARN_ON(dapm->dev != dai->dev);
3553
3554 memset(&template, 0, sizeof(template));
3555 template.reg = SND_SOC_NOPM;
3556
3557 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003558 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003559 template.name = dai->driver->playback.stream_name;
3560 template.sname = dai->driver->playback.stream_name;
3561
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003562 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003563 template.name);
3564
3565 w = snd_soc_dapm_new_control(dapm, &template);
3566 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003567 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003568 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003569 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003570 }
3571
3572 w->priv = dai;
3573 dai->playback_widget = w;
3574 }
3575
3576 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003577 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003578 template.name = dai->driver->capture.stream_name;
3579 template.sname = dai->driver->capture.stream_name;
3580
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003581 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003582 template.name);
3583
3584 w = snd_soc_dapm_new_control(dapm, &template);
3585 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003586 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003587 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003588 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003589 }
3590
3591 w->priv = dai;
3592 dai->capture_widget = w;
3593 }
3594
3595 return 0;
3596}
3597
3598int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3599{
3600 struct snd_soc_dapm_widget *dai_w, *w;
3601 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003602
3603 /* For each DAI widget... */
3604 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003605 switch (dai_w->id) {
3606 case snd_soc_dapm_dai_in:
3607 case snd_soc_dapm_dai_out:
3608 break;
3609 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003610 continue;
Mark Brown46162742013-06-05 19:36:11 +01003611 }
Mark Brown888df392012-02-16 19:37:51 -08003612
3613 dai = dai_w->priv;
3614
3615 /* ...find all widgets with the same stream and link them */
3616 list_for_each_entry(w, &card->widgets, list) {
3617 if (w->dapm != dai_w->dapm)
3618 continue;
3619
Mark Brown46162742013-06-05 19:36:11 +01003620 switch (w->id) {
3621 case snd_soc_dapm_dai_in:
3622 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003623 continue;
Mark Brown46162742013-06-05 19:36:11 +01003624 default:
3625 break;
3626 }
Mark Brown888df392012-02-16 19:37:51 -08003627
Russell King19c2c5f2013-08-04 20:24:03 +01003628 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003629 continue;
3630
3631 if (dai->driver->playback.stream_name &&
3632 strstr(w->sname,
3633 dai->driver->playback.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003634 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003635 dai->playback_widget->name, w->name);
Mark Brown888df392012-02-16 19:37:51 -08003636
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003637 snd_soc_dapm_add_path(w->dapm,
3638 dai->playback_widget, w, NULL, NULL);
Mark Brown888df392012-02-16 19:37:51 -08003639 }
3640
3641 if (dai->driver->capture.stream_name &&
3642 strstr(w->sname,
3643 dai->driver->capture.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003644 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003645 w->name, dai->capture_widget->name);
Mark Brown888df392012-02-16 19:37:51 -08003646
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003647 snd_soc_dapm_add_path(w->dapm, w,
3648 dai->capture_widget, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003649 }
3650 }
3651 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003652
Mark Brown888df392012-02-16 19:37:51 -08003653 return 0;
3654}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003655
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003656void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3657{
3658 struct snd_soc_pcm_runtime *rtd = card->rtd;
3659 struct snd_soc_dai *cpu_dai, *codec_dai;
3660 struct snd_soc_dapm_route r;
3661 int i;
3662
3663 memset(&r, 0, sizeof(r));
3664
3665 /* for each BE DAI link... */
3666 for (i = 0; i < card->num_rtd; i++) {
3667 rtd = &card->rtd[i];
3668 cpu_dai = rtd->cpu_dai;
3669 codec_dai = rtd->codec_dai;
3670
3671 /* dynamic FE links have no fixed DAI mapping */
3672 if (rtd->dai_link->dynamic)
3673 continue;
3674
3675 /* there is no point in connecting BE DAI links with dummies */
3676 if (snd_soc_dai_is_dummy(codec_dai) ||
3677 snd_soc_dai_is_dummy(cpu_dai))
3678 continue;
3679
3680 /* connect BE DAI playback if widgets are valid */
3681 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3682 r.source = cpu_dai->playback_widget->name;
3683 r.sink = codec_dai->playback_widget->name;
3684 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3685 cpu_dai->codec->name, r.source,
3686 codec_dai->platform->name, r.sink);
3687
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003688 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003689 }
3690
3691 /* connect BE DAI capture if widgets are valid */
3692 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3693 r.source = codec_dai->capture_widget->name;
3694 r.sink = cpu_dai->capture_widget->name;
3695 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3696 codec_dai->codec->name, r.source,
3697 cpu_dai->platform->name, r.sink);
3698
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003699 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003700 }
3701
3702 }
3703}
3704
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003705static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3706 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003707{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003708
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003709 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3710 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3711 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003712
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003713 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3714 w_cpu = cpu_dai->playback_widget;
3715 w_codec = codec_dai->playback_widget;
3716 } else {
3717 w_cpu = cpu_dai->capture_widget;
3718 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003719 }
3720
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003721 if (w_cpu) {
3722
3723 dapm_mark_dirty(w_cpu, "stream event");
3724
3725 switch (event) {
3726 case SND_SOC_DAPM_STREAM_START:
3727 w_cpu->active = 1;
3728 break;
3729 case SND_SOC_DAPM_STREAM_STOP:
3730 w_cpu->active = 0;
3731 break;
3732 case SND_SOC_DAPM_STREAM_SUSPEND:
3733 case SND_SOC_DAPM_STREAM_RESUME:
3734 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3735 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3736 break;
3737 }
3738 }
3739
3740 if (w_codec) {
3741
3742 dapm_mark_dirty(w_codec, "stream event");
3743
3744 switch (event) {
3745 case SND_SOC_DAPM_STREAM_START:
3746 w_codec->active = 1;
3747 break;
3748 case SND_SOC_DAPM_STREAM_STOP:
3749 w_codec->active = 0;
3750 break;
3751 case SND_SOC_DAPM_STREAM_SUSPEND:
3752 case SND_SOC_DAPM_STREAM_RESUME:
3753 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3754 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3755 break;
3756 }
3757 }
3758
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003759 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003760}
3761
3762/**
3763 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3764 * @rtd: PCM runtime data
3765 * @stream: stream name
3766 * @event: stream event
3767 *
3768 * Sends a stream event to the dapm core. The core then makes any
3769 * necessary widget power changes.
3770 *
3771 * Returns 0 for success else error.
3772 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003773void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3774 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003775{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003776 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003777
Liam Girdwood3cd04342012-03-09 12:02:08 +00003778 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003779 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003780 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003781}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003782
3783/**
Charles Keepax11391102014-02-18 15:22:14 +00003784 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3785 * @dapm: DAPM context
3786 * @pin: pin name
3787 *
3788 * Enables input/output pin and its parents or children widgets iff there is
3789 * a valid audio route and active audio stream.
3790 *
3791 * Requires external locking.
3792 *
3793 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3794 * do any widget power switching.
3795 */
3796int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3797 const char *pin)
3798{
3799 return snd_soc_dapm_set_pin(dapm, pin, 1);
3800}
3801EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3802
3803/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003804 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003805 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003806 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003807 *
Mark Brown74b8f952009-06-06 11:26:15 +01003808 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003809 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003810 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003811 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3812 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003813 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003814int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003815{
Charles Keepax11391102014-02-18 15:22:14 +00003816 int ret;
3817
3818 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3819
3820 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3821
3822 mutex_unlock(&dapm->card->dapm_mutex);
3823
3824 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003825}
Liam Girdwooda5302182008-07-07 13:35:17 +01003826EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003827
3828/**
Charles Keepax11391102014-02-18 15:22:14 +00003829 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3830 * @dapm: DAPM context
3831 * @pin: pin name
3832 *
3833 * Enables input/output pin regardless of any other state. This is
3834 * intended for use with microphone bias supplies used in microphone
3835 * jack detection.
3836 *
3837 * Requires external locking.
3838 *
3839 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3840 * do any widget power switching.
3841 */
3842int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3843 const char *pin)
3844{
3845 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3846
3847 if (!w) {
3848 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3849 return -EINVAL;
3850 }
3851
3852 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3853 w->connected = 1;
3854 w->force = 1;
3855 dapm_mark_dirty(w, "force enable");
3856
3857 return 0;
3858}
3859EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3860
3861/**
Mark Brownda341832010-03-15 19:23:37 +00003862 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003863 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003864 * @pin: pin name
3865 *
3866 * Enables input/output pin regardless of any other state. This is
3867 * intended for use with microphone bias supplies used in microphone
3868 * jack detection.
3869 *
3870 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3871 * do any widget power switching.
3872 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003873int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3874 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003875{
Charles Keepax11391102014-02-18 15:22:14 +00003876 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003877
Charles Keepax11391102014-02-18 15:22:14 +00003878 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003879
Charles Keepax11391102014-02-18 15:22:14 +00003880 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003881
Charles Keepax11391102014-02-18 15:22:14 +00003882 mutex_unlock(&dapm->card->dapm_mutex);
3883
3884 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003885}
3886EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3887
3888/**
Charles Keepax11391102014-02-18 15:22:14 +00003889 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3890 * @dapm: DAPM context
3891 * @pin: pin name
3892 *
3893 * Disables input/output pin and its parents or children widgets.
3894 *
3895 * Requires external locking.
3896 *
3897 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3898 * do any widget power switching.
3899 */
3900int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3901 const char *pin)
3902{
3903 return snd_soc_dapm_set_pin(dapm, pin, 0);
3904}
3905EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3906
3907/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003908 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003909 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003910 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003911 *
Mark Brown74b8f952009-06-06 11:26:15 +01003912 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003913 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003914 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3915 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003916 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003917int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3918 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003919{
Charles Keepax11391102014-02-18 15:22:14 +00003920 int ret;
3921
3922 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3923
3924 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3925
3926 mutex_unlock(&dapm->card->dapm_mutex);
3927
3928 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003929}
3930EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3931
3932/**
Charles Keepax11391102014-02-18 15:22:14 +00003933 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3934 * @dapm: DAPM context
3935 * @pin: pin name
3936 *
3937 * Marks the specified pin as being not connected, disabling it along
3938 * any parent or child widgets. At present this is identical to
3939 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3940 * additional things such as disabling controls which only affect
3941 * paths through the pin.
3942 *
3943 * Requires external locking.
3944 *
3945 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3946 * do any widget power switching.
3947 */
3948int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3949 const char *pin)
3950{
3951 return snd_soc_dapm_set_pin(dapm, pin, 0);
3952}
3953EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3954
3955/**
Mark Brown5817b522008-09-24 11:23:11 +01003956 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003957 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003958 * @pin: pin name
3959 *
3960 * Marks the specified pin as being not connected, disabling it along
3961 * any parent or child widgets. At present this is identical to
3962 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3963 * additional things such as disabling controls which only affect
3964 * paths through the pin.
3965 *
3966 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3967 * do any widget power switching.
3968 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003969int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003970{
Charles Keepax11391102014-02-18 15:22:14 +00003971 int ret;
3972
3973 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3974
3975 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3976
3977 mutex_unlock(&dapm->card->dapm_mutex);
3978
3979 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003980}
3981EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3982
3983/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003984 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003985 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003986 * @pin: audio signal pin endpoint (or start point)
3987 *
3988 * Get audio pin status - connected or disconnected.
3989 *
3990 * Returns 1 for connected otherwise 0.
3991 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003992int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3993 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003994{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003995 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003996
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003997 if (w)
3998 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003999
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004000 return 0;
4001}
Liam Girdwooda5302182008-07-07 13:35:17 +01004002EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004003
4004/**
Mark Brown1547aba2010-05-07 21:11:40 +01004005 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004006 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004007 * @pin: audio signal pin endpoint (or start point)
4008 *
4009 * Mark the given endpoint or pin as ignoring suspend. When the
4010 * system is disabled a path between two endpoints flagged as ignoring
4011 * suspend will not be disabled. The path must already be enabled via
4012 * normal means at suspend time, it will not be turned on if it was not
4013 * already enabled.
4014 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004015int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4016 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004017{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004018 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004019
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004020 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004021 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004022 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004023 }
4024
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004025 w->ignore_suspend = 1;
4026
4027 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004028}
4029EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4030
Stephen Warren16332812011-11-23 12:42:04 -07004031static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
4032 struct snd_soc_dapm_widget *w)
4033{
4034 struct snd_soc_dapm_path *p;
4035
4036 list_for_each_entry(p, &card->paths, list) {
4037 if ((p->source == w) || (p->sink == w)) {
4038 dev_dbg(card->dev,
4039 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
4040 p->source->name, p->source->id, p->source->dapm,
4041 p->sink->name, p->sink->id, p->sink->dapm);
4042
4043 /* Connected to something other than the codec */
4044 if (p->source->dapm != p->sink->dapm)
4045 return true;
4046 /*
4047 * Loopback connection from codec external pin to
4048 * codec external pin
4049 */
4050 if (p->sink->id == snd_soc_dapm_input) {
4051 switch (p->source->id) {
4052 case snd_soc_dapm_output:
4053 case snd_soc_dapm_micbias:
4054 return true;
4055 default:
4056 break;
4057 }
4058 }
4059 }
4060 }
4061
4062 return false;
4063}
4064
4065/**
4066 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
4067 * @codec: The codec whose pins should be processed
4068 *
4069 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
4070 * which are unused. Pins are used if they are connected externally to the
4071 * codec, whether that be to some other device, or a loop-back connection to
4072 * the codec itself.
4073 */
4074void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
4075{
4076 struct snd_soc_card *card = codec->card;
4077 struct snd_soc_dapm_context *dapm = &codec->dapm;
4078 struct snd_soc_dapm_widget *w;
4079
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004080 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07004081 &card->dapm, &codec->dapm);
4082
4083 list_for_each_entry(w, &card->widgets, list) {
4084 if (w->dapm != dapm)
4085 continue;
4086 switch (w->id) {
4087 case snd_soc_dapm_input:
4088 case snd_soc_dapm_output:
4089 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004090 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07004091 w->name);
4092 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00004093 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07004094 "... Not in map; disabling\n");
4095 snd_soc_dapm_nc_pin(dapm, w->name);
4096 }
4097 break;
4098 default:
4099 break;
4100 }
4101 }
4102}
4103
Mark Brown1547aba2010-05-07 21:11:40 +01004104/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004105 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004106 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004107 *
4108 * Free all dapm widgets and resources.
4109 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004110void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004111{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004112 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004113 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004114 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004115 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004116}
4117EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4118
Xiang Xiao57996352014-03-02 00:04:02 +08004119static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004120{
Liam Girdwood01005a72012-07-06 16:57:05 +01004121 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004122 struct snd_soc_dapm_widget *w;
4123 LIST_HEAD(down_list);
4124 int powerdown = 0;
4125
Liam Girdwood01005a72012-07-06 16:57:05 +01004126 mutex_lock(&card->dapm_mutex);
4127
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004128 list_for_each_entry(w, &dapm->card->widgets, list) {
4129 if (w->dapm != dapm)
4130 continue;
Mark Brown51737472009-06-22 13:16:51 +01004131 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004132 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01004133 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004134 powerdown = 1;
4135 }
4136 }
4137
4138 /* If there were no widgets to power down we're already in
4139 * standby.
4140 */
4141 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004142 if (dapm->bias_level == SND_SOC_BIAS_ON)
4143 snd_soc_dapm_set_bias_level(dapm,
4144 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004145 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004146 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4147 snd_soc_dapm_set_bias_level(dapm,
4148 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004149 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004150
4151 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004152}
Mark Brown51737472009-06-22 13:16:51 +01004153
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004154/*
4155 * snd_soc_dapm_shutdown - callback for system shutdown
4156 */
4157void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4158{
Xiang Xiao57996352014-03-02 00:04:02 +08004159 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004160
Xiang Xiao57996352014-03-02 00:04:02 +08004161 list_for_each_entry(dapm, &card->dapm_list, list) {
4162 soc_dapm_shutdown_dapm(dapm);
4163 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4164 snd_soc_dapm_set_bias_level(dapm,
Mark Brown7679e422012-02-22 15:52:56 +00004165 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004166 }
Mark Brown51737472009-06-22 13:16:51 +01004167}
4168
Richard Purdie2b97eab2006-10-06 18:32:18 +02004169/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004170MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004171MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4172MODULE_LICENSE("GPL");