blob: 77743e51420a8d8a3a7b3371f998b9e50176e6a5 [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
Xiang Xiao17282ba2014-03-02 00:04:03 +08001901 /* Run card bias changes at first */
1902 dapm_pre_sequence_async(&card->dapm, 0);
1903 /* Run other bias changes in parallel */
1904 list_for_each_entry(d, &card->dapm_list, list) {
1905 if (d != &card->dapm)
1906 async_schedule_domain(dapm_pre_sequence_async, d,
1907 &async_domain);
1908 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001909 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001910
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001911 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001912 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001913 }
1914
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001915 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001916 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001917 }
1918
Mark Brown6d3ddc82009-05-16 17:47:29 +01001919 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001920 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001921
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001922 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001923
Mark Brown6d3ddc82009-05-16 17:47:29 +01001924 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001925 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001926
Mark Brown9d0624a2011-02-18 11:49:43 -08001927 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001928 list_for_each_entry(d, &card->dapm_list, list) {
1929 if (d != &card->dapm)
1930 async_schedule_domain(dapm_post_sequence_async, d,
1931 &async_domain);
1932 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001933 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001934 /* Run card bias changes at last */
1935 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001936
Liam Girdwood8078d872012-02-15 15:15:35 +00001937 /* do we need to notify any clients that DAPM event is complete */
1938 list_for_each_entry(d, &card->dapm_list, list) {
1939 if (d->stream_event)
1940 d->stream_event(d, event);
1941 }
1942
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001943 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001944 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001945 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001946
Mark Brown84e90932010-11-04 00:07:02 -04001947 trace_snd_soc_dapm_done(card);
1948
Mark Brown42aa3412009-03-01 19:21:10 +00001949 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001950}
1951
Mark Brown79fb9382009-08-21 16:38:13 +01001952#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001953static ssize_t dapm_widget_power_read_file(struct file *file,
1954 char __user *user_buf,
1955 size_t count, loff_t *ppos)
1956{
1957 struct snd_soc_dapm_widget *w = file->private_data;
1958 char *buf;
1959 int in, out;
1960 ssize_t ret;
1961 struct snd_soc_dapm_path *p = NULL;
1962
1963 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1964 if (!buf)
1965 return -ENOMEM;
1966
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001967 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001968 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001969 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001970 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001971
Mark Brownf13ebad2012-03-03 18:01:01 +00001972 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1973 w->name, w->power ? "On" : "Off",
1974 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001975
Mark Brownd033c362009-12-04 15:25:56 +00001976 if (w->reg >= 0)
1977 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001978 " - R%d(0x%x) mask 0x%x",
1979 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001980
1981 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1982
Mark Brown3eef08b2009-09-14 16:49:00 +01001983 if (w->sname)
1984 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1985 w->sname,
1986 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001987
1988 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001989 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001990 continue;
1991
Mark Brown79fb9382009-08-21 16:38:13 +01001992 if (p->connect)
1993 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001994 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001995 p->name ? p->name : "static",
1996 p->source->name);
1997 }
1998 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001999 if (p->connected && !p->connected(w, p->sink))
2000 continue;
2001
Mark Brown79fb9382009-08-21 16:38:13 +01002002 if (p->connect)
2003 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00002004 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01002005 p->name ? p->name : "static",
2006 p->sink->name);
2007 }
2008
2009 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2010
2011 kfree(buf);
2012 return ret;
2013}
2014
2015static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002016 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002017 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002018 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002019};
2020
Mark Brownef49e4f2011-04-04 20:48:13 +09002021static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2022 size_t count, loff_t *ppos)
2023{
2024 struct snd_soc_dapm_context *dapm = file->private_data;
2025 char *level;
2026
2027 switch (dapm->bias_level) {
2028 case SND_SOC_BIAS_ON:
2029 level = "On\n";
2030 break;
2031 case SND_SOC_BIAS_PREPARE:
2032 level = "Prepare\n";
2033 break;
2034 case SND_SOC_BIAS_STANDBY:
2035 level = "Standby\n";
2036 break;
2037 case SND_SOC_BIAS_OFF:
2038 level = "Off\n";
2039 break;
2040 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002041 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002042 level = "Unknown\n";
2043 break;
2044 }
2045
2046 return simple_read_from_buffer(user_buf, count, ppos, level,
2047 strlen(level));
2048}
2049
2050static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002051 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002052 .read = dapm_bias_read_file,
2053 .llseek = default_llseek,
2054};
2055
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002056void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2057 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002058{
Mark Brown79fb9382009-08-21 16:38:13 +01002059 struct dentry *d;
2060
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002061 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2062
2063 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002064 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002065 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002066 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002067 }
Mark Brown79fb9382009-08-21 16:38:13 +01002068
Mark Brownef49e4f2011-04-04 20:48:13 +09002069 d = debugfs_create_file("bias_level", 0444,
2070 dapm->debugfs_dapm, dapm,
2071 &dapm_bias_fops);
2072 if (!d)
2073 dev_warn(dapm->dev,
2074 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002075}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002076
2077static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2078{
2079 struct snd_soc_dapm_context *dapm = w->dapm;
2080 struct dentry *d;
2081
2082 if (!dapm->debugfs_dapm || !w->name)
2083 return;
2084
2085 d = debugfs_create_file(w->name, 0444,
2086 dapm->debugfs_dapm, w,
2087 &dapm_widget_power_fops);
2088 if (!d)
2089 dev_warn(w->dapm->dev,
2090 "ASoC: Failed to create %s debugfs file\n",
2091 w->name);
2092}
2093
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002094static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2095{
2096 debugfs_remove_recursive(dapm->debugfs_dapm);
2097}
2098
Mark Brown79fb9382009-08-21 16:38:13 +01002099#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002100void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2101 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002102{
2103}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002104
2105static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2106{
2107}
2108
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002109static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2110{
2111}
2112
Mark Brown79fb9382009-08-21 16:38:13 +01002113#endif
2114
Richard Purdie2b97eab2006-10-06 18:32:18 +02002115/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002116static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002117 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002118{
2119 struct snd_soc_dapm_path *path;
2120 int found = 0;
2121
Richard Purdie2b97eab2006-10-06 18:32:18 +02002122 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002123 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002124 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002125 continue;
2126
2127 found = 1;
2128 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002129 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002130 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002131 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002132 } else {
2133 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002134 dapm_mark_dirty(path->source,
2135 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002136 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002137 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002138 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002139 }
2140
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002141 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002142 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002143
Liam Girdwood618dae12012-04-25 12:12:51 +01002144 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002145}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002146
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002147int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002148 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2149 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002150{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002151 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002152 int ret;
2153
Liam Girdwood3cd04342012-03-09 12:02:08 +00002154 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002155 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002156 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002157 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002158 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002159 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002160 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002161 return ret;
2162}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002163EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002164
Milan plzik1b075e32008-01-10 14:39:46 +01002165/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002166static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002167 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002168{
2169 struct snd_soc_dapm_path *path;
2170 int found = 0;
2171
Richard Purdie2b97eab2006-10-06 18:32:18 +02002172 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002173 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002174 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002175 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002176 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002177 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002178 }
2179
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002180 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002181 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002182
Liam Girdwood618dae12012-04-25 12:12:51 +01002183 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002184}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002185
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002186int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002187 struct snd_kcontrol *kcontrol, int connect,
2188 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002189{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002190 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002191 int ret;
2192
Liam Girdwood3cd04342012-03-09 12:02:08 +00002193 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002194 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002195 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002196 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002197 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002198 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002199 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002200 return ret;
2201}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002202EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002203
2204/* show dapm widget status in sys fs */
2205static ssize_t dapm_widget_show(struct device *dev,
2206 struct device_attribute *attr, char *buf)
2207{
Mark Brown36ae1a92012-01-06 17:12:45 -08002208 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002209 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002210 struct snd_soc_dapm_widget *w;
2211 int count = 0;
2212 char *state = "not set";
2213
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002214 list_for_each_entry(w, &codec->card->widgets, list) {
2215 if (w->dapm != &codec->dapm)
2216 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002217
2218 /* only display widgets that burnm power */
2219 switch (w->id) {
2220 case snd_soc_dapm_hp:
2221 case snd_soc_dapm_mic:
2222 case snd_soc_dapm_spk:
2223 case snd_soc_dapm_line:
2224 case snd_soc_dapm_micbias:
2225 case snd_soc_dapm_dac:
2226 case snd_soc_dapm_adc:
2227 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002228 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002229 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002230 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002231 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002232 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002233 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002234 if (w->name)
2235 count += sprintf(buf + count, "%s: %s\n",
2236 w->name, w->power ? "On":"Off");
2237 break;
2238 default:
2239 break;
2240 }
2241 }
2242
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002243 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002244 case SND_SOC_BIAS_ON:
2245 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002246 break;
Mark Brown0be98982008-05-19 12:31:28 +02002247 case SND_SOC_BIAS_PREPARE:
2248 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002249 break;
Mark Brown0be98982008-05-19 12:31:28 +02002250 case SND_SOC_BIAS_STANDBY:
2251 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002252 break;
Mark Brown0be98982008-05-19 12:31:28 +02002253 case SND_SOC_BIAS_OFF:
2254 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002255 break;
2256 }
2257 count += sprintf(buf + count, "PM State: %s\n", state);
2258
2259 return count;
2260}
2261
2262static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2263
2264int snd_soc_dapm_sys_add(struct device *dev)
2265{
Troy Kisky12ef1932008-10-13 17:42:14 -07002266 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002267}
2268
2269static void snd_soc_dapm_sys_remove(struct device *dev)
2270{
Mark Brownaef90842009-05-16 17:53:16 +01002271 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002272}
2273
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002274static void dapm_free_path(struct snd_soc_dapm_path *path)
2275{
2276 list_del(&path->list_sink);
2277 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002278 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002279 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002280 kfree(path);
2281}
2282
Richard Purdie2b97eab2006-10-06 18:32:18 +02002283/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002284static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002285{
2286 struct snd_soc_dapm_widget *w, *next_w;
2287 struct snd_soc_dapm_path *p, *next_p;
2288
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002289 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2290 if (w->dapm != dapm)
2291 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002292 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002293 /*
2294 * remove source and sink paths associated to this widget.
2295 * While removing the path, remove reference to it from both
2296 * source and sink widgets so that path is removed only once.
2297 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002298 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2299 dapm_free_path(p);
2300
2301 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2302 dapm_free_path(p);
2303
Stephen Warrenfad59882011-04-28 17:37:59 -06002304 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002305 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002306 kfree(w);
2307 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002308}
2309
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002310static struct snd_soc_dapm_widget *dapm_find_widget(
2311 struct snd_soc_dapm_context *dapm, const char *pin,
2312 bool search_other_contexts)
2313{
2314 struct snd_soc_dapm_widget *w;
2315 struct snd_soc_dapm_widget *fallback = NULL;
2316
2317 list_for_each_entry(w, &dapm->card->widgets, list) {
2318 if (!strcmp(w->name, pin)) {
2319 if (w->dapm == dapm)
2320 return w;
2321 else
2322 fallback = w;
2323 }
2324 }
2325
2326 if (search_other_contexts)
2327 return fallback;
2328
2329 return NULL;
2330}
2331
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002332static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002333 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002334{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002335 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002336
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002337 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002338 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002339 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002340 }
2341
Mark Brown1a8b2d92012-02-16 11:50:07 -08002342 if (w->connected != status)
2343 dapm_mark_dirty(w, "pin configuration");
2344
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002345 w->connected = status;
2346 if (status == 0)
2347 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002348
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002349 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002350}
2351
Richard Purdie2b97eab2006-10-06 18:32:18 +02002352/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002353 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2354 * @dapm: DAPM context
2355 *
2356 * Walks all dapm audio paths and powers widgets according to their
2357 * stream or path usage.
2358 *
2359 * Requires external locking.
2360 *
2361 * Returns 0 for success.
2362 */
2363int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2364{
2365 /*
2366 * Suppress early reports (eg, jacks syncing their state) to avoid
2367 * silly DAPM runs during card startup.
2368 */
2369 if (!dapm->card || !dapm->card->instantiated)
2370 return 0;
2371
2372 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2373}
2374EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2375
2376/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002377 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002378 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002379 *
2380 * Walks all dapm audio paths and powers widgets according to their
2381 * stream or path usage.
2382 *
2383 * Returns 0 for success.
2384 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002385int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002386{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002387 int ret;
2388
Liam Girdwood3cd04342012-03-09 12:02:08 +00002389 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002390 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002391 mutex_unlock(&dapm->card->dapm_mutex);
2392 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002393}
Liam Girdwooda5302182008-07-07 13:35:17 +01002394EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002395
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002396static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2397 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2398 const char *control,
2399 int (*connected)(struct snd_soc_dapm_widget *source,
2400 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002401{
2402 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002403 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002404
2405 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2406 if (!path)
2407 return -ENOMEM;
2408
2409 path->source = wsource;
2410 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002411 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002412 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002413 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002414 INIT_LIST_HEAD(&path->list_source);
2415 INIT_LIST_HEAD(&path->list_sink);
2416
2417 /* check for external widgets */
2418 if (wsink->id == snd_soc_dapm_input) {
2419 if (wsource->id == snd_soc_dapm_micbias ||
2420 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002421 wsource->id == snd_soc_dapm_line ||
2422 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002423 wsink->ext = 1;
2424 }
2425 if (wsource->id == snd_soc_dapm_output) {
2426 if (wsink->id == snd_soc_dapm_spk ||
2427 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002428 wsink->id == snd_soc_dapm_line ||
2429 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002430 wsource->ext = 1;
2431 }
2432
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002433 dapm_mark_dirty(wsource, "Route added");
2434 dapm_mark_dirty(wsink, "Route added");
2435
Richard Purdie2b97eab2006-10-06 18:32:18 +02002436 /* connect static paths */
2437 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002438 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002439 list_add(&path->list_sink, &wsink->sources);
2440 list_add(&path->list_source, &wsource->sinks);
2441 path->connect = 1;
2442 return 0;
2443 }
2444
2445 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002446 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002447 case snd_soc_dapm_adc:
2448 case snd_soc_dapm_dac:
2449 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002450 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002451 case snd_soc_dapm_input:
2452 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002453 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002454 case snd_soc_dapm_micbias:
2455 case snd_soc_dapm_vmid:
2456 case snd_soc_dapm_pre:
2457 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002458 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002459 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002460 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002461 case snd_soc_dapm_aif_in:
2462 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002463 case snd_soc_dapm_dai_in:
2464 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002465 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002466 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002467 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002468 list_add(&path->list_sink, &wsink->sources);
2469 list_add(&path->list_source, &wsource->sinks);
2470 path->connect = 1;
2471 return 0;
2472 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002473 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002474 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002475 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002476 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002477 if (ret != 0)
2478 goto err;
2479 break;
2480 case snd_soc_dapm_switch:
2481 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002482 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002483 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002484 if (ret != 0)
2485 goto err;
2486 break;
2487 case snd_soc_dapm_hp:
2488 case snd_soc_dapm_mic:
2489 case snd_soc_dapm_line:
2490 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002491 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002492 list_add(&path->list_sink, &wsink->sources);
2493 list_add(&path->list_source, &wsource->sinks);
2494 path->connect = 0;
2495 return 0;
2496 }
Mark Brownfabd0382012-07-05 17:20:06 +01002497
Richard Purdie2b97eab2006-10-06 18:32:18 +02002498 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002499err:
2500 kfree(path);
2501 return ret;
2502}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002503
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002504static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002505 const struct snd_soc_dapm_route *route,
2506 unsigned int is_prefixed)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002507{
2508 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2509 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2510 const char *sink;
2511 const char *source;
2512 char prefixed_sink[80];
2513 char prefixed_source[80];
2514 int ret;
2515
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002516 if (dapm->codec && dapm->codec->name_prefix && !is_prefixed) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002517 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2518 dapm->codec->name_prefix, route->sink);
2519 sink = prefixed_sink;
2520 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2521 dapm->codec->name_prefix, route->source);
2522 source = prefixed_source;
2523 } else {
2524 sink = route->sink;
2525 source = route->source;
2526 }
2527
2528 /*
2529 * find src and dest widgets over all widgets but favor a widget from
2530 * current DAPM context
2531 */
2532 list_for_each_entry(w, &dapm->card->widgets, list) {
2533 if (!wsink && !(strcmp(w->name, sink))) {
2534 wtsink = w;
2535 if (w->dapm == dapm)
2536 wsink = w;
2537 continue;
2538 }
2539 if (!wsource && !(strcmp(w->name, source))) {
2540 wtsource = w;
2541 if (w->dapm == dapm)
2542 wsource = w;
2543 }
2544 }
2545 /* use widget from another DAPM context if not found from this */
2546 if (!wsink)
2547 wsink = wtsink;
2548 if (!wsource)
2549 wsource = wtsource;
2550
2551 if (wsource == NULL) {
2552 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2553 route->source);
2554 return -ENODEV;
2555 }
2556 if (wsink == NULL) {
2557 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2558 route->sink);
2559 return -ENODEV;
2560 }
2561
2562 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2563 route->connected);
2564 if (ret)
2565 goto err;
2566
2567 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002568err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002569 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002570 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002571 return ret;
2572}
Mark Brown105f1c22008-05-13 14:52:19 +02002573
Mark Brownefcc3c62012-07-05 17:24:19 +01002574static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2575 const struct snd_soc_dapm_route *route)
2576{
2577 struct snd_soc_dapm_path *path, *p;
2578 const char *sink;
2579 const char *source;
2580 char prefixed_sink[80];
2581 char prefixed_source[80];
2582
2583 if (route->control) {
2584 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002585 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002586 return -EINVAL;
2587 }
2588
2589 if (dapm->codec && dapm->codec->name_prefix) {
2590 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2591 dapm->codec->name_prefix, route->sink);
2592 sink = prefixed_sink;
2593 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2594 dapm->codec->name_prefix, route->source);
2595 source = prefixed_source;
2596 } else {
2597 sink = route->sink;
2598 source = route->source;
2599 }
2600
2601 path = NULL;
2602 list_for_each_entry(p, &dapm->card->paths, list) {
2603 if (strcmp(p->source->name, source) != 0)
2604 continue;
2605 if (strcmp(p->sink->name, sink) != 0)
2606 continue;
2607 path = p;
2608 break;
2609 }
2610
2611 if (path) {
2612 dapm_mark_dirty(path->source, "Route removed");
2613 dapm_mark_dirty(path->sink, "Route removed");
2614
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002615 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002616 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002617 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002618 source, sink);
2619 }
2620
2621 return 0;
2622}
2623
Mark Brown105f1c22008-05-13 14:52:19 +02002624/**
Mark Brown105f1c22008-05-13 14:52:19 +02002625 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002626 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002627 * @route: audio routes
2628 * @num: number of routes
2629 *
2630 * Connects 2 dapm widgets together via a named audio path. The sink is
2631 * the widget receiving the audio signal, whilst the source is the sender
2632 * of the audio signal.
2633 *
2634 * Returns 0 for success else error. On error all resources can be freed
2635 * with a call to snd_soc_card_free().
2636 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002637int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002638 const struct snd_soc_dapm_route *route, int num)
2639{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002640 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002641
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002642 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002643 for (i = 0; i < num; i++) {
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002644 r = snd_soc_dapm_add_route(dapm, route, false);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002645 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002646 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2647 route->source,
2648 route->control ? route->control : "direct",
2649 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002650 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002651 }
2652 route++;
2653 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002654 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002655
Dan Carpenter60884c22012-04-13 22:25:43 +03002656 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002657}
2658EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2659
Mark Brownefcc3c62012-07-05 17:24:19 +01002660/**
2661 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2662 * @dapm: DAPM context
2663 * @route: audio routes
2664 * @num: number of routes
2665 *
2666 * Removes routes from the DAPM context.
2667 */
2668int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2669 const struct snd_soc_dapm_route *route, int num)
2670{
2671 int i, ret = 0;
2672
2673 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2674 for (i = 0; i < num; i++) {
2675 snd_soc_dapm_del_route(dapm, route);
2676 route++;
2677 }
2678 mutex_unlock(&dapm->card->dapm_mutex);
2679
2680 return ret;
2681}
2682EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2683
Mark Brownbf3a9e12011-06-13 16:42:29 +01002684static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2685 const struct snd_soc_dapm_route *route)
2686{
2687 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2688 route->source,
2689 true);
2690 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2691 route->sink,
2692 true);
2693 struct snd_soc_dapm_path *path;
2694 int count = 0;
2695
2696 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002697 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002698 route->source);
2699 return -ENODEV;
2700 }
2701
2702 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002703 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002704 route->sink);
2705 return -ENODEV;
2706 }
2707
2708 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002709 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002710 route->source, route->sink);
2711
2712 list_for_each_entry(path, &source->sinks, list_source) {
2713 if (path->sink == sink) {
2714 path->weak = 1;
2715 count++;
2716 }
2717 }
2718
2719 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002720 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002721 route->source, route->sink);
2722 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002723 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002724 count, route->source, route->sink);
2725
2726 return 0;
2727}
2728
2729/**
2730 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2731 * @dapm: DAPM context
2732 * @route: audio routes
2733 * @num: number of routes
2734 *
2735 * Mark existing routes matching those specified in the passed array
2736 * as being weak, meaning that they are ignored for the purpose of
2737 * power decisions. The main intended use case is for sidetone paths
2738 * which couple audio between other independent paths if they are both
2739 * active in order to make the combination work better at the user
2740 * level but which aren't intended to be "used".
2741 *
2742 * Note that CODEC drivers should not use this as sidetone type paths
2743 * can frequently also be used as bypass paths.
2744 */
2745int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2746 const struct snd_soc_dapm_route *route, int num)
2747{
2748 int i, err;
2749 int ret = 0;
2750
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002751 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002752 for (i = 0; i < num; i++) {
2753 err = snd_soc_dapm_weak_route(dapm, route);
2754 if (err)
2755 ret = err;
2756 route++;
2757 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002758 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002759
2760 return ret;
2761}
2762EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2763
Mark Brown105f1c22008-05-13 14:52:19 +02002764/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002765 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002766 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002767 *
2768 * Checks the codec for any new dapm widgets and creates them if found.
2769 *
2770 * Returns 0 for success.
2771 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002772int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002773{
2774 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002775 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002776
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002777 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002778
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002779 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002780 {
2781 if (w->new)
2782 continue;
2783
Stephen Warrenfad59882011-04-28 17:37:59 -06002784 if (w->num_kcontrols) {
2785 w->kcontrols = kzalloc(w->num_kcontrols *
2786 sizeof(struct snd_kcontrol *),
2787 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002788 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002789 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002790 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002791 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002792 }
2793
Richard Purdie2b97eab2006-10-06 18:32:18 +02002794 switch(w->id) {
2795 case snd_soc_dapm_switch:
2796 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002797 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002798 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002799 break;
2800 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002801 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002802 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002803 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002804 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002805 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002806 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002807 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002808 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002809 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002810 break;
2811 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002812
2813 /* Read the initial power state from the device */
2814 if (w->reg >= 0) {
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002815 soc_widget_read(w, w->reg, &val);
2816 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002817 val &= w->mask;
2818 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002819 w->power = 1;
2820 }
2821
Richard Purdie2b97eab2006-10-06 18:32:18 +02002822 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002823
Mark Brown7508b122011-10-05 12:09:12 +01002824 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002825 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002826 }
2827
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002828 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2829 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002830 return 0;
2831}
2832EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2833
2834/**
2835 * snd_soc_dapm_get_volsw - dapm mixer get callback
2836 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002837 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002838 *
2839 * Callback to get the value of a dapm mixer control.
2840 *
2841 * Returns 0 for success.
2842 */
2843int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2844 struct snd_ctl_elem_value *ucontrol)
2845{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002846 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002847 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002848 struct soc_mixer_control *mc =
2849 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002850 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002851 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002852 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002853 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002854 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002855 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002856
2857 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002858 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002859 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002860 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002861
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002862 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002863 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002864 val = (snd_soc_read(codec, reg) >> shift) & mask;
2865 else
2866 val = dapm_kcontrol_get_value(kcontrol);
2867 mutex_unlock(&card->dapm_mutex);
2868
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002869 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002870 ucontrol->value.integer.value[0] = max - val;
2871 else
2872 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002873
2874 return 0;
2875}
2876EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2877
2878/**
2879 * snd_soc_dapm_put_volsw - dapm mixer set callback
2880 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002881 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002882 *
2883 * Callback to set the value of a dapm mixer control.
2884 *
2885 * Returns 0 for success.
2886 */
2887int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2888 struct snd_ctl_elem_value *ucontrol)
2889{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002890 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002891 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002892 struct soc_mixer_control *mc =
2893 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002894 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002895 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002896 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002897 unsigned int mask = (1 << fls(max)) - 1;
2898 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002899 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002900 int connect, change;
2901 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002902 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002903
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002904 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002905 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002906 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002907 kcontrol->id.name);
2908
Richard Purdie2b97eab2006-10-06 18:32:18 +02002909 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002910 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002911
2912 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002913 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002914
Liam Girdwood3cd04342012-03-09 12:02:08 +00002915 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002916
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002917 change = dapm_kcontrol_set_value(kcontrol, val);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002918
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002919 if (reg != SND_SOC_NOPM) {
2920 mask = mask << shift;
2921 val = val << shift;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002922
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002923 change = snd_soc_test_bits(codec, reg, mask, val);
2924 }
2925
Mark Brown97404f22010-12-14 16:13:57 +00002926 if (change) {
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002927 if (reg != SND_SOC_NOPM) {
2928 update.kcontrol = kcontrol;
2929 update.reg = reg;
2930 update.mask = mask;
2931 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002932
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002933 card->update = &update;
2934 }
Mark Brown97404f22010-12-14 16:13:57 +00002935
Nenghua Cao52765972013-12-13 20:13:49 +08002936 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002937
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002938 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002939 }
2940
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002941 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002942
2943 if (ret > 0)
2944 soc_dpcm_runtime_update(card);
2945
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002946 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002947}
2948EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2949
2950/**
2951 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2952 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002953 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002954 *
2955 * Callback to get the value of a dapm enumerated double mixer control.
2956 *
2957 * Returns 0 for success.
2958 */
2959int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2960 struct snd_ctl_elem_value *ucontrol)
2961{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002962 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002963 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002964 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002965
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002966 val = snd_soc_read(codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002967 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002968 if (e->shift_l != e->shift_r)
2969 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002970 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002971
2972 return 0;
2973}
2974EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2975
2976/**
2977 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2978 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002979 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002980 *
2981 * Callback to set the value of a dapm enumerated double mixer control.
2982 *
2983 * Returns 0 for success.
2984 */
2985int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2986 struct snd_ctl_elem_value *ucontrol)
2987{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002988 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002989 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002990 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002991 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002992 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002993 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002994 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002995
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002996 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002997 return -EINVAL;
2998 mux = ucontrol->value.enumerated.item[0];
2999 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003000 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003001 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01003002 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003003 return -EINVAL;
3004 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003005 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003006 }
3007
Liam Girdwood3cd04342012-03-09 12:02:08 +00003008 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003009
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003010 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003011 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02003012 update.kcontrol = kcontrol;
3013 update.reg = e->reg;
3014 update.mask = mask;
3015 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003016 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00003017
Nenghua Cao52765972013-12-13 20:13:49 +08003018 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003019
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003020 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003021 }
3022
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003023 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003024
3025 if (ret > 0)
3026 soc_dpcm_runtime_update(card);
3027
Mark Brown97404f22010-12-14 16:13:57 +00003028 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003029}
3030EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3031
3032/**
Mark Brownd2b247a2009-10-06 15:21:04 +01003033 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
3034 * @kcontrol: mixer control
3035 * @ucontrol: control element information
3036 *
3037 * Returns 0 for success.
3038 */
3039int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
3040 struct snd_ctl_elem_value *ucontrol)
3041{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003042 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
Mark Brownd2b247a2009-10-06 15:21:04 +01003043 return 0;
3044}
3045EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
3046
3047/**
3048 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
3049 * @kcontrol: mixer control
3050 * @ucontrol: control element information
3051 *
3052 * Returns 0 for success.
3053 */
3054int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
3055 struct snd_ctl_elem_value *ucontrol)
3056{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003057 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003058 struct snd_soc_card *card = codec->card;
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003059 unsigned int value;
Mark Brownd2b247a2009-10-06 15:21:04 +01003060 struct soc_enum *e =
3061 (struct soc_enum *)kcontrol->private_value;
3062 int change;
Nenghua Cao52765972013-12-13 20:13:49 +08003063 int ret = 0;
Mark Brownd2b247a2009-10-06 15:21:04 +01003064
3065 if (ucontrol->value.enumerated.item[0] >= e->max)
3066 return -EINVAL;
3067
Liam Girdwood3cd04342012-03-09 12:02:08 +00003068 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01003069
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003070 value = ucontrol->value.enumerated.item[0];
3071 change = dapm_kcontrol_set_value(kcontrol, value);
3072 if (change)
Nenghua Cao52765972013-12-13 20:13:49 +08003073 ret = soc_dapm_mux_update_power(card, kcontrol, value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003074
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003075 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003076
3077 if (ret > 0)
3078 soc_dpcm_runtime_update(card);
3079
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003080 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01003081}
3082EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3083
3084/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003085 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3086 * callback
3087 * @kcontrol: mixer control
3088 * @ucontrol: control element information
3089 *
3090 * Callback to get the value of a dapm semi enumerated double mixer control.
3091 *
3092 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3093 * used for handling bitfield coded enumeration for example.
3094 *
3095 * Returns 0 for success.
3096 */
3097int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
3098 struct snd_ctl_elem_value *ucontrol)
3099{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003100 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02003101 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003102 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003103
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003104 reg_val = snd_soc_read(codec, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003105 val = (reg_val >> e->shift_l) & e->mask;
3106 for (mux = 0; mux < e->max; mux++) {
3107 if (val == e->values[mux])
3108 break;
3109 }
3110 ucontrol->value.enumerated.item[0] = mux;
3111 if (e->shift_l != e->shift_r) {
3112 val = (reg_val >> e->shift_r) & e->mask;
3113 for (mux = 0; mux < e->max; mux++) {
3114 if (val == e->values[mux])
3115 break;
3116 }
3117 ucontrol->value.enumerated.item[1] = mux;
3118 }
3119
3120 return 0;
3121}
3122EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
3123
3124/**
3125 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3126 * callback
3127 * @kcontrol: mixer control
3128 * @ucontrol: control element information
3129 *
3130 * Callback to set the value of a dapm semi enumerated double mixer control.
3131 *
3132 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3133 * used for handling bitfield coded enumeration for example.
3134 *
3135 * Returns 0 for success.
3136 */
3137int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
3138 struct snd_ctl_elem_value *ucontrol)
3139{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003140 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003141 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02003142 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01003143 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003144 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00003145 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003146 int ret = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003147
3148 if (ucontrol->value.enumerated.item[0] > e->max - 1)
3149 return -EINVAL;
3150 mux = ucontrol->value.enumerated.item[0];
3151 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
3152 mask = e->mask << e->shift_l;
3153 if (e->shift_l != e->shift_r) {
3154 if (ucontrol->value.enumerated.item[1] > e->max - 1)
3155 return -EINVAL;
3156 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
3157 mask |= e->mask << e->shift_r;
3158 }
3159
Liam Girdwood3cd04342012-03-09 12:02:08 +00003160 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003161
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003162 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003163 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02003164 update.kcontrol = kcontrol;
3165 update.reg = e->reg;
3166 update.mask = mask;
3167 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003168 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00003169
Nenghua Cao52765972013-12-13 20:13:49 +08003170 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003171
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003172 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003173 }
3174
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003175 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003176
3177 if (ret > 0)
3178 soc_dpcm_runtime_update(card);
3179
Mark Brown97404f22010-12-14 16:13:57 +00003180 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003181}
3182EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3183
3184/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003185 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3186 *
3187 * @kcontrol: mixer control
3188 * @uinfo: control element information
3189 *
3190 * Callback to provide information about a pin switch control.
3191 */
3192int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3193 struct snd_ctl_elem_info *uinfo)
3194{
3195 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3196 uinfo->count = 1;
3197 uinfo->value.integer.min = 0;
3198 uinfo->value.integer.max = 1;
3199
3200 return 0;
3201}
3202EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3203
3204/**
3205 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3206 *
3207 * @kcontrol: mixer control
3208 * @ucontrol: Value
3209 */
3210int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3211 struct snd_ctl_elem_value *ucontrol)
3212{
Mark Brown48a8c392012-02-14 17:11:15 -08003213 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003214 const char *pin = (const char *)kcontrol->private_value;
3215
Liam Girdwood3cd04342012-03-09 12:02:08 +00003216 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003217
3218 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003219 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003220
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003221 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003222
3223 return 0;
3224}
3225EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3226
3227/**
3228 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3229 *
3230 * @kcontrol: mixer control
3231 * @ucontrol: Value
3232 */
3233int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3234 struct snd_ctl_elem_value *ucontrol)
3235{
Mark Brown48a8c392012-02-14 17:11:15 -08003236 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003237 const char *pin = (const char *)kcontrol->private_value;
3238
Mark Brown8b37dbd2009-02-28 21:14:20 +00003239 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003240 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003241 else
Mark Brown48a8c392012-02-14 17:11:15 -08003242 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003243
Mark Brown48a8c392012-02-14 17:11:15 -08003244 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003245 return 0;
3246}
3247EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3248
Mark Brown5ba06fc2012-02-16 11:07:13 -08003249static struct snd_soc_dapm_widget *
3250snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3251 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003252{
3253 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003254 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003255
3256 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003257 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003258
Mark Brown62ea8742012-01-21 21:14:48 +00003259 switch (w->id) {
3260 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003261 w->regulator = devm_regulator_get(dapm->dev, w->name);
3262 if (IS_ERR(w->regulator)) {
3263 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003264 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003265 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003266 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003267 }
Mark Brown8784c772013-01-10 19:33:47 +00003268
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003269 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003270 ret = regulator_allow_bypass(w->regulator, true);
3271 if (ret != 0)
3272 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003273 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003274 w->name, ret);
3275 }
Mark Brown62ea8742012-01-21 21:14:48 +00003276 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003277 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003278#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003279 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003280 if (IS_ERR(w->clk)) {
3281 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003282 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003283 w->name, ret);
3284 return NULL;
3285 }
Mark Brownec029952012-06-04 08:16:20 +01003286#else
3287 return NULL;
3288#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003289 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003290 default:
3291 break;
3292 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003293
Mark Brown88e8b9a2011-03-02 18:18:24 +00003294 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003295 w->name = kasprintf(GFP_KERNEL, "%s %s",
3296 dapm->codec->name_prefix, widget->name);
3297 else
3298 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3299
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003300 if (w->name == NULL) {
3301 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003302 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003303 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003304
Mark Brown7ca3a182011-10-08 14:04:50 +01003305 switch (w->id) {
3306 case snd_soc_dapm_switch:
3307 case snd_soc_dapm_mixer:
3308 case snd_soc_dapm_mixer_named_ctl:
3309 w->power_check = dapm_generic_check_power;
3310 break;
3311 case snd_soc_dapm_mux:
3312 case snd_soc_dapm_virt_mux:
3313 case snd_soc_dapm_value_mux:
3314 w->power_check = dapm_generic_check_power;
3315 break;
Mark Brown46162742013-06-05 19:36:11 +01003316 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003317 w->power_check = dapm_adc_check_power;
3318 break;
Mark Brown46162742013-06-05 19:36:11 +01003319 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003320 w->power_check = dapm_dac_check_power;
3321 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003322 case snd_soc_dapm_adc:
3323 case snd_soc_dapm_aif_out:
3324 case snd_soc_dapm_dac:
3325 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003326 case snd_soc_dapm_pga:
3327 case snd_soc_dapm_out_drv:
3328 case snd_soc_dapm_input:
3329 case snd_soc_dapm_output:
3330 case snd_soc_dapm_micbias:
3331 case snd_soc_dapm_spk:
3332 case snd_soc_dapm_hp:
3333 case snd_soc_dapm_mic:
3334 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003335 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003336 w->power_check = dapm_generic_check_power;
3337 break;
3338 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003339 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003340 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003341 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003342 w->power_check = dapm_supply_check_power;
3343 break;
3344 default:
3345 w->power_check = dapm_always_on_check_power;
3346 break;
3347 }
3348
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003349 w->dapm = dapm;
3350 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003351 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003352 INIT_LIST_HEAD(&w->sources);
3353 INIT_LIST_HEAD(&w->sinks);
3354 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003355 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003356 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003357
3358 /* machine layer set ups unconnected pins and insertions */
3359 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003360 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003361}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003362
3363/**
Mark Brown4ba13272008-05-13 14:51:19 +02003364 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003365 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003366 * @widget: widget array
3367 * @num: number of widgets
3368 *
3369 * Creates new DAPM controls based upon the templates.
3370 *
3371 * Returns 0 for success else error.
3372 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003373int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003374 const struct snd_soc_dapm_widget *widget,
3375 int num)
3376{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003377 struct snd_soc_dapm_widget *w;
3378 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003379 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003380
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003381 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003382 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003383 w = snd_soc_dapm_new_control(dapm, widget);
3384 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003385 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003386 "ASoC: Failed to create DAPM control %s\n",
3387 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003388 ret = -ENOMEM;
3389 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003390 }
Mark Brown4ba13272008-05-13 14:51:19 +02003391 widget++;
3392 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003393 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003394 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003395}
3396EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3397
Mark Brownc74184e2012-04-04 22:12:09 +01003398static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3399 struct snd_kcontrol *kcontrol, int event)
3400{
3401 struct snd_soc_dapm_path *source_p, *sink_p;
3402 struct snd_soc_dai *source, *sink;
3403 const struct snd_soc_pcm_stream *config = w->params;
3404 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003405 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003406 u64 fmt;
3407 int ret;
3408
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003409 if (WARN_ON(!config) ||
3410 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3411 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003412
3413 /* We only support a single source and sink, pick the first */
3414 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3415 list_sink);
3416 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3417 list_source);
3418
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003419 if (WARN_ON(!source_p || !sink_p) ||
3420 WARN_ON(!sink_p->source || !source_p->sink) ||
3421 WARN_ON(!source_p->source || !sink_p->sink))
3422 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003423
3424 source = source_p->source->priv;
3425 sink = sink_p->sink->priv;
3426
3427 /* Be a little careful as we don't want to overflow the mask array */
3428 if (config->formats) {
3429 fmt = ffs(config->formats) - 1;
3430 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003431 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003432 config->formats);
3433 fmt = 0;
3434 }
3435
3436 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003437 params = kzalloc(sizeof(*params), GFP_KERNEL);
3438 if (!params) {
3439 ret = -ENOMEM;
3440 goto out;
3441 }
3442 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003443
Mark Brown9747cec2012-04-26 19:12:21 +01003444 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003445 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003446 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003447 config->rate_max;
3448
Mark Brown9747cec2012-04-26 19:12:21 +01003449 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003450 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003451 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003452 = config->channels_max;
3453
3454 memset(&substream, 0, sizeof(substream));
3455
3456 switch (event) {
3457 case SND_SOC_DAPM_PRE_PMU:
3458 if (source->driver->ops && source->driver->ops->hw_params) {
3459 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3460 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003461 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003462 if (ret != 0) {
3463 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003464 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003465 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003466 }
3467 }
3468
3469 if (sink->driver->ops && sink->driver->ops->hw_params) {
3470 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003471 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003472 sink);
3473 if (ret != 0) {
3474 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003475 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003476 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003477 }
3478 }
3479 break;
3480
3481 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003482 ret = snd_soc_dai_digital_mute(sink, 0,
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 unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003486 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003487 break;
3488
3489 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003490 ret = snd_soc_dai_digital_mute(sink, 1,
3491 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003492 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003493 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003494 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003495 break;
3496
3497 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003498 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003499 return -EINVAL;
3500 }
3501
Mark Brown9747cec2012-04-26 19:12:21 +01003502out:
3503 kfree(params);
3504 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003505}
3506
3507int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3508 const struct snd_soc_pcm_stream *params,
3509 struct snd_soc_dapm_widget *source,
3510 struct snd_soc_dapm_widget *sink)
3511{
3512 struct snd_soc_dapm_route routes[2];
3513 struct snd_soc_dapm_widget template;
3514 struct snd_soc_dapm_widget *w;
3515 size_t len;
3516 char *link_name;
3517
3518 len = strlen(source->name) + strlen(sink->name) + 2;
3519 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3520 if (!link_name)
3521 return -ENOMEM;
3522 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3523
3524 memset(&template, 0, sizeof(template));
3525 template.reg = SND_SOC_NOPM;
3526 template.id = snd_soc_dapm_dai_link;
3527 template.name = link_name;
3528 template.event = snd_soc_dai_link_event;
3529 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3530 SND_SOC_DAPM_PRE_PMD;
3531
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003532 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003533
3534 w = snd_soc_dapm_new_control(&card->dapm, &template);
3535 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003536 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003537 link_name);
3538 return -ENOMEM;
3539 }
3540
3541 w->params = params;
3542
3543 memset(&routes, 0, sizeof(routes));
3544
3545 routes[0].source = source->name;
3546 routes[0].sink = link_name;
3547 routes[1].source = link_name;
3548 routes[1].sink = sink->name;
3549
3550 return snd_soc_dapm_add_routes(&card->dapm, routes,
3551 ARRAY_SIZE(routes));
3552}
3553
Mark Brown888df392012-02-16 19:37:51 -08003554int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3555 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003556{
Mark Brown888df392012-02-16 19:37:51 -08003557 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003558 struct snd_soc_dapm_widget *w;
3559
Mark Brown888df392012-02-16 19:37:51 -08003560 WARN_ON(dapm->dev != dai->dev);
3561
3562 memset(&template, 0, sizeof(template));
3563 template.reg = SND_SOC_NOPM;
3564
3565 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003566 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003567 template.name = dai->driver->playback.stream_name;
3568 template.sname = dai->driver->playback.stream_name;
3569
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003570 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003571 template.name);
3572
3573 w = snd_soc_dapm_new_control(dapm, &template);
3574 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003575 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003576 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003577 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003578 }
3579
3580 w->priv = dai;
3581 dai->playback_widget = w;
3582 }
3583
3584 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003585 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003586 template.name = dai->driver->capture.stream_name;
3587 template.sname = dai->driver->capture.stream_name;
3588
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003589 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003590 template.name);
3591
3592 w = snd_soc_dapm_new_control(dapm, &template);
3593 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003594 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003595 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003596 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003597 }
3598
3599 w->priv = dai;
3600 dai->capture_widget = w;
3601 }
3602
3603 return 0;
3604}
3605
3606int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3607{
3608 struct snd_soc_dapm_widget *dai_w, *w;
3609 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003610
3611 /* For each DAI widget... */
3612 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003613 switch (dai_w->id) {
3614 case snd_soc_dapm_dai_in:
3615 case snd_soc_dapm_dai_out:
3616 break;
3617 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003618 continue;
Mark Brown46162742013-06-05 19:36:11 +01003619 }
Mark Brown888df392012-02-16 19:37:51 -08003620
3621 dai = dai_w->priv;
3622
3623 /* ...find all widgets with the same stream and link them */
3624 list_for_each_entry(w, &card->widgets, list) {
3625 if (w->dapm != dai_w->dapm)
3626 continue;
3627
Mark Brown46162742013-06-05 19:36:11 +01003628 switch (w->id) {
3629 case snd_soc_dapm_dai_in:
3630 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003631 continue;
Mark Brown46162742013-06-05 19:36:11 +01003632 default:
3633 break;
3634 }
Mark Brown888df392012-02-16 19:37:51 -08003635
Russell King19c2c5f2013-08-04 20:24:03 +01003636 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003637 continue;
3638
3639 if (dai->driver->playback.stream_name &&
3640 strstr(w->sname,
3641 dai->driver->playback.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003642 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003643 dai->playback_widget->name, w->name);
Mark Brown888df392012-02-16 19:37:51 -08003644
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003645 snd_soc_dapm_add_path(w->dapm,
3646 dai->playback_widget, w, NULL, NULL);
Mark Brown888df392012-02-16 19:37:51 -08003647 }
3648
3649 if (dai->driver->capture.stream_name &&
3650 strstr(w->sname,
3651 dai->driver->capture.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003652 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003653 w->name, dai->capture_widget->name);
Mark Brown888df392012-02-16 19:37:51 -08003654
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003655 snd_soc_dapm_add_path(w->dapm, w,
3656 dai->capture_widget, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003657 }
3658 }
3659 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003660
Mark Brown888df392012-02-16 19:37:51 -08003661 return 0;
3662}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003663
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003664void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3665{
3666 struct snd_soc_pcm_runtime *rtd = card->rtd;
3667 struct snd_soc_dai *cpu_dai, *codec_dai;
3668 struct snd_soc_dapm_route r;
3669 int i;
3670
3671 memset(&r, 0, sizeof(r));
3672
3673 /* for each BE DAI link... */
3674 for (i = 0; i < card->num_rtd; i++) {
3675 rtd = &card->rtd[i];
3676 cpu_dai = rtd->cpu_dai;
3677 codec_dai = rtd->codec_dai;
3678
3679 /* dynamic FE links have no fixed DAI mapping */
3680 if (rtd->dai_link->dynamic)
3681 continue;
3682
3683 /* there is no point in connecting BE DAI links with dummies */
3684 if (snd_soc_dai_is_dummy(codec_dai) ||
3685 snd_soc_dai_is_dummy(cpu_dai))
3686 continue;
3687
3688 /* connect BE DAI playback if widgets are valid */
3689 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3690 r.source = cpu_dai->playback_widget->name;
3691 r.sink = codec_dai->playback_widget->name;
3692 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3693 cpu_dai->codec->name, r.source,
3694 codec_dai->platform->name, r.sink);
3695
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003696 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003697 }
3698
3699 /* connect BE DAI capture if widgets are valid */
3700 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3701 r.source = codec_dai->capture_widget->name;
3702 r.sink = cpu_dai->capture_widget->name;
3703 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3704 codec_dai->codec->name, r.source,
3705 cpu_dai->platform->name, r.sink);
3706
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003707 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003708 }
3709
3710 }
3711}
3712
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003713static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3714 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003715{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003716
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003717 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3718 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3719 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003720
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003721 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3722 w_cpu = cpu_dai->playback_widget;
3723 w_codec = codec_dai->playback_widget;
3724 } else {
3725 w_cpu = cpu_dai->capture_widget;
3726 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003727 }
3728
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003729 if (w_cpu) {
3730
3731 dapm_mark_dirty(w_cpu, "stream event");
3732
3733 switch (event) {
3734 case SND_SOC_DAPM_STREAM_START:
3735 w_cpu->active = 1;
3736 break;
3737 case SND_SOC_DAPM_STREAM_STOP:
3738 w_cpu->active = 0;
3739 break;
3740 case SND_SOC_DAPM_STREAM_SUSPEND:
3741 case SND_SOC_DAPM_STREAM_RESUME:
3742 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3743 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3744 break;
3745 }
3746 }
3747
3748 if (w_codec) {
3749
3750 dapm_mark_dirty(w_codec, "stream event");
3751
3752 switch (event) {
3753 case SND_SOC_DAPM_STREAM_START:
3754 w_codec->active = 1;
3755 break;
3756 case SND_SOC_DAPM_STREAM_STOP:
3757 w_codec->active = 0;
3758 break;
3759 case SND_SOC_DAPM_STREAM_SUSPEND:
3760 case SND_SOC_DAPM_STREAM_RESUME:
3761 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3762 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3763 break;
3764 }
3765 }
3766
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003767 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003768}
3769
3770/**
3771 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3772 * @rtd: PCM runtime data
3773 * @stream: stream name
3774 * @event: stream event
3775 *
3776 * Sends a stream event to the dapm core. The core then makes any
3777 * necessary widget power changes.
3778 *
3779 * Returns 0 for success else error.
3780 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003781void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3782 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003783{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003784 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003785
Liam Girdwood3cd04342012-03-09 12:02:08 +00003786 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003787 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003788 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003789}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003790
3791/**
Charles Keepax11391102014-02-18 15:22:14 +00003792 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3793 * @dapm: DAPM context
3794 * @pin: pin name
3795 *
3796 * Enables input/output pin and its parents or children widgets iff there is
3797 * a valid audio route and active audio stream.
3798 *
3799 * Requires external locking.
3800 *
3801 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3802 * do any widget power switching.
3803 */
3804int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3805 const char *pin)
3806{
3807 return snd_soc_dapm_set_pin(dapm, pin, 1);
3808}
3809EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3810
3811/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003812 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003813 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003814 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003815 *
Mark Brown74b8f952009-06-06 11:26:15 +01003816 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003817 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003818 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003819 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3820 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003821 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003822int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003823{
Charles Keepax11391102014-02-18 15:22:14 +00003824 int ret;
3825
3826 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3827
3828 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3829
3830 mutex_unlock(&dapm->card->dapm_mutex);
3831
3832 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003833}
Liam Girdwooda5302182008-07-07 13:35:17 +01003834EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003835
3836/**
Charles Keepax11391102014-02-18 15:22:14 +00003837 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3838 * @dapm: DAPM context
3839 * @pin: pin name
3840 *
3841 * Enables input/output pin regardless of any other state. This is
3842 * intended for use with microphone bias supplies used in microphone
3843 * jack detection.
3844 *
3845 * Requires external locking.
3846 *
3847 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3848 * do any widget power switching.
3849 */
3850int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3851 const char *pin)
3852{
3853 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3854
3855 if (!w) {
3856 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3857 return -EINVAL;
3858 }
3859
3860 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3861 w->connected = 1;
3862 w->force = 1;
3863 dapm_mark_dirty(w, "force enable");
3864
3865 return 0;
3866}
3867EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3868
3869/**
Mark Brownda341832010-03-15 19:23:37 +00003870 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003871 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003872 * @pin: pin name
3873 *
3874 * Enables input/output pin regardless of any other state. This is
3875 * intended for use with microphone bias supplies used in microphone
3876 * jack detection.
3877 *
3878 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3879 * do any widget power switching.
3880 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003881int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3882 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003883{
Charles Keepax11391102014-02-18 15:22:14 +00003884 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003885
Charles Keepax11391102014-02-18 15:22:14 +00003886 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003887
Charles Keepax11391102014-02-18 15:22:14 +00003888 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003889
Charles Keepax11391102014-02-18 15:22:14 +00003890 mutex_unlock(&dapm->card->dapm_mutex);
3891
3892 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003893}
3894EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3895
3896/**
Charles Keepax11391102014-02-18 15:22:14 +00003897 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3898 * @dapm: DAPM context
3899 * @pin: pin name
3900 *
3901 * Disables input/output pin and its parents or children widgets.
3902 *
3903 * Requires external locking.
3904 *
3905 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3906 * do any widget power switching.
3907 */
3908int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3909 const char *pin)
3910{
3911 return snd_soc_dapm_set_pin(dapm, pin, 0);
3912}
3913EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3914
3915/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003916 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003917 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003918 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003919 *
Mark Brown74b8f952009-06-06 11:26:15 +01003920 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003921 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003922 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3923 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003924 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003925int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3926 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003927{
Charles Keepax11391102014-02-18 15:22:14 +00003928 int ret;
3929
3930 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3931
3932 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3933
3934 mutex_unlock(&dapm->card->dapm_mutex);
3935
3936 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003937}
3938EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3939
3940/**
Charles Keepax11391102014-02-18 15:22:14 +00003941 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3942 * @dapm: DAPM context
3943 * @pin: pin name
3944 *
3945 * Marks the specified pin as being not connected, disabling it along
3946 * any parent or child widgets. At present this is identical to
3947 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3948 * additional things such as disabling controls which only affect
3949 * paths through the pin.
3950 *
3951 * Requires external locking.
3952 *
3953 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3954 * do any widget power switching.
3955 */
3956int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3957 const char *pin)
3958{
3959 return snd_soc_dapm_set_pin(dapm, pin, 0);
3960}
3961EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3962
3963/**
Mark Brown5817b522008-09-24 11:23:11 +01003964 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003965 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003966 * @pin: pin name
3967 *
3968 * Marks the specified pin as being not connected, disabling it along
3969 * any parent or child widgets. At present this is identical to
3970 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3971 * additional things such as disabling controls which only affect
3972 * paths through the pin.
3973 *
3974 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3975 * do any widget power switching.
3976 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003977int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003978{
Charles Keepax11391102014-02-18 15:22:14 +00003979 int ret;
3980
3981 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3982
3983 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3984
3985 mutex_unlock(&dapm->card->dapm_mutex);
3986
3987 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003988}
3989EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3990
3991/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003992 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003993 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003994 * @pin: audio signal pin endpoint (or start point)
3995 *
3996 * Get audio pin status - connected or disconnected.
3997 *
3998 * Returns 1 for connected otherwise 0.
3999 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004000int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4001 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004002{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004003 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004004
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004005 if (w)
4006 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004007
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004008 return 0;
4009}
Liam Girdwooda5302182008-07-07 13:35:17 +01004010EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004011
4012/**
Mark Brown1547aba2010-05-07 21:11:40 +01004013 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004014 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004015 * @pin: audio signal pin endpoint (or start point)
4016 *
4017 * Mark the given endpoint or pin as ignoring suspend. When the
4018 * system is disabled a path between two endpoints flagged as ignoring
4019 * suspend will not be disabled. The path must already be enabled via
4020 * normal means at suspend time, it will not be turned on if it was not
4021 * already enabled.
4022 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004023int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4024 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004025{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004026 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004027
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004028 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004029 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004030 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004031 }
4032
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004033 w->ignore_suspend = 1;
4034
4035 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004036}
4037EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4038
Stephen Warren16332812011-11-23 12:42:04 -07004039static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
4040 struct snd_soc_dapm_widget *w)
4041{
4042 struct snd_soc_dapm_path *p;
4043
4044 list_for_each_entry(p, &card->paths, list) {
4045 if ((p->source == w) || (p->sink == w)) {
4046 dev_dbg(card->dev,
4047 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
4048 p->source->name, p->source->id, p->source->dapm,
4049 p->sink->name, p->sink->id, p->sink->dapm);
4050
4051 /* Connected to something other than the codec */
4052 if (p->source->dapm != p->sink->dapm)
4053 return true;
4054 /*
4055 * Loopback connection from codec external pin to
4056 * codec external pin
4057 */
4058 if (p->sink->id == snd_soc_dapm_input) {
4059 switch (p->source->id) {
4060 case snd_soc_dapm_output:
4061 case snd_soc_dapm_micbias:
4062 return true;
4063 default:
4064 break;
4065 }
4066 }
4067 }
4068 }
4069
4070 return false;
4071}
4072
4073/**
4074 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
4075 * @codec: The codec whose pins should be processed
4076 *
4077 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
4078 * which are unused. Pins are used if they are connected externally to the
4079 * codec, whether that be to some other device, or a loop-back connection to
4080 * the codec itself.
4081 */
4082void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
4083{
4084 struct snd_soc_card *card = codec->card;
4085 struct snd_soc_dapm_context *dapm = &codec->dapm;
4086 struct snd_soc_dapm_widget *w;
4087
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004088 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07004089 &card->dapm, &codec->dapm);
4090
4091 list_for_each_entry(w, &card->widgets, list) {
4092 if (w->dapm != dapm)
4093 continue;
4094 switch (w->id) {
4095 case snd_soc_dapm_input:
4096 case snd_soc_dapm_output:
4097 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004098 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07004099 w->name);
4100 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00004101 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07004102 "... Not in map; disabling\n");
4103 snd_soc_dapm_nc_pin(dapm, w->name);
4104 }
4105 break;
4106 default:
4107 break;
4108 }
4109 }
4110}
4111
Mark Brown1547aba2010-05-07 21:11:40 +01004112/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004113 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004114 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004115 *
4116 * Free all dapm widgets and resources.
4117 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004118void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004119{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004120 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004121 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004122 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004123 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004124}
4125EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4126
Xiang Xiao57996352014-03-02 00:04:02 +08004127static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004128{
Liam Girdwood01005a72012-07-06 16:57:05 +01004129 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004130 struct snd_soc_dapm_widget *w;
4131 LIST_HEAD(down_list);
4132 int powerdown = 0;
4133
Liam Girdwood01005a72012-07-06 16:57:05 +01004134 mutex_lock(&card->dapm_mutex);
4135
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004136 list_for_each_entry(w, &dapm->card->widgets, list) {
4137 if (w->dapm != dapm)
4138 continue;
Mark Brown51737472009-06-22 13:16:51 +01004139 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004140 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01004141 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004142 powerdown = 1;
4143 }
4144 }
4145
4146 /* If there were no widgets to power down we're already in
4147 * standby.
4148 */
4149 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004150 if (dapm->bias_level == SND_SOC_BIAS_ON)
4151 snd_soc_dapm_set_bias_level(dapm,
4152 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004153 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004154 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4155 snd_soc_dapm_set_bias_level(dapm,
4156 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004157 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004158
4159 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004160}
Mark Brown51737472009-06-22 13:16:51 +01004161
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004162/*
4163 * snd_soc_dapm_shutdown - callback for system shutdown
4164 */
4165void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4166{
Xiang Xiao57996352014-03-02 00:04:02 +08004167 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004168
Xiang Xiao57996352014-03-02 00:04:02 +08004169 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004170 if (dapm != &card->dapm) {
4171 soc_dapm_shutdown_dapm(dapm);
4172 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4173 snd_soc_dapm_set_bias_level(dapm,
4174 SND_SOC_BIAS_OFF);
4175 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004176 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004177
4178 soc_dapm_shutdown_dapm(&card->dapm);
4179 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4180 snd_soc_dapm_set_bias_level(&card->dapm,
4181 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004182}
4183
Richard Purdie2b97eab2006-10-06 18:32:18 +02004184/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004185MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004186MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4187MODULE_LICENSE("GPL");