blob: 2026a64a0afba9b9fc5e252ac39155facc62651c [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
389static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
390{
391 if (w->codec)
392 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100393 else if (w->platform)
394 return snd_soc_platform_write(w->platform, reg, val);
395
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000396 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100397 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100398}
399
Liam Girdwood49575fb52012-03-06 18:16:19 +0000400static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
401{
Mark Browne06ab3b2012-03-06 23:58:22 +0000402 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000403 mutex_lock(&w->codec->mutex);
404 else if (w->platform)
405 mutex_lock(&w->platform->mutex);
406}
407
408static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
409{
Mark Browne06ab3b2012-03-06 23:58:22 +0000410 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000411 mutex_unlock(&w->codec->mutex);
412 else if (w->platform)
413 mutex_unlock(&w->platform->mutex);
414}
415
Mark Browneb270e92013-10-09 13:52:52 +0100416static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
417{
418 if (dapm->codec && dapm->codec->using_regmap)
419 regmap_async_complete(dapm->codec->control_data);
420}
421
Liam Girdwood49575fb52012-03-06 18:16:19 +0000422static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100423 unsigned short reg, unsigned int mask, unsigned int value)
424{
Mark Brown8a713da2011-12-03 12:33:55 +0000425 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100426 unsigned int old, new;
427 int ret;
428
Mark Brown8a713da2011-12-03 12:33:55 +0000429 if (w->codec && w->codec->using_regmap) {
Mark Browneb270e92013-10-09 13:52:52 +0100430 ret = regmap_update_bits_check_async(w->codec->control_data,
431 reg, mask, value,
432 &change);
Mark Brown8a713da2011-12-03 12:33:55 +0000433 if (ret != 0)
434 return ret;
435 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000436 soc_widget_lock(w);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800437 ret = soc_widget_read(w, reg, &old);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000438 if (ret < 0) {
439 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100440 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000441 }
Mark Brown8a713da2011-12-03 12:33:55 +0000442
Mark Brown8a713da2011-12-03 12:33:55 +0000443 new = (old & ~mask) | (value & mask);
444 change = old != new;
445 if (change) {
446 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000447 if (ret < 0) {
448 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000449 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000450 }
Mark Brown8a713da2011-12-03 12:33:55 +0000451 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000452 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100453 }
454
455 return change;
456}
457
Mark Brown452c5ea2009-05-17 21:41:23 +0100458/**
459 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800460 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100461 * @level: level to configure
462 *
463 * Configure the bias (power) levels for the SoC audio device.
464 *
465 * Returns 0 for success else error.
466 */
Mark Browned5a4c42011-02-18 11:12:42 -0800467static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200468 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100469{
Mark Browned5a4c42011-02-18 11:12:42 -0800470 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100471 int ret = 0;
472
Mark Brown84e90932010-11-04 00:07:02 -0400473 trace_snd_soc_bias_level_start(card, level);
474
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000475 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100476 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100477 if (ret != 0)
478 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100479
Mark Browncc4c6702011-06-06 19:03:34 +0100480 if (dapm->codec) {
481 if (dapm->codec->driver->set_bias_level)
482 ret = dapm->codec->driver->set_bias_level(dapm->codec,
483 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100484 else
485 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100486 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100487 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100488 }
Liam Girdwood41231282012-07-06 16:56:16 +0100489
Mark Brown171ec6b2011-06-06 18:15:19 +0100490 if (ret != 0)
491 goto out;
492
493 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100494 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100495out:
Mark Brown84e90932010-11-04 00:07:02 -0400496 trace_snd_soc_bias_level_done(card, level);
497
Mark Brown452c5ea2009-05-17 21:41:23 +0100498 return ret;
499}
500
Richard Purdie2b97eab2006-10-06 18:32:18 +0200501/* set up initial codec paths */
502static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
503 struct snd_soc_dapm_path *p, int i)
504{
505 switch (w->id) {
506 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000507 case snd_soc_dapm_mixer:
508 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200509 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100510 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600511 w->kcontrol_news[i].private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +0200512 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400513 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100514 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400515 unsigned int mask = (1 << fls(max)) - 1;
516 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200517
Lars-Peter Clausen249ce132013-10-06 13:43:49 +0200518 if (reg != SND_SOC_NOPM) {
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800519 soc_widget_read(w, reg, &val);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +0200520 val = (val >> shift) & mask;
521 if (invert)
522 val = max - val;
523 p->connect = !!val;
524 } else {
525 p->connect = 0;
526 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200527
Richard Purdie2b97eab2006-10-06 18:32:18 +0200528 }
529 break;
530 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600531 struct soc_enum *e = (struct soc_enum *)
532 w->kcontrol_news[i].private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200533 int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200534
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800535 soc_widget_read(w, e->reg, &val);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200536 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200537
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100538 if (item < e->items && !strcmp(p->name, e->texts[item]))
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200539 p->connect = 1;
540 else
541 p->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200542 }
543 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000544 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600545 struct soc_enum *e = (struct soc_enum *)
546 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000547
548 p->connect = 0;
549 /* since a virtual mux has no backing registers to
550 * decide which path to connect, it will try to match
551 * with the first enumeration. This is to ensure
552 * that the default mux choice (the first) will be
553 * correctly powered up during initialization.
554 */
555 if (!strcmp(p->name, e->texts[0]))
556 p->connect = 1;
557 }
558 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200559 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200560 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600561 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200562 int val, item;
563
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800564 soc_widget_read(w, e->reg, &val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200565 val = (val >> e->shift_l) & e->mask;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100566 for (item = 0; item < e->items; item++) {
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200567 if (val == e->values[item])
568 break;
569 }
570
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100571 if (item < e->items && !strcmp(p->name, e->texts[item]))
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200572 p->connect = 1;
573 else
574 p->connect = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200575 }
576 break;
Mark Brown56563102011-10-03 22:41:09 +0100577 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200578 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600579 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200580 case snd_soc_dapm_output:
581 case snd_soc_dapm_adc:
582 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000583 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200584 case snd_soc_dapm_dac:
585 case snd_soc_dapm_micbias:
586 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100587 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000588 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200589 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100590 case snd_soc_dapm_aif_in:
591 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100592 case snd_soc_dapm_dai_in:
593 case snd_soc_dapm_dai_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200594 case snd_soc_dapm_hp:
595 case snd_soc_dapm_mic:
596 case snd_soc_dapm_spk:
597 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100598 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200599 case snd_soc_dapm_kcontrol:
Mark Brown56563102011-10-03 22:41:09 +0100600 p->connect = 1;
601 break;
602 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200603 case snd_soc_dapm_pre:
604 case snd_soc_dapm_post:
605 p->connect = 0;
606 break;
607 }
608}
609
Mark Brown74b8f952009-06-06 11:26:15 +0100610/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200611static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200612 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
613 struct snd_soc_dapm_path *path, const char *control_name,
614 const struct snd_kcontrol_new *kcontrol)
615{
616 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
617 int i;
618
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100619 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200620 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200621 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200622 list_add(&path->list_sink, &dest->sources);
623 list_add(&path->list_source, &src->sinks);
624 path->name = (char*)e->texts[i];
625 dapm_set_path_status(dest, path, 0);
626 return 0;
627 }
628 }
629
630 return -ENODEV;
631}
632
Mark Brown74b8f952009-06-06 11:26:15 +0100633/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200634static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200635 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
636 struct snd_soc_dapm_path *path, const char *control_name)
637{
638 int i;
639
640 /* search for mixer kcontrol */
641 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600642 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200643 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200644 list_add(&path->list_sink, &dest->sources);
645 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600646 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200647 dapm_set_path_status(dest, path, i);
648 return 0;
649 }
650 }
651 return -ENODEV;
652}
653
Stephen Warrenaf468002011-04-28 17:38:01 -0600654static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600655 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600656 const struct snd_kcontrol_new *kcontrol_new,
657 struct snd_kcontrol **kcontrol)
658{
659 struct snd_soc_dapm_widget *w;
660 int i;
661
662 *kcontrol = NULL;
663
664 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600665 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
666 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600667 for (i = 0; i < w->num_kcontrols; i++) {
668 if (&w->kcontrol_news[i] == kcontrol_new) {
669 if (w->kcontrols)
670 *kcontrol = w->kcontrols[i];
671 return 1;
672 }
673 }
674 }
675
676 return 0;
677}
678
Stephen Warren85762e72013-03-29 15:40:10 -0600679/*
680 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
681 * create it. Either way, add the widget into the control's widget list
682 */
683static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100684 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200685{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200686 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000687 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000688 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600689 size_t prefix_len;
690 int shared;
691 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600692 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600693 char *long_name;
694 const char *name;
695 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000696
697 if (dapm->codec)
698 prefix = dapm->codec->name_prefix;
699 else
700 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200701
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000702 if (prefix)
703 prefix_len = strlen(prefix) + 1;
704 else
705 prefix_len = 0;
706
Stephen Warren85762e72013-03-29 15:40:10 -0600707 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
708 &kcontrol);
709
Stephen Warren85762e72013-03-29 15:40:10 -0600710 if (!kcontrol) {
711 if (shared) {
712 wname_in_long_name = false;
713 kcname_in_long_name = true;
714 } else {
715 switch (w->id) {
716 case snd_soc_dapm_switch:
717 case snd_soc_dapm_mixer:
718 wname_in_long_name = true;
719 kcname_in_long_name = true;
720 break;
721 case snd_soc_dapm_mixer_named_ctl:
722 wname_in_long_name = false;
723 kcname_in_long_name = true;
724 break;
725 case snd_soc_dapm_mux:
726 case snd_soc_dapm_virt_mux:
727 case snd_soc_dapm_value_mux:
728 wname_in_long_name = true;
729 kcname_in_long_name = false;
730 break;
731 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600732 return -EINVAL;
733 }
734 }
735
736 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600737 /*
738 * The control will get a prefix from the control
739 * creation process but we're also using the same
740 * prefix for widgets so cut the prefix off the
741 * front of the widget name.
742 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200743 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600744 w->name + prefix_len,
745 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200746 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200747 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600748
749 name = long_name;
750 } else if (wname_in_long_name) {
751 long_name = NULL;
752 name = w->name + prefix_len;
753 } else {
754 long_name = NULL;
755 name = w->kcontrol_news[kci].name;
756 }
757
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200758 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600759 prefix);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200760 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200761 if (!kcontrol)
762 return -ENOMEM;
763 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200764
765 ret = dapm_kcontrol_data_alloc(w, kcontrol);
766 if (ret) {
767 snd_ctl_free_one(kcontrol);
768 return ret;
769 }
770
Stephen Warren85762e72013-03-29 15:40:10 -0600771 ret = snd_ctl_add(card, kcontrol);
772 if (ret < 0) {
773 dev_err(dapm->dev,
774 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
775 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600776 return ret;
777 }
Stephen Warren85762e72013-03-29 15:40:10 -0600778 }
779
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200780 ret = dapm_kcontrol_add_widget(kcontrol, w);
781 if (ret)
782 return ret;
783
Stephen Warren85762e72013-03-29 15:40:10 -0600784 w->kcontrols[kci] = kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600785
786 return 0;
787}
788
789/* create new dapm mixer control */
790static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
791{
792 int i, ret;
793 struct snd_soc_dapm_path *path;
794
Richard Purdie2b97eab2006-10-06 18:32:18 +0200795 /* add kcontrol */
796 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200797 /* match name */
798 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200799 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600800 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200801 continue;
802
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200803 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200804 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200805 continue;
806 }
807
Mark Brown946d92a2013-08-12 23:28:42 +0100808 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600809 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200810 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100811
812 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200813 }
814 }
Stephen Warren85762e72013-03-29 15:40:10 -0600815
816 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200817}
818
819/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200820static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200821{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200822 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600823 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600824 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200825
Stephen Warrenaf468002011-04-28 17:38:01 -0600826 if (w->num_kcontrols != 1) {
827 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000828 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600829 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200830 return -EINVAL;
831 }
832
Lars-Peter Clausenfe581392013-08-01 18:30:38 +0200833 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600834 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
835 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600836 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200837
Mark Brown946d92a2013-08-12 23:28:42 +0100838 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600839 if (ret < 0)
840 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600841
Richard Purdie2b97eab2006-10-06 18:32:18 +0200842 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200843 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200844
Stephen Warrenaf468002011-04-28 17:38:01 -0600845 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200846}
847
848/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200849static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200850{
Mark Browna6c65732010-03-03 17:45:21 +0000851 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200852 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000853 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200854
Mark Browna6c65732010-03-03 17:45:21 +0000855 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200856}
857
858/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100859static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
860 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200861{
862 struct snd_soc_dapm_path *p;
863
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100864 list_for_each_entry(p, sink, list_source) {
865 if (p->walked) {
866 p->walked = 0;
867 dapm_clear_walk_output(dapm, &p->sink->sinks);
868 }
869 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200870}
871
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100872static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
873 struct list_head *source)
874{
875 struct snd_soc_dapm_path *p;
876
877 list_for_each_entry(p, source, list_sink) {
878 if (p->walked) {
879 p->walked = 0;
880 dapm_clear_walk_input(dapm, &p->source->sources);
881 }
882 }
883}
884
885
Mark Brown99497882010-05-07 20:24:05 +0100886/* We implement power down on suspend by checking the power state of
887 * the ALSA card - when we are suspending the ALSA state for the card
888 * is set to D3.
889 */
890static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
891{
Mark Brown12ea2c72011-03-02 18:17:32 +0000892 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100893
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000894 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100895 case SNDRV_CTL_POWER_D3hot:
896 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100897 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000898 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200899 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100900 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100901 default:
902 return 1;
903 }
904}
905
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100906/* add widget to list if it's not already in the list */
907static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
908 struct snd_soc_dapm_widget *w)
909{
910 struct snd_soc_dapm_widget_list *wlist;
911 int wlistsize, wlistentries, i;
912
913 if (*list == NULL)
914 return -EINVAL;
915
916 wlist = *list;
917
918 /* is this widget already in the list */
919 for (i = 0; i < wlist->num_widgets; i++) {
920 if (wlist->widgets[i] == w)
921 return 0;
922 }
923
924 /* allocate some new space */
925 wlistentries = wlist->num_widgets + 1;
926 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
927 wlistentries * sizeof(struct snd_soc_dapm_widget *);
928 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
929 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000930 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100931 w->name);
932 return -ENOMEM;
933 }
934 wlist = *list;
935
936 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000937 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100938 w->name, wlist->num_widgets);
939
940 wlist->widgets[wlist->num_widgets] = w;
941 wlist->num_widgets++;
942 return 1;
943}
944
Richard Purdie2b97eab2006-10-06 18:32:18 +0200945/*
946 * Recursively check for a completed path to an active or physically connected
947 * output widget. Returns number of complete paths.
948 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100949static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
950 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200951{
952 struct snd_soc_dapm_path *path;
953 int con = 0;
954
Mark Brown024dc072011-10-09 11:52:05 +0100955 if (widget->outputs >= 0)
956 return widget->outputs;
957
Mark Brownde02d072011-09-20 21:43:24 +0100958 DAPM_UPDATE_STAT(widget, path_checks);
959
Mark Brown62ea8742012-01-21 21:14:48 +0000960 switch (widget->id) {
961 case snd_soc_dapm_supply:
962 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200963 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200964 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100965 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000966 default:
967 break;
968 }
Mark Brown246d0a12009-04-22 18:24:55 +0100969
Mark Brown010ff262009-08-17 17:39:22 +0100970 switch (widget->id) {
971 case snd_soc_dapm_adc:
972 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100973 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100974 if (widget->active) {
975 widget->outputs = snd_soc_dapm_suspend_check(widget);
976 return widget->outputs;
977 }
Mark Brown010ff262009-08-17 17:39:22 +0100978 default:
979 break;
980 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200981
982 if (widget->connected) {
983 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100984 if (widget->id == snd_soc_dapm_output && !widget->ext) {
985 widget->outputs = snd_soc_dapm_suspend_check(widget);
986 return widget->outputs;
987 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200988
989 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100990 if (widget->id == snd_soc_dapm_hp ||
991 widget->id == snd_soc_dapm_spk ||
992 (widget->id == snd_soc_dapm_line &&
993 !list_empty(&widget->sources))) {
994 widget->outputs = snd_soc_dapm_suspend_check(widget);
995 return widget->outputs;
996 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200997 }
998
999 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +01001000 DAPM_UPDATE_STAT(widget, neighbour_checks);
1001
Mark Brownbf3a9e12011-06-13 16:42:29 +01001002 if (path->weak)
1003 continue;
1004
Mark Brown8af294b2013-02-22 17:48:15 +00001005 if (path->walking)
1006 return 1;
1007
Richard Purdie2b97eab2006-10-06 18:32:18 +02001008 if (path->walked)
1009 continue;
1010
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001011 trace_snd_soc_dapm_output_path(widget, path);
1012
Richard Purdie2b97eab2006-10-06 18:32:18 +02001013 if (path->sink && path->connect) {
1014 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001015 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001016
1017 /* do we need to add this widget to the list ? */
1018 if (list) {
1019 int err;
1020 err = dapm_list_add_widget(list, path->sink);
1021 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001022 dev_err(widget->dapm->dev,
1023 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001024 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001025 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001026 return con;
1027 }
1028 }
1029
1030 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001031
1032 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001033 }
1034 }
1035
Mark Brown024dc072011-10-09 11:52:05 +01001036 widget->outputs = con;
1037
Richard Purdie2b97eab2006-10-06 18:32:18 +02001038 return con;
1039}
1040
1041/*
1042 * Recursively check for a completed path to an active or physically connected
1043 * input widget. Returns number of complete paths.
1044 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001045static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1046 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001047{
1048 struct snd_soc_dapm_path *path;
1049 int con = 0;
1050
Mark Brown024dc072011-10-09 11:52:05 +01001051 if (widget->inputs >= 0)
1052 return widget->inputs;
1053
Mark Brownde02d072011-09-20 21:43:24 +01001054 DAPM_UPDATE_STAT(widget, path_checks);
1055
Mark Brown62ea8742012-01-21 21:14:48 +00001056 switch (widget->id) {
1057 case snd_soc_dapm_supply:
1058 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001059 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001060 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +01001061 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +00001062 default:
1063 break;
1064 }
Mark Brown246d0a12009-04-22 18:24:55 +01001065
Richard Purdie2b97eab2006-10-06 18:32:18 +02001066 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +01001067 switch (widget->id) {
1068 case snd_soc_dapm_dac:
1069 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +01001070 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +01001071 if (widget->active) {
1072 widget->inputs = snd_soc_dapm_suspend_check(widget);
1073 return widget->inputs;
1074 }
Mark Brown010ff262009-08-17 17:39:22 +01001075 default:
1076 break;
1077 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001078
1079 if (widget->connected) {
1080 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +01001081 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1082 widget->inputs = snd_soc_dapm_suspend_check(widget);
1083 return widget->inputs;
1084 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001085
1086 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +01001087 if (widget->id == snd_soc_dapm_vmid) {
1088 widget->inputs = snd_soc_dapm_suspend_check(widget);
1089 return widget->inputs;
1090 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001091
1092 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +03001093 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +01001094 (widget->id == snd_soc_dapm_line &&
1095 !list_empty(&widget->sinks))) {
1096 widget->inputs = snd_soc_dapm_suspend_check(widget);
1097 return widget->inputs;
1098 }
1099
Mark Brown1ab97c82011-11-27 16:21:51 +00001100 /* signal generator */
1101 if (widget->id == snd_soc_dapm_siggen) {
1102 widget->inputs = snd_soc_dapm_suspend_check(widget);
1103 return widget->inputs;
1104 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001105 }
1106
1107 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +01001108 DAPM_UPDATE_STAT(widget, neighbour_checks);
1109
Mark Brownbf3a9e12011-06-13 16:42:29 +01001110 if (path->weak)
1111 continue;
1112
Mark Brown8af294b2013-02-22 17:48:15 +00001113 if (path->walking)
1114 return 1;
1115
Richard Purdie2b97eab2006-10-06 18:32:18 +02001116 if (path->walked)
1117 continue;
1118
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001119 trace_snd_soc_dapm_input_path(widget, path);
1120
Richard Purdie2b97eab2006-10-06 18:32:18 +02001121 if (path->source && path->connect) {
1122 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001123 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001124
1125 /* do we need to add this widget to the list ? */
1126 if (list) {
1127 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001128 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001129 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001130 dev_err(widget->dapm->dev,
1131 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001132 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001133 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001134 return con;
1135 }
1136 }
1137
1138 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001139
1140 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001141 }
1142 }
1143
Mark Brown024dc072011-10-09 11:52:05 +01001144 widget->inputs = con;
1145
Richard Purdie2b97eab2006-10-06 18:32:18 +02001146 return con;
1147}
1148
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001149/**
1150 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1151 * @dai: the soc DAI.
1152 * @stream: stream direction.
1153 * @list: list of active widgets for this stream.
1154 *
1155 * Queries DAPM graph as to whether an valid audio stream path exists for
1156 * the initial stream specified by name. This takes into account
1157 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1158 *
1159 * Returns the number of valid paths or negative error.
1160 */
1161int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1162 struct snd_soc_dapm_widget_list **list)
1163{
1164 struct snd_soc_card *card = dai->card;
1165 int paths;
1166
1167 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1168 dapm_reset(card);
1169
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001170 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001171 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001172 dapm_clear_walk_output(&card->dapm,
1173 &dai->playback_widget->sinks);
1174 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001175 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001176 dapm_clear_walk_input(&card->dapm,
1177 &dai->capture_widget->sources);
1178 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001179
1180 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001181 mutex_unlock(&card->dapm_mutex);
1182
1183 return paths;
1184}
1185
Richard Purdie2b97eab2006-10-06 18:32:18 +02001186/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001187 * Handler for generic register modifier widget.
1188 */
1189int dapm_reg_event(struct snd_soc_dapm_widget *w,
1190 struct snd_kcontrol *kcontrol, int event)
1191{
1192 unsigned int val;
1193
1194 if (SND_SOC_DAPM_EVENT_ON(event))
1195 val = w->on_val;
1196 else
1197 val = w->off_val;
1198
Liam Girdwood49575fb52012-03-06 18:16:19 +00001199 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001200 w->mask << w->shift, val << w->shift);
1201
1202 return 0;
1203}
Mark Brown11589412008-07-29 11:42:23 +01001204EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001205
Mark Brown62ea8742012-01-21 21:14:48 +00001206/*
1207 * Handler for regulator supply widget.
1208 */
1209int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1210 struct snd_kcontrol *kcontrol, int event)
1211{
Mark Brownc05b84d2012-09-07 12:57:11 +08001212 int ret;
1213
Mark Browneb270e92013-10-09 13:52:52 +01001214 soc_dapm_async_complete(w->dapm);
1215
Mark Brownc05b84d2012-09-07 12:57:11 +08001216 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001217 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001218 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001219 if (ret != 0)
1220 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001221 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001222 w->name, ret);
1223 }
1224
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001225 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001226 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001227 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001228 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001229 if (ret != 0)
1230 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001231 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001232 w->name, ret);
1233 }
1234
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001235 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001236 }
Mark Brown62ea8742012-01-21 21:14:48 +00001237}
1238EXPORT_SYMBOL_GPL(dapm_regulator_event);
1239
Ola Liljad7e7eb92012-05-24 15:26:25 +02001240/*
1241 * Handler for clock supply widget.
1242 */
1243int dapm_clock_event(struct snd_soc_dapm_widget *w,
1244 struct snd_kcontrol *kcontrol, int event)
1245{
1246 if (!w->clk)
1247 return -EIO;
1248
Mark Browneb270e92013-10-09 13:52:52 +01001249 soc_dapm_async_complete(w->dapm);
1250
Mark Brownec029952012-06-04 08:16:20 +01001251#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001252 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001253 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001254 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001255 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001256 return 0;
1257 }
Mark Brownec029952012-06-04 08:16:20 +01001258#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001259 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001260}
1261EXPORT_SYMBOL_GPL(dapm_clock_event);
1262
Mark Brownd8050022011-09-28 18:28:23 +01001263static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1264{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001265 if (w->power_checked)
1266 return w->new_power;
1267
Mark Brownd8050022011-09-28 18:28:23 +01001268 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001269 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001270 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001271 w->new_power = w->power_check(w);
1272
1273 w->power_checked = true;
1274
1275 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001276}
1277
Mark Browncd0f2d42009-04-20 16:56:59 +01001278/* Generic check to see if a widget should be powered.
1279 */
1280static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1281{
1282 int in, out;
1283
Mark Brownde02d072011-09-20 21:43:24 +01001284 DAPM_UPDATE_STAT(w, power_checks);
1285
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001286 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001287 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001288 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001289 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001290 return out != 0 && in != 0;
1291}
1292
Mark Brown6ea31b92009-04-20 17:15:41 +01001293/* Check to see if an ADC has power */
1294static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1295{
1296 int in;
1297
Mark Brownde02d072011-09-20 21:43:24 +01001298 DAPM_UPDATE_STAT(w, power_checks);
1299
Mark Brown6ea31b92009-04-20 17:15:41 +01001300 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001301 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001302 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001303 return in != 0;
1304 } else {
1305 return dapm_generic_check_power(w);
1306 }
1307}
1308
1309/* Check to see if a DAC has power */
1310static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1311{
1312 int out;
1313
Mark Brownde02d072011-09-20 21:43:24 +01001314 DAPM_UPDATE_STAT(w, power_checks);
1315
Mark Brown6ea31b92009-04-20 17:15:41 +01001316 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001317 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001318 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001319 return out != 0;
1320 } else {
1321 return dapm_generic_check_power(w);
1322 }
1323}
1324
Mark Brown246d0a12009-04-22 18:24:55 +01001325/* Check to see if a power supply is needed */
1326static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1327{
1328 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001329
Mark Brownde02d072011-09-20 21:43:24 +01001330 DAPM_UPDATE_STAT(w, power_checks);
1331
Mark Brown246d0a12009-04-22 18:24:55 +01001332 /* Check if one of our outputs is connected */
1333 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001334 DAPM_UPDATE_STAT(w, neighbour_checks);
1335
Mark Brownbf3a9e12011-06-13 16:42:29 +01001336 if (path->weak)
1337 continue;
1338
Mark Brown215edda2009-09-08 18:59:05 +01001339 if (path->connected &&
1340 !path->connected(path->source, path->sink))
1341 continue;
1342
Mark Brown30173582011-02-11 11:42:19 +00001343 if (!path->sink)
1344 continue;
1345
Mark Brownf68d7e12011-10-04 22:57:50 +01001346 if (dapm_widget_power_check(path->sink))
1347 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001348 }
1349
Mark Brownf68d7e12011-10-04 22:57:50 +01001350 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001351}
1352
Mark Brown35c64bc2011-09-28 18:23:53 +01001353static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1354{
1355 return 1;
1356}
1357
Mark Brown38357ab2009-06-06 19:03:23 +01001358static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1359 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001360 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001361{
Mark Brown828a8422011-01-15 13:14:30 +00001362 int *sort;
1363
1364 if (power_up)
1365 sort = dapm_up_seq;
1366 else
1367 sort = dapm_down_seq;
1368
Mark Brown38357ab2009-06-06 19:03:23 +01001369 if (sort[a->id] != sort[b->id])
1370 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001371 if (a->subseq != b->subseq) {
1372 if (power_up)
1373 return a->subseq - b->subseq;
1374 else
1375 return b->subseq - a->subseq;
1376 }
Mark Brownb22ead22009-06-07 12:51:26 +01001377 if (a->reg != b->reg)
1378 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001379 if (a->dapm != b->dapm)
1380 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001381
Mark Brown38357ab2009-06-06 19:03:23 +01001382 return 0;
1383}
Mark Brown42aa3412009-03-01 19:21:10 +00001384
Mark Brown38357ab2009-06-06 19:03:23 +01001385/* Insert a widget in order into a DAPM power sequence. */
1386static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1387 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001388 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001389{
1390 struct snd_soc_dapm_widget *w;
1391
1392 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001393 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001394 list_add_tail(&new_widget->power_list, &w->power_list);
1395 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001396 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001397
Mark Brown38357ab2009-06-06 19:03:23 +01001398 list_add_tail(&new_widget->power_list, list);
1399}
Mark Brown42aa3412009-03-01 19:21:10 +00001400
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001401static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001402 struct snd_soc_dapm_widget *w, int event)
1403{
Mark Brown68f89ad2010-11-03 23:51:49 -04001404 const char *ev_name;
1405 int power, ret;
1406
1407 switch (event) {
1408 case SND_SOC_DAPM_PRE_PMU:
1409 ev_name = "PRE_PMU";
1410 power = 1;
1411 break;
1412 case SND_SOC_DAPM_POST_PMU:
1413 ev_name = "POST_PMU";
1414 power = 1;
1415 break;
1416 case SND_SOC_DAPM_PRE_PMD:
1417 ev_name = "PRE_PMD";
1418 power = 0;
1419 break;
1420 case SND_SOC_DAPM_POST_PMD:
1421 ev_name = "POST_PMD";
1422 power = 0;
1423 break;
Mark Brown80114122013-02-25 15:14:19 +00001424 case SND_SOC_DAPM_WILL_PMU:
1425 ev_name = "WILL_PMU";
1426 power = 1;
1427 break;
1428 case SND_SOC_DAPM_WILL_PMD:
1429 ev_name = "WILL_PMD";
1430 power = 0;
1431 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001432 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001433 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001434 return;
1435 }
1436
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001437 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001438 return;
1439
1440 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001441 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001442 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001443 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001444 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001445 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001446 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001447 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001448 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001449 ev_name, w->name, ret);
1450 }
1451}
1452
Mark Brownb22ead22009-06-07 12:51:26 +01001453/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001454static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001455 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001456{
Mark Brown68f89ad2010-11-03 23:51:49 -04001457 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001458 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001459 unsigned int value = 0;
1460 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001461
1462 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1463 power_list)->reg;
1464
1465 list_for_each_entry(w, pending, power_list) {
Takashi Iwaibf4edea2013-11-07 18:38:47 +01001466 WARN_ON(reg != w->reg);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001467 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001468
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001469 mask |= w->mask << w->shift;
1470 if (w->power)
1471 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001472 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001473 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001474
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001475 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001476 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1477 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001478
Mark Brown68f89ad2010-11-03 23:51:49 -04001479 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001480 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1481 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001482 }
1483
Mark Brown81628102009-06-07 13:21:24 +01001484 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001485 /* Any widget will do, they should all be updating the
1486 * same register.
1487 */
1488 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1489 power_list);
1490
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001491 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001492 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001493 value, mask, reg, card->pop_time);
1494 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001495 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001496 }
1497
1498 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001499 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1500 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001501 }
Mark Brown42aa3412009-03-01 19:21:10 +00001502}
1503
Mark Brownb22ead22009-06-07 12:51:26 +01001504/* Apply a DAPM power sequence.
1505 *
1506 * We walk over a pre-sorted list of widgets to apply power to. In
1507 * order to minimise the number of writes to the device required
1508 * multiple widgets will be updated in a single write where possible.
1509 * Currently anything that requires more than a single write is not
1510 * handled.
1511 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001512static void dapm_seq_run(struct snd_soc_card *card,
1513 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001514{
1515 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001516 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001517 LIST_HEAD(pending);
1518 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001519 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001520 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001521 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001522 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001523 int *sort;
1524
1525 if (power_up)
1526 sort = dapm_up_seq;
1527 else
1528 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001529
Mark Brownb22ead22009-06-07 12:51:26 +01001530 list_for_each_entry_safe(w, n, list, power_list) {
1531 ret = 0;
1532
1533 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001534 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001535 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001536 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001537 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001538
Mark Brown474b62d2011-01-18 16:14:44 +00001539 if (cur_dapm && cur_dapm->seq_notifier) {
1540 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1541 if (sort[i] == cur_sort)
1542 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001543 i,
1544 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001545 }
1546
Mark Browneb270e92013-10-09 13:52:52 +01001547 if (cur_dapm && w->dapm != cur_dapm)
1548 soc_dapm_async_complete(cur_dapm);
1549
Mark Brownb22ead22009-06-07 12:51:26 +01001550 INIT_LIST_HEAD(&pending);
1551 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001552 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001553 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001554 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001555 }
1556
Mark Brown163cac02009-06-07 10:12:52 +01001557 switch (w->id) {
1558 case snd_soc_dapm_pre:
1559 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001560 list_for_each_entry_safe_continue(w, n, list,
1561 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001562
Mark Brownb22ead22009-06-07 12:51:26 +01001563 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001564 ret = w->event(w,
1565 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001566 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001567 ret = w->event(w,
1568 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001569 break;
1570
1571 case snd_soc_dapm_post:
1572 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001573 list_for_each_entry_safe_continue(w, n, list,
1574 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001575
Mark Brownb22ead22009-06-07 12:51:26 +01001576 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001577 ret = w->event(w,
1578 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001579 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001580 ret = w->event(w,
1581 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001582 break;
1583
Mark Brown163cac02009-06-07 10:12:52 +01001584 default:
Mark Brown81628102009-06-07 13:21:24 +01001585 /* Queue it up for application */
1586 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001587 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001588 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001589 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001590 list_move(&w->power_list, &pending);
1591 break;
Mark Brown163cac02009-06-07 10:12:52 +01001592 }
Mark Brownb22ead22009-06-07 12:51:26 +01001593
1594 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001595 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001596 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001597 }
Mark Brownb22ead22009-06-07 12:51:26 +01001598
1599 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001600 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001601
1602 if (cur_dapm && cur_dapm->seq_notifier) {
1603 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1604 if (sort[i] == cur_sort)
1605 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001606 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001607 }
Mark Browneb270e92013-10-09 13:52:52 +01001608
1609 list_for_each_entry(d, &card->dapm_list, list) {
1610 soc_dapm_async_complete(d);
1611 }
Mark Brown163cac02009-06-07 10:12:52 +01001612}
1613
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001614static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001615{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001616 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001617 struct snd_soc_dapm_widget_list *wlist;
1618 struct snd_soc_dapm_widget *w = NULL;
1619 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001620 int ret;
1621
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001622 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001623 return;
1624
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001625 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001626
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001627 for (wi = 0; wi < wlist->num_widgets; wi++) {
1628 w = wlist->widgets[wi];
1629
1630 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1631 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1632 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001633 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001634 w->name, ret);
1635 }
Mark Brown97404f22010-12-14 16:13:57 +00001636 }
1637
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001638 if (!w)
1639 return;
1640
Liam Girdwood49575fb52012-03-06 18:16:19 +00001641 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001642 update->val);
1643 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001644 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001645 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001646
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001647 for (wi = 0; wi < wlist->num_widgets; wi++) {
1648 w = wlist->widgets[wi];
1649
1650 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1651 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1652 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001653 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001654 w->name, ret);
1655 }
Mark Brown97404f22010-12-14 16:13:57 +00001656 }
1657}
1658
Mark Brown9d0624a2011-02-18 11:49:43 -08001659/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1660 * they're changing state.
1661 */
1662static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1663{
1664 struct snd_soc_dapm_context *d = data;
1665 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001666
Mark Brown56fba412011-06-04 11:25:10 +01001667 /* If we're off and we're not supposed to be go into STANDBY */
1668 if (d->bias_level == SND_SOC_BIAS_OFF &&
1669 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001670 if (d->dev)
1671 pm_runtime_get_sync(d->dev);
1672
Mark Brown9d0624a2011-02-18 11:49:43 -08001673 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1674 if (ret != 0)
1675 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001676 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001677 }
1678
Mark Brown56fba412011-06-04 11:25:10 +01001679 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1680 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001681 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1682 if (ret != 0)
1683 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001684 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001685 }
1686}
1687
1688/* Async callback run prior to DAPM sequences - brings to their final
1689 * state.
1690 */
1691static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1692{
1693 struct snd_soc_dapm_context *d = data;
1694 int ret;
1695
1696 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001697 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1698 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1699 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001700 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1701 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001702 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001703 ret);
1704 }
1705
1706 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001707 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1708 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001709 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1710 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001711 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1712 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001713
1714 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001715 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001716 }
1717
1718 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001719 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1720 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001721 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1722 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001723 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001724 ret);
1725 }
1726}
Mark Brown97404f22010-12-14 16:13:57 +00001727
Mark Brownfe4fda52011-10-03 22:36:57 +01001728static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1729 bool power, bool connect)
1730{
1731 /* If a connection is being made or broken then that update
1732 * will have marked the peer dirty, otherwise the widgets are
1733 * not connected and this update has no impact. */
1734 if (!connect)
1735 return;
1736
1737 /* If the peer is already in the state we're moving to then we
1738 * won't have an impact on it. */
1739 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001740 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001741}
1742
Mark Brown05623c42011-09-28 17:02:31 +01001743static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1744 struct list_head *up_list,
1745 struct list_head *down_list)
1746{
Mark Browndb432b42011-10-03 21:06:40 +01001747 struct snd_soc_dapm_path *path;
1748
Mark Brown05623c42011-09-28 17:02:31 +01001749 if (w->power == power)
1750 return;
1751
1752 trace_snd_soc_dapm_widget_power(w, power);
1753
Mark Browndb432b42011-10-03 21:06:40 +01001754 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001755 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001756 */
1757 list_for_each_entry(path, &w->sources, list_sink) {
1758 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001759 dapm_widget_set_peer_power(path->source, power,
1760 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001761 }
1762 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001763 switch (w->id) {
1764 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001765 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001766 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001767 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001768 /* Supplies can't affect their outputs, only their inputs */
1769 break;
1770 default:
1771 list_for_each_entry(path, &w->sinks, list_source) {
1772 if (path->sink) {
1773 dapm_widget_set_peer_power(path->sink, power,
1774 path->connect);
1775 }
Mark Browndb432b42011-10-03 21:06:40 +01001776 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001777 break;
Mark Browndb432b42011-10-03 21:06:40 +01001778 }
1779
Mark Brown05623c42011-09-28 17:02:31 +01001780 if (power)
1781 dapm_seq_insert(w, up_list, true);
1782 else
1783 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001784}
1785
Mark Brown7c81beb2011-09-20 22:22:32 +01001786static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1787 struct list_head *up_list,
1788 struct list_head *down_list)
1789{
Mark Brown7c81beb2011-09-20 22:22:32 +01001790 int power;
1791
1792 switch (w->id) {
1793 case snd_soc_dapm_pre:
1794 dapm_seq_insert(w, down_list, false);
1795 break;
1796 case snd_soc_dapm_post:
1797 dapm_seq_insert(w, up_list, true);
1798 break;
1799
1800 default:
Mark Brownd8050022011-09-28 18:28:23 +01001801 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001802
Mark Brown05623c42011-09-28 17:02:31 +01001803 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001804 break;
1805 }
1806}
1807
Mark Brown42aa3412009-03-01 19:21:10 +00001808/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001809 * Scan each dapm widget for complete audio path.
1810 * A complete path is a route that has valid endpoints i.e.:-
1811 *
1812 * o DAC to output pin.
1813 * o Input Pin to ADC.
1814 * o Input pin to Output pin (bypass, sidetone)
1815 * o DAC to ADC (loopback).
1816 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001817static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001818{
1819 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001820 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001821 LIST_HEAD(up_list);
1822 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001823 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001824 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001825
Mark Brown84e90932010-11-04 00:07:02 -04001826 trace_snd_soc_dapm_start(card);
1827
Mark Brown56fba412011-06-04 11:25:10 +01001828 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001829 if (d->idle_bias_off)
1830 d->target_bias_level = SND_SOC_BIAS_OFF;
1831 else
1832 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001833 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001834
Liam Girdwood6c120e12012-02-15 15:15:34 +00001835 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001836
Mark Brown6d3ddc82009-05-16 17:47:29 +01001837 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001838 * lists indicating if they should be powered up or down. We
1839 * only check widgets that have been flagged as dirty but note
1840 * that new widgets may be added to the dirty list while we
1841 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001842 */
Mark Browndb432b42011-10-03 21:06:40 +01001843 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001844 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001845 }
1846
Mark Brownf9de6d72011-09-28 17:19:47 +01001847 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001848 switch (w->id) {
1849 case snd_soc_dapm_pre:
1850 case snd_soc_dapm_post:
1851 /* These widgets always need to be powered */
1852 break;
1853 default:
1854 list_del_init(&w->dirty);
1855 break;
1856 }
Mark Browndb432b42011-10-03 21:06:40 +01001857
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001858 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001859 d = w->dapm;
1860
1861 /* Supplies and micbiases only bring the
1862 * context up to STANDBY as unless something
1863 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001864 * generally don't require full power. Signal
1865 * generators are virtual pins and have no
1866 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001867 */
1868 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001869 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001870 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001871 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001872 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001873 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001874 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001875 case snd_soc_dapm_micbias:
1876 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1877 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1878 break;
1879 default:
1880 d->target_bias_level = SND_SOC_BIAS_ON;
1881 break;
1882 }
1883 }
1884
1885 }
1886
Mark Brown85a843c2011-09-21 21:29:47 +01001887 /* Force all contexts in the card to the same bias state if
1888 * they're not ground referenced.
1889 */
Mark Brown56fba412011-06-04 11:25:10 +01001890 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001891 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001892 if (d->target_bias_level > bias)
1893 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001894 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001895 if (!d->idle_bias_off)
1896 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001897
Mark Brownde02d072011-09-20 21:43:24 +01001898 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001899
Mark Brown9d0624a2011-02-18 11:49:43 -08001900 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001901 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001902 async_schedule_domain(dapm_pre_sequence_async, d,
1903 &async_domain);
1904 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001905
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001906 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001907 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001908 }
1909
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001910 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001911 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001912 }
1913
Mark Brown6d3ddc82009-05-16 17:47:29 +01001914 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001915 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001916
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001917 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001918
Mark Brown6d3ddc82009-05-16 17:47:29 +01001919 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001920 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001921
Mark Brown9d0624a2011-02-18 11:49:43 -08001922 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001923 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001924 async_schedule_domain(dapm_post_sequence_async, d,
1925 &async_domain);
1926 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001927
Liam Girdwood8078d872012-02-15 15:15:35 +00001928 /* do we need to notify any clients that DAPM event is complete */
1929 list_for_each_entry(d, &card->dapm_list, list) {
1930 if (d->stream_event)
1931 d->stream_event(d, event);
1932 }
1933
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001934 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001935 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001936 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001937
Mark Brown84e90932010-11-04 00:07:02 -04001938 trace_snd_soc_dapm_done(card);
1939
Mark Brown42aa3412009-03-01 19:21:10 +00001940 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001941}
1942
Mark Brown79fb9382009-08-21 16:38:13 +01001943#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001944static ssize_t dapm_widget_power_read_file(struct file *file,
1945 char __user *user_buf,
1946 size_t count, loff_t *ppos)
1947{
1948 struct snd_soc_dapm_widget *w = file->private_data;
1949 char *buf;
1950 int in, out;
1951 ssize_t ret;
1952 struct snd_soc_dapm_path *p = NULL;
1953
1954 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1955 if (!buf)
1956 return -ENOMEM;
1957
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001958 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001959 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001960 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001961 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001962
Mark Brownf13ebad2012-03-03 18:01:01 +00001963 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1964 w->name, w->power ? "On" : "Off",
1965 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001966
Mark Brownd033c362009-12-04 15:25:56 +00001967 if (w->reg >= 0)
1968 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001969 " - R%d(0x%x) mask 0x%x",
1970 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001971
1972 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1973
Mark Brown3eef08b2009-09-14 16:49:00 +01001974 if (w->sname)
1975 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1976 w->sname,
1977 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001978
1979 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001980 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001981 continue;
1982
Mark Brown79fb9382009-08-21 16:38:13 +01001983 if (p->connect)
1984 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001985 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001986 p->name ? p->name : "static",
1987 p->source->name);
1988 }
1989 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001990 if (p->connected && !p->connected(w, p->sink))
1991 continue;
1992
Mark Brown79fb9382009-08-21 16:38:13 +01001993 if (p->connect)
1994 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001995 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001996 p->name ? p->name : "static",
1997 p->sink->name);
1998 }
1999
2000 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2001
2002 kfree(buf);
2003 return ret;
2004}
2005
2006static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002007 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002008 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002009 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002010};
2011
Mark Brownef49e4f2011-04-04 20:48:13 +09002012static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2013 size_t count, loff_t *ppos)
2014{
2015 struct snd_soc_dapm_context *dapm = file->private_data;
2016 char *level;
2017
2018 switch (dapm->bias_level) {
2019 case SND_SOC_BIAS_ON:
2020 level = "On\n";
2021 break;
2022 case SND_SOC_BIAS_PREPARE:
2023 level = "Prepare\n";
2024 break;
2025 case SND_SOC_BIAS_STANDBY:
2026 level = "Standby\n";
2027 break;
2028 case SND_SOC_BIAS_OFF:
2029 level = "Off\n";
2030 break;
2031 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002032 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002033 level = "Unknown\n";
2034 break;
2035 }
2036
2037 return simple_read_from_buffer(user_buf, count, ppos, level,
2038 strlen(level));
2039}
2040
2041static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002042 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002043 .read = dapm_bias_read_file,
2044 .llseek = default_llseek,
2045};
2046
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002047void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2048 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002049{
Mark Brown79fb9382009-08-21 16:38:13 +01002050 struct dentry *d;
2051
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002052 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2053
2054 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002055 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002056 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002057 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002058 }
Mark Brown79fb9382009-08-21 16:38:13 +01002059
Mark Brownef49e4f2011-04-04 20:48:13 +09002060 d = debugfs_create_file("bias_level", 0444,
2061 dapm->debugfs_dapm, dapm,
2062 &dapm_bias_fops);
2063 if (!d)
2064 dev_warn(dapm->dev,
2065 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002066}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002067
2068static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2069{
2070 struct snd_soc_dapm_context *dapm = w->dapm;
2071 struct dentry *d;
2072
2073 if (!dapm->debugfs_dapm || !w->name)
2074 return;
2075
2076 d = debugfs_create_file(w->name, 0444,
2077 dapm->debugfs_dapm, w,
2078 &dapm_widget_power_fops);
2079 if (!d)
2080 dev_warn(w->dapm->dev,
2081 "ASoC: Failed to create %s debugfs file\n",
2082 w->name);
2083}
2084
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002085static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2086{
2087 debugfs_remove_recursive(dapm->debugfs_dapm);
2088}
2089
Mark Brown79fb9382009-08-21 16:38:13 +01002090#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002091void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2092 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002093{
2094}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002095
2096static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2097{
2098}
2099
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002100static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2101{
2102}
2103
Mark Brown79fb9382009-08-21 16:38:13 +01002104#endif
2105
Richard Purdie2b97eab2006-10-06 18:32:18 +02002106/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002107static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002108 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002109{
2110 struct snd_soc_dapm_path *path;
2111 int found = 0;
2112
Richard Purdie2b97eab2006-10-06 18:32:18 +02002113 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002114 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002115 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002116 continue;
2117
2118 found = 1;
2119 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002120 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002121 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002122 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002123 } else {
2124 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002125 dapm_mark_dirty(path->source,
2126 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002127 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002128 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002129 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002130 }
2131
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002132 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002133 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002134
Liam Girdwood618dae12012-04-25 12:12:51 +01002135 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002136}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002137
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002138int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002139 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2140 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002141{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002142 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002143 int ret;
2144
Liam Girdwood3cd04342012-03-09 12:02:08 +00002145 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002146 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002147 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002148 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002149 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002150 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002151 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002152 return ret;
2153}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002154EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002155
Milan plzik1b075e32008-01-10 14:39:46 +01002156/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002157static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002158 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002159{
2160 struct snd_soc_dapm_path *path;
2161 int found = 0;
2162
Richard Purdie2b97eab2006-10-06 18:32:18 +02002163 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002164 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002165 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002166 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002167 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002168 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002169 }
2170
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002171 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002172 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002173
Liam Girdwood618dae12012-04-25 12:12:51 +01002174 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002175}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002176
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002177int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002178 struct snd_kcontrol *kcontrol, int connect,
2179 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002180{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002181 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002182 int ret;
2183
Liam Girdwood3cd04342012-03-09 12:02:08 +00002184 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002185 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002186 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002187 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002188 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002189 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002190 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002191 return ret;
2192}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002193EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002194
2195/* show dapm widget status in sys fs */
2196static ssize_t dapm_widget_show(struct device *dev,
2197 struct device_attribute *attr, char *buf)
2198{
Mark Brown36ae1a92012-01-06 17:12:45 -08002199 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002200 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002201 struct snd_soc_dapm_widget *w;
2202 int count = 0;
2203 char *state = "not set";
2204
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002205 list_for_each_entry(w, &codec->card->widgets, list) {
2206 if (w->dapm != &codec->dapm)
2207 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002208
2209 /* only display widgets that burnm power */
2210 switch (w->id) {
2211 case snd_soc_dapm_hp:
2212 case snd_soc_dapm_mic:
2213 case snd_soc_dapm_spk:
2214 case snd_soc_dapm_line:
2215 case snd_soc_dapm_micbias:
2216 case snd_soc_dapm_dac:
2217 case snd_soc_dapm_adc:
2218 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002219 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002220 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002221 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002222 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002223 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002224 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002225 if (w->name)
2226 count += sprintf(buf + count, "%s: %s\n",
2227 w->name, w->power ? "On":"Off");
2228 break;
2229 default:
2230 break;
2231 }
2232 }
2233
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002234 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002235 case SND_SOC_BIAS_ON:
2236 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002237 break;
Mark Brown0be98982008-05-19 12:31:28 +02002238 case SND_SOC_BIAS_PREPARE:
2239 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002240 break;
Mark Brown0be98982008-05-19 12:31:28 +02002241 case SND_SOC_BIAS_STANDBY:
2242 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002243 break;
Mark Brown0be98982008-05-19 12:31:28 +02002244 case SND_SOC_BIAS_OFF:
2245 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002246 break;
2247 }
2248 count += sprintf(buf + count, "PM State: %s\n", state);
2249
2250 return count;
2251}
2252
2253static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2254
2255int snd_soc_dapm_sys_add(struct device *dev)
2256{
Troy Kisky12ef1932008-10-13 17:42:14 -07002257 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002258}
2259
2260static void snd_soc_dapm_sys_remove(struct device *dev)
2261{
Mark Brownaef90842009-05-16 17:53:16 +01002262 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002263}
2264
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002265static void dapm_free_path(struct snd_soc_dapm_path *path)
2266{
2267 list_del(&path->list_sink);
2268 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002269 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002270 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002271 kfree(path);
2272}
2273
Richard Purdie2b97eab2006-10-06 18:32:18 +02002274/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002275static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002276{
2277 struct snd_soc_dapm_widget *w, *next_w;
2278 struct snd_soc_dapm_path *p, *next_p;
2279
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002280 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2281 if (w->dapm != dapm)
2282 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002283 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002284 /*
2285 * remove source and sink paths associated to this widget.
2286 * While removing the path, remove reference to it from both
2287 * source and sink widgets so that path is removed only once.
2288 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002289 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2290 dapm_free_path(p);
2291
2292 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2293 dapm_free_path(p);
2294
Stephen Warrenfad59882011-04-28 17:37:59 -06002295 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002296 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002297 kfree(w);
2298 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002299}
2300
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002301static struct snd_soc_dapm_widget *dapm_find_widget(
2302 struct snd_soc_dapm_context *dapm, const char *pin,
2303 bool search_other_contexts)
2304{
2305 struct snd_soc_dapm_widget *w;
2306 struct snd_soc_dapm_widget *fallback = NULL;
2307
2308 list_for_each_entry(w, &dapm->card->widgets, list) {
2309 if (!strcmp(w->name, pin)) {
2310 if (w->dapm == dapm)
2311 return w;
2312 else
2313 fallback = w;
2314 }
2315 }
2316
2317 if (search_other_contexts)
2318 return fallback;
2319
2320 return NULL;
2321}
2322
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002323static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002324 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002325{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002326 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002327
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002328 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002329 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002330 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002331 }
2332
Mark Brown1a8b2d92012-02-16 11:50:07 -08002333 if (w->connected != status)
2334 dapm_mark_dirty(w, "pin configuration");
2335
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002336 w->connected = status;
2337 if (status == 0)
2338 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002339
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002340 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002341}
2342
Richard Purdie2b97eab2006-10-06 18:32:18 +02002343/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002344 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002345 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002346 *
2347 * Walks all dapm audio paths and powers widgets according to their
2348 * stream or path usage.
2349 *
2350 * Returns 0 for success.
2351 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002352int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002353{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002354 int ret;
2355
Mark Brown4f4c0072011-10-07 14:29:19 +01002356 /*
2357 * Suppress early reports (eg, jacks syncing their state) to avoid
2358 * silly DAPM runs during card startup.
2359 */
2360 if (!dapm->card || !dapm->card->instantiated)
2361 return 0;
2362
Liam Girdwood3cd04342012-03-09 12:02:08 +00002363 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002364 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002365 mutex_unlock(&dapm->card->dapm_mutex);
2366 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002367}
Liam Girdwooda5302182008-07-07 13:35:17 +01002368EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002369
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002370static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2371 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2372 const char *control,
2373 int (*connected)(struct snd_soc_dapm_widget *source,
2374 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002375{
2376 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002377 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002378
2379 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2380 if (!path)
2381 return -ENOMEM;
2382
2383 path->source = wsource;
2384 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002385 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002386 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002387 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002388 INIT_LIST_HEAD(&path->list_source);
2389 INIT_LIST_HEAD(&path->list_sink);
2390
2391 /* check for external widgets */
2392 if (wsink->id == snd_soc_dapm_input) {
2393 if (wsource->id == snd_soc_dapm_micbias ||
2394 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002395 wsource->id == snd_soc_dapm_line ||
2396 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002397 wsink->ext = 1;
2398 }
2399 if (wsource->id == snd_soc_dapm_output) {
2400 if (wsink->id == snd_soc_dapm_spk ||
2401 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002402 wsink->id == snd_soc_dapm_line ||
2403 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002404 wsource->ext = 1;
2405 }
2406
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002407 dapm_mark_dirty(wsource, "Route added");
2408 dapm_mark_dirty(wsink, "Route added");
2409
Richard Purdie2b97eab2006-10-06 18:32:18 +02002410 /* connect static paths */
2411 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002412 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002413 list_add(&path->list_sink, &wsink->sources);
2414 list_add(&path->list_source, &wsource->sinks);
2415 path->connect = 1;
2416 return 0;
2417 }
2418
2419 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002420 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002421 case snd_soc_dapm_adc:
2422 case snd_soc_dapm_dac:
2423 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002424 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002425 case snd_soc_dapm_input:
2426 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002427 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002428 case snd_soc_dapm_micbias:
2429 case snd_soc_dapm_vmid:
2430 case snd_soc_dapm_pre:
2431 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002432 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002433 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002434 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002435 case snd_soc_dapm_aif_in:
2436 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002437 case snd_soc_dapm_dai_in:
2438 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002439 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002440 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002441 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002442 list_add(&path->list_sink, &wsink->sources);
2443 list_add(&path->list_source, &wsource->sinks);
2444 path->connect = 1;
2445 return 0;
2446 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002447 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002448 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002449 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002450 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002451 if (ret != 0)
2452 goto err;
2453 break;
2454 case snd_soc_dapm_switch:
2455 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002456 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002457 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002458 if (ret != 0)
2459 goto err;
2460 break;
2461 case snd_soc_dapm_hp:
2462 case snd_soc_dapm_mic:
2463 case snd_soc_dapm_line:
2464 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002465 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002466 list_add(&path->list_sink, &wsink->sources);
2467 list_add(&path->list_source, &wsource->sinks);
2468 path->connect = 0;
2469 return 0;
2470 }
Mark Brownfabd0382012-07-05 17:20:06 +01002471
Richard Purdie2b97eab2006-10-06 18:32:18 +02002472 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002473err:
2474 kfree(path);
2475 return ret;
2476}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002477
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002478static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002479 const struct snd_soc_dapm_route *route,
2480 unsigned int is_prefixed)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002481{
2482 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2483 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2484 const char *sink;
2485 const char *source;
2486 char prefixed_sink[80];
2487 char prefixed_source[80];
2488 int ret;
2489
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002490 if (dapm->codec && dapm->codec->name_prefix && !is_prefixed) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002491 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2492 dapm->codec->name_prefix, route->sink);
2493 sink = prefixed_sink;
2494 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2495 dapm->codec->name_prefix, route->source);
2496 source = prefixed_source;
2497 } else {
2498 sink = route->sink;
2499 source = route->source;
2500 }
2501
2502 /*
2503 * find src and dest widgets over all widgets but favor a widget from
2504 * current DAPM context
2505 */
2506 list_for_each_entry(w, &dapm->card->widgets, list) {
2507 if (!wsink && !(strcmp(w->name, sink))) {
2508 wtsink = w;
2509 if (w->dapm == dapm)
2510 wsink = w;
2511 continue;
2512 }
2513 if (!wsource && !(strcmp(w->name, source))) {
2514 wtsource = w;
2515 if (w->dapm == dapm)
2516 wsource = w;
2517 }
2518 }
2519 /* use widget from another DAPM context if not found from this */
2520 if (!wsink)
2521 wsink = wtsink;
2522 if (!wsource)
2523 wsource = wtsource;
2524
2525 if (wsource == NULL) {
2526 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2527 route->source);
2528 return -ENODEV;
2529 }
2530 if (wsink == NULL) {
2531 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2532 route->sink);
2533 return -ENODEV;
2534 }
2535
2536 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2537 route->connected);
2538 if (ret)
2539 goto err;
2540
2541 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002542err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002543 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002544 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002545 return ret;
2546}
Mark Brown105f1c22008-05-13 14:52:19 +02002547
Mark Brownefcc3c62012-07-05 17:24:19 +01002548static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2549 const struct snd_soc_dapm_route *route)
2550{
2551 struct snd_soc_dapm_path *path, *p;
2552 const char *sink;
2553 const char *source;
2554 char prefixed_sink[80];
2555 char prefixed_source[80];
2556
2557 if (route->control) {
2558 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002559 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002560 return -EINVAL;
2561 }
2562
2563 if (dapm->codec && dapm->codec->name_prefix) {
2564 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2565 dapm->codec->name_prefix, route->sink);
2566 sink = prefixed_sink;
2567 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2568 dapm->codec->name_prefix, route->source);
2569 source = prefixed_source;
2570 } else {
2571 sink = route->sink;
2572 source = route->source;
2573 }
2574
2575 path = NULL;
2576 list_for_each_entry(p, &dapm->card->paths, list) {
2577 if (strcmp(p->source->name, source) != 0)
2578 continue;
2579 if (strcmp(p->sink->name, sink) != 0)
2580 continue;
2581 path = p;
2582 break;
2583 }
2584
2585 if (path) {
2586 dapm_mark_dirty(path->source, "Route removed");
2587 dapm_mark_dirty(path->sink, "Route removed");
2588
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002589 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002590 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002591 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002592 source, sink);
2593 }
2594
2595 return 0;
2596}
2597
Mark Brown105f1c22008-05-13 14:52:19 +02002598/**
Mark Brown105f1c22008-05-13 14:52:19 +02002599 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002600 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002601 * @route: audio routes
2602 * @num: number of routes
2603 *
2604 * Connects 2 dapm widgets together via a named audio path. The sink is
2605 * the widget receiving the audio signal, whilst the source is the sender
2606 * of the audio signal.
2607 *
2608 * Returns 0 for success else error. On error all resources can be freed
2609 * with a call to snd_soc_card_free().
2610 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002611int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002612 const struct snd_soc_dapm_route *route, int num)
2613{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002614 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002615
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002616 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002617 for (i = 0; i < num; i++) {
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002618 r = snd_soc_dapm_add_route(dapm, route, false);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002619 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002620 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2621 route->source,
2622 route->control ? route->control : "direct",
2623 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002624 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002625 }
2626 route++;
2627 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002628 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002629
Dan Carpenter60884c22012-04-13 22:25:43 +03002630 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002631}
2632EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2633
Mark Brownefcc3c62012-07-05 17:24:19 +01002634/**
2635 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2636 * @dapm: DAPM context
2637 * @route: audio routes
2638 * @num: number of routes
2639 *
2640 * Removes routes from the DAPM context.
2641 */
2642int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2643 const struct snd_soc_dapm_route *route, int num)
2644{
2645 int i, ret = 0;
2646
2647 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2648 for (i = 0; i < num; i++) {
2649 snd_soc_dapm_del_route(dapm, route);
2650 route++;
2651 }
2652 mutex_unlock(&dapm->card->dapm_mutex);
2653
2654 return ret;
2655}
2656EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2657
Mark Brownbf3a9e12011-06-13 16:42:29 +01002658static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2659 const struct snd_soc_dapm_route *route)
2660{
2661 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2662 route->source,
2663 true);
2664 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2665 route->sink,
2666 true);
2667 struct snd_soc_dapm_path *path;
2668 int count = 0;
2669
2670 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002671 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002672 route->source);
2673 return -ENODEV;
2674 }
2675
2676 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002677 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002678 route->sink);
2679 return -ENODEV;
2680 }
2681
2682 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002683 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002684 route->source, route->sink);
2685
2686 list_for_each_entry(path, &source->sinks, list_source) {
2687 if (path->sink == sink) {
2688 path->weak = 1;
2689 count++;
2690 }
2691 }
2692
2693 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002694 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002695 route->source, route->sink);
2696 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002697 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002698 count, route->source, route->sink);
2699
2700 return 0;
2701}
2702
2703/**
2704 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2705 * @dapm: DAPM context
2706 * @route: audio routes
2707 * @num: number of routes
2708 *
2709 * Mark existing routes matching those specified in the passed array
2710 * as being weak, meaning that they are ignored for the purpose of
2711 * power decisions. The main intended use case is for sidetone paths
2712 * which couple audio between other independent paths if they are both
2713 * active in order to make the combination work better at the user
2714 * level but which aren't intended to be "used".
2715 *
2716 * Note that CODEC drivers should not use this as sidetone type paths
2717 * can frequently also be used as bypass paths.
2718 */
2719int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2720 const struct snd_soc_dapm_route *route, int num)
2721{
2722 int i, err;
2723 int ret = 0;
2724
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002725 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002726 for (i = 0; i < num; i++) {
2727 err = snd_soc_dapm_weak_route(dapm, route);
2728 if (err)
2729 ret = err;
2730 route++;
2731 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002732 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002733
2734 return ret;
2735}
2736EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2737
Mark Brown105f1c22008-05-13 14:52:19 +02002738/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002739 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002740 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002741 *
2742 * Checks the codec for any new dapm widgets and creates them if found.
2743 *
2744 * Returns 0 for success.
2745 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002746int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002747{
2748 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002749 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002750
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002751 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002752
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002753 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002754 {
2755 if (w->new)
2756 continue;
2757
Stephen Warrenfad59882011-04-28 17:37:59 -06002758 if (w->num_kcontrols) {
2759 w->kcontrols = kzalloc(w->num_kcontrols *
2760 sizeof(struct snd_kcontrol *),
2761 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002762 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002763 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002764 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002765 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002766 }
2767
Richard Purdie2b97eab2006-10-06 18:32:18 +02002768 switch(w->id) {
2769 case snd_soc_dapm_switch:
2770 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002771 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002772 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002773 break;
2774 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002775 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002776 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002777 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002778 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002779 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002780 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002781 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002782 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002783 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002784 break;
2785 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002786
2787 /* Read the initial power state from the device */
2788 if (w->reg >= 0) {
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002789 soc_widget_read(w, w->reg, &val);
2790 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002791 val &= w->mask;
2792 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002793 w->power = 1;
2794 }
2795
Richard Purdie2b97eab2006-10-06 18:32:18 +02002796 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002797
Mark Brown7508b122011-10-05 12:09:12 +01002798 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002799 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002800 }
2801
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002802 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2803 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002804 return 0;
2805}
2806EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2807
2808/**
2809 * snd_soc_dapm_get_volsw - dapm mixer get callback
2810 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002811 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002812 *
2813 * Callback to get the value of a dapm mixer control.
2814 *
2815 * Returns 0 for success.
2816 */
2817int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2818 struct snd_ctl_elem_value *ucontrol)
2819{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002820 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002821 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002822 struct soc_mixer_control *mc =
2823 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002824 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002825 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002826 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002827 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002828 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002829 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002830
2831 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002832 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002833 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002834 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002835
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002836 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002837 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002838 val = (snd_soc_read(codec, reg) >> shift) & mask;
2839 else
2840 val = dapm_kcontrol_get_value(kcontrol);
2841 mutex_unlock(&card->dapm_mutex);
2842
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002843 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002844 ucontrol->value.integer.value[0] = max - val;
2845 else
2846 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002847
2848 return 0;
2849}
2850EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2851
2852/**
2853 * snd_soc_dapm_put_volsw - dapm mixer set callback
2854 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002855 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002856 *
2857 * Callback to set the value of a dapm mixer control.
2858 *
2859 * Returns 0 for success.
2860 */
2861int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2862 struct snd_ctl_elem_value *ucontrol)
2863{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002864 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002865 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002866 struct soc_mixer_control *mc =
2867 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002868 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002869 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002870 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002871 unsigned int mask = (1 << fls(max)) - 1;
2872 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002873 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002874 int connect, change;
2875 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002876 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002877
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002878 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002879 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002880 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002881 kcontrol->id.name);
2882
Richard Purdie2b97eab2006-10-06 18:32:18 +02002883 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002884 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002885
2886 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002887 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002888
Liam Girdwood3cd04342012-03-09 12:02:08 +00002889 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002890
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002891 change = dapm_kcontrol_set_value(kcontrol, val);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002892
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002893 if (reg != SND_SOC_NOPM) {
2894 mask = mask << shift;
2895 val = val << shift;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002896
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002897 change = snd_soc_test_bits(codec, reg, mask, val);
2898 }
2899
Mark Brown97404f22010-12-14 16:13:57 +00002900 if (change) {
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002901 if (reg != SND_SOC_NOPM) {
2902 update.kcontrol = kcontrol;
2903 update.reg = reg;
2904 update.mask = mask;
2905 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002906
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002907 card->update = &update;
2908 }
Mark Brown97404f22010-12-14 16:13:57 +00002909
Nenghua Cao52765972013-12-13 20:13:49 +08002910 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002911
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002912 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002913 }
2914
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002915 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002916
2917 if (ret > 0)
2918 soc_dpcm_runtime_update(card);
2919
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002920 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002921}
2922EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2923
2924/**
2925 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2926 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002927 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002928 *
2929 * Callback to get the value of a dapm enumerated double mixer control.
2930 *
2931 * Returns 0 for success.
2932 */
2933int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2934 struct snd_ctl_elem_value *ucontrol)
2935{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002936 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002937 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002938 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002939
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002940 val = snd_soc_read(codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002941 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002942 if (e->shift_l != e->shift_r)
2943 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002944 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002945
2946 return 0;
2947}
2948EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2949
2950/**
2951 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set 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 set the value of a dapm enumerated double mixer control.
2956 *
2957 * Returns 0 for success.
2958 */
2959int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2960 struct snd_ctl_elem_value *ucontrol)
2961{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002962 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002963 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002964 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002965 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002966 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002967 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002968 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002969
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002970 if (ucontrol->value.enumerated.item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002971 return -EINVAL;
2972 mux = ucontrol->value.enumerated.item[0];
2973 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002974 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002975 if (e->shift_l != e->shift_r) {
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002976 if (ucontrol->value.enumerated.item[1] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002977 return -EINVAL;
2978 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002979 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002980 }
2981
Liam Girdwood3cd04342012-03-09 12:02:08 +00002982 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002983
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002984 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002985 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002986 update.kcontrol = kcontrol;
2987 update.reg = e->reg;
2988 update.mask = mask;
2989 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002990 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002991
Nenghua Cao52765972013-12-13 20:13:49 +08002992 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002993
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002994 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002995 }
2996
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002997 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002998
2999 if (ret > 0)
3000 soc_dpcm_runtime_update(card);
3001
Mark Brown97404f22010-12-14 16:13:57 +00003002 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003003}
3004EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3005
3006/**
Mark Brownd2b247a2009-10-06 15:21:04 +01003007 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
3008 * @kcontrol: mixer control
3009 * @ucontrol: control element information
3010 *
3011 * Returns 0 for success.
3012 */
3013int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
3014 struct snd_ctl_elem_value *ucontrol)
3015{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003016 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
Mark Brownd2b247a2009-10-06 15:21:04 +01003017 return 0;
3018}
3019EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
3020
3021/**
3022 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
3023 * @kcontrol: mixer control
3024 * @ucontrol: control element information
3025 *
3026 * Returns 0 for success.
3027 */
3028int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
3029 struct snd_ctl_elem_value *ucontrol)
3030{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003031 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003032 struct snd_soc_card *card = codec->card;
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003033 unsigned int value;
Mark Brownd2b247a2009-10-06 15:21:04 +01003034 struct soc_enum *e =
3035 (struct soc_enum *)kcontrol->private_value;
3036 int change;
Nenghua Cao52765972013-12-13 20:13:49 +08003037 int ret = 0;
Mark Brownd2b247a2009-10-06 15:21:04 +01003038
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01003039 if (ucontrol->value.enumerated.item[0] >= e->items)
Mark Brownd2b247a2009-10-06 15:21:04 +01003040 return -EINVAL;
3041
Liam Girdwood3cd04342012-03-09 12:02:08 +00003042 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01003043
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003044 value = ucontrol->value.enumerated.item[0];
3045 change = dapm_kcontrol_set_value(kcontrol, value);
3046 if (change)
Nenghua Cao52765972013-12-13 20:13:49 +08003047 ret = soc_dapm_mux_update_power(card, kcontrol, value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003048
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003049 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003050
3051 if (ret > 0)
3052 soc_dpcm_runtime_update(card);
3053
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003054 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01003055}
3056EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3057
3058/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003059 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3060 * callback
3061 * @kcontrol: mixer control
3062 * @ucontrol: control element information
3063 *
3064 * Callback to get the value of a dapm semi enumerated double mixer control.
3065 *
3066 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3067 * used for handling bitfield coded enumeration for example.
3068 *
3069 * Returns 0 for success.
3070 */
3071int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
3072 struct snd_ctl_elem_value *ucontrol)
3073{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003074 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02003075 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003076 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003077
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003078 reg_val = snd_soc_read(codec, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003079 val = (reg_val >> e->shift_l) & e->mask;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01003080 for (mux = 0; mux < e->items; mux++) {
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003081 if (val == e->values[mux])
3082 break;
3083 }
3084 ucontrol->value.enumerated.item[0] = mux;
3085 if (e->shift_l != e->shift_r) {
3086 val = (reg_val >> e->shift_r) & e->mask;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01003087 for (mux = 0; mux < e->items; mux++) {
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003088 if (val == e->values[mux])
3089 break;
3090 }
3091 ucontrol->value.enumerated.item[1] = mux;
3092 }
3093
3094 return 0;
3095}
3096EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
3097
3098/**
3099 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3100 * callback
3101 * @kcontrol: mixer control
3102 * @ucontrol: control element information
3103 *
3104 * Callback to set the value of a dapm semi enumerated double mixer control.
3105 *
3106 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3107 * used for handling bitfield coded enumeration for example.
3108 *
3109 * Returns 0 for success.
3110 */
3111int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
3112 struct snd_ctl_elem_value *ucontrol)
3113{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003114 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003115 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02003116 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01003117 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003118 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00003119 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003120 int ret = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003121
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01003122 if (ucontrol->value.enumerated.item[0] >= e->items)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003123 return -EINVAL;
3124 mux = ucontrol->value.enumerated.item[0];
3125 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
3126 mask = e->mask << e->shift_l;
3127 if (e->shift_l != e->shift_r) {
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01003128 if (ucontrol->value.enumerated.item[1] >= e->items)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003129 return -EINVAL;
3130 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
3131 mask |= e->mask << e->shift_r;
3132 }
3133
Liam Girdwood3cd04342012-03-09 12:02:08 +00003134 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003135
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003136 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003137 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02003138 update.kcontrol = kcontrol;
3139 update.reg = e->reg;
3140 update.mask = mask;
3141 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003142 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00003143
Nenghua Cao52765972013-12-13 20:13:49 +08003144 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003145
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003146 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003147 }
3148
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003149 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003150
3151 if (ret > 0)
3152 soc_dpcm_runtime_update(card);
3153
Mark Brown97404f22010-12-14 16:13:57 +00003154 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003155}
3156EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3157
3158/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003159 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3160 *
3161 * @kcontrol: mixer control
3162 * @uinfo: control element information
3163 *
3164 * Callback to provide information about a pin switch control.
3165 */
3166int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3167 struct snd_ctl_elem_info *uinfo)
3168{
3169 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3170 uinfo->count = 1;
3171 uinfo->value.integer.min = 0;
3172 uinfo->value.integer.max = 1;
3173
3174 return 0;
3175}
3176EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3177
3178/**
3179 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3180 *
3181 * @kcontrol: mixer control
3182 * @ucontrol: Value
3183 */
3184int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3185 struct snd_ctl_elem_value *ucontrol)
3186{
Mark Brown48a8c392012-02-14 17:11:15 -08003187 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003188 const char *pin = (const char *)kcontrol->private_value;
3189
Liam Girdwood3cd04342012-03-09 12:02:08 +00003190 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003191
3192 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003193 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003194
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003195 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003196
3197 return 0;
3198}
3199EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3200
3201/**
3202 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3203 *
3204 * @kcontrol: mixer control
3205 * @ucontrol: Value
3206 */
3207int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3208 struct snd_ctl_elem_value *ucontrol)
3209{
Mark Brown48a8c392012-02-14 17:11:15 -08003210 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003211 const char *pin = (const char *)kcontrol->private_value;
3212
Liam Girdwood3cd04342012-03-09 12:02:08 +00003213 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003214
3215 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003216 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003217 else
Mark Brown48a8c392012-02-14 17:11:15 -08003218 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003219
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003220 mutex_unlock(&card->dapm_mutex);
3221
Mark Brown48a8c392012-02-14 17:11:15 -08003222 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003223 return 0;
3224}
3225EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3226
Mark Brown5ba06fc2012-02-16 11:07:13 -08003227static struct snd_soc_dapm_widget *
3228snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3229 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003230{
3231 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003232 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003233
3234 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003235 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003236
Mark Brown62ea8742012-01-21 21:14:48 +00003237 switch (w->id) {
3238 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003239 w->regulator = devm_regulator_get(dapm->dev, w->name);
3240 if (IS_ERR(w->regulator)) {
3241 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003242 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003243 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003244 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003245 }
Mark Brown8784c772013-01-10 19:33:47 +00003246
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003247 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003248 ret = regulator_allow_bypass(w->regulator, true);
3249 if (ret != 0)
3250 dev_warn(w->dapm->dev,
3251 "ASoC: Failed to unbypass %s: %d\n",
3252 w->name, ret);
3253 }
Mark Brown62ea8742012-01-21 21:14:48 +00003254 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003255 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003256#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003257 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003258 if (IS_ERR(w->clk)) {
3259 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003260 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003261 w->name, ret);
3262 return NULL;
3263 }
Mark Brownec029952012-06-04 08:16:20 +01003264#else
3265 return NULL;
3266#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003267 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003268 default:
3269 break;
3270 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003271
Mark Brown88e8b9a2011-03-02 18:18:24 +00003272 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003273 w->name = kasprintf(GFP_KERNEL, "%s %s",
3274 dapm->codec->name_prefix, widget->name);
3275 else
3276 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3277
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003278 if (w->name == NULL) {
3279 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003280 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003281 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003282
Mark Brown7ca3a182011-10-08 14:04:50 +01003283 switch (w->id) {
3284 case snd_soc_dapm_switch:
3285 case snd_soc_dapm_mixer:
3286 case snd_soc_dapm_mixer_named_ctl:
3287 w->power_check = dapm_generic_check_power;
3288 break;
3289 case snd_soc_dapm_mux:
3290 case snd_soc_dapm_virt_mux:
3291 case snd_soc_dapm_value_mux:
3292 w->power_check = dapm_generic_check_power;
3293 break;
Mark Brown46162742013-06-05 19:36:11 +01003294 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003295 w->power_check = dapm_adc_check_power;
3296 break;
Mark Brown46162742013-06-05 19:36:11 +01003297 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003298 w->power_check = dapm_dac_check_power;
3299 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003300 case snd_soc_dapm_adc:
3301 case snd_soc_dapm_aif_out:
3302 case snd_soc_dapm_dac:
3303 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003304 case snd_soc_dapm_pga:
3305 case snd_soc_dapm_out_drv:
3306 case snd_soc_dapm_input:
3307 case snd_soc_dapm_output:
3308 case snd_soc_dapm_micbias:
3309 case snd_soc_dapm_spk:
3310 case snd_soc_dapm_hp:
3311 case snd_soc_dapm_mic:
3312 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003313 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003314 w->power_check = dapm_generic_check_power;
3315 break;
3316 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003317 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003318 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003319 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003320 w->power_check = dapm_supply_check_power;
3321 break;
3322 default:
3323 w->power_check = dapm_always_on_check_power;
3324 break;
3325 }
3326
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003327 w->dapm = dapm;
3328 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003329 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003330 INIT_LIST_HEAD(&w->sources);
3331 INIT_LIST_HEAD(&w->sinks);
3332 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003333 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003334 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003335
3336 /* machine layer set ups unconnected pins and insertions */
3337 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003338 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003339}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003340
3341/**
Mark Brown4ba13272008-05-13 14:51:19 +02003342 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003343 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003344 * @widget: widget array
3345 * @num: number of widgets
3346 *
3347 * Creates new DAPM controls based upon the templates.
3348 *
3349 * Returns 0 for success else error.
3350 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003351int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003352 const struct snd_soc_dapm_widget *widget,
3353 int num)
3354{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003355 struct snd_soc_dapm_widget *w;
3356 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003357 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003358
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003359 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003360 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003361 w = snd_soc_dapm_new_control(dapm, widget);
3362 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003363 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003364 "ASoC: Failed to create DAPM control %s\n",
3365 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003366 ret = -ENOMEM;
3367 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003368 }
Mark Brown4ba13272008-05-13 14:51:19 +02003369 widget++;
3370 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003371 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003372 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003373}
3374EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3375
Mark Brownc74184e2012-04-04 22:12:09 +01003376static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3377 struct snd_kcontrol *kcontrol, int event)
3378{
3379 struct snd_soc_dapm_path *source_p, *sink_p;
3380 struct snd_soc_dai *source, *sink;
3381 const struct snd_soc_pcm_stream *config = w->params;
3382 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003383 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003384 u64 fmt;
3385 int ret;
3386
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003387 if (WARN_ON(!config) ||
3388 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3389 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003390
3391 /* We only support a single source and sink, pick the first */
3392 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3393 list_sink);
3394 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3395 list_source);
3396
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003397 if (WARN_ON(!source_p || !sink_p) ||
3398 WARN_ON(!sink_p->source || !source_p->sink) ||
3399 WARN_ON(!source_p->source || !sink_p->sink))
3400 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003401
3402 source = source_p->source->priv;
3403 sink = sink_p->sink->priv;
3404
3405 /* Be a little careful as we don't want to overflow the mask array */
3406 if (config->formats) {
3407 fmt = ffs(config->formats) - 1;
3408 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003409 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003410 config->formats);
3411 fmt = 0;
3412 }
3413
3414 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003415 params = kzalloc(sizeof(*params), GFP_KERNEL);
3416 if (!params) {
3417 ret = -ENOMEM;
3418 goto out;
3419 }
3420 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003421
Mark Brown9747cec2012-04-26 19:12:21 +01003422 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003423 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003424 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003425 config->rate_max;
3426
Mark Brown9747cec2012-04-26 19:12:21 +01003427 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003428 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003429 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003430 = config->channels_max;
3431
3432 memset(&substream, 0, sizeof(substream));
3433
3434 switch (event) {
3435 case SND_SOC_DAPM_PRE_PMU:
3436 if (source->driver->ops && source->driver->ops->hw_params) {
3437 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3438 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003439 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003440 if (ret != 0) {
3441 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003442 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003443 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003444 }
3445 }
3446
3447 if (sink->driver->ops && sink->driver->ops->hw_params) {
3448 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003449 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003450 sink);
3451 if (ret != 0) {
3452 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003453 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003454 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003455 }
3456 }
3457 break;
3458
3459 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003460 ret = snd_soc_dai_digital_mute(sink, 0,
3461 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003462 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003463 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003464 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003465 break;
3466
3467 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003468 ret = snd_soc_dai_digital_mute(sink, 1,
3469 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003470 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003471 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003472 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003473 break;
3474
3475 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003476 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003477 return -EINVAL;
3478 }
3479
Mark Brown9747cec2012-04-26 19:12:21 +01003480out:
3481 kfree(params);
3482 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003483}
3484
3485int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3486 const struct snd_soc_pcm_stream *params,
3487 struct snd_soc_dapm_widget *source,
3488 struct snd_soc_dapm_widget *sink)
3489{
3490 struct snd_soc_dapm_route routes[2];
3491 struct snd_soc_dapm_widget template;
3492 struct snd_soc_dapm_widget *w;
3493 size_t len;
3494 char *link_name;
3495
3496 len = strlen(source->name) + strlen(sink->name) + 2;
3497 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3498 if (!link_name)
3499 return -ENOMEM;
3500 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3501
3502 memset(&template, 0, sizeof(template));
3503 template.reg = SND_SOC_NOPM;
3504 template.id = snd_soc_dapm_dai_link;
3505 template.name = link_name;
3506 template.event = snd_soc_dai_link_event;
3507 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3508 SND_SOC_DAPM_PRE_PMD;
3509
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003510 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003511
3512 w = snd_soc_dapm_new_control(&card->dapm, &template);
3513 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003514 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003515 link_name);
3516 return -ENOMEM;
3517 }
3518
3519 w->params = params;
3520
3521 memset(&routes, 0, sizeof(routes));
3522
3523 routes[0].source = source->name;
3524 routes[0].sink = link_name;
3525 routes[1].source = link_name;
3526 routes[1].sink = sink->name;
3527
3528 return snd_soc_dapm_add_routes(&card->dapm, routes,
3529 ARRAY_SIZE(routes));
3530}
3531
Mark Brown888df392012-02-16 19:37:51 -08003532int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3533 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003534{
Mark Brown888df392012-02-16 19:37:51 -08003535 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003536 struct snd_soc_dapm_widget *w;
3537
Mark Brown888df392012-02-16 19:37:51 -08003538 WARN_ON(dapm->dev != dai->dev);
3539
3540 memset(&template, 0, sizeof(template));
3541 template.reg = SND_SOC_NOPM;
3542
3543 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003544 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003545 template.name = dai->driver->playback.stream_name;
3546 template.sname = dai->driver->playback.stream_name;
3547
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003548 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003549 template.name);
3550
3551 w = snd_soc_dapm_new_control(dapm, &template);
3552 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003553 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003554 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003555 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003556 }
3557
3558 w->priv = dai;
3559 dai->playback_widget = w;
3560 }
3561
3562 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003563 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003564 template.name = dai->driver->capture.stream_name;
3565 template.sname = dai->driver->capture.stream_name;
3566
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003567 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003568 template.name);
3569
3570 w = snd_soc_dapm_new_control(dapm, &template);
3571 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003572 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003573 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003574 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003575 }
3576
3577 w->priv = dai;
3578 dai->capture_widget = w;
3579 }
3580
3581 return 0;
3582}
3583
3584int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3585{
3586 struct snd_soc_dapm_widget *dai_w, *w;
3587 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003588
3589 /* For each DAI widget... */
3590 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003591 switch (dai_w->id) {
3592 case snd_soc_dapm_dai_in:
3593 case snd_soc_dapm_dai_out:
3594 break;
3595 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003596 continue;
Mark Brown46162742013-06-05 19:36:11 +01003597 }
Mark Brown888df392012-02-16 19:37:51 -08003598
3599 dai = dai_w->priv;
3600
3601 /* ...find all widgets with the same stream and link them */
3602 list_for_each_entry(w, &card->widgets, list) {
3603 if (w->dapm != dai_w->dapm)
3604 continue;
3605
Mark Brown46162742013-06-05 19:36:11 +01003606 switch (w->id) {
3607 case snd_soc_dapm_dai_in:
3608 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003609 continue;
Mark Brown46162742013-06-05 19:36:11 +01003610 default:
3611 break;
3612 }
Mark Brown888df392012-02-16 19:37:51 -08003613
Russell King19c2c5f2013-08-04 20:24:03 +01003614 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003615 continue;
3616
3617 if (dai->driver->playback.stream_name &&
3618 strstr(w->sname,
3619 dai->driver->playback.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003620 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003621 dai->playback_widget->name, w->name);
Mark Brown888df392012-02-16 19:37:51 -08003622
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003623 snd_soc_dapm_add_path(w->dapm,
3624 dai->playback_widget, w, NULL, NULL);
Mark Brown888df392012-02-16 19:37:51 -08003625 }
3626
3627 if (dai->driver->capture.stream_name &&
3628 strstr(w->sname,
3629 dai->driver->capture.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003630 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003631 w->name, dai->capture_widget->name);
Mark Brown888df392012-02-16 19:37:51 -08003632
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003633 snd_soc_dapm_add_path(w->dapm, w,
3634 dai->capture_widget, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003635 }
3636 }
3637 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003638
Mark Brown888df392012-02-16 19:37:51 -08003639 return 0;
3640}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003641
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003642void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3643{
3644 struct snd_soc_pcm_runtime *rtd = card->rtd;
3645 struct snd_soc_dai *cpu_dai, *codec_dai;
3646 struct snd_soc_dapm_route r;
3647 int i;
3648
3649 memset(&r, 0, sizeof(r));
3650
3651 /* for each BE DAI link... */
3652 for (i = 0; i < card->num_rtd; i++) {
3653 rtd = &card->rtd[i];
3654 cpu_dai = rtd->cpu_dai;
3655 codec_dai = rtd->codec_dai;
3656
3657 /* dynamic FE links have no fixed DAI mapping */
3658 if (rtd->dai_link->dynamic)
3659 continue;
3660
3661 /* there is no point in connecting BE DAI links with dummies */
3662 if (snd_soc_dai_is_dummy(codec_dai) ||
3663 snd_soc_dai_is_dummy(cpu_dai))
3664 continue;
3665
3666 /* connect BE DAI playback if widgets are valid */
3667 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3668 r.source = cpu_dai->playback_widget->name;
3669 r.sink = codec_dai->playback_widget->name;
3670 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3671 cpu_dai->codec->name, r.source,
3672 codec_dai->platform->name, r.sink);
3673
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003674 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003675 }
3676
3677 /* connect BE DAI capture if widgets are valid */
3678 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3679 r.source = codec_dai->capture_widget->name;
3680 r.sink = cpu_dai->capture_widget->name;
3681 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3682 codec_dai->codec->name, r.source,
3683 cpu_dai->platform->name, r.sink);
3684
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003685 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003686 }
3687
3688 }
3689}
3690
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003691static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3692 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003693{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003694
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003695 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3696 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3697 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003698
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003699 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3700 w_cpu = cpu_dai->playback_widget;
3701 w_codec = codec_dai->playback_widget;
3702 } else {
3703 w_cpu = cpu_dai->capture_widget;
3704 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003705 }
3706
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003707 if (w_cpu) {
3708
3709 dapm_mark_dirty(w_cpu, "stream event");
3710
3711 switch (event) {
3712 case SND_SOC_DAPM_STREAM_START:
3713 w_cpu->active = 1;
3714 break;
3715 case SND_SOC_DAPM_STREAM_STOP:
3716 w_cpu->active = 0;
3717 break;
3718 case SND_SOC_DAPM_STREAM_SUSPEND:
3719 case SND_SOC_DAPM_STREAM_RESUME:
3720 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3721 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3722 break;
3723 }
3724 }
3725
3726 if (w_codec) {
3727
3728 dapm_mark_dirty(w_codec, "stream event");
3729
3730 switch (event) {
3731 case SND_SOC_DAPM_STREAM_START:
3732 w_codec->active = 1;
3733 break;
3734 case SND_SOC_DAPM_STREAM_STOP:
3735 w_codec->active = 0;
3736 break;
3737 case SND_SOC_DAPM_STREAM_SUSPEND:
3738 case SND_SOC_DAPM_STREAM_RESUME:
3739 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3740 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3741 break;
3742 }
3743 }
3744
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003745 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003746}
3747
3748/**
3749 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3750 * @rtd: PCM runtime data
3751 * @stream: stream name
3752 * @event: stream event
3753 *
3754 * Sends a stream event to the dapm core. The core then makes any
3755 * necessary widget power changes.
3756 *
3757 * Returns 0 for success else error.
3758 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003759void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3760 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003761{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003762 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003763
Liam Girdwood3cd04342012-03-09 12:02:08 +00003764 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003765 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003766 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003767}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003768
3769/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003770 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003771 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003772 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003773 *
Mark Brown74b8f952009-06-06 11:26:15 +01003774 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003775 * a valid audio route and active audio stream.
3776 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3777 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003778 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003779int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003780{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003781 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003782}
Liam Girdwooda5302182008-07-07 13:35:17 +01003783EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003784
3785/**
Mark Brownda341832010-03-15 19:23:37 +00003786 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003787 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003788 * @pin: pin name
3789 *
3790 * Enables input/output pin regardless of any other state. This is
3791 * intended for use with microphone bias supplies used in microphone
3792 * jack detection.
3793 *
3794 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3795 * do any widget power switching.
3796 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003797int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3798 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003799{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003800 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003801
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003802 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003803 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003804 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003805 }
3806
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003807 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003808 w->connected = 1;
3809 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003810 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003811
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003812 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003813}
3814EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3815
3816/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003817 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003818 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003819 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003820 *
Mark Brown74b8f952009-06-06 11:26:15 +01003821 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003822 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3823 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003824 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003825int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3826 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003827{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003828 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003829}
3830EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3831
3832/**
Mark Brown5817b522008-09-24 11:23:11 +01003833 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003834 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003835 * @pin: pin name
3836 *
3837 * Marks the specified pin as being not connected, disabling it along
3838 * any parent or child widgets. At present this is identical to
3839 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3840 * additional things such as disabling controls which only affect
3841 * paths through the pin.
3842 *
3843 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3844 * do any widget power switching.
3845 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003846int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003847{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003848 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003849}
3850EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3851
3852/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003853 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003854 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003855 * @pin: audio signal pin endpoint (or start point)
3856 *
3857 * Get audio pin status - connected or disconnected.
3858 *
3859 * Returns 1 for connected otherwise 0.
3860 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003861int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3862 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003863{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003864 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003865
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003866 if (w)
3867 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003868
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003869 return 0;
3870}
Liam Girdwooda5302182008-07-07 13:35:17 +01003871EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003872
3873/**
Mark Brown1547aba2010-05-07 21:11:40 +01003874 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003875 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003876 * @pin: audio signal pin endpoint (or start point)
3877 *
3878 * Mark the given endpoint or pin as ignoring suspend. When the
3879 * system is disabled a path between two endpoints flagged as ignoring
3880 * suspend will not be disabled. The path must already be enabled via
3881 * normal means at suspend time, it will not be turned on if it was not
3882 * already enabled.
3883 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003884int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3885 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003886{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003887 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003888
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003889 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003890 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003891 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003892 }
3893
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003894 w->ignore_suspend = 1;
3895
3896 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003897}
3898EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3899
Stephen Warren16332812011-11-23 12:42:04 -07003900static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3901 struct snd_soc_dapm_widget *w)
3902{
3903 struct snd_soc_dapm_path *p;
3904
3905 list_for_each_entry(p, &card->paths, list) {
3906 if ((p->source == w) || (p->sink == w)) {
3907 dev_dbg(card->dev,
3908 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3909 p->source->name, p->source->id, p->source->dapm,
3910 p->sink->name, p->sink->id, p->sink->dapm);
3911
3912 /* Connected to something other than the codec */
3913 if (p->source->dapm != p->sink->dapm)
3914 return true;
3915 /*
3916 * Loopback connection from codec external pin to
3917 * codec external pin
3918 */
3919 if (p->sink->id == snd_soc_dapm_input) {
3920 switch (p->source->id) {
3921 case snd_soc_dapm_output:
3922 case snd_soc_dapm_micbias:
3923 return true;
3924 default:
3925 break;
3926 }
3927 }
3928 }
3929 }
3930
3931 return false;
3932}
3933
3934/**
3935 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3936 * @codec: The codec whose pins should be processed
3937 *
3938 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3939 * which are unused. Pins are used if they are connected externally to the
3940 * codec, whether that be to some other device, or a loop-back connection to
3941 * the codec itself.
3942 */
3943void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3944{
3945 struct snd_soc_card *card = codec->card;
3946 struct snd_soc_dapm_context *dapm = &codec->dapm;
3947 struct snd_soc_dapm_widget *w;
3948
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003949 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003950 &card->dapm, &codec->dapm);
3951
3952 list_for_each_entry(w, &card->widgets, list) {
3953 if (w->dapm != dapm)
3954 continue;
3955 switch (w->id) {
3956 case snd_soc_dapm_input:
3957 case snd_soc_dapm_output:
3958 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003959 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003960 w->name);
3961 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003962 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003963 "... Not in map; disabling\n");
3964 snd_soc_dapm_nc_pin(dapm, w->name);
3965 }
3966 break;
3967 default:
3968 break;
3969 }
3970 }
3971}
3972
Mark Brown1547aba2010-05-07 21:11:40 +01003973/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003974 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003975 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003976 *
3977 * Free all dapm widgets and resources.
3978 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003979void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003980{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003981 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003982 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003983 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003984 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003985}
3986EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3987
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003988static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003989{
Liam Girdwood01005a72012-07-06 16:57:05 +01003990 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003991 struct snd_soc_dapm_widget *w;
3992 LIST_HEAD(down_list);
3993 int powerdown = 0;
3994
Liam Girdwood01005a72012-07-06 16:57:05 +01003995 mutex_lock(&card->dapm_mutex);
3996
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003997 list_for_each_entry(w, &dapm->card->widgets, list) {
3998 if (w->dapm != dapm)
3999 continue;
Mark Brown51737472009-06-22 13:16:51 +01004000 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004001 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01004002 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004003 powerdown = 1;
4004 }
4005 }
4006
4007 /* If there were no widgets to power down we're already in
4008 * standby.
4009 */
4010 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004011 if (dapm->bias_level == SND_SOC_BIAS_ON)
4012 snd_soc_dapm_set_bias_level(dapm,
4013 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004014 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004015 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4016 snd_soc_dapm_set_bias_level(dapm,
4017 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004018 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004019
4020 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004021}
Mark Brown51737472009-06-22 13:16:51 +01004022
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004023/*
4024 * snd_soc_dapm_shutdown - callback for system shutdown
4025 */
4026void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4027{
4028 struct snd_soc_codec *codec;
4029
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06004030 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004031 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00004032 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4033 snd_soc_dapm_set_bias_level(&codec->dapm,
4034 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004035 }
Mark Brown51737472009-06-22 13:16:51 +01004036}
4037
Richard Purdie2b97eab2006-10-06 18:32:18 +02004038/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004039MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004040MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4041MODULE_LICENSE("GPL");